-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLBGamePredictor.py
More file actions
342 lines (299 loc) · 17.1 KB
/
MLBGamePredictor.py
File metadata and controls
342 lines (299 loc) · 17.1 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import requests
import json
import pandas as pd
import datetime
from statistics import mean
todaysGames = datetime.datetime.now().strftime("%m/%d/%Y")
def mlb_schedule():
game_ids = []
request = requests.get(f"http://statsapi.mlb.com/api/v1/schedule/games/?sportId=1&date={todaysGames}").text
request_json = json.loads(request)
games = (request_json['dates'][0]['games'])
for game in games:
game_ids.append(game['gamePk'])
return game_ids
mlb_teamStats = []
mlb_advantages = []
teamAdvatage = []
projectedOutcome = []
for games in mlb_schedule():
homeBA = []
awayBA = []
homeSLG = []
awaySLG = []
homeOBP = []
awayOBP = []
homeSO = []
awaySO = []
homeOPS = []
awayOPS = []
homeBullpenERA = []
awayBullpenERA = []
homeBullpenWHIP = []
awayBullpenWHIP = []
request = requests.get(f"https://statsapi.mlb.com/api/v1/schedule?gamePk={games}&language=en&hydrate=lineups").text
games_request_json = json.loads(request)
try:
(games_request_json['dates'][0]['games'][0]['lineups']['homePlayers'])
except:
continue
try:
(games_request_json['dates'][0]['games'][0]['lineups']['awayPlayers'])
except:
continue
homeTeamName = (games_request_json['dates'][0]['games'][0]['teams']['home']['team']['name'])
awayTeamName = (games_request_json['dates'][0]['games'][0]['teams']['away']['team']['name'])
homeTeam = (games_request_json['dates'][0]['games'][0]['lineups']['homePlayers'])
awayTeam = (games_request_json['dates'][0]['games'][0]['lineups']['awayPlayers'])
# Getting Batters Career Averages
for player in homeTeam:
try:
id = (player['id'])
request = requests.get(f"http://lookup-service-prod.mlb.com/json/named.sport_career_hitting.bam?league_list_id='mlb'&game_type='R'&player_id='{id}'").text
request_json = json.loads(request)
playerStats = (request_json['sport_career_hitting']['queryResults']['row'])
homeBA.append(float(playerStats['avg']))
homeSLG.append(float(playerStats['slg']))
homeOBP.append(float(playerStats['obp']))
homeSO.append(float(playerStats['so']) / float(playerStats['ab']))
homeOPS.append(float(playerStats['ops']))
except:
continue
for player in awayTeam:
try:
id = (player['id'])
request = requests.get(f"http://lookup-service-prod.mlb.com/json/named.sport_career_hitting.bam?league_list_id='mlb'&game_type='R'&player_id='{id}'").text
request_json = json.loads(request)
playerStats = (request_json['sport_career_hitting']['queryResults']['row'])
awayBA.append(float(playerStats['avg']))
awaySLG.append(float(playerStats['slg']))
awayOBP.append(float(playerStats['obp']))
awaySO.append(float(playerStats['so']) / float(playerStats['ab']))
awayOPS.append(float(playerStats['ops']))
except:
continue
# Get Starting Pitcher Stats
try:
pitcherRequest = requests.get(f"http://statsapi.mlb.com/api/v1/game/{games}/boxscore").text
pitcher_json = json.loads(pitcherRequest)
homePitcher = pitcher_json['teams']['home']['pitchers'][0]
awayPitcher = pitcher_json['teams']['away']['pitchers'][0]
homePitcherRequest = requests.get(f"http://lookup-service-prod.mlb.com/json/named.sport_career_pitching.bam?league_list_id='mlb'&game_type='R'&player_id='{homePitcher}'").text
homepitcherRequest_json = json.loads(homePitcherRequest)
awayPitcherRequest = requests.get(f"http://lookup-service-prod.mlb.com/json/named.sport_career_pitching.bam?league_list_id='mlb'&game_type='R'&player_id='{awayPitcher}'").text
awaypitcherRequest_json = json.loads(awayPitcherRequest)
homePitcherStats = (homepitcherRequest_json['sport_career_pitching']['queryResults']['row'])
awayPitcherStats = (awaypitcherRequest_json['sport_career_pitching']['queryResults']['row'])
except:
continue
# Get Bullpen Pitching Stats
try:
pitcherRequest = requests.get(f"http://statsapi.mlb.com/api/v1/game/{games}/boxscore").text
pitcher_json = json.loads(pitcherRequest)
homeBullpen = pitcher_json['teams']['home']['bullpen']
awayBullpen = pitcher_json['teams']['away']['bullpen']
for pitcher in homeBullpen:
homePitcherRequest = requests.get(f"http://lookup-service-prod.mlb.com/json/named.sport_career_pitching.bam?league_list_id='mlb'&game_type='R'&player_id='{pitcher}'").text
homepitcherRequest_json = json.loads(homePitcherRequest)
homePitcherStats = (homepitcherRequest_json['sport_career_pitching']['queryResults']['row'])
homeBullpenERA.append(float(homePitcherStats['era']))
homeBullpenWHIP.append(float(homePitcherStats['whip']))
for pitcher in awayBullpen:
awayPitcherRequest = requests.get(f"http://lookup-service-prod.mlb.com/json/named.sport_career_pitching.bam?league_list_id='mlb'&game_type='R'&player_id='{pitcher}'").text
awaypitcherRequest_json = json.loads(awayPitcherRequest)
awayPitcherStats = (awaypitcherRequest_json['sport_career_pitching']['queryResults']['row'])
awayBullpenERA.append(float(awayPitcherStats['era']))
awayBullpenWHIP.append(float(awayPitcherStats['whip']))
except:
continue
stats = {
'Home Team': homeTeamName,
"Away Team": awayTeamName,
"Home Batting Average": round(mean(homeBA), 3),
"Home Slugging %": round(mean(homeSLG), 3),
"Home OBP %": round(mean(homeOBP), 3),
"Home OPS %": round(mean(homeOPS), 3),
"Home SO %": round(mean(homeSO), 2),
"Home Starting ERA": round(float(homePitcherStats['era']), 2),
"Home Starting WHIP": round(float(homePitcherStats['whip']), 2),
"Home Starting OBP Against": round(float(homePitcherStats['obp']), 2),
"Home Starting Homeruns/9 Against": round(float(homePitcherStats['h9']), 2),
"Home Starting BB/9 Against": round(float(homePitcherStats['bb9']), 2),
"Home Bullpen ERA": round(mean(homeBullpenERA), 3),
"Home Bullpen WHIP": round(mean(homeBullpenWHIP), 3),
"Away Batting Average": round(mean(awayBA), 3),
"Away Slugging %": round(mean(awaySLG), 3),
"Away OBP %": round(mean(awayOBP), 3),
"Away OPS %": round(mean(awayOPS), 3),
"Away SO %": round(mean(awaySO), 2),
"Away Starting ERA": round(float(awayPitcherStats['era']), 2),
"Away Starting WHIP": round(float(awayPitcherStats['whip']), 2),
"Away Starting OBP Against": round(float(awayPitcherStats['obp']), 2),
"Away Starting Homeruns/9 Against": round(float(awayPitcherStats['h9']), 2),
"Away Starting BB/9 Against": round(float(awayPitcherStats['bb9']), 2),
"Away Bullpen ERA": round(mean(awayBullpenERA), 3),
"Away Bullpen WHIP": round(mean(awayBullpenWHIP), 3)
}
advantages = {
'Home Team': homeTeamName,
"Away Team": awayTeamName,
"Home BA": round(mean(homeBA) - mean(awayBA), 3),
"Home Slugging %": round(mean(homeSLG) - mean(awaySLG), 3),
"Home OBP %": round(mean(homeOBP) - mean(awayOBP), 3),
"Home OPS %": round(mean(homeOPS) - mean(awayOPS), 3),
"Home SO %": round(mean(homeSO) - mean(awaySO), 3) * -1,
"Home ERA": round(float(homePitcherStats['era']) - float(awayPitcherStats['era']), 3) * -1,
"Home WHIP": round(float(homePitcherStats['whip']) - float(awayPitcherStats['whip']), 3) * -1,
"Home OBP Against": (round(float(homePitcherStats['obp']), 2) - round(float(awayPitcherStats['obp']), 2)) * -1,
"Home Homeruns/9 Against": (round(float(homePitcherStats['h9']), 2) - round(float(awayPitcherStats['h9']), 2)) * -1,
"Home BB/9 Against": (round(float(homePitcherStats['bb9']), 2) - round(float(awayPitcherStats['bb9']),2)) * -1,
"Home Bullpen ERA": (round(mean(homeBullpenERA), 3) - round(mean(awayBullpenERA), 3)) * -1,
"Home Bullpen WHIP": (round(mean(homeBullpenWHIP), 3) - round(mean(awayBullpenWHIP), 3)) * -1,
"Away BA": round(mean(awayBA) - mean(homeBA), 3),
"Away Slugging %": round(mean(awaySLG) - mean(homeSLG), 3),
"Away OBP %": round(mean(awayOBP) - mean(homeOBP), 3),
"Away OPS %": round(mean(awayOPS) - mean(homeOPS), 3),
"Away SO %": round(mean(awaySO) - mean(homeSO), 3) * -1,
"Away ERA": round(float(awayPitcherStats['era']) - float(homePitcherStats['era']), 3) * -1,
"Away WHIP": round(float(awayPitcherStats['whip']) - float(homePitcherStats['whip']), 3) * -1,
"Away OBP Against": (round(float(awayPitcherStats['obp']), 2) - round(float(homePitcherStats['obp']), 2)) * -1,
"Away Homeruns/9 Against": (round(float(awayPitcherStats['h9']), 2) - round(float(homePitcherStats['h9']), 2)) * -1,
"Away BB/9 Against": (round(float(awayPitcherStats['bb9']), 2) - round(float(homePitcherStats['bb9']), 2)) * -1,
"Away Bullpen ERA": (round(mean(awayBullpenERA), 3) - round(mean(homeBullpenERA), 3)) * -1,
"Away Bullpen WHIP": (round(mean(awayBullpenWHIP), 3) - round(mean(homeBullpenWHIP), 3)) * -1
}
# Get Game Info
game_request = requests.get(f"http://statsapi.mlb.com/api/v1.1/game/{games}/feed/live").text
game_info = json.loads(game_request)
odds_date_formatted = datetime.datetime.now().strftime("%Y_%m_%d")
game_odds_request = requests.get(f"https://www.fantasylabs.com/api/sportevents/3/{odds_date_formatted}").text
game_odds_json = json.loads(game_odds_request)
if game_info['gameData']['game']['doubleHeader'] == 'Y':
continue
for games in game_odds_json:
if games['HomeTeam'] == homeTeamName:
spread = (games['SpreadSummary'])
ou = (games['OU'])
first_pitch = games['EventTime']
ml_away = games['MLVisitor']
ml_home = games['MLHome']
try:
if (games_request_json['dates'][0]['games'][0]['teams']['home']['isWinner']) == True:
homeWinner = 'Yes'
elif (games_request_json['dates'][0]['games'][0]['teams']['home']['isWinner']) == False:
homeWinner = 'No'
else:
homeWinner = 'TBD'
except:
homeWinner = 'TBD'
try:
if (games_request_json['dates'][0]['games'][0]['teams']['away']['isWinner']) == True:
awayWinner = 'Yes'
elif (games_request_json['dates'][0]['games'][0]['teams']['away']['isWinner']) == False:
awayWinner = 'No'
else:
awayWinner = 'TBD'
except:
awayWinner = 'TBD'
if advantages['Home SO %'] > advantages['Away SO %'] and advantages['Home BA'] > advantages['Away BA'] and advantages['Home OBP %'] > advantages['Away OBP %'] and advantages['Home Slugging %'] > advantages['Away Slugging %'] and advantages[
'Home OBP Against'] > advantages['Away OBP Against'] and advantages['Home Homeruns/9 Against'] > advantages['Home Homeruns/9 Against'] and advantages['Home ERA'] > advantages['Away ERA'] and advantages['Home WHIP'] > advantages['Away WHIP']:
try:
homeAdvantage = (100 - 100 * stats['Home SO %']/stats['Away SO %']) * 0.30 \
+ (100 - 100 * stats['Away Batting Average']/stats['Home Batting Average']) * 0.20 \
+ (100 - 100 * stats['Away OBP %'] / stats['Home OBP %']) * 0.20 \
+ (100 - 100 * stats['Away OPS %'] / stats['Home OPS %']) * 0.15 \
+ (100 - 100 * stats['Away Slugging %']/stats['Home Slugging %']) * 0.15 \
+ (100 - 100 * stats['Home Starting OBP Against']/stats['Away Starting OBP Against']) * 0.30 \
+ (100 - 100 * stats['Home Starting Homeruns/9 Against'] / stats['Away Starting Homeruns/9 Against']) * 0.25 \
+ (100 - 100 * stats['Home Starting ERA'] / stats['Away Starting ERA']) * 0.20 \
+ (100 - 100 * stats['Home Starting WHIP'] / stats['Away Starting WHIP']) * 0.20
except:
continue
projectedWinner = {
'Projected Winner': advantages['Home Team'],
'Winner Advantage (Beta)': round(homeAdvantage, 2),
'Winner ML': ml_home,
'Opponent': advantages['Away Team'],
'Opponent Deficit': -round(homeAdvantage, 2),
'Opponent ML': ml_away,
'Spread': spread,
'Over/Under': ou,
'First Pitch': first_pitch,
'Projected Winning Team Probable Pitcher': game_info['gameData']['probablePitchers']['home']['fullName'],
'Opponent Teams Probable Pitcher': game_info['gameData']['probablePitchers']['away']['fullName'],
'Winner': homeWinner,
'Score': f"{advantages['Home Team']}: {games_request_json['dates'][0]['games'][0]['teams']['home']['score']} {advantages['Away Team']}: {games_request_json['dates'][0]['games'][0]['teams']['away']['score']} ",
'Weather': f"{game_info['gameData']['weather']['temp']}, {game_info['gameData']['weather']['condition']}"
}
if projectedWinner['Winner Advantage (Beta)'] > 20:
projectedOutcome.append(projectedWinner)
if advantages['Away SO %'] > advantages['Home SO %'] and advantages['Away BA'] > advantages['Home BA'] and advantages['Away OBP %'] > advantages['Home OBP %'] and advantages['Away Slugging %'] > advantages['Home Slugging %'] and advantages[
'Away OBP Against'] > advantages['Home OBP Against'] and advantages['Away Homeruns/9 Against'] > advantages['Home Homeruns/9 Against'] and advantages['Away ERA'] > advantages['Home ERA'] and advantages['Away WHIP'] > advantages['Home WHIP']:
try:
awayAdvantage = (100 - 100 * stats['Away SO %']/stats['Home SO %']) * 0.30 \
+ (100 - 100 * stats['Home Batting Average']/stats['Away Batting Average']) * 0.20 \
+ (100 - 100 * stats['Home OBP %']/stats['Away OBP %']) * 0.20 \
+ (100 - 100 * stats['Home OPS %']/stats['Away OPS %']) * 0.15 \
+ (100 - 100 * stats['Home Slugging %']/stats['Away Slugging %']) * 0.15 \
+ (100 - 100 * stats['Away Starting OBP Against']/stats['Home Starting OBP Against']) * 0.30 \
+ (100 - 100 * stats['Away Starting Homeruns/9 Against']/stats['Home Starting Homeruns/9 Against']) * 0.25 \
+ (100 - 100 * stats['Away Starting ERA'] / stats['Home Starting ERA']) * 0.20 \
+ (100 - 100 * stats['Away Starting WHIP'] / stats['Home Starting WHIP']) * 0.20
except:
continue
projectedWinner = {
'Projected Winner': advantages['Away Team'],
'Winner Advantage (Beta)': round(awayAdvantage, 2),
'Winner ML': ml_away,
'Opponent': advantages['Home Team'],
'Opponent Deficit': -round(awayAdvantage, 2),
'Opponent ML': ml_home,
'Spread': spread,
'Over/Under': ou,
'First Pitch': first_pitch,
'Projected Winning Team Probable Pitcher': game_info['gameData']['probablePitchers']['away']['fullName'],
'Opponent Teams Probable Pitcher': game_info['gameData']['probablePitchers']['home']['fullName'],
'Winner': awayWinner,
'Score': f"{advantages['Away Team']}: {games_request_json['dates'][0]['games'][0]['teams']['away']['score']} {advantages['Home Team']}: {games_request_json['dates'][0]['games'][0]['teams']['home']['score']} ",
'Weather': f"{game_info['gameData']['weather']['temp']}, {game_info['gameData']['weather']['condition']}"
}
if projectedWinner['Winner Advantage (Beta)'] > 20:
projectedOutcome.append(projectedWinner)
mlb_teamStats.append(stats)
mlb_advantages.append(advantages)
projectedOutcome_dataframe = pd.DataFrame(data=projectedOutcome)
stats_dataframe = pd.DataFrame(data=mlb_teamStats)
advantages_dataframe = pd.DataFrame(data=mlb_advantages)
stats_dataframe_sorted = stats_dataframe.sort_values(by='Home Team')
advantages_dataframe_sorted = advantages_dataframe.sort_values(by='Home Team')
todaysDate = datetime.datetime.now().strftime("%A, %B %d, %Y")
updateTime = datetime.datetime.now().strftime("%m/%d/%Y %I:%M:%S")
htmlgameanalysis = """<h1>Game Analysis</h1>
<h3>Note: Once our Data shows a favorable matchup, the Game information will appear below</h3>
"""
# Setup HTML for Webpage
htmlheader = "<br></br> <h1> Team Advantages (Differences) </h1>"
htmlheader2 = "<h1>Team Statistics</h1>"
htmltop = f"""
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./MLBStyle.css">
<link href="style.css?t=[timestamp]" type="text/css" rel="stylesheet">
<div class="topnav">
<a class="active" href="#home">Home</a>
</div>
</head>
<body>
<h1> Welcome to MLB Game Predictor</h1>
<h2> {todaysDate} </h2>
"""
htmlbottom = f"""
<br> </br>
<h4> Updated Time: {updateTime} </h4>
</body>
</html>
"""
# Export Tables to HTML Page
with open('/var/www/html/index.html', 'w') as _file:
_file.write(htmltop + htmlgameanalysis + projectedOutcome_dataframe.to_html(index=False) + htmlheader + advantages_dataframe_sorted.to_html(index=False) + htmlheader2 + stats_dataframe_sorted.to_html(index=False) + htmlbottom)