-
Notifications
You must be signed in to change notification settings - Fork 2
/
nba_dfs.py
executable file
·379 lines (316 loc) · 11.6 KB
/
nba_dfs.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import requests
import pandas
from bs4 import BeautifulSoup
import pulp
from numpy import random, ceil
import MySQLdb
from datetime import datetime, timedelta
import re
# TODO
#-- INJURIES. avoid q's? limit experts in FP to those updated most recently (on sunday)
#-- remove zeroes when assigning df to speed it up
#-- more loops?
#-- automatically submit
#-- nba adjustments
#-- better formatting
#-- backtesting with data from old games
#-- what to do when player is two positions? cory harkey
#-- add more projections (yahoo, nfl, fleaflicker?)
#-- fix dst and K scoring
#-- when do salaries update? do it earlier? auto adjust?
#-- single factor for all? up and down scenario?
#-- use player scoring stddev
current_time = datetime.now()
week = ceil((current_time - datetime.strptime('2013-10-29', '%Y-%m-%d')).days/7.0)
gamedate = current_time.date()
mydb = MySQLdb.connect(host="localhost", user='root', db="dfs")
mycursor = mydb.cursor()
mycursor.execute('delete from optimization where date = "%s" and sport = "nba"' % gamedate)
site_settings = {
"draft-street": {
"caps": [100000],
"roster": {
"pg": 0,
"sg": 0,
"pf": 0,
"sf": 0,
"c": 1,
"g": 3,
"f": 3,
"u": 1
},
"scoring": {
"pts": 1,
"reb": 1.25,
"ast": 1.5,
"stl": 2,
"blk": 2,
"bonus_3pt": 0,
"to": -1,
"mft": -0.5,
"mfg": -0.5,
"dd": 0,
"td": 0
}
},
"draftkings": {
"caps": [50000],
"roster": {
"pg": 1,
"sg": 1,
"pf": 1,
"sf": 1,
"c": 1,
"g": 1,
"f": 1,
"u": 1
},
"scoring": {
"pts": 1,
"reb": 1.25,
"ast": 1.5,
"stl": 2,
"blk": 2,
"bonus_3pt": 0.5,
"to": -0.5,
"mft": 0,
"mfg": 0,
"dd": 1.5,
"td": 3
}
},
"fanthrowdown": {
"caps": [100000], #maybe others, none listed
"roster": {
"pg": 2,
"sg": 2,
"pf": 2,
"sf": 2,
"c": 1,
"g": 0,
"f": 0,
"u": 1
},
"scoring": {
"pts": 1,
"reb": 1.25,
"ast": 1.5,
"stl": 2,
"blk": 2,
"bonus_3pt": 0,
"to": -1,
"mft": 0,
"mfg": 0,
"dd": 0,
"td": 0
}
},
"fantasyaces": {
"caps": [50000,55000], #unclear which. also salarypro, not sure what that is
"roster": {
"pg": 0,
"sg": 0,
"pf": 0,
"sf": 0,
"c": 1,
"g": 3,
"f": 3,
"u": 2
},
"scoring": {
"pts": 1,
"reb": 1.25,
"ast": 1.5,
"stl": 2,
"blk": 2,
"bonus_3pt": 0,
"to": -1,
"mft": 0,
"mfg": 0,
"dd": 0,
"td": 0
}
},
"starstreet": {
"caps": [100000],
"roster": {
"pg": 1,
"sg": 1,
"pf": 1,
"sf": 1,
"c": 1,
"g": 1,
"f": 1,
"u": 2
},
"scoring": {
"pts": 1,
"reb": 1.25,
"ast": 1.5,
"stl": 2,
"blk": 2,
"bonus_3pt": 0,
"to": -1,
"mft": 0,
"mfg": 0,
"dd": 0,
"td": 0
}
},
"fantasy-feud": {
"caps": [1000000],
"roster": {
"pg": 0,
"sg": 0,
"pf": 0,
"sf": 0,
"c": 2,
"g": 3,
"f": 3,
"u": 2
},
"scoring": {
"pts": 1,
"reb": 1.25,
"ast": 1.5,
"stl": 2,
"blk": 2,
"bonus_3pt": 0,
"to": -1,
"mft": 0,
"mfg": 0,
"dd": 0,
"td": 0
}
},
"fan-duel": {
"caps": [55000,60000,65000],
"roster": {
"pg": 2,
"sg": 2,
"pf": 2,
"sf": 2,
"c": 1,
"g": 0,
"f": 0,
"u": 0
},
"scoring": {
"pts": 1,
"reb": 1.2,
"ast": 1.5,
"stl": 2,
"blk": 2,
"bonus_3pt": 0,
"to": -1,
"mft": 0,
"mfg": 0,
"dd": 0,
"td": 0
}
},
"draftday": {
"caps": [100000],
"roster": {
"pg": 2,
"sg": 2,
"pf": 2,
"sf": 2,
"c": 1,
"g": 0,
"f": 0,
"u": 0
},
"scoring": {
"pts": 1,
"reb": 1.25,
"ast": 1.5,
"stl": 2,
"blk": 2,
"bonus_3pt": 0,
"to": -1,
"mft": -0.25,
"mfg": -0.25,
"dd": 0,
"td": 0
}
}
}
#- Calculate projected points for each site
for k in site_settings.keys():
player_data = {}
position_data = {}
site = site_settings[k]
caps = site['caps']
roster = site['roster']
for ps in roster.keys():
position_data[ps] = []
print '====================> Optimizing site: ', k, ' <===================='
dfs = requests.get('http://dfsedge.com/nba-tool/?site=%s' % k).text
soup = BeautifulSoup(dfs)
dfs_table = soup.find(id='UTILTab').table.tbody.find_all('tr')
for player in dfs_table:
tds = player.find_all('td')
name = tds[1].string.strip()
pos = tds[2].string.lower()
sal = int(tds[3].string.strip().replace('$','').replace(',',''))
pts = float(tds[7].string.strip())
#if tds[4].span.string in ('MEM', 'DET'):
# continue
positions = re.findall(r'[\w]+', pos)
player_data[name] = (positions, sal, pts)
for p in positions:
position_data[p].append(name)
if name not in position_data[p[-1]]:
position_data[p[-1]].append(name)
mycursor.execute('insert into players values ("%s", "%s", "%s", "nba", %s, "%s", %s, %s, %s, %s, "%s") on duplicate key update salary = %s, points = %s, points_1 = %s, points_2 = %s, updated = "%s";' % (name, p, k, week, gamedate, player_data[name][1], player_data[name][2], player_data[name][2], player_data[name][2], current_time, player_data[name][1], player_data[name][2], player_data[name][2], player_data[name][2], current_time))
pg_names = position_data['pg']
sg_names = position_data['sg']
pf_names = position_data['pf']
sf_names = position_data['sf']
c_names = position_data['c']
g_names = position_data['g']
f_names = position_data['f']
all_names = set(c_names + g_names + f_names)
for cap in caps:
print '----- Cap of %s -----' % cap
prob = pulp.LpProblem("%s Optimization" % k, pulp.LpMaximize)
#- Set the variables (boolean player names)
player_vars = pulp.LpVariable.dicts("Players", all_names, cat="Binary")
#- Set the objective (maximize points)
prob += pulp.lpSum([player_data[i][2] * player_vars[i] for i in all_names]), "Total Points"
#- Set the contraints (budget and then roster)
prob += pulp.lpSum([player_data[i][1] * player_vars[i] for i in all_names]) <= cap, "Total Cost"
prob += pulp.lpSum([player_vars[i] for i in pg_names]) >= roster['pg'], "Total PGs"
prob += pulp.lpSum([player_vars[i] for i in sg_names]) >= roster['sg'], "Total SGs"
prob += pulp.lpSum([player_vars[i] for i in pf_names]) >= roster['pf'], "Total PFs"
prob += pulp.lpSum([player_vars[i] for i in sf_names]) >= roster['sf'], "Total SFs"
prob += pulp.lpSum([player_vars[i] for i in c_names]) >= roster['c'], "Total Cs"
prob += pulp.lpSum([player_vars[i] for i in g_names]) >= roster['pg'] + roster['sg'] + roster['g'], "Total Gs"
prob += pulp.lpSum([player_vars[i] for i in f_names]) >= roster['pf'] + roster['sf'] + roster['f'], "Total Fs"
prob += pulp.lpSum([player_vars[i] for i in all_names]) == roster['pg'] + roster['sg'] + roster['pf'] + roster['sf'] + roster['c'] + roster['g'] + roster['f'] + roster['u'], "Total Us"
#- Solve
prob.writeLP("%s.lp" % k)
prob.solve()
if pulp.LpStatus[prob.status] != 'Optimal':
print 'Status not optimal -> %s' % pulp.LpStatus[prob.status]
else:
new_player_vars = {}
for on,nn in player_vars.iteritems():
new_player_vars[nn.name] = on
total_salary = 0
print "%-*s%-*s%-*s%s" % (25, 'Player', 10, 'Pos', 10, 'Cost', 'Points')
for v in prob.variables():
if v.varValue == 1:
old_name = new_player_vars[v.name]
po = player_data[old_name][0]
sal = player_data[old_name][1]
pts = player_data[old_name][2]
total_salary += sal
mycursor.execute('insert into optimization values("%s", "%s", "nba", %s, "%s", %s, "%s", "%s");' % (old_name, k, week, gamedate, cap, 'norm', current_time))
print "%-*s%-*s%-*s%s" % (25, old_name, 10, '/'.join(up.upper() for up in po), 10, sal, pts)
print "Total Points: ", pulp.value(prob.objective)
print "Total Salary: %s (%s%% of %s)" % (total_salary, (total_salary*1.0/cap*1.0)*100.0, cap)
print '====================> Done (', k, ') <===================='