Scripts for optimising fantasy Allsvenskan teams using player history, forecasting, and integer linear programming.
- Python 3.12+
- uv
- PostgreSQL 18+
1. Install dependencies
uv sync2. Configure the database
Create a .env file in the project root:
DATABASE_URL=postgresql://<user>:<password>@localhost:5432/fantasy_allsvenskan
3. Create tables
uv run python scripts/init_db.py4. Ingest data
uv run python scripts/ingest.py # uses local JSON cache for player histories
uv run python scripts/ingest.py --refresh # force re-fetch everything from the APIThe venv is registered as a Jupyter kernel named "Fantasy Allsvenskan".
uv run jupyter notebookSelect the Fantasy Allsvenskan kernel in the notebook interface.
Run these scripts in order after each gameweek:
# 1. Refresh the database
uv run python scripts/ingest.py
# 2. Build forecasts
uv run python scripts/build_forecasts.py
# 3. Run the optimiser
uv run python scripts/optimize_team.py --team-file data/curr_team/myteam.jsonfantasy_optimizer/ # Core library
api_client.py # API fetching with local JSON cache
db/ # Database layer (SQLAlchemy models, upsert helpers)
models/ # Pydantic models for API data validation
scripts/
ingest.py # All data ingestion (players, teams, fixtures, histories)
init_db.py # Creates database tables (run once on new setup)
build_forecasts.py # Builds per-player expected points forecasts
optimize_team.py # Team optimisation (CVXPY integer linear programming)
data_fetching/ # Fetch helpers called by ingest.py
data/ # Local JSON cache (gitignored)
notebooks/ # Jupyter notebooks
Install pre-commit hooks (runs isort, black, ruff on each commit):
uv run pre-commit install