DimaKudosh / pydfs-lineup-optimizer

Daily Fantasy Sports lineup optimzer for all popular daily fantasy sports sites
MIT License
424 stars 157 forks source link

Position Stack opposing team (either position) #378

Open wizardous23 opened 2 years ago

wizardous23 commented 2 years ago

I can force a stack for a player on an opposing team for my QB as follows:

optimizer.force_positions_for_opposing_team(('QB', 'WR'))

But how can I say that I want the opposing position to be EITHER a WR or TE

Ideally I'm looking for something like this:

optimizer.force_positions_for_opposing_team(('QB', ['WR', 'TE']))

chrisbach1 commented 2 years ago

optimizer.add_stack(PositionsStack(['QB', ('WR', 'TE', 'DST')])) I think this is what you are looking for. It will stack a QB with any of the 3 positions...WR,TE, or DST

lightninglarry commented 2 years ago

optimizer.add_stack(PositionsStack(['QB', ('WR', 'TE', 'DST')])) I think this is what you are looking for. It will stack a QB with any of the 3 positions...WR,TE, or DST

i think hes wanting a wr or te from the OPPOSING team, not the qbs team.

chrisbach1 commented 2 years ago

I see. Not sure why you would stack a QB with his opposing teams pass receivers. Seems counter productive. Maybe the shoot out between MIA and BAL would have been good for that.

I put it in my optimizer twice and it worked. Sometimes it was a TE and sometimes it was a WR

optimizer.force_positions_for_opposing_team(('QB', 'WR')) optimizer.force_positions_for_opposing_team(('QB', 'TE'))

lightninglarry commented 2 years ago

getting a "bringback" is good lineup building philosophy. Roughly a 1/3 of the time something like qb + wr + opp wr or opp Te, or qb + te + opp wr + opp te is in the optimal lineup.

With your example does it put only 1 opposing wr or Te with a QB?

chrisbach1 commented 2 years ago

That's good to know, maybe incorporate that now! I added both lines and ran 150 lineups. Sometimes it was a TE and sometimes it was a WR. Sometimes both.

Note I had a max_exposure setting on all lineups to 0.25

tlabropoulos commented 2 years ago

@chrisbach1 @lightninglarry

optimizer.force_positions_for_opposing_team(('QB', 'WR')) optimizer.force_positions_for_opposing_team(('QB', 'TE'))

That doesn't seem to work. I'm trying to figure out something similar as well, and tried the above recommended but it still seems to stick with 1 vs either/or. Breaking my head over this!

chrisbach1 commented 2 years ago

If you change your TE's projection to something higher, will it work? It's still going to optimize the lineup, so if your wide receivers projection is a lot higher, it will probably include him vs a TE

PochynokB commented 2 years ago

I am having the same issue. The optimizer will select whichever statement you use last. I will follow this thread incase this is resolved.

cdioriodfs commented 1 year ago

This code allows you to have a bringback into your lineups, as this was something I have been trying to figure out as well and finally did:

bringback_options = [('QB', 'RB'), ('QB', 'WR'), ('QB', 'TE')] selected_bringback = random.choice(bringback_options)

optimizer.add_stack(PositionsStack(['QB', 'WR', ('WR', 'TE')], stack_teams)) optimizer.force_positions_for_opposing_team(selected_bringback)

The stack is a QB with a WR and a WR or TE from one of my stack teams. Then force the positions from the opposing to be the random selection of RB, WR, or TE in the bringback options. This has worked for me to get a double stack and bringback in each lineup.

chrisbach1 commented 1 year ago

@cdioriodfs where is 'stack_teams' coming from? its saying not defined

Denwen12 commented 1 year ago

from pydfs_lineup_optimizer.stacks import TeamStack, PositionsStack, PlayersGroup, Stack, GameStack

chrisbach1 commented 1 year ago

stack_teams is not a function is "Stacks.py"

cdioriodfs commented 1 year ago

@chrisbach1 stack_teams is a just a variable I created that stores the teams that I want to stack. Would looks something like this:

stack_teams = [“PHI”, “KC”, “JAX”, “LAC”]

This was just so I didn’t get stacks/qbs in my lineups that I didn’t want to stack that week.

Denwen12 commented 1 year ago

Try stack_teams = ['PHI', 'KC', 'JAX', 'LAC']

On Mon, Nov 6, 2023, 3:49 PM cdioriodfs @.***> wrote:

@chrisbach1 https://github.com/chrisbach1 stack_teams is a just a variable I created that stores the teams that I want to stack. Would looks something like this:

stack_teams = [“PHI”, “KC”, “JAX”, “LAC”]

This was just so I didn’t get stacks/qbs in my lineups that I didn’t want to stack that week.

— Reply to this email directly, view it on GitHub https://github.com/DimaKudosh/pydfs-lineup-optimizer/issues/378#issuecomment-1796411064, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANBWO7V56YKBF5P5GVHWRGLYDFENFAVCNFSM6AAAAAAQPWWAVCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOJWGQYTCMBWGQ . You are receiving this because you commented.Message ID: @.***>

cdioriodfs commented 1 year ago

The code I posted works to get a double stack and a bring back, but the same position is used on the bringback for each qb, For example, every Jalen Hurts lineup would have a double stack and a WR bringback for every one, no RB or TE. I cannot get the code to choose between RB, WR, or TE for each lineup, bit it chooses for each QB if that makes sense.

Denwen12 commented 1 year ago

Can i see all the code

On Mon, Nov 6, 2023, 3:54 PM cdioriodfs @.***> wrote:

The code I posted works to get a double stack and a bring back, but the same position is used on the bringback for each qb, For example, every Jalen Hurts lineup would have a double stack and a WR bringback for every one, no RB or TE. I cannot get the code to choose between RB, WR, or TE for each lineup, bit it chooses for each QB if that makes sense.

— Reply to this email directly, view it on GitHub https://github.com/DimaKudosh/pydfs-lineup-optimizer/issues/378#issuecomment-1796417301, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANBWO7S5EMH4HSXRCTBHWFDYDFE6XAVCNFSM6AAAAAAQPWWAVCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOJWGQYTOMZQGE . You are receiving this because you commented.Message ID: @.***>

chrisbach1 commented 1 year ago

Doesnt work for me. Only tight ends work

Denwen12 commented 1 year ago

Use randomness

On Mon, Nov 6, 2023, 5:12 PM Chris Bachmann @.***> wrote:

Doesnt work for me. Only tight ends work

— Reply to this email directly, view it on GitHub https://github.com/DimaKudosh/pydfs-lineup-optimizer/issues/378#issuecomment-1796843543, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANBWO7UYKUJKVRXZFION3ITYDFODDAVCNFSM6AAAAAAQPWWAVCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOJWHA2DGNJUGM . You are receiving this because you commented.Message ID: @.***>

chrisbach1 commented 1 year ago

I did

chrisbach1 commented 1 year ago

Its fine. Im not too worried about it. Thanks for the help!

Denwen12 commented 1 year ago

Post your code

On Mon, Nov 6, 2023, 5:17 PM Chris Bachmann @.***> wrote:

I did

— Reply to this email directly, view it on GitHub https://github.com/DimaKudosh/pydfs-lineup-optimizer/issues/378#issuecomment-1796855945, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANBWO7UUBW33HK6HCUFJ5MTYDFOWXAVCNFSM6AAAAAAQPWWAVCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOJWHA2TKOJUGU . You are receiving this because you commented.Message ID: @.***>

cdioriodfs commented 1 year ago

@Denwen12

import pydfs_lineup_optimizer from pydfs_lineup_optimizer import get_optimizer, Site, Sport, LineupOptimizer from pydfs_lineup_optimizer.stacks import GameStack, Stack, NestedPlayersGroup, OptimizerStack, TeamStack, PositionsStack from pydfs_lineup_optimizer import PlayersGroup, PlayerFilter, Player from pydfs_lineup_optimizer.utils import get_players_grouped_by_teams import random

Initialize the optimizer and load players from the CSV file

optimizer = get_optimizer(Site.DRAFTKINGS, Sport.FOOTBALL) optimizer.load_players_from_csv("#####")

optimizer.set_min_salary_cap(49700)

stack_teams = ['PHI', 'LAC', 'CIN', 'JAX', 'BAL']

qb_filter = PlayerFilter(positions=['QB'], from_value=15, filter_by='fppg') rb_filter = PlayerFilter(positions=['RB'], from_value=6, filter_by='fppg') wr_filter = PlayerFilter(positions=['WR'], from_value=6, filter_by='fppg') te_filter = PlayerFilter(positions=['TE'], from_value=4, filter_by='fppg') dst_filter = PlayerFilter(positions=['DST'], from_value=2, filter_by='fppg')

optimizer.player_pool.add_filters(qb_filter, rb_filter, wr_filter, te_filter, dst_filter)

qbs = optimizer.player_pool.get_players(qb_filter) rbs = optimizer.player_pool.get_players(rb_filter) wrs = optimizer.player_pool.get_players(wr_filter) tes = optimizer.player_pool.get_players(te_filter) dsts = optimizer.player_pool.get_players(dst_filter)

players_by_team = get_players_grouped_by_teams(optimizer.player_pool.filtered_players)

dataGames = [a for a in optimizer.player_pool.games]

games = [f'{a.home_team}, {a.away_team}, game {list(optimizer.player_pool.games).index(a)}' for a in optimizer.player_pool.games]

qb_exposure = 0.20 rb_exposure = 0.35 wr_exposure = 0.60 te_exposure = 0.25 dst_exposure = 0.40

for qb in qbs: qb.max_exposure = qb_exposure

for rb in rbs: rb.max_exposure = rb_exposure

for wr in wrs: wr.max_exposure = wr_exposure

for te in tes: te.max_exposure = te_exposure

for dst in dsts: dst.max_exposure = dst_exposure

all_teams = list({player.team for player in optimizer.player_pool.filtered_players})

optimizer.restrict_positions_for_same_team(('RB', 'RB'),('QB', 'DST'),('QB', 'RB'), ('WR', 'RB')) optimizer.restrict_positions_for_opposing_team(('QB','RB','WR','TE'), 'DST')

optimizer.set_max_repeating_players(3)

te_group = PlayersGroup(tes, max_from_group=1) rb_group = PlayersGroup(rbs, max_from_group=2) optimizer.add_players_group(te_group) optimizer.add_players_group(rb_group)

bringback_options = [('QB', 'WR'), ('QB', 'RB'), ('QB', 'TE')] selected_bringback = random.choice(bringback_options)

optimizer.add_stack(PositionsStack(['QB', 'WR', ('WR', 'TE')], for_teams=stack_teams)) optimizer.force_positions_for_opposing_team(selected_bringback) optimizer.force_positions_for_opposing_team optimizer.add_stack(GameStack(2, min_from_team=1))

Generate lineups

lineups = list(optimizer.optimize(n=20))

Export lineups to CSV file

optimizer.export("#####")

dfsdude commented 2 weeks ago

@Denwen12

import pydfs_lineup_optimizer from pydfs_lineup_optimizer import get_optimizer, Site, Sport, LineupOptimizer from pydfs_lineup_optimizer.stacks import GameStack, Stack, NestedPlayersGroup, OptimizerStack, TeamStack, PositionsStack from pydfs_lineup_optimizer import PlayersGroup, PlayerFilter, Player from pydfs_lineup_optimizer.utils import get_players_grouped_by_teams import random

Initialize the optimizer and load players from the CSV file

optimizer = get_optimizer(Site.DRAFTKINGS, Sport.FOOTBALL) optimizer.load_players_from_csv("#####")

optimizer.set_min_salary_cap(49700)

stack_teams = ['PHI', 'LAC', 'CIN', 'JAX', 'BAL']

qb_filter = PlayerFilter(positions=['QB'], from_value=15, filter_by='fppg') rb_filter = PlayerFilter(positions=['RB'], from_value=6, filter_by='fppg') wr_filter = PlayerFilter(positions=['WR'], from_value=6, filter_by='fppg') te_filter = PlayerFilter(positions=['TE'], from_value=4, filter_by='fppg') dst_filter = PlayerFilter(positions=['DST'], from_value=2, filter_by='fppg')

optimizer.player_pool.add_filters(qb_filter, rb_filter, wr_filter, te_filter, dst_filter)

qbs = optimizer.player_pool.get_players(qb_filter) rbs = optimizer.player_pool.get_players(rb_filter) wrs = optimizer.player_pool.get_players(wr_filter) tes = optimizer.player_pool.get_players(te_filter) dsts = optimizer.player_pool.get_players(dst_filter)

players_by_team = get_players_grouped_by_teams(optimizer.player_pool.filtered_players)

dataGames = [a for a in optimizer.player_pool.games]

games = [f'{a.home_team}, {a.away_team}, game {list(optimizer.player_pool.games).index(a)}' for a in optimizer.player_pool.games]

qb_exposure = 0.20 rb_exposure = 0.35 wr_exposure = 0.60 te_exposure = 0.25 dst_exposure = 0.40

for qb in qbs: qb.max_exposure = qb_exposure

for rb in rbs: rb.max_exposure = rb_exposure

for wr in wrs: wr.max_exposure = wr_exposure

for te in tes: te.max_exposure = te_exposure

for dst in dsts: dst.max_exposure = dst_exposure

all_teams = list({player.team for player in optimizer.player_pool.filtered_players})

optimizer.restrict_positions_for_same_team(('RB', 'RB'),('QB', 'DST'),('QB', 'RB'), ('WR', 'RB')) optimizer.restrict_positions_for_opposing_team(('QB','RB','WR','TE'), 'DST')

optimizer.set_max_repeating_players(3)

te_group = PlayersGroup(tes, max_from_group=1) rb_group = PlayersGroup(rbs, max_from_group=2) optimizer.add_players_group(te_group) optimizer.add_players_group(rb_group)

bringback_options = [('QB', 'WR'), ('QB', 'RB'), ('QB', 'TE')] selected_bringback = random.choice(bringback_options)

optimizer.add_stack(PositionsStack(['QB', 'WR', ('WR', 'TE')], for_teams=stack_teams)) optimizer.force_positions_for_opposing_team(selected_bringback) optimizer.force_positions_for_opposing_team optimizer.add_stack(GameStack(2, min_from_team=1))

Generate lineups

lineups = list(optimizer.optimize(n=20))

Export lineups to CSV file

optimizer.export("#####")

Much appreciated! I've been looking for a simple solution like this all season

dfsdude commented 2 weeks ago

@cdioriodfs thank you again for sharing your code. You helped me win a pretty big tournament yesterday