% python generate.py player_data1.csv
Usage of programs is provided here.
scrape.py: Not to be called by user. Walks from DraftKing’s NBA lobby into a contest and downloads that day’s lineup of available players and their salaries into player_data.csv. It then searches for every player in this csv on basketball-reference.com, enters their individual page, and writes to player_data.csv the data it finds on their previous 4 games. Each player’s fantasy projection is written as the average of their last two games in the column ‘last two.’ If they scored more in the two games prior to their most recent two then they are removed from player_data.csv as we do not want players with negative momentum.
optimize.py: Not to be called by user. Uses scrape.py generated csv to print best lineup. First creates list of all players playing that night, the list is sorted by a value column which is equal to the player’s projection / (their salary / 1000). Then determines best lineup by populating the guard, forward, and utility positions with the first 3 players in the sorted playerlist who can legally fill the 3 positions. To fill the 5 remaining positions (PG,SG,SF,PF,C), the program brute force tries every legal combination. It then prints the best lineup, unless that lineup is projected to score less than 270 points (270 is the safety threshold point I determined where larger scores could usually win DraftKings 50-50 contests). Optimize.py can alternatively just print the best value players at a specified position as explained below.
generate.py: Main function to be called by user. Calls the other two programs dependent on what arguments it is passed. Called in the following ways from shell:
% python generate.py
calls scrape.py (which can take up to 10 minutes) and then optimize.py. Prints optimal lineup.
% python generate.py player_data.csv
runs optimize.py on csv specified (csv must have been previously generated by scrape.py). Prints optimal lineup..
% python generate.py player_data.csv position
Prints list of players in the specified csv sorted by value . List include only players who play specified position. position options are: "powerforward", "smallforward","guard", "pointguard”, "shootingguard, "util", “forward”, ”center”:
The next step is to use the gathered data to put in a real model for predicting each night's performance. Currently the projection is just an average of recent performance.