Draftfast DFS Optimization Tool

repository·master·Indexed 43 days ago

https://github.com/benbrostoff/draftfast

A DFS (Daily Fantasy Sports) optimization tool for automating lineup building on platforms like DraftKings and FanDuel. It supports various sports including NFL, NBA, and MLB, providing features for custom RuleSets, player pool generation from CSVs, stack configurations, and conditional logic via CustomRules. Compatible with Python 3.12+.

Tokens
2.9K
Snippets
10
Records
10
Agent score
5%

What's inside Draftfast

  1. Run lineup optimization with draftfast

    master

    To optimize lineups, use the run function from draftfast.optimize. You must provide a rule_set (defining the game constraints) and a player_pool (a list of Player objects or players generated from a CSV).

    from draftfast import rules
    from draftfast.optimize import run
    from draftfast.orm import Player
    
    # Define a player pool
    player_pool = [
        Player(name='A1', cost=5500, proj=55, pos='PG'),
        Player(name='A2', cost=5500, proj=55, pos='PG'),
        # ... more players
    ]
    
    # Run optimization
    roster = run(
        rule_set=rules.DK_NBA_RULE_SET,
        player_pool=player_pool,
        verbose=True,
    )
  2. Run Draftfast via Docker Compose

    master

    You can run the Draftfast service using Docker Compose. The service is configured to run on the linux/arm64 platform (optimized for Apple Silicon Macs) using the intel-ben image. The current configuration mounts the local directory to /dk inside the container and uses tail -F anything as the command to keep the container running.

    services:
      draftfast:
        platform: linux/arm64
        image: intel-ben
        command: tail -F anything
        volumes:
          - .:/dk
  3. Create a custom RuleSet

    master

    If a game is not pre-defined, you can instantiate a rules.RuleSet object to define custom roster sizes, position limits, and salary caps.

    from draftfast import rules
    
    golf_rules = rules.RuleSet(
        site=rules.DRAFT_KINGS,
        league='PGA',
        roster_size='6',
        position_limits=[['G', 6, 6]],
        salary_max=50_000,
    )
  4. Upload rosters via DraftKingsNBAUploader

    master

    To export generated rosters to a file format compatible with DraftKings, use the uploaders module.

    from draftfast.csv_parse import uploaders
    
    uploader = uploaders.DraftKingsNBAUploader(
        pid_file='./pid_file.csv',
    )
    uploader.write_rosters(rosters)
  5. Configure OptimizerSettings with Stacks

    master

    Use OptimizerSettings to pass Stack objects to the run function. Stacks allow you to force the optimizer to pick multiple players from the same team, often with specific position requirements.

    from draftfast.optimize import run
    from draftfast.settings import OptimizerSettings
    
    # Example: A simple team stack
    # Example: A specific QB-WR stack
    stack_config = Stack(
        team='NE',
        count=5,
        stack_lock_pos=['QB'],
        stack_eligible_pos=['WR'],
    )
    
    roster = run(
        rule_set=rules.DK_NHL_RULE_SET,
        player_pool=player_pool,
        verbose=True,
        optimizer_settings=OptimizerSettings(
            stacks=[stack_config]
        ),
    )
  6. Apply CustomRules for conditional logic

    master

    You can define complex conditional logic using CustomRule within OptimizerSettings. This allows you to implement 'if-then' scenarios (e.g., 'If Player A is in, Player B must be in') or 'never together' constraints.

    ```python
    from draftfast.optimize import run
    from draftfast.settings import OptimizerSettings, CustomRule
    
    # Example: If two WRs on one team, play the QB from same team
    settings = OptimizerSettings(
        custom_rules=[
            CustomRule(
                group_a=lambda p: p.pos == 'WR' and p.team == 'Patriots',
                group_b=lambda p: p.pos == 'QB' and p.team == 'Patriots',
                comparison=lambda sum, a, b: sum(a) + 1 <= sum(b)
            )
        ]
    )
    
    roster = run(
        rule_set=rules.DK_NFL_RULE_SET,
        player_pool=nfl_pool,
        verbose=True,
        optimizer_settings=settings,
    )

    Note: Passing closures into CustomRules (e.g., lambda p: p.team == team) currently does not work due to ortools limitations. You cannot dynamically generate rules using external variables inside the lambda.

  7. Generate players from CSV files

    master

    Instead of manually creating Player objects, you can use salary_download.generate_players_from_csvs to parse salary files into a player pool.

    from draftfast.csv_parse import salary_download
    from draftfast import rules
    
    players = salary_download.generate_players_from_csvs(
      salary_file_location='./salaries.csv',
      game=rules.DRAFT_KINGS,
    )
  8. Use LineupConstraints to lock or ban players

    master

    Use LineupConstraints to explicitly lock specific players into lineups, ban players from being selected, or enforce constraints on specific groups of players.

    from draftfast.optimize import run
    from draftfast.settings import LineupConstraints
    
    roster = run(
        rule_set=rules.DK_NFL_RULE_SET,
        player_pool=player_pool,
        verbose=True,
        constraints=LineupConstraints(
            locked=['Rob Gronkowski'],
            banned=['Mark Ingram', 'Doug Martin'],
            groups=[
                # Group constraint: pick between 2 and 3 players from this list
                [('Todd Gurley', 'Melvin Gordon', 'Christian McCaffrey'), (2, 3)],
                # Group constraint: pick exactly 1 player from this list
                [('Chris Carson', 'Mike Davis'), 1],
            ]
        )
    )
  9. Reference: Built-in Game Rule Sets

    master

    The library provides pre-configured RuleSet constants for various leagues and sites. Use these to quickly set up optimization for standard games.

    | League       | Site           | Reference                |
    | ------------- |:-------------:| :-----------------------|
    | NFL           | DraftKings     | `DK_NFL_RULE_SET`        |
    | NFL           | FanDuel        | `FD_NFL_RULE_SET`        |
    | NBA           | DraftKings     | `DK_NBA_RULE_SET`        |
    | NBA           | FanDuel        | `FD_NBA_RULE_SET`        |
    | MLB           | DraftKings     | `DK_MLB_RULE_SET`        |
    | MLB           | FanDuel        | `FD_MLB_RULE_SET`        |
    | WNBA          | DraftKings     | `DK_WNBA_RULE_SET`       |
    | WNBA          | FanDuel        | `FD_WNBA_RULE_SET`       |
    | PGA           | FanDuel        | `FD_PGA_RULE_SET`        |
    | PGA           | DraftKings     | `DK_PGA_RULE_SET`        |
    | PGA_CAPTAIN   | DraftKings     | `DK_PGA_CAPTAIN_RULE_SET`|
    | NASCAR        | FanDuel        | `FD_NASCAR_RULE_SET`     |
    | NASCAR        | DraftKings     | `DK_NASCAR_RULE_SET`     |
    | SOCCER        | DraftKings     | `DK_SOCCER_RULE_SET`     |
    | EuroLeague     | DraftKings     | `DK_EURO_LEAGUE_RULE_SET`|
    | NHL           | DraftKings     | `DK_NHL_RULE_SET`        |
    | NBA Pickem    | DraftKings     | `DK_NBA_PICKEM_RULE_SET` |
    | NFL Showdown  | DraftKings     | `DK_NFL_SHOWDOWN_RULE_SET`|
    | NBA Showdown  | DraftKings     | `DK_NBA_SHOWDOWN_RULE_SET`|
    | MLB Showdown  | DraftKings     | `DK_MLB_SHOWDOWN_RULE_SET`|
    | XFL           | DraftKings     | `DK_XFL_CLASSIC_RULE_SET`|
    | Tennis         | DraftKings     | `DK_TEN_CLASSIC_RULE_SET`|
    | CS:GO          | DraftKings     | `DK_CSGO_SHOWDOWN`       |
    | F1            | DraftKings     | `DK_F1_SHOWDOWN`         |
    | NFL MVP       | FanDuel        | `FD_NFL_MVP_RULE_SET`    |
    | MLB MVP       | FanDuel        | `FD_MLB_MVP_RULE_SET`    |
    | NBA MVP       | FanDuel        | `FD_NBA_MVP_RULE_SET`    |