-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetTargetLines.py
150 lines (115 loc) · 4.61 KB
/
GetTargetLines.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
import Five38MLBData
import csv
import datetime
import GameOdds
import requests
import xml.etree.ElementTree as ET
import BookMakerData
import BetHandler
import jsonpickle
import smtplib
def get_kelly_criterion(prob, win_rate):
return prob - ((1-prob)/win_rate)
def get_bookmaker_odds(response):
root = ET.fromstring(response)
games = []
for child in root.findall("./Leagues/league"):
if child.attrib['IdLeague'] == "5":
mlb_elem = child
break
for child in mlb_elem.findall("./game"):
try:
games.append(BookMakerData.BmMlbGame(child))
except Exception as e:
print(e)
continue
return games
five38_url = "https://projects.fivethirtyeight.com/mlb-api/mlb_elo_latest.csv"
bookmaker_url = "http://lines.bookmaker.eu/"
prediction_data = []
available_lines = []
notified = []
bets_to_make = []
with open('bets_notified.json') as lines:
notified = jsonpickle.decode(lines.read())
target_adv = 0.05
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
five38_response = requests.get(five38_url)
with open('mlb_elo_latest.csv', 'wb') as file:
file.write(five38_response.content)
with open('mlb_elo_latest.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
line_count += 1
else:
prediction_data.append(Five38MLBData.Five38MlbDataPoint(row))
bookmaker_response = requests.get(bookmaker_url)
available_lines = get_bookmaker_odds(bookmaker_response.content)
for line in available_lines:
game_match = None
for game in prediction_data:
if datetime.datetime.strptime(game.date, '%Y-%m-%d').date() == line.date and GameOdds.convert_team_to_five38name(line.home_team) == game.home_team:
game_match = game
break
if game_match is None:
continue
home_imp_prob = GameOdds.odds_to_imp_prob(line.home_odds)
away_imp_prob = GameOdds.odds_to_imp_prob(line.away_odds)
home_adv = float(game_match.rating_prob1) - home_imp_prob
away_adv = float(game_match.rating_prob2) - away_imp_prob
if home_adv > away_adv:
if home_adv < target_adv:
continue
prob_diff = float(game_match.rating_prob1) - home_imp_prob
decimal_odds = BetHandler.convert_odds_to_decimal(line.home_odds)
kelly = get_kelly_criterion(float(game_match.rating_prob1), (decimal_odds - 1))
if kelly < 0:
continue
bet_to_make = BetHandler.BetToMake(line.game_id, line.date, line.home_team, line.away_team, True, line.home_odds, line.away_odds, kelly, home_adv)
bets_to_make.append(bet_to_make)
else:
if away_adv < target_adv:
continue
prob_diff = float(game_match.rating_prob2) - away_imp_prob
decimal_odds = BetHandler.convert_odds_to_decimal(line.away_odds)
kelly = get_kelly_criterion(float(game_match.rating_prob2), (decimal_odds - 1))
if kelly < 0:
continue
bet_to_make = BetHandler.BetToMake(line.game_id, line.date, line.home_team, line.away_team, False, line.home_odds, line.away_odds, kelly, away_adv)
bets_to_make.append(bet_to_make)
sort_bets = sorted(bets_to_make, reverse=True)
email_text = ""
email_text += str(datetime.datetime.now())+"\n"
email_text += "New bets with perceived advantage over "+str(target_adv)+"\n"
recommended_bets = 0
for bet in sort_bets:
new_bet = True
for old in notified:
if bet.game_id == old.game_id:
new_bet = False
break
if new_bet:
notified.append(bet)
recommended_bets += 1
email_text += bet.output()+"\n"
if recommended_bets > 0:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.connect('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login("[email protected]", "lnkyaliduvshwicn")
try:
server.sendmail("[email protected]", "[email protected]", email_text)
with open('bets_notified.json', 'w') as file:
file.write(jsonpickle.encode(notified))
file.close()
except Exception as e:
print(e)
# for prediction in prediction_data:
# if datetime.datetime.strptime(prediction.date, '%Y-%m-%d').date() != tomorrow:
# continue
# home_target_line = GameOdds.imp_prob_to_odds(float(prediction.rating_prob1) - target_adv)
# away_target_line = GameOdds.imp_prob_to_odds(float(prediction.rating_prob2) - target_adv)
# print(prediction.home_team+" " + str(home_target_line)+" "+prediction.away_team+" "+str(away_target_line))