-
Notifications
You must be signed in to change notification settings - Fork 2
/
AAL.py
248 lines (223 loc) · 8.77 KB
/
AAL.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
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
import requests
import urllib.request
import time
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
from datetime import datetime, timedelta, date
import math
from scipy.stats import poisson, expon
from pandas import json_normalize
from functools import reduce
import fuzzywuzzy
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
import os
import tabulate
import time
import warnings
warnings.filterwarnings("ignore")
def matching(arrayStrOne,arrayStrTwo):
matches = []
for i in arrayStrOne:
attempt = [fuzz.token_sort_ratio(str(i), str(j)) for j in arrayStrTwo]
#print(attempt)
matches += [arrayStrTwo[np.argmax(attempt)]]
return attempt
def tryMatch(i,j):
return fuzz.token_sort_ratio(str(i), str(j))
def to_dataframe(listing):
home, away, scoreH, scoreA = [], [], [], []
for i in range(len(listing)):
#print(i%3, listing[i])
if i%3 ==0:
home.append(listing[i].lower())
elif i%3 == 1:
away.append(listing[i].lower())
else:
score = listing[i].split('-')
#print(score)
if len(score) ==2:
scoreH.append(int(score[0].strip()))
scoreA.append(int(score[1].strip()))
else:
scoreH.append(np.NaN)
scoreA.append(np.NaN)
gameLog = pd.DataFrame({'gameDate':[i for i in range(len(home))],'Home':home, 'Away':away,'HomeGoals':scoreH,'AwayGoals':scoreA})
#print(gameLog)
return gameLog.dropna()
def parse_data(jsonData):
results_df = pd.DataFrame()
#print(jsonData)
for alpha in jsonData['events']:
gameday = (alpha['tsstart'][:10])
if (gameday == str(date.today())):
print ('Gathering %s data: %s @ %s' %(alpha['sportname'],alpha['participantname_away'],alpha['participantname_home']))
alpha_df = json_normalize(alpha).drop('markets',axis=1)
for beta in alpha['markets']:
#print(beta['selections']) #merge "getOdds" with this parse
beta_df = json_normalize(beta).drop('selections',axis=1)
beta_df.columns = [str(col) + '.markets' for col in beta_df.columns]
for theta in beta['selections']:
theta_df = json_normalize(theta)
theta_df.columns = [str(col) + '.selections' for col in theta_df.columns]
temp_df = reduce(lambda left,right: pd.merge(left,right, left_index=True, right_index=True), [alpha_df, beta_df, theta_df])
results_df = results_df.append(temp_df, sort=True).reset_index(drop=True)
return results_df #time right for <7 on prev day
def fullSet(eventID):
return requests.get('https://sportsbook.fanduel.com//cache/psevent/UK/1/false/'+ str(eventID) + '.json').json()
def searchingForGame(jsonData):
results_df = pd.DataFrame()
alpha = jsonData['events'][0]
gameday = alpha['tsstart'][:10]
today = str(date.today())
#print(today, gameday)
return today == gameday
def gameToday():
jsonData_fanduel_epl = requests.get('https://sportsbook.fanduel.com/cache/psmg/UK/56609.3.json').json()
boolean = searchingForGame(jsonData_fanduel_epl)
return boolean
def build(oddsDataFrame,dataInput): #NEEDS WORK !!!!!!!
betting = []
for i in range(len(oddsDataFrame.iloc[:,0].values)):
betName = oddsDataFrame.iloc[:,1].values[i]
game = oddsDataFrame.iloc[:,0].values[i]
for i in oddsDataFrame.iloc[i,2:].values:
if i!=None:
betting += [betFunction(game, betName,i, GoalsLookup)]
df = pd.DataFrame(betting).dropna()
df = df.reset_index()
df.columns = ['Bet Number','Game','Team','Payout','Type']
return df
def getOdds(listing):
bets = []
#print(len(listing))
for game in listing:
for i in game['eventmarketgroups'][0]['markets']:
#print(i['name'])
betName = [game['externaldescription'], i['name']]
if i['name'] == 'Moneyline':
for i in i['selections']:
betName+=[[i['name'], 1+(i['currentpriceup']/i['currentpricedown'])]] #, i['currenthandicap']
bets += [betName]
return bets
def fetch():
try:
jsonData_fanduel_epl = requests.get('https://sportsbook.fanduel.com/cache/psmg/UK/56609.3.json').json() #gives the game id
except:
print('Not a problem, the XHR has been changed for the EPL, go ahead and fix that then run again')
epl = parse_data(jsonData_fanduel_epl)
#print(epl)
EPL = pd.DataFrame(epl)[['eventname','tsstart','idfoevent.markets']]
EPL.columns = ['Teams','Date','EventID']
listing = []
for i in np.unique(EPL.EventID.values):
listing.append((fullSet(i)))
df = (pd.DataFrame(getOdds(listing)))
df.columns = ['GameName', 'Type', 'HomeTeamandOdds', 'DrawOdds', 'AwayTeamandOdds']
df = df[df.Type=='Moneyline']
#df = df[df.GameName != 'Shrewsbury v Lincoln']
probabilities = fetchName()
#print(probabilities)
#check if all of them are there
valued = []
#print(probabilities.gameNum.values)
for i in np.unique(probabilities.gameNum.values):
newdf = probabilities[probabilities.gameNum == i]
valued += [newdf.ID.values[1][:]]
#print(valued)
sorting = np.sort(valued)
indices, counterArray, soughtGameArray = [], [], []
counter = 0
gamed = []
#print((len(df.GameName.values), len(sorting)))
for i in (df.GameName.values):
temp = []
for j in np.unique(sorting):
temp += [tryMatch(i,j)]
#print(temp)
sought = (sorting[temp.index(np.max(temp))])
soughtgameNum = probabilities[probabilities.ID == sought].gameNum.values[0]
counterArray += [counter]
soughtGameArray += [soughtgameNum]
counter += 1
fixed = pd.DataFrame({'sought':soughtGameArray, 'linked':counterArray}).sort_values(['sought'])
linker = []
for i in fixed.linked.values:
linker += [i]
linker += [i]
linker += [i]
probabilities['gameNum'] = linker
array ,counter = [], 0
for i in probabilities.gameNum.values:
if counter%3 == 0:
indexed = probabilities.gameNum.values[counter]
valued = df.HomeTeamandOdds.values[i][-1]
array+= [valued]
counter = counter+1
elif counter%3 == 1:
indexed = probabilities.gameNum.values[counter]
valued = df.DrawOdds.values[i][-1]
array+= [valued]
counter = counter+1
else:
indexed = probabilities.gameNum.values[counter]
valued = df.AwayTeamandOdds.values[i][-1]
array += [valued]
counter = counter+1
EV = []
for i in range(len(array)):
EV += [probabilities.Probabilities.values[i]*array[i]]
Result = pd.DataFrame({'Team':probabilities.ID.values, 'Probability': probabilities.Probabilities.values, 'Odds':array, 'EV':EV})
Bet = Result[Result.EV >1.00]
kelly = [Kelly(Bet.Odds.values[i], Bet.Probability.values[i]) for i in range(len(Bet.Probability.values))]
Betting = pd.DataFrame({'Bet State Chosen':Bet.Team.values, 'Kelly Criterion Suggestion': kelly, 'Payouts (per Dollar)':Bet.Odds.values})
return Betting
def fetchName():
url = 'https://projects.fivethirtyeight.com/soccer-predictions/a-league/'
page_response = requests.get(url, timeout=10, headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9,fr;q=0.8,ro;q=0.7,ru;q=0.6,la;q=0.5,pt;q=0.4,de;q=0.3',
'cache-control': 'max-age=0',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'})
page_content = BeautifulSoup(page_response.content, "html.parser")
navigate = page_content.findAll('div', class_="games-container upcoming")[0]
Today = navigate.findAll('tbody')
teams, prob = [], []
for i in Today:
if (i.find('div').text == str(date.today().strftime("%-m/%-d"))): #this is to be changed
home = i.findAll('td', class_ = "team")[0]['data-str']
away = i.findAll('td', class_ = "team")[1]['data-str']
teams += [home, 'Draw ' + str(home)+ ' v ' +str(away),away]
prob +=[float(j.text[:-1])/100 for j in i.findAll('td', class_="prob")]
indexed = []
epl = pd.DataFrame({'ID':teams, 'Probabilities':prob})
epi = epl.drop_duplicates()
for i in range(int(len(epi)/3)):
indexed += [i]*3
epi['gameNum'] = indexed
return epi
def oddstoPayout(odds,dollarsIn):
if odds<0:
multiplier = 1/(abs(odds/100))
return dollarsIn + dollarsIn*multiplier
else:
multiplier = odds/100
return dollarsIn + dollarsIn*multiplier
def Kelly(oddsDecimal, probability):
return (oddsDecimal*probability - (1-probability))/oddsDecimal
def picks(): #this needs some work/checking
result = fetch().round(decimals=2)
print(result.to_markdown())
resulting = result[['Bet State Chosen', 'Kelly Criterion Suggestion','Payouts (per Dollar)']]
resulting['League'] = ['AAL']*len(resulting['Bet State Chosen'])
resulting['Date'] = [str(date.today())]*len(resulting['Bet State Chosen'])
return resulting
def run():
if gameToday():
return picks()
else:
return 'No AAL games today.'
print(run())