DFS_Portfolio_Manager / global_func /hedging_preset.py
James McCool
Replace distribute_preset with hedging_preset to manage player exposure in lineup generation. Update app.py to reflect the new preset option and remove the obsolete distribute_preset function. This change enhances the flexibility of lineup strategies by allowing users to hedge against high-exposure players while maintaining performance metrics.
119b2bf
raw
history blame
1.64 kB
import pandas as pd
import math
from small_field_preset import small_field_preset
from large_field_preset import large_field_preset
def hedging_preset(portfolio: pd.DataFrame, lineup_target: int, projections_file: pd.DataFrame):
excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Size', 'Win%', 'Lineup Edge', 'Weighted Own', 'Geomean', 'Similarity Score']
check_own_df = projections_file.copy()
check_own_df = check_own_df.sort_values(by='Own', ascending=False)
top_owned = check_own_df['player_names'].head(3).tolist()
concat_portfolio = pd.DataFrame(columns=portfolio.columns)
for players in top_owned:
working_df = portfolio.copy()
# Create mask for lineups that contain any of the removed players
player_columns = [col for col in working_df.columns if col not in excluded_cols]
remove_mask = working_df[player_columns].apply(
lambda row: not any(player in list(row) for player in players), axis=1
)
lock_mask = working_df[player_columns].apply(
lambda row: all(player in list(row) for player in players), axis=1
)
removed_df = working_df[remove_mask]
locked_df = working_df[lock_mask]
removed_lineups = small_field_preset(removed_df, math.ceil(lineup_target / 2), excluded_cols)
locked_lineups = large_field_preset(locked_df, math.ceil(lineup_target / 2), excluded_cols)
concat_portfolio = pd.concat([concat_portfolio, removed_lineups, locked_lineups])
return concat_portfolio.sort_values(by='median', ascending=False).head(lineup_target)