Why am I getting a duplicate key error if Iβm assigning a unique key to both of the select boxes?
import streamlit as st
import time
from modules.page import Page
import pandas as pd
import json
class BacktestResults(Page):
def __init__(self):
super().__init__()
self.based_on_dict = {
'Closed Trades': ['overview', 'closed_trades'],
'Net Profit': ['overview', 'net_profit'],
'Percent Profitable': ['overview', 'percent_profitable'],
'Expectancy': ['overview', 'average_profit'],
'Buy & Hold Return': ['performance_summary', 'buy_n_hold_return'],
'Max Drawdown': ['overview', 'max_drawdown'],
'Sharpe Ratio': ['performance_summary', 'results', 'sharpe_ratio'],
'Sortino Ratio': ['performance_summary', 'results', 'sortino_ratio'],
'Calmar Ratio': ['performance_summary', 'results', 'calmar_ratio']
}
def _setup(self):
self.setup(
layout = "wide",
initial_sidebar_state = "expanded"
)
self._top50()
self._best_indexes()
def _top50(self):
sort_by_combinations = st.selectbox(
"Sort By",
options = ['Select an option', 'Closed Trades', 'Net Profit', 'Percent Profitable', 'Expectancy', 'Profit Factor (Pain to Gain)', 'Rate of Return (Annualized)', 'Buy & Hold Return', 'Max Drawdown', 'Risk of Ruin', 'Sharpe Ratio', 'Sortino Ratio', 'Calmar Ratio'],
on_change = self._top50,
key = "sort_by_combinations_selectbox"
)
if sort_by_combinations != 'Select an option':
try:
top_combinations = self.data_analysis.get_top_combinations(
based_on = self.based_on_dict[sort_by_combinations],
limit = 50
)
# Generate results table
dicts = []
for combination in top_combinations:
d = {
"Combination": combination[0],
"Closed Trades": combination[1]["overview"]["closed_trades"],
"Net Profit": combination[1]["overview"]["net_profit"],
"Percent Profitable": combination[1]["overview"]["percent_profitable"],
"Expectancy": combination[1]["overview"]["average_profit"],
"Profit Factor (Pain to Gain)": combination[1]["overview"]["profit_factor"],
"Buy & Hold Return": combination[1]["performance_summary"]["results"]["buy_n_hold_return"],
"Max Drawdown": combination[1]["overview"]["max_drawdown"],
"Sharpe Ratio": combination[1]["performance_summary"]["results"]["sharpe_ratio"],
"Sortino Ratio": combination[1]["performance_summary"]["results"]["sortino_ratio"],
"Calmar Ratio": combination[1]["performance_summary"]["results"]["calmar_ratio"]
}
dicts.append(d)
# Convert the list of dictionaries into a pandas DataFrame
df = pd.DataFrame(dicts)
st.markdown("## Top 50 Combinations")
st.dataframe(df, use_container_width = True)
except Exception as error:
print(error)
def _best_indexes(self):
st.markdown(
"---"
)
sort_by_indexes = st.selectbox(
label = "Sort By",
options = ('Select an option', 'Closed Trades', 'Net Profit', 'Percent Profitable', 'Expectancy', 'Profit Factor (Pain to Gain)', 'Rate of Return (Annualized)', 'Buy & Hold Return', 'Max Drawdown', 'Risk of Ruin', 'Sharpe Ratio', 'Sortino Ratio', 'Calmar Ratio'),
key = "sort_by_indexes_selectbox"
)
if sort_by_indexes != 'Select an option':
best_indexes = self.data_analysis.best_indexes(
based_on = self.based_on_dict[sort_by_indexes],
format_message = False
)
best_indexes_table = ""
for key, value in best_indexes["data"].items():
best_indexes_table += f"""
### {self.data_handler.variables[int(key)]['name']}
"""
for repeated in value:
best_indexes_table += f"""
{repeated['value']} is repeated {repeated['repeated']} times with a {repeated['average']} average
"""
st.markdown(
best_indexes_table
)
backtest_results = BacktestResults()
backtest_results._setup()