File size: 12,063 Bytes
88c5476
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import numpy as np
import pandas as pd
import streamlit as st
from itertools import combinations
import pymongo

st.set_page_config(layout="wide")

@st.cache_resource
def init_conn():
        
        uri = st.secrets['mongo_uri']
        client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
        db = client["MLB_Database"]

        return db
    
db = init_conn()

game_format = {'Win Percentage': '{:.2%}','First Inning Lead Percentage': '{:.2%}',
              'Fifth Inning Lead Percentage': '{:.2%}', '8+ runs': '{:.2%}', 'DK LevX': '{:.2%}', 'FD LevX': '{:.2%}'}

team_roo_format = {'Top Score%': '{:.2%}','0 Runs': '{:.2%}', '1 Run': '{:.2%}', '2 Runs': '{:.2%}', '3 Runs': '{:.2%}', '4 Runs': '{:.2%}',
                   '5 Runs': '{:.2%}','6 Runs': '{:.2%}', '7 Runs': '{:.2%}', '8 Runs': '{:.2%}', '9 Runs': '{:.2%}', '10 Runs': '{:.2%}'}

wrong_acro = ['WSH', 'AZ', 'CHW']
right_acro = ['WAS', 'ARI', 'CWS']

st.markdown("""
<style>
    /* Tab styling */
    .stTabs [data-baseweb="tab-list"] {
        gap: 8px;
        padding: 4px;
    }
    .stTabs [data-baseweb="tab"] {
        height: 50px;
        white-space: pre-wrap;
        background-color: #FFD700;
        color: white;
        border-radius: 10px;
        gap: 1px;
        padding: 10px 20px;
        font-weight: bold;
        transition: all 0.3s ease;
    }
    .stTabs [aria-selected="true"] {
        background-color: #DAA520;
        color: white;
    }
    .stTabs [data-baseweb="tab"]:hover {
        background-color: #DAA520;
        cursor: pointer;
    }
</style>""", unsafe_allow_html=True)

@st.cache_resource(ttl = 60)
def init_stat_load():

    collection = db["Player_Range_Of_Outcomes"] 
    cursor = collection.find()

    raw_display = pd.DataFrame(list(cursor))
    raw_display = raw_display[['Player', 'Position', 'Team', 'Salary', 'Floor', 'Median', 'Ceiling', 'Own%', 'Site', 'Slate']]
    raw_display = raw_display.rename(columns={'Own%': 'Own'})
    initial_concat = raw_display.sort_values(by='Own', ascending=False)

    return initial_concat

@st.cache_data
def convert_df_to_csv(df):
    return df.to_csv().encode('utf-8')

proj_raw = init_stat_load()

col1, col2 = st.columns([1, 5])

with col1:
    with st.expander("Info and Filters"):
        if st.button("Load/Reset Data", key='reset1'):
            st.cache_data.clear()
            proj_raw, timestamp = init_stat_load()
            t_stamp = f"Last Update: " + str(timestamp) + f" CST"
            for key in st.session_state.keys():
                del st.session_state[key]
        site_var1 = st.radio("What site are you working with?", ('Draftkings', 'Fanduel'), key='site_var1')
        slate_var1 = st.radio("What slate are you working with?", ('Main Slate', 'Secondary Slate'), key='slate_var1')
        if site_var1 == 'Draftkings':
            raw_baselines = proj_raw[proj_raw['Site'] == 'Draftkings']
            if slate_var1 == 'Main Slate':
                raw_baselines = raw_baselines[raw_baselines['Slate'] == 'main_slate']
            elif slate_var1 == 'Secondary Slate':
                raw_baselines = raw_baselines[raw_baselines['Slate'] == 'secondary_slate']
            raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
        elif site_var1 == 'Fanduel':
            raw_baselines = proj_raw[proj_raw['Site'] == 'Fanduel']
            if slate_var1 == 'Main Slate':
                raw_baselines = raw_baselines[raw_baselines['Slate'] == 'main_slate']
            elif slate_var1 == 'Secondary Slate':
                raw_baselines = raw_baselines[raw_baselines['Slate'] == 'secondary_slate']
            raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
        split_var2 = st.radio("Would you like to run stack analysis for the full slate or individual teams?", ('Full Slate Run', 'Specific Teams'), key='split_var2')
        if split_var2 == 'Specific Teams':
            team_var2 = st.multiselect('Which teams would you like to include in the analysis?', options = raw_baselines['Team'].unique(), key='team_var2')
        elif split_var2 == 'Full Slate Run':
            team_var2 = raw_baselines.Team.unique().tolist()
        pos_split2 = st.radio("Are you viewing all positions, specific groups, or specific positions?", ('All Positions', 'Specific Positions'), key='pos_split2')
        if pos_split2 == 'Specific Positions':
            pos_var2 = st.multiselect('What Positions would you like to view?', options = ['SP', 'P', 'C', '1B', '2B', '3B', 'SS', 'OF'])
        elif pos_split2 == 'All Positions':
            pos_var2 = 'All'
        if site_var1 == 'Draftkings':
            max_sal2 = st.number_input('Max Salary', min_value = 5000, max_value = 50000, value = 35000, step = 100, key='max_sal2')
        elif site_var1 == 'Fanduel':
            max_sal2 = st.number_input('Max Salary', min_value = 5000, max_value = 35000, value = 25000, step = 100, key='max_sal2')
        size_var2 = st.selectbox('What size of stacks are you analyzing?', options = ['3-man', '4-man', '5-man'])
        if size_var2 == '3-man':
            stack_size = 3
        if size_var2 == '4-man':
            stack_size = 4
        if size_var2 == '5-man':
            stack_size = 5

        team_dict = dict(zip(raw_baselines.Player, raw_baselines.Team))
        proj_dict = dict(zip(raw_baselines.Player, raw_baselines.Median))
        own_dict = dict(zip(raw_baselines.Player, raw_baselines.Own))
        cost_dict = dict(zip(raw_baselines.Player, raw_baselines.Salary))

with col2:
    stack_hold_container = st.empty()
    if st.button('Run stack analysis'):
        comb_list = []
        if pos_split2 == 'All Positions':
            raw_baselines = raw_baselines
        elif pos_split2 != 'All Positions':
            raw_baselines = raw_baselines[raw_baselines['Position'].str.contains('|'.join(pos_var2))]

        for cur_team in team_var2:
            working_baselines = raw_baselines
            working_baselines = working_baselines[working_baselines['Team'] == cur_team]
            working_baselines = working_baselines[working_baselines['Position'] != 'SP']
            working_baselines = working_baselines[working_baselines['Position'] != 'P']
            order_list = working_baselines['Player']

            comb = combinations(order_list, stack_size)

            for i in list(comb):
                comb_list.append(i)

        comb_DF = pd.DataFrame(comb_list)

        if stack_size == 3:
            comb_DF['Team'] = comb_DF[0].map(team_dict)

            comb_DF['Proj'] = sum([comb_DF[0].map(proj_dict),
                    comb_DF[1].map(proj_dict),
                    comb_DF[2].map(proj_dict)])

            comb_DF['Salary'] = sum([comb_DF[0].map(cost_dict),
                    comb_DF[1].map(cost_dict),
                    comb_DF[2].map(cost_dict)])

            comb_DF['Own%'] = sum([comb_DF[0].map(own_dict),
                    comb_DF[1].map(own_dict),
                    comb_DF[2].map(own_dict)])
        elif stack_size == 4:
            comb_DF['Team'] = comb_DF[0].map(team_dict)

            comb_DF['Proj'] = sum([comb_DF[0].map(proj_dict),
                    comb_DF[1].map(proj_dict),
                    comb_DF[2].map(proj_dict),
                    comb_DF[3].map(proj_dict)])

            comb_DF['Salary'] = sum([comb_DF[0].map(cost_dict),
                    comb_DF[1].map(cost_dict),
                    comb_DF[2].map(cost_dict),
                    comb_DF[3].map(cost_dict)])

            comb_DF['Own%'] = sum([comb_DF[0].map(own_dict),
                    comb_DF[1].map(own_dict),
                    comb_DF[2].map(own_dict),
                    comb_DF[3].map(own_dict)])
        elif stack_size == 5:
            comb_DF['Team'] = comb_DF[0].map(team_dict)

            comb_DF['Proj'] = sum([comb_DF[0].map(proj_dict),
                    comb_DF[1].map(proj_dict),
                    comb_DF[2].map(proj_dict),
                    comb_DF[3].map(proj_dict),
                    comb_DF[4].map(proj_dict)])

            comb_DF['Salary'] = sum([comb_DF[0].map(cost_dict),
                    comb_DF[1].map(cost_dict),
                    comb_DF[2].map(cost_dict),
                    comb_DF[3].map(cost_dict),
                    comb_DF[4].map(cost_dict)])

            comb_DF['Own%'] = sum([comb_DF[0].map(own_dict),
                    comb_DF[1].map(own_dict),
                    comb_DF[2].map(own_dict),
                    comb_DF[3].map(own_dict),
                    comb_DF[4].map(own_dict)])

        comb_DF = comb_DF.sort_values(by='Proj', ascending=False)
        comb_DF = comb_DF.loc[comb_DF['Salary'] <= max_sal2]

        cut_var = 0

        if stack_size == 3:
            while cut_var <= int(len(comb_DF)):
                try:
                    if int(cut_var) == 0:
                        cur_proj = float(comb_DF.iat[cut_var,4])
                        cur_own = float(comb_DF.iat[cut_var,6])
                    elif int(cut_var) >= 1:
                        check_own = float(comb_DF.iat[cut_var,6])
                        if check_own > cur_own:
                            comb_DF = comb_DF.drop([cut_var])
                            cur_own = cur_own
                            cut_var = cut_var - 1
                            comb_DF = comb_DF.reset_index()
                            comb_DF = comb_DF.drop(['index'], axis=1)
                        elif check_own <= cur_own:
                            cur_own = float(comb_DF.iat[cut_var,6])
                            cut_var = cut_var
                    cut_var += 1
                except:
                    cut_var += 1
        elif stack_size == 4:
            while cut_var <= int(len(comb_DF)):
                try:
                    if int(cut_var) == 0:
                        cur_proj = float(comb_DF.iat[cut_var,5])
                        cur_own = float(comb_DF.iat[cut_var,7])
                    elif int(cut_var) >= 1:
                        check_own = float(comb_DF.iat[cut_var,7])
                        if check_own > cur_own:
                            comb_DF = comb_DF.drop([cut_var])
                            cur_own = cur_own
                            cut_var = cut_var - 1
                            comb_DF = comb_DF.reset_index()
                            comb_DF = comb_DF.drop(['index'], axis=1)
                        elif check_own <= cur_own:
                            cur_own = float(comb_DF.iat[cut_var,7])
                            cut_var = cut_var
                    cut_var += 1
                except:
                    cut_var += 1
        elif stack_size == 5:
            while cut_var <= int(len(comb_DF)):
                try:
                    if int(cut_var) == 0:
                        cur_proj = float(comb_DF.iat[cut_var,6])
                        cur_own = float(comb_DF.iat[cut_var,8])
                    elif int(cut_var) >= 1:
                        check_own = float(comb_DF.iat[cut_var,8])
                        if check_own > cur_own:
                            comb_DF = comb_DF.drop([cut_var])
                            cur_own = cur_own
                            cut_var = cut_var - 1
                            comb_DF = comb_DF.reset_index()
                            comb_DF = comb_DF.drop(['index'], axis=1)
                        elif check_own <= cur_own:
                            cur_own = float(comb_DF.iat[cut_var,8])
                            cut_var = cut_var
                    cut_var += 1
                except:
                    cut_var += 1

        with stack_hold_container:
            stack_hold_container = st.empty()
            st.dataframe(comb_DF.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
        st.download_button(
                label="Export Tables",
                data=convert_df_to_csv(comb_DF),
                file_name='MLB_Stack_Options_export.csv',
                mime='text/csv',
        )