MLB Trade Analyzer

To assist in my fantasy baseball league, I developed a tool to analyze and suggest trades that would be beneficial to both teams. The tool implements web scraping, data analysis, and forecasting to model the rest of a given fantasy baseball season.

Data Scraping and Cleaning

Since ESPN provides no public API to get fantasy data, the tool first uses the Selenium library to scrape the data needed for the project. It launches a web driver, logs in to ESPN, and scrapes the tables for the standings as well as each team’s player stats. This data is then cleaned and placed into Pandas DataFrames for the standings and players for each team, such as the following tables.

OwnerHRTBRBI...SVHDERA
Michael Smith260269085482143.802
William Booth261274690085273.637
Kenan Bateman256260085058433.638
Dan Hay26026178527233.702
Josh Melton212247176483113.451
SlotPlayerPositions(Season, HR)(Season, RBI)(Season, BB)...(7 Days, AVG)(7 Days, OBP)(7 Days, FPCT)
CJ.T. RealmutoC144829.278.3501.000
1BMatt Olson1B4310779.414.5751.000
2BAndres Gimenez2B114527.435.4801.000
...
UTILRonald Acuna Jr.OF, DH267165.313.4501.000
UTILBrandon NimmoOF164858.391.4810.929
UTILJustin Turner3B, 1B, 2B, DH197339.444.4441.000

Roster Optimization

For my personal roster, I created an additional tool that optimizes the starting lineup by prioritizing certain stat categories. This is a generic constraint satisfaction problem as each players can only occupy certain roster positions. So, the tool first finds the top 14 players on the roster for a given stat category. It then tries to find a roster lineup using just those players.

If it can’t fill every roster spot, it loosens the constraint and tries to fill roster spots with the top 13 players before filling the final spot with the next best player of that position. If this doesn’t work, it continues loosening the constraint and trying to fill the roster with the top n-1 number of players until it successfully staffs those players.

The image below shows how the tool steps through the logic using an example stat. It repeatedly iterates through the players and staffs any player with only a single position remaining (tightest constraint). Once a position has been staffed, the tool removes that position from the other remaining players. If no positions remain, it staffs the player in the utility spot. It continues iterating until all players are staffed.

Season Forecasting

Once the roster is staffed, the tool then

 

The tool checks every stat to see which lineup projects the best season-ending points standings.

 

  • – used these stats to forecast out the season and project season-ending stats
  • – using the strengths and weaknesses of each team’s rosters to suggest trades that would improve both players rosters
  • – For each of these trades, it had to consider how adding/removing players from each roster would both affect the possible starting lineups as well as stat projections
  • – It then showed a summary of each trade and how much it projected the trade would shift each team’s season-ending ranking

Languages

Python, HTML, CSS

Technology

Selenium, Pandas, Numpy