-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelTester.py
157 lines (139 loc) · 6.63 KB
/
ModelTester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import csv
import Five38MLBData
import jsonpickle
import datetime
import GameOdds
import BetHandler
import random
def get_kelly_criterion(prob, win_rate):
return prob - ((1-prob)/win_rate)
prediction_data = []
with open('mlb_elo.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
#print(f'Column names are {", ".join(row)}')
line_count += 1
else:
prediction_data.append(Five38MLBData.Five38MlbDataPoint(row))
with open('mlb2016endinglines.json') as lines:
betting_odds2016 = jsonpickle.decode(lines.read())
betting_odds2016.reverse()
with open('mlb2017endinglines.json') as lines:
betting_odds2017 = jsonpickle.decode(lines.read())
betting_odds2017.reverse()
with open('mlb2018endinglines.json') as lines:
betting_odds2018 = jsonpickle.decode(lines.read())
betting_odds2018.reverse()
betting_odds = betting_odds2016 + betting_odds2017 + betting_odds2018
random.shuffle(betting_odds)
random.shuffle(betting_odds)
#betting_odds = betting_odds2018
bankroll = 500.00
goal_adv = 0.07
total_bets = 0
total_wagered = 0.0
sharp_money = 0.0
sharp_bets = 0
won_bets = 0
profit_expectation_percent = 0.0
profit_expectation = 0.0
for odd in betting_odds:
#time.sleep(1)
try:
game_match = None
bet_obj = None
if bankroll <= 0:
break
odd_date = datetime.datetime.strptime(odd.date, '%d %b %Y').date()
for game in prediction_data:
if datetime.datetime.strptime(game.date, '%Y-%m-%d').date() == odd_date and GameOdds.convert_team_to_five38name(odd.home_team) == game.home_team:
game_match = game
break
if game_match is None:
continue
opening_home_imp_prob = GameOdds.odds_to_imp_prob(odd.get_odd_by_bookmaker("Pinnacle").opening_home)
opening_away_imp_prob = GameOdds.odds_to_imp_prob(odd.get_odd_by_bookmaker("Pinnacle").opening_away)
home_adv = float(game_match.rating_prob1) - opening_home_imp_prob
away_adv = float(game_match.rating_prob2) - opening_away_imp_prob
if home_adv > away_adv:
if home_adv > goal_adv:
prob_diff = float(game_match.rating_prob1) - opening_home_imp_prob
best_opening_odd = odd.get_odd_by_bookmaker("Pinnacle")
opening_decimal = BetHandler.convert_odds_to_decimal(odd.get_odd_by_bookmaker("Pinnacle").opening_home)
current_decimal = BetHandler.convert_odds_to_decimal(odd.get_odd_by_bookmaker("Pinnacle").current_home)
if best_opening_odd.opening_home > 250:
# there are some weirdly high values in the opening odds sometimes, this filters them out
continue
kelly = get_kelly_criterion(float(game_match.rating_prob1), (opening_decimal-1))
if kelly < 0:
continue
print("Kelly Criterion: "+str(kelly))
amount = 0.5*kelly * bankroll
total_wagered += amount
bet_obj = BetHandler.MoneylineBet(best_opening_odd.opening_home, amount, True, game_match.score1, game_match.score2)
if game_match.score1 > game_match.score2:
won_bets += 1
print("Betting on "+odd.home_team+" on "+odd.date+" at "+best_opening_odd.bookmaker)
bet_obj.output()
print("Perceived edge is " + str(prob_diff * 100) + "%")
bankroll += bet_obj.outcome()
print("Bankroll is now " + str(bankroll))
total_bets += 1
line_edge = (opening_decimal / (current_decimal*1.015)) - 1
print("Profit Expectation: "+str(line_edge))
profit_expectation_percent += line_edge
profit_expectation += line_edge * amount
if opening_decimal > 1.015*current_decimal:
sharp_bets += 1
sharp_money += amount
print("")
continue
else:
if away_adv > goal_adv:
prob_diff = float(game_match.rating_prob2) - opening_away_imp_prob
best_opening_odd = odd.get_odd_by_bookmaker("Pinnacle")
opening_decimal = BetHandler.convert_odds_to_decimal(odd.get_odd_by_bookmaker("Pinnacle").opening_away)
current_decimal = BetHandler.convert_odds_to_decimal(odd.get_odd_by_bookmaker("Pinnacle").current_away)
if best_opening_odd.opening_away > 250:
# there are some weirdly high values in the opening odds sometimes, this filters them out
continue
kelly = get_kelly_criterion(float(game_match.rating_prob2), (opening_decimal-1))
if kelly < 0:
continue
print("Kelly Criterion: " + str(kelly))
amount = 0.5*kelly * bankroll
total_wagered += amount
bet_obj = BetHandler.MoneylineBet(best_opening_odd.opening_away, amount, False, game_match.score1, game_match.score2)
print("Betting on " + odd.away_team + " on " + odd.date+" at "+best_opening_odd.bookmaker)
if game_match.score2 > game_match.score1:
won_bets += 1
bet_obj.output()
print("Perceived edge is " + str(prob_diff * 100) + "%")
bankroll += bet_obj.outcome()
print("Bankroll is now " + str(bankroll))
total_bets += 1
line_edge = (opening_decimal / current_decimal) - 1
print("Profit Expectation: " + str(line_edge))
profit_expectation_percent += line_edge
profit_expectation += line_edge * amount
if opening_decimal > current_decimal:
sharp_bets += 1
sharp_money += amount
print("")
continue
except Exception as e:
print(e)
continue
print("")
print("*****************************************************")
print("Minimum Advantage is: "+str(goal_adv))
print("Ending bankroll: "+str(bankroll))
print("Total bets placed: "+str(total_bets))
print("Total amount wagered: "+str(total_wagered))
print("Percentage of bets won: "+str(100*(won_bets/total_bets))+"%")
print("Percentage of sharp bets: "+str(100*(sharp_bets/total_bets))+"%")
print("Average Profit Expectation Percentage per bet: "+str(100*(profit_expectation_percent/total_bets)))
print("Total profit expectation: "+str(profit_expectation))
print("Percentage of sharp money: "+str(100*(sharp_money/total_wagered))+"%")