Daveabc12 commited on
Commit
b86d2a6
Β·
1 Parent(s): 0adf836

Updated the chart drops

Browse files
Files changed (2) hide show
  1. app.py +45 -20
  2. app.txt +813 -0
app.py CHANGED
@@ -586,9 +586,6 @@ def main():
586
  st.session_state.confidence_results_df = None
587
  st.session_state.open_trades_df = None
588
  st.session_state.best_params = None
589
- st.session_state.advisor_df = None
590
- st.session_state.run_analysis_button = False
591
- st.session_state.run_advanced_advisor = False
592
 
593
  st.title("πŸ“ˆ Stock Backtesting Sandbox")
594
  st.success(f"Good morning! Today is {date.today().strftime('%A, %d %B %Y')}.")
@@ -621,9 +618,9 @@ def main():
621
  st.sidebar.date_input("End Date", master_df.index.max().date(), key='end_date')
622
  st.markdown("""<style>div[data-testid="stSidebar"] button[kind="primary"] { background-color: #4CAF50; color: white; border-color: #4CAF50;}</style>""", unsafe_allow_html=True)
623
 
624
- if st.sidebar.button("πŸš€ Run Analysis", type="primary"):
625
- st.session_state.run_analysis_button = True
626
- st.rerun()
627
 
628
  st.sidebar.markdown("---")
629
 
@@ -648,25 +645,52 @@ def main():
648
 
649
  st.sidebar.markdown("---")
650
  st.sidebar.header("4. Find Best Parameters")
651
- with st.sidebar.expander("Set Optimisation Ranges", expanded=False):
652
- # Omitted for brevity
653
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
654
 
655
  st.sidebar.markdown("---")
656
  st.sidebar.header("5. Find Best/Worst Confidence Setup")
657
- with st.sidebar.expander("Optimise Confidence Factors", expanded=False):
658
- # Omitted for brevity
659
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
660
 
661
  st.sidebar.markdown("---")
662
- st.sidebar.header("6. Advanced Advisor")
663
- st.sidebar.info("Uses saved top setups from Section 5. Re-run an optimisation to update them.")
664
- if st.sidebar.button("πŸ” Generate Advisor Report"):
665
- st.session_state.run_advanced_advisor = True
666
- st.rerun()
667
-
668
- st.sidebar.markdown("---")
669
- # --- FIX: Correct indentation for this block ---
670
  if st.session_state.get('veto_setup'):
671
  st.sidebar.header("Veto Filter")
672
  st.sidebar.success("Veto filter is ACTIVE.")
@@ -680,6 +704,7 @@ def main():
680
 
681
  if st.sidebar.button("πŸ’Ύ Save Settings as Default"):
682
  save_settings({ "large_ma_period": st.session_state.ma_period, "bband_period": st.session_state.bb_period, "bband_std_dev": st.session_state.bb_std, "confidence_threshold": st.session_state.confidence_slider, "long_entry_threshold_pct": st.session_state.long_entry / 100, "long_exit_ma_threshold_pct": st.session_state.long_exit / 100, "long_stop_loss_pct": st.session_state.long_sl / 100, "long_delay_days": st.session_state.long_delay, "short_entry_threshold_pct": st.session_state.short_entry / 100, "short_exit_ma_threshold_pct": st.session_state.short_exit / 100, "short_stop_loss_pct": st.session_state.short_sl / 100, "short_delay_days": st.session_state.short_delay, })
 
683
 
684
  # --- Trigger actions based on session state flags ---
685
  if st.session_state.get('run_analysis_button'):
 
586
  st.session_state.confidence_results_df = None
587
  st.session_state.open_trades_df = None
588
  st.session_state.best_params = None
 
 
 
589
 
590
  st.title("πŸ“ˆ Stock Backtesting Sandbox")
591
  st.success(f"Good morning! Today is {date.today().strftime('%A, %d %B %Y')}.")
 
618
  st.sidebar.date_input("End Date", master_df.index.max().date(), key='end_date')
619
  st.markdown("""<style>div[data-testid="stSidebar"] button[kind="primary"] { background-color: #4CAF50; color: white; border-color: #4CAF50;}</style>""", unsafe_allow_html=True)
620
 
621
+ if st.sidebar.button("πŸš€ Run Analysis", type="primary", key="run_analysis_button"):
622
+ st.session_state.confidence_results_df = None
623
+ st.session_state.best_params = None
624
 
625
  st.sidebar.markdown("---")
626
 
 
645
 
646
  st.sidebar.markdown("---")
647
  st.sidebar.header("4. Find Best Parameters")
648
+ with st.sidebar.expander("Set Optimisation Ranges"):
649
+ use_squared_weighting = st.toggle("Prioritise Profit per Trade (Squared Weighting)")
650
+ st.markdown("---")
651
+ optimise_ma = st.checkbox("Optimise MA Period", False, key="opt_ma_cb")
652
+ c1,c2,c3 = st.columns(3); st.session_state.ma_start_num = c1.number_input("MA Start", 10, 200, 50, 5, disabled=not optimise_ma, key='ma_start'); st.session_state.ma_end_num = c2.number_input("MA End", 10, 200, 55, 5, disabled=not optimise_ma, key='ma_end'); st.session_state.ma_step_num = c3.number_input("MA Step", 1, 20, 5, disabled=not optimise_ma, key='ma_step')
653
+ optimise_bb = st.checkbox("Optimise BB Period", False, key="opt_bb_cb")
654
+ c1,c2,c3 = st.columns(3); st.session_state.bb_start_num = c1.number_input("BB Start", 10, 100, 20, 5, disabled=not optimise_bb, key='bb_start'); st.session_state.bb_end_num = c2.number_input("BB End", 10, 100, 25, 5, disabled=not optimise_bb, key='bb_end'); st.session_state.bb_step_num = c3.number_input("BB Step", 1, 10, 5, disabled=not optimise_bb, key='bb_step')
655
+ optimise_std = st.checkbox("Optimise BB Std Dev", False, key="opt_std_cb")
656
+ c1,c2,c3 = st.columns(3); st.session_state.std_start_num = c1.number_input("Std Start", 1.0, 4.0, 2.0, 0.1, format="%.1f", disabled=not optimise_std, key='std_start'); st.session_state.std_end_num = c2.number_input("Std End", 1.0, 4.0, 2.1, 0.1, format="%.1f", disabled=not optimise_std, key='std_end'); st.session_state.std_step_num = c3.number_input("Std Step", 0.1, 1.0, 0.1, format="%.1f", disabled=not optimise_std, key='std_step')
657
+ st.markdown("---")
658
+ optimise_conf = st.checkbox("Optimise Confidence Threshold", False, key="opt_conf_cb")
659
+ c1,c2,c3 = st.columns(3); st.session_state.conf_start_num = c1.number_input("Conf Start", 0, 100, 50, 5, disabled=not optimise_conf, key='conf_start'); st.session_state.conf_end_num = c2.number_input("Conf End", 0, 100, 75, 5, disabled=not optimise_conf, key='conf_end'); st.session_state.conf_step_num = c3.number_input("Conf Step", 5, 25, 5, disabled=not optimise_conf, key='conf_step')
660
+ optimise_sl = st.checkbox("Optimise Stop Loss %", False, key="opt_sl_cb")
661
+ c1,c2,c3 = st.columns(3); st.session_state.sl_start_num = c1.number_input("SL Start", 0.0, 30.0, 2.0, 0.5, disabled=not optimise_sl, key='sl_start'); st.session_state.sl_end_num = c2.number_input("SL End", 0.0, 30.0, 5.0, 0.5, disabled=not optimise_sl, key='sl_end'); st.session_state.sl_step_num = c3.number_input("SL Step", 0.1, 5.0, 0.5, disabled=not optimise_sl, key='sl_step')
662
+ optimise_delay = st.checkbox("Optimise Delay Days", False, key="opt_delay_cb")
663
+ c1,c2,c3 = st.columns(3); st.session_state.delay_start_num = c1.number_input("Delay Start", 0, 5, 0, 1, disabled=not optimise_delay, key='delay_start'); st.session_state.delay_end_num = c2.number_input("Delay End", 0, 5, 1, 1, disabled=not optimise_delay, key='delay_end'); st.session_state.delay_step_num = c3.number_input("Delay Step", 1, 5, 1, disabled=not optimise_delay, key='delay_step')
664
+ optimise_entry = st.checkbox("Optimise Entry %", False, key="opt_entry_cb")
665
+ c1,c2,c3 = st.columns(3); st.session_state.entry_start_num = c1.number_input("Entry Start", 0.0, 10.0, 0.0, 0.1, disabled=not optimise_entry, key='entry_start'); st.session_state.entry_end_num = c2.number_input("Entry End", 0.0, 10.0, 1.0, 0.1, disabled=not optimise_entry, key='entry_end'); st.session_state.entry_step_num = c3.number_input("Entry Step", 0.1, 1.0, 0.1, disabled=not optimise_entry, key='entry_step')
666
+ optimise_exit = st.checkbox("Optimise Exit MA %", False, key="opt_exit_cb")
667
+ c1,c2,c3 = st.columns(3); st.session_state.exit_start_num = c1.number_input("Exit Start", 0.0, 10.0, 0.0, 0.1, disabled=not optimise_exit, key='exit_start'); st.session_state.exit_end_num = c2.number_input("Exit End", 0.0, 10.0, 1.0, 0.1, disabled=not optimise_exit, key='exit_end'); st.session_state.exit_step_num = c3.number_input("Exit Step", 0.1, 1.0, 0.1, disabled=not optimise_exit, key='exit_step')
668
+ st.markdown("---")
669
+ col1, col2 = st.columns(2)
670
+ if col1.button("πŸ’‘ Find Best Long"): generate_and_run_optimisation(master_df, main_content_placeholder, 'long', use_squared_weighting)
671
+ if col2.button("πŸ’‘ Find Best Short"): generate_and_run_optimisation(master_df, main_content_placeholder, 'short', use_squared_weighting)
672
 
673
  st.sidebar.markdown("---")
674
  st.sidebar.header("5. Find Best/Worst Confidence Setup")
675
+ with st.sidebar.expander("Optimise Confidence Factors"):
676
+ st.info("Finds good setups (using Section 2 factors) or bad setups (using the factors below).")
677
+ st.write("**Find Best Setups (High Profit)**"); c1, c2 = st.columns(2)
678
+ if c1.button("πŸ’‘ Find Best Long Confidence"): run_confidence_optimisation('long', 'best', master_df, main_content_placeholder, None)
679
+ if c2.button("πŸ’‘ Find Best Short Confidence"): run_confidence_optimisation('short', 'best', master_df, main_content_placeholder, None)
680
+ st.markdown("---")
681
+ st.write("**Find Worst Setups (for Veto Filter)**")
682
+ st.caption("Select the factors to test for the Veto signal:")
683
+ c1, c2 = st.columns(2)
684
+ veto_rsi = c1.toggle("Veto RSI", value=True)
685
+ veto_vol = c2.toggle("Veto Volatility", value=True)
686
+ veto_trend = c1.toggle("Veto Trend", value=True)
687
+ veto_volume = c2.toggle("Veto Volume", value=True)
688
+ veto_factors = (veto_rsi, veto_vol, veto_trend, veto_volume)
689
+ c1, c2 = st.columns(2)
690
+ if c1.button("❌ Find Worst Long"): run_confidence_optimisation('long', 'worst', master_df, main_content_placeholder, veto_factors)
691
+ if c2.button("❌ Find Worst Short"): run_confidence_optimisation('short', 'worst', master_df, main_content_placeholder, veto_factors)
692
 
693
  st.sidebar.markdown("---")
 
 
 
 
 
 
 
 
694
  if st.session_state.get('veto_setup'):
695
  st.sidebar.header("Veto Filter")
696
  st.sidebar.success("Veto filter is ACTIVE.")
 
704
 
705
  if st.sidebar.button("πŸ’Ύ Save Settings as Default"):
706
  save_settings({ "large_ma_period": st.session_state.ma_period, "bband_period": st.session_state.bb_period, "bband_std_dev": st.session_state.bb_std, "confidence_threshold": st.session_state.confidence_slider, "long_entry_threshold_pct": st.session_state.long_entry / 100, "long_exit_ma_threshold_pct": st.session_state.long_exit / 100, "long_stop_loss_pct": st.session_state.long_sl / 100, "long_delay_days": st.session_state.long_delay, "short_entry_threshold_pct": st.session_state.short_entry / 100, "short_exit_ma_threshold_pct": st.session_state.short_exit / 100, "short_stop_loss_pct": st.session_state.short_sl / 100, "short_delay_days": st.session_state.short_delay, })
707
+
708
 
709
  # --- Trigger actions based on session state flags ---
710
  if st.session_state.get('run_analysis_button'):
app.txt ADDED
@@ -0,0 +1,813 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import os
4
+ import numpy as np
5
+ from datetime import date
6
+ import plotly.graph_objects as go
7
+ import itertools
8
+ import json
9
+ # --- MODIFIED IMPORTS: Changed to import specific indicator classes from 'ta' ---
10
+ from ta.volatility import BollingerBands
11
+ from ta.momentum import RSIIndicator
12
+ from multiprocessing import Pool, cpu_count
13
+ from functools import partial
14
+
15
+ # --- 0. Settings Management Functions ---
16
+ CONFIG_FILE = "config.json"
17
+ VETO_CONFIG_FILE = "veto_config.json"
18
+ TOP_SETUPS_FILE = "top_setups.json"
19
+
20
+ def save_settings(params_to_save):
21
+ with open(CONFIG_FILE, 'w') as f:
22
+ json.dump(params_to_save, f, indent=4)
23
+ st.sidebar.success("Settings saved as default!")
24
+
25
+ def load_settings():
26
+ default_structure = { "large_ma_period": 50, "bband_period": 20, "bband_std_dev": 2.0, "long_entry_threshold_pct": 0.0, "long_exit_ma_threshold_pct": 0.0, "long_stop_loss_pct": 0.0, "long_delay_days": 0, "short_entry_threshold_pct": 0.0, "short_exit_ma_threshold_pct": 0.0, "short_stop_loss_pct": 0.0, "short_delay_days": 0, "confidence_threshold": 50 }
27
+ if os.path.exists(CONFIG_FILE):
28
+ with open(CONFIG_FILE, 'r') as f:
29
+ loaded = json.load(f)
30
+ default_structure.update(loaded)
31
+ return default_structure
32
+ return default_structure
33
+
34
+ def save_veto_setup(veto_setup):
35
+ with open(VETO_CONFIG_FILE, 'w') as f:
36
+ json.dump(veto_setup, f, indent=4)
37
+ st.sidebar.success("Veto filter saved as default!")
38
+
39
+ def load_veto_setup():
40
+ if os.path.exists(VETO_CONFIG_FILE):
41
+ with open(VETO_CONFIG_FILE, 'r') as f:
42
+ return json.load(f)
43
+ return None
44
+
45
+ def save_top_setups(results_df, side, num_setups=6):
46
+ df = results_df.copy()
47
+
48
+ deduplication_cols = [
49
+ 'Conf. Threshold', 'Avg Profit/Trade', 'Good/Bad Ratio',
50
+ 'Winning Tickers', 'Losing Tickers', 'Avg Entry Conf.',
51
+ 'Good Score', 'Bad Score', 'Norm. Score %', 'Total Trades'
52
+ ]
53
+
54
+ df['FactorsOn'] = df[['RSI', 'Volatility', 'TREND', 'Volume']].apply(lambda row: (row == 'On').sum(), axis=1)
55
+ sort_col = 'Good Score' if side in ['long', 'best'] else 'Bad Score'
56
+
57
+ sorted_df = df.sort_values(
58
+ by=[sort_col, 'FactorsOn'],
59
+ ascending=[False, True]
60
+ )
61
+ deduplicated_df = sorted_df.drop_duplicates(subset=deduplication_cols, keep='first')
62
+
63
+ top_setups = deduplicated_df.head(num_setups).to_dict('records')
64
+
65
+ if os.path.exists(TOP_SETUPS_FILE):
66
+ with open(TOP_SETUPS_FILE, 'r') as f:
67
+ all_top_setups = json.load(f)
68
+ else:
69
+ all_top_setups = {}
70
+
71
+ all_top_setups[side] = top_setups
72
+
73
+ with open(TOP_SETUPS_FILE, 'w') as f:
74
+ json.dump(all_top_setups, f, indent=4)
75
+
76
+ st.sidebar.success(f"Top {len(top_setups)} unique {side.title()} setups saved!")
77
+
78
+ def load_top_setups():
79
+ if os.path.exists(TOP_SETUPS_FILE):
80
+ with open(TOP_SETUPS_FILE, 'r') as f:
81
+ return json.load(f)
82
+ return None
83
+
84
+ # --- 1. Data Loading and Cleaning Functions ---
85
+ @st.cache_data
86
+ def load_all_data(folder_path):
87
+ all_files = [f for f in os.listdir(folder_path) if f.endswith('.csv')]
88
+ if not all_files:
89
+ st.error("No CSV files found in the 'csv_data' folder.")
90
+ return None, None
91
+
92
+ df_list = []
93
+
94
+ for file_name in all_files:
95
+ file_path = os.path.join(folder_path, file_name)
96
+ try:
97
+ df = pd.read_csv(file_path, header=0, index_col=0, dayfirst=True, parse_dates=True)
98
+ df_list.append(df)
99
+ except Exception as e:
100
+ return None, f"Could not read or process {file_name}. Error: {e}"
101
+
102
+ if not df_list:
103
+ return None, "No data could be loaded from the CSV files."
104
+
105
+ master_df = pd.concat(df_list)
106
+ master_df.index = pd.to_datetime(master_df.index, errors='coerce')
107
+ master_df = master_df[master_df.index.notna()]
108
+
109
+ if master_df.index.has_duplicates:
110
+ master_df = master_df.loc[~master_df.index.duplicated(keep='last')]
111
+
112
+ master_df.sort_index(inplace=True)
113
+ return master_df, f"Successfully combined data from {len(all_files)} files."
114
+
115
+ def clean_data_and_report_outliers(df):
116
+ outlier_report = []
117
+ price_columns = [col for col in df.columns if '_volume' not in str(col).lower()]
118
+ for ticker in price_columns:
119
+ numeric_prices = pd.to_numeric(df[ticker], errors='coerce')
120
+ daily_pct_change = numeric_prices.pct_change().abs()
121
+ outlier_days = daily_pct_change[daily_pct_change > 1.0].index
122
+ if not outlier_days.empty:
123
+ outlier_report.append({'Ticker': ticker, 'Outliers Removed': len(outlier_days)})
124
+ df.loc[outlier_days, ticker] = np.nan
125
+ return df, outlier_report
126
+
127
+ def normalise_strategy_score(raw_score, benchmark_for_100_percent=0.25):
128
+ if raw_score <= 0: return 0.0
129
+ return min((raw_score / benchmark_for_100_percent) * 100, 100.0)
130
+
131
+ # --- 2. Custom Backtesting Engine ---
132
+ def calculate_confidence_score(df, use_rsi, use_volatility, use_trend, use_volume, rsi_w, vol_w, trend_w, vol_w_val):
133
+ long_score = pd.Series(0.0, index=df.index)
134
+ short_score = pd.Series(0.0, index=df.index)
135
+ total_weight = 0.0
136
+ if use_rsi and 'RSI' in df.columns:
137
+ total_weight += rsi_w
138
+ long_score += ((30 - df['RSI']) / 30).clip(0, 1) * rsi_w
139
+ short_score += ((df['RSI'] - 70) / 30).clip(0, 1) * rsi_w
140
+ if use_volatility and 'Volatility_p' in df.columns:
141
+ total_weight += vol_w
142
+ score = (df['Volatility_p'] > 0.025).astype(float) * vol_w
143
+ long_score += score
144
+ short_score += score
145
+ if use_trend and 'SMA_200' in df.columns:
146
+ total_weight += trend_w
147
+ pct_dist = (df['Close'] - df['SMA_200']) / df['SMA_200']
148
+ long_score += (pct_dist / 0.10).clip(0, 1) * trend_w
149
+ short_score += (-pct_dist / 0.10).clip(0, 1) * trend_w
150
+ if use_volume and 'Volume_Ratio' in df.columns:
151
+ total_weight += vol_w_val
152
+ score = ((df['Volume_Ratio'] - 1.75) / 2.25).clip(0, 1) * vol_w_val
153
+ long_score += score
154
+ short_score += score
155
+ if total_weight > 0:
156
+ return (long_score / total_weight) * 100, (short_score / total_weight) * 100
157
+ return pd.Series(100.0, index=df.index), pd.Series(100.0, index=df.index)
158
+
159
+ def run_backtest(data, params, use_rsi, use_volatility, use_trend, use_volume, rsi_weight, volatility_weight, trend_weight, volume_weight, veto_setup=None):
160
+ df = data.copy()
161
+ df['Close'] = pd.to_numeric(df['Close'], errors='coerce').replace(0, np.nan)
162
+ df.dropna(subset=['Close'], inplace=True)
163
+ if len(df) < params.get('large_ma_period', 200) or len(df) < params.get('bband_period', 20):
164
+ return 0, 0, 0, 0, None, ([], [], [], []), []
165
+ df['large_ma'] = df['Close'].rolling(window=params['large_ma_period']).mean()
166
+
167
+ # --- CORRECTED: Calculate Bollinger Bands using the 'ta' library ---
168
+ indicator_bb = BollingerBands(close=df['Close'], window=params['bband_period'], window_dev=params['bband_std_dev'])
169
+ df['bband_lower'] = indicator_bb.bollinger_lband()
170
+ df['bband_upper'] = indicator_bb.bollinger_hband()
171
+
172
+ # --- CORRECTED: Calculate RSI using the 'ta' library ---
173
+ indicator_rsi = RSIIndicator(close=df['Close'], window=14)
174
+ df['RSI'] = indicator_rsi.rsi()
175
+
176
+ df['Volatility_p'] = df['Close'].pct_change().rolling(window=14).std()
177
+ df['SMA_200'] = df['Close'].rolling(window=200, min_periods=1).mean()
178
+ if 'Volume' in df.columns:
179
+ df['Volume'] = pd.to_numeric(df['Volume'], errors='coerce').fillna(0)
180
+ df['Volume_MA50'] = df['Volume'].rolling(window=50, min_periods=1).mean()
181
+ df['Volume_Ratio'] = (df['Volume'] / df['Volume_MA50']).replace([np.inf, -np.inf], np.nan).fillna(0)
182
+ df['long_confidence_score'], df['short_confidence_score'] = calculate_confidence_score(df, use_rsi, use_volatility, use_trend, use_volume, rsi_weight, volatility_weight, trend_weight, volume_weight)
183
+ if veto_setup:
184
+ veto_weight = veto_setup.get('Weight', 1.0)
185
+ df['long_veto_score'], df['short_veto_score'] = calculate_confidence_score(df, veto_setup['RSI'], veto_setup['Volatility'], veto_setup['TREND'], veto_setup['Volume'], veto_weight, veto_weight, veto_weight, veto_weight)
186
+ base_long_trigger = df['Close'] < (df['bband_lower'] * (1 - params['long_entry_threshold_pct']))
187
+ base_short_trigger = df['Close'] > (df['bband_upper'] * (1 + params['short_entry_threshold_pct']))
188
+ long_entry_trigger = base_long_trigger & (df['long_confidence_score'] >= params['confidence_threshold'])
189
+ short_entry_trigger = base_short_trigger & (df['short_confidence_score'] >= params['confidence_threshold'])
190
+ if veto_setup:
191
+ long_veto_trigger = df['long_veto_score'] >= veto_setup['Conf. Threshold']
192
+ short_veto_trigger = df['short_veto_score'] >= veto_setup['Conf. Threshold']
193
+ long_entry_trigger &= ~long_veto_trigger
194
+ short_entry_trigger &= ~short_veto_trigger
195
+ long_exit_trigger = (df['Close'] >= (df['large_ma'] * (1 + params['long_exit_ma_threshold_pct']))) | (df['Close'] >= df['bband_upper'])
196
+ short_exit_trigger = (df['Close'] <= (df['large_ma'] * (1 - params['short_exit_ma_threshold_pct']))) | (df['Close'] <= df['bband_lower'])
197
+ df['long_signal'] = np.nan; df.loc[long_entry_trigger, 'long_signal'] = 1; df.loc[long_exit_trigger, 'long_signal'] = 0
198
+ df['short_signal'] = np.nan; df.loc[short_entry_trigger, 'short_signal'] = -1; df.loc[short_exit_trigger, 'short_signal'] = 0
199
+ df['long_position'] = df['long_signal'].ffill().fillna(0); df['short_position'] = df['short_signal'].ffill().fillna(0)
200
+ if params['long_delay_days'] > 0: df['long_position'] = df['long_position'].shift(params['long_delay_days']).fillna(0)
201
+ if params['short_delay_days'] > 0: df['short_position'] = df['short_position'].shift(params['short_delay_days']).fillna(0)
202
+ if params['long_stop_loss_pct'] > 0:
203
+ long_entry_prices = df['Close'].where((df['long_position'] == 1) & (df['long_position'].shift(1) == 0)).ffill()
204
+ long_sl_hit = (df['Close'] < (long_entry_prices * (1 - params['long_stop_loss_pct']))) & (df['long_position'] == 1)
205
+ for index in long_sl_hit[long_sl_hit].index: df.loc[index:, 'long_position'] = 0
206
+ if params['short_stop_loss_pct'] > 0:
207
+ short_entry_prices = df['Close'].where((df['short_position'] == -1) & (df['short_position'].shift(1) == 0)).ffill()
208
+ short_sl_hit = (df['Close'] > (short_entry_prices * (1 + params['short_stop_loss_pct']))) & (df['short_position'] == -1)
209
+ for index in short_sl_hit[short_sl_hit].index: df.loc[index:, 'short_position'] = 0
210
+ df['daily_return'] = df['Close'].pct_change()
211
+ df['long_strategy_return'] = df['long_position'].shift(1) * df['daily_return']
212
+ df['short_strategy_return'] = df['short_position'].shift(1) * df['daily_return']
213
+ final_long_pnl = (1 + df['long_strategy_return']).prod(skipna=True) - 1
214
+ final_short_pnl = (1 + df['short_strategy_return']).prod(skipna=True) - 1
215
+ long_entries = df[(df['long_position'] == 1) & (df['long_position'].shift(1) == 0)]
216
+ long_exits = df[(df['long_position'] == 0) & (df['long_position'].shift(1) == 1)]
217
+ short_entries = df[(df['short_position'] == -1) & (df['short_position'].shift(1) == 0)]
218
+ short_exits = df[(df['short_position'] == 0) & (df['short_position'].shift(1) == -1)]
219
+ long_trade_profits = []
220
+ for idx, row in long_entries.iterrows():
221
+ future_exits = long_exits[long_exits.index > idx]
222
+ if not future_exits.empty: long_trade_profits.append((future_exits.iloc[0]['Close'] / row['Close']) - 1)
223
+ avg_long_profit_per_trade = np.mean(long_trade_profits) if long_trade_profits else 0
224
+ short_trade_profits = []
225
+ for idx, row in short_entries.iterrows():
226
+ future_exits = short_exits[short_exits.index > idx]
227
+ if not future_exits.empty: short_trade_profits.append(((future_exits.iloc[0]['Close'] / row['Close']) - 1) * -1)
228
+ avg_short_profit_per_trade = np.mean(short_trade_profits) if short_trade_profits else 0
229
+ long_trades_log = [{'date': idx, 'price': row['Close'], 'confidence': row['long_confidence_score']} for idx, row in long_entries.iterrows()]
230
+ short_trades_log = [{'date': idx, 'price': row['Close'], 'confidence': row['short_confidence_score']} for idx, row in short_entries.iterrows()]
231
+ open_trades = []
232
+ if not df.empty:
233
+ last_close = df['Close'].iloc[-1]
234
+ if df['long_position'].iloc[-1] == 1 and not long_entries.empty:
235
+ last_entry = long_entries.iloc[-1]
236
+ pnl = (last_close / last_entry['Close']) - 1
237
+ open_trades.append({'Side': 'Long', 'Date Open': last_entry.name, 'Start Confidence': last_entry['long_confidence_score'], 'Current % P/L': pnl})
238
+ if df['short_position'].iloc[-1] == -1 and not short_entries.empty:
239
+ last_entry = short_entries.iloc[-1]
240
+ pnl = ((last_close / last_entry['Close']) - 1) * -1
241
+ open_trades.append({'Side': 'Short', 'Date Open': last_entry.name, 'Start Confidence': last_entry['short_confidence_score'], 'Current % P/L': pnl})
242
+ df.sort_index(inplace=True)
243
+ return final_long_pnl, final_short_pnl, avg_long_profit_per_trade, avg_short_profit_per_trade, df, (long_trades_log, long_exits.index, short_trades_log, short_exits.index), open_trades
244
+
245
+ # --- 3. Charting and Display Functions ---
246
+ def generate_long_plot(df, trades, ticker):
247
+ fig = go.Figure(); fig.add_trace(go.Scatter(x=df.index, y=df['Close'], mode='lines', name='Close Price', line=dict(color='blue'))); fig.add_trace(go.Scatter(x=df.index, y=df['large_ma'], mode='lines', name='Large MA', line=dict(color='orange', dash='dash'))); fig.add_trace(go.Scatter(x=df.index, y=df['bband_upper'], mode='lines', name='Upper Band', line=dict(color='gray', width=0.5))); fig.add_trace(go.Scatter(x=df.index, y=df['bband_lower'], mode='lines', name='Lower Band', line=dict(color='gray', width=0.5), fill='tonexty', fillcolor='rgba(211,211,211,0.2)'))
248
+ long_entries_log, long_exits, _, _ = trades
249
+ if long_entries_log:
250
+ dates = [t['date'] for t in long_entries_log]; prices = [t['price'] for t in long_entries_log]; scores = [f"Confidence: {t['confidence']:.0f}%" for t in long_entries_log]
251
+ fig.add_trace(go.Scatter(x=dates, y=prices, mode='markers', name='Long Entry', marker=dict(color='green', symbol='triangle-up', size=12), text=scores, hoverinfo='text'))
252
+ if not long_exits.empty: fig.add_trace(go.Scatter(x=long_exits, y=df.loc[long_exits,'Close'], mode='markers', name='Long Exit', marker=dict(color='darkgreen', symbol='x', size=8)))
253
+ fig.update_layout(title=f'Long Trades for {ticker}', xaxis_title='Date', yaxis_title='Price', legend_title="Indicator"); return fig
254
+
255
+ def generate_short_plot(df, trades, ticker):
256
+ fig = go.Figure(); fig.add_trace(go.Scatter(x=df.index, y=df['Close'], mode='lines', name='Close Price', line=dict(color='blue'))); fig.add_trace(go.Scatter(x=df.index, y=df['large_ma'], mode='lines', name='Large MA', line=dict(color='orange', dash='dash'))); fig.add_trace(go.Scatter(x=df.index, y=df['bband_upper'], mode='lines', name='Upper Band', line=dict(color='gray', width=0.5))); fig.add_trace(go.Scatter(x=df.index, y=df['bband_lower'], mode='lines', name='Lower Band', line=dict(color='gray', width=0.5), fill='tonexty', fillcolor='rgba(211,211,211,0.2)'))
257
+ _, _, short_entries_log, short_exits = trades
258
+ if short_entries_log:
259
+ dates = [t['date'] for t in short_entries_log]; prices = [t['price'] for t in short_entries_log]; scores = [f"Confidence: {t['confidence']:.0f}%" for t in short_entries_log]
260
+ fig.add_trace(go.Scatter(x=dates, y=prices, mode='markers', name='Short Entry', marker=dict(color='red', symbol='triangle-down', size=12), text=scores, hoverinfo='text'))
261
+ if not short_exits.empty: fig.add_trace(go.Scatter(x=short_exits, y=df.loc[short_exits,'Close'], mode='markers', name='Short Exit', marker=dict(color='darkred', symbol='x', size=8)))
262
+ fig.update_layout(title=f'Short Trades for {ticker}', xaxis_title='Date', yaxis_title='Price', legend_title="Indicator"); return fig
263
+
264
+ def display_summary_analytics(summary_df):
265
+ st.subheader("Overall Strategy Performance")
266
+ col1, col2 = st.columns(2)
267
+ for side in ["Long", "Short"]:
268
+ active_trades_df = summary_df[summary_df[f'Num {side} Trades'] > 0]
269
+ container = col1 if side == "Long" else col2
270
+ with container:
271
+ st.subheader(f"{side} Trades")
272
+ if not active_trades_df.empty:
273
+ total_trades = active_trades_df[f'Num {side} Trades'].sum()
274
+ avg_trade_profit = (active_trades_df[f'Avg {side} Profit per Trade'] * active_trades_df[f'Num {side} Trades']).sum() / total_trades if total_trades > 0 else 0
275
+ avg_cumulative_profit = active_trades_df[f'Cumulative {side} P&L'].mean()
276
+ avg_confidence = active_trades_df[f'Avg {side} Confidence'].mean()
277
+ if pd.isna(avg_confidence): avg_confidence = 0
278
+ good_tickers = (active_trades_df[f'Cumulative {side} P&L'] > 0).sum(); bad_tickers = (active_trades_df[f'Cumulative {side} P&L'] < 0).sum()
279
+ good_bad_ratio = good_tickers / bad_tickers if bad_tickers > 0 else float('inf')
280
+ raw_strategy_score = avg_trade_profit * good_bad_ratio if np.isfinite(good_bad_ratio) else 0.0
281
+ display_score = normalise_strategy_score(raw_strategy_score)
282
+ st.metric("Strategy Score", f"{display_score:.2f}%"); st.metric("Avg Cumulative Profit (Active Tickers)", f"{avg_cumulative_profit:.2%}"); st.metric("Avg Profit per Trade (Active Tickers)", f"{avg_trade_profit:.2%}"); st.metric(f"Average Entry Confidence", f"{avg_confidence:.0f}%")
283
+ st.text(f"Profitable Tickers: {good_tickers}")
284
+ st.text(f"Losing Tickers: {bad_tickers}")
285
+ st.text(f"Total Individual Trades: {int(total_trades)}")
286
+ st.text(f"Good/Bad Ratio: {good_bad_ratio:.2f}")
287
+ else: st.info("No trades found for this side with current settings.")
288
+
289
+ # --- 4. Optimisation Functions (Parallelised) ---
290
+ def run_single_parameter_test(params, master_df, optimise_for, tickers, date_range, power, confidence_settings):
291
+ total_profit_weighted_avg, total_trades, winning_tickers, losing_tickers = 0, 0, 0, 0
292
+ use_rsi, use_vol, use_trend, use_volume = confidence_settings['toggles']
293
+ rsi_w, vol_w, trend_w, volume_w = confidence_settings['weights']
294
+
295
+ if not isinstance(tickers, list): tickers = [tickers]
296
+ for ticker in tickers:
297
+ cols_to_use = [ticker]
298
+ if f'{ticker}_Volume' in master_df.columns: cols_to_use.append(f'{ticker}_Volume')
299
+ ticker_data = master_df.loc[date_range[0]:date_range[1], cols_to_use]
300
+ rename_dict = {ticker: 'Close', f'{ticker}_Volume': 'Volume'}
301
+ ticker_data = ticker_data.rename(columns=rename_dict)
302
+ if not ticker_data.empty:
303
+ long_pnl, short_pnl, avg_long_trade, avg_short_trade, _, trades, _ = run_backtest(
304
+ ticker_data, params, use_rsi, use_vol, use_trend, use_volume, rsi_w, vol_w, trend_w, volume_w
305
+ )
306
+ if optimise_for == 'long': pnl, avg_trade_profit, num_trades = long_pnl, avg_long_trade, len(trades[0])
307
+ else: pnl, avg_trade_profit, num_trades = short_pnl, avg_short_trade, len(trades[2])
308
+ if num_trades > 0:
309
+ total_trades += num_trades; total_profit_weighted_avg += avg_trade_profit * num_trades
310
+ if pnl > 0: winning_tickers += 1
311
+ elif pnl < 0: losing_tickers += 1
312
+ current_metric = -np.inf
313
+ if total_trades > 0:
314
+ overall_avg_profit_per_trade = total_profit_weighted_avg / total_trades
315
+ if losing_tickers > 0: good_bad_ratio = winning_tickers / losing_tickers
316
+ elif winning_tickers > 0: good_bad_ratio = np.inf
317
+ else: good_bad_ratio = 0
318
+ if overall_avg_profit_per_trade > 0: current_metric = (overall_avg_profit_per_trade ** power) * good_bad_ratio
319
+ else: current_metric = overall_avg_profit_per_trade
320
+ return (current_metric, params)
321
+
322
+ def generate_and_run_optimisation(main_df, main_content_placeholder, optimise_for, use_squared_weighting):
323
+ st.session_state.summary_df = None
324
+ st.session_state.single_ticker_results = None
325
+ st.session_state.confidence_results_df = None
326
+ st.session_state.open_trades_df = None
327
+ st.session_state.advisor_df = None
328
+
329
+ with main_content_placeholder.container():
330
+ defaults = st.session_state.widget_defaults
331
+ ma_range = range(st.session_state.ma_start_num, st.session_state.ma_end_num + 1, st.session_state.ma_step_num) if st.session_state.opt_ma_cb else [defaults['large_ma_period']]
332
+ bb_range = range(st.session_state.bb_start_num, st.session_state.bb_end_num + 1, st.session_state.bb_step_num) if st.session_state.opt_bb_cb else [defaults['bband_period']]
333
+ std_range = np.arange(st.session_state.std_start_num, st.session_state.std_end_num + 0.001, st.session_state.std_step_num) if st.session_state.opt_std_cb else [defaults['bband_std_dev']]
334
+ sl_range = np.arange(st.session_state.sl_start_num, st.session_state.sl_end_num + 0.001, st.session_state.sl_step_num) / 100 if st.session_state.opt_sl_cb else [defaults['long_stop_loss_pct']]
335
+ delay_range = range(st.session_state.delay_start_num, st.session_state.delay_end_num + 1, st.session_state.delay_step_num) if st.session_state.opt_delay_cb else [defaults['long_delay_days']]
336
+ entry_range = np.arange(st.session_state.entry_start_num, st.session_state.entry_end_num + 0.001, st.session_state.entry_step_num) / 100 if st.session_state.opt_entry_cb else [defaults['long_entry_threshold_pct']]
337
+ exit_range = np.arange(st.session_state.exit_start_num, st.session_state.exit_end_num + 0.001, st.session_state.exit_step_num) / 100 if st.session_state.opt_exit_cb else [defaults['long_exit_ma_threshold_pct']]
338
+ conf_range = range(st.session_state.conf_start_num, st.session_state.conf_end_num + 1, st.session_state.conf_step_num) if st.session_state.opt_conf_cb else [defaults['confidence_threshold']]
339
+ param_product = itertools.product(ma_range, bb_range, std_range, sl_range, delay_range, entry_range, exit_range, conf_range)
340
+ param_combinations = [{ "large_ma_period": p[0], "bband_period": p[1], "bband_std_dev": p[2], "long_stop_loss_pct": p[3], "short_stop_loss_pct": p[3], "long_delay_days": p[4], "short_delay_days": p[4], "long_entry_threshold_pct": p[5], "short_entry_threshold_pct": p[5], "long_exit_ma_threshold_pct": p[6], "short_exit_ma_threshold_pct": p[6], "confidence_threshold": p[7] } for p in param_product]
341
+ total_combinations = len(param_combinations)
342
+ if total_combinations <= 1:
343
+ st.warning("No optimisation parameters selected."); return
344
+
345
+ confidence_settings = {
346
+ 'toggles': (st.session_state.use_rsi, st.session_state.use_vol, st.session_state.use_trend, st.session_state.use_volume),
347
+ 'weights': (st.session_state.rsi_w, st.session_state.vol_w, st.session_state.trend_w, st.session_state.volume_w)
348
+ }
349
+
350
+ num_cores = cpu_count()
351
+ st.info(f"Starting {optimise_for.upper()} optimisation on {num_cores} cores... Testing {total_combinations} combinations.")
352
+ tickers_to_run = [col for col in main_df.columns if '_volume' not in str(col).lower()] if st.session_state.run_mode == "Analyse Full List" else [st.session_state.ticker_select]
353
+ date_range = (pd.Timestamp(st.session_state.start_date), pd.Timestamp(st.session_state.end_date))
354
+ power = 2 if use_squared_weighting else 1
355
+ best_metric, best_params = -np.inf, {}
356
+ status_text = st.empty(); status_text.text("Optimisation starting...")
357
+ progress_bar = st.progress(0)
358
+ worker_func = partial(run_single_parameter_test, master_df=main_df, optimise_for=optimise_for, tickers=tickers_to_run, date_range=date_range, power=power, confidence_settings=confidence_settings)
359
+ with Pool(processes=num_cores) as pool:
360
+ iterator = pool.imap_unordered(worker_func, param_combinations)
361
+ for i, (metric, params) in enumerate(iterator, 1):
362
+ if metric > best_metric:
363
+ best_metric, best_params = metric, params
364
+ display_score = normalise_strategy_score(best_metric)
365
+ status_text.text(f"Testing... New Best Score: {display_score:.2f}%")
366
+ progress_bar.progress(i / total_combinations, text=f"Optimising... {i}/{total_combinations} combinations complete.")
367
+ status_text.empty()
368
+ if best_params:
369
+ display_score = normalise_strategy_score(best_metric)
370
+ st.success(f"Optimisation Complete! Best Strategy Score: {display_score:.2f}%")
371
+ st.subheader("Optimal Parameters Found"); st.json(best_params)
372
+ st.session_state.best_params = best_params
373
+ else:
374
+ st.warning("Optimisation finished, but no profitable combinations were found.")
375
+
376
+ def run_single_confidence_test(task, base_params, master_df, date_range, tickers_to_run, optimise_for, factor_weights):
377
+ combo, threshold, _ = task
378
+ use_rsi, use_volatility, use_trend, use_volume = combo
379
+ test_params = base_params.copy()
380
+ test_params["confidence_threshold"] = threshold
381
+
382
+ total_profit_weighted_avg, total_trades, winning_tickers, losing_tickers = 0, 0, 0, 0
383
+ all_confidences = []
384
+
385
+ for ticker in tickers_to_run:
386
+ cols_to_use = [ticker]
387
+ if f'{ticker}_Volume' in master_df.columns: cols_to_use.append(f'{ticker}_Volume')
388
+ ticker_data = master_df.loc[date_range[0]:date_range[1], cols_to_use]
389
+ rename_dict = {ticker: 'Close', f'{ticker}_Volume': 'Volume'}
390
+ ticker_data = ticker_data.rename(columns=rename_dict)
391
+
392
+ if not ticker_data.empty:
393
+ long_pnl, short_pnl, avg_long_trade, avg_short_trade, _, trades, _ = run_backtest(
394
+ ticker_data, test_params, use_rsi, use_volatility, use_trend, use_volume,
395
+ factor_weights['rsi'], factor_weights['vol'], factor_weights['trend'], factor_weights['volume']
396
+ )
397
+
398
+ if optimise_for == 'long':
399
+ pnl, avg_trade_profit, trade_log = long_pnl, avg_long_trade, trades[0]
400
+ else:
401
+ pnl, avg_trade_profit, trade_log = short_pnl, avg_short_trade, trades[2]
402
+
403
+ num_trades = len(trade_log)
404
+
405
+ if num_trades > 0:
406
+ total_trades += num_trades
407
+ total_profit_weighted_avg += avg_trade_profit * num_trades
408
+ if pnl > 0: winning_tickers += 1
409
+ elif pnl < 0: losing_tickers += 1
410
+ all_confidences.extend([trade['confidence'] for trade in trade_log])
411
+
412
+ raw_score, badness_score, overall_avg_profit, good_bad_ratio = 0.0, 0.0, 0.0, 0.0
413
+
414
+ if total_trades > 0:
415
+ overall_avg_profit = total_profit_weighted_avg / total_trades
416
+ if losing_tickers > 0:
417
+ good_bad_ratio = winning_tickers / losing_tickers
418
+ raw_score = overall_avg_profit * good_bad_ratio
419
+ elif winning_tickers > 0:
420
+ good_bad_ratio = float('inf')
421
+ raw_score = overall_avg_profit * 100
422
+
423
+ if winning_tickers > 0 and overall_avg_profit < 0:
424
+ badness_score = (losing_tickers / winning_tickers) * abs(overall_avg_profit)
425
+
426
+ avg_entry_confidence = np.mean(all_confidences) if all_confidences else 0
427
+
428
+ return {
429
+ "RSI": use_rsi, "Volatility": use_volatility, "TREND": use_trend, "Volume": use_volume,
430
+ "Conf. Threshold": threshold, "Avg Profit/Trade": overall_avg_profit,
431
+ "Good/Bad Ratio": good_bad_ratio, "Winning Tickers": winning_tickers, "Losing Tickers": losing_tickers,
432
+ "Avg Entry Conf.": avg_entry_confidence, "Good Score": raw_score, "Bad Score": badness_score,
433
+ "Norm. Score %": normalise_strategy_score(raw_score), "Total Trades": total_trades
434
+ }
435
+
436
+ def run_confidence_optimisation(optimise_for, find_mode, master_df, main_content_placeholder, veto_factors):
437
+ st.session_state.summary_df = None
438
+ st.session_state.single_ticker_results = None
439
+ st.session_state.open_trades_df = None
440
+ st.session_state.best_params = None
441
+ st.session_state.advisor_df = None
442
+
443
+ with main_content_placeholder.container():
444
+ num_cores = cpu_count()
445
+ st.info(f"Starting to find **{find_mode.upper()}** {optimise_for.upper()} setups on {num_cores} CPU cores...")
446
+ factors = ['RSI', 'Volatility', 'TREND', 'Volume']
447
+
448
+ if find_mode == 'worst':
449
+ use_rsi, use_vol, use_trend, use_volume = veto_factors
450
+ on_off_combos = [c for c in itertools.product([False, True], repeat=4) if c == (use_rsi, use_vol, use_trend, use_volume)]
451
+ if not any(on_off_combos[0]):
452
+ st.warning("Please select at least one factor for the Veto search."); return
453
+ else:
454
+ on_off_combos = [c for c in itertools.product([False, True], repeat=len(factors)) if any(c)]
455
+
456
+ thresholds_to_test = [10, 25, 50, 85]
457
+ tasks = list(itertools.product(on_off_combos, thresholds_to_test, [1.0]))
458
+ total_tasks = len(tasks)
459
+
460
+ base_params = { "large_ma_period": st.session_state.ma_period, "bband_period": st.session_state.bb_period, "bband_std_dev": st.session_state.bb_std, "long_entry_threshold_pct": st.session_state.long_entry / 100, "long_exit_ma_threshold_pct": st.session_state.long_exit / 100, "long_stop_loss_pct": st.session_state.long_sl / 100, "long_delay_days": st.session_state.long_delay, "short_entry_threshold_pct": st.session_state.short_entry / 100, "short_exit_ma_threshold_pct": st.session_state.short_exit / 100, "short_stop_loss_pct": st.session_state.short_sl / 100, "short_delay_days": st.session_state.short_delay, }
461
+ tickers_to_run = sorted([col for col in master_df.columns if '_volume' not in str(col).lower()])
462
+ date_range = (pd.Timestamp(st.session_state.start_date), pd.Timestamp(st.session_state.end_date))
463
+
464
+ factor_weights = {
465
+ "rsi": st.session_state.rsi_w, "vol": st.session_state.vol_w,
466
+ "trend": st.session_state.trend_w, "volume": st.session_state.volume_w
467
+ }
468
+
469
+ worker_func = partial(run_single_confidence_test, base_params=base_params, master_df=master_df, date_range=date_range, tickers_to_run=tickers_to_run, optimise_for=optimise_for, factor_weights=factor_weights)
470
+
471
+ results_list = []
472
+ progress_bar = st.progress(0, text="Optimisation starting...")
473
+
474
+ with Pool(processes=num_cores) as pool:
475
+ iterator = pool.imap_unordered(worker_func, tasks)
476
+ for i, result in enumerate(iterator, 1):
477
+ results_list.append(result)
478
+ progress_bar.progress(i / total_tasks, text=f"Optimising... {i}/{total_tasks} combinations complete.")
479
+
480
+ if results_list:
481
+ results_df = pd.DataFrame(results_list)
482
+ sort_col = "Good Score" if find_mode == 'best' else "Bad Score"
483
+ results_df = results_df.sort_values(by=sort_col, ascending=False).reset_index(drop=True)
484
+
485
+ for factor in factors:
486
+ results_df[factor] = results_df[factor].apply(lambda x: "On" if x else "Off")
487
+
488
+ st.subheader(f"πŸ† Top {find_mode.title()} Confidence Setup Found ({optimise_for.title()} Trades)")
489
+ best_setup = results_df.iloc[0]
490
+ st.dataframe(best_setup)
491
+
492
+ if find_mode == 'best':
493
+ st.session_state.best_confidence_setup = best_setup.to_dict()
494
+ save_top_setups(results_df, optimise_for)
495
+ else:
496
+ st.session_state.worst_confidence_setup = best_setup.to_dict()
497
+
498
+ st.session_state.confidence_results_df = results_df
499
+ else:
500
+ st.warning("Confidence optimisation completed but no results were generated.")
501
+ st.session_state.confidence_results_df = None
502
+
503
+ def generate_advisor_report(main_df, main_content_placeholder):
504
+ st.session_state.summary_df = None
505
+ st.session_state.single_ticker_results = None
506
+ st.session_state.confidence_results_df = None
507
+ st.session_state.open_trades_df = None
508
+ st.session_state.best_params = None
509
+
510
+ with main_content_placeholder.container():
511
+ st.header("πŸ“ˆ Advanced Advisor Report")
512
+ top_setups = load_top_setups()
513
+
514
+ if not top_setups:
515
+ st.warning("No saved top setups found. Please run a 'Find Best Confidence' optimisation from Section 5 first.")
516
+ return
517
+
518
+ side = st.radio("Generate report for which setups?", ("Long", "Short"), horizontal=True)
519
+ setups_to_run = top_setups.get(side.lower())
520
+
521
+ if not setups_to_run:
522
+ st.warning(f"No saved top {side.lower()} setups found in the file.")
523
+ return
524
+
525
+ st.info(f"Scanning all tickers for open trades based on the top {len(setups_to_run)} saved {side} setups...")
526
+
527
+ base_params = {"large_ma_period": st.session_state.ma_period, "bband_period": st.session_state.bb_period, "bband_std_dev": st.session_state.bb_std, "long_entry_threshold_pct": st.session_state.long_entry / 100, "long_exit_ma_threshold_pct": st.session_state.long_exit / 100, "long_stop_loss_pct": st.session_state.long_sl / 100, "long_delay_days": st.session_state.long_delay, "short_entry_threshold_pct": st.session_state.short_entry / 100, "short_exit_ma_threshold_pct": st.session_state.short_exit / 100, "short_stop_loss_pct": st.session_state.short_sl / 100, "short_delay_days": st.session_state.short_delay, }
528
+ factor_weights = {"rsi": st.session_state.rsi_w, "vol": st.session_state.vol_w, "trend": st.session_state.trend_w, "volume": st.session_state.volume_w}
529
+
530
+ all_advisor_trades = []
531
+ ticker_list = sorted([col for col in main_df.columns if '_volume' not in str(col).lower()])
532
+ progress_bar = st.progress(0, text="Scanning setups...")
533
+
534
+ for i, setup in enumerate(setups_to_run):
535
+ progress_bar.progress((i + 1) / len(setups_to_run), text=f"Scanning with Setup #{i+1}...")
536
+
537
+ use_rsi = setup.get('RSI') == 'On'
538
+ use_vol = setup.get('Volatility') == 'On'
539
+ use_trend = setup.get('TREND') == 'On'
540
+ use_volume = setup.get('Volume') == 'On'
541
+
542
+ params_for_run = base_params.copy()
543
+ params_for_run['confidence_threshold'] = setup.get('Conf. Threshold')
544
+
545
+ for ticker_symbol in ticker_list:
546
+ cols_to_use = [ticker_symbol]
547
+ if f'{ticker_symbol}_Volume' in main_df.columns: cols_to_use.append(f'{ticker_symbol}_Volume')
548
+ data_for_backtest = main_df[cols_to_use].rename(columns={ticker_symbol: 'Close', f'{ticker_symbol}_Volume': 'Volume'})
549
+
550
+ _, _, _, _, _, _, open_trades = run_backtest(data_for_backtest, params_for_run,
551
+ use_rsi, use_vol, use_trend, use_volume,
552
+ factor_weights['rsi'], factor_weights['vol'],
553
+ factor_weights['trend'], factor_weights['volume'])
554
+
555
+ if open_trades:
556
+ for trade in open_trades:
557
+ if trade['Side'].lower() == side.lower():
558
+ trade['Ticker'] = ticker_symbol
559
+ trade['Setup Rank'] = i + 1
560
+ trade['Setup G/B Ratio'] = setup.get('Good/Bad Ratio')
561
+ trade['Setup Avg Profit'] = setup.get('Avg Profit/Trade')
562
+ all_advisor_trades.append(trade)
563
+
564
+ progress_bar.empty()
565
+
566
+ if all_advisor_trades:
567
+ advisor_df = pd.DataFrame(all_advisor_trades)
568
+ cols_order = ['Ticker', 'Setup Rank', 'Current % P/L', 'Side', 'Date Open',
569
+ 'Start Confidence', 'Setup G/B Ratio', 'Setup Avg Profit']
570
+ advisor_df = advisor_df[cols_order]
571
+ st.session_state.advisor_df = advisor_df
572
+ else:
573
+ st.success(f"No open {side} trades found matching any of the top setups.")
574
+ st.session_state.advisor_df = pd.DataFrame()
575
+
576
+
577
+ # --- 5. Streamlit User Interface ---
578
+ def main():
579
+ st.set_page_config(page_title="Stock Backtesting Sandbox", page_icon="πŸ“ˆ", layout="wide")
580
+ if 'first_run' not in st.session_state:
581
+ st.session_state.first_run = True
582
+ st.session_state.widget_defaults = load_settings()
583
+ st.session_state.veto_setup = load_veto_setup()
584
+ st.session_state.summary_df = None
585
+ st.session_state.single_ticker_results = None
586
+ st.session_state.confidence_results_df = None
587
+ st.session_state.open_trades_df = None
588
+ st.session_state.best_params = None
589
+ st.session_state.advisor_df = None
590
+ st.session_state.run_analysis_button = False
591
+ st.session_state.run_advanced_advisor = False
592
+
593
+ st.title("πŸ“ˆ Stock Backtesting Sandbox")
594
+ st.success(f"Good morning! Today is {date.today().strftime('%A, %d %B %Y')}.")
595
+ main_content_placeholder = st.empty()
596
+
597
+ if 'master_df' not in st.session_state:
598
+ with main_content_placeholder.container():
599
+ master_df, load_message = load_all_data('csv_data')
600
+ if master_df is None:
601
+ st.error(load_message); st.stop()
602
+ else:
603
+ st.info(load_message)
604
+ master_df, outlier_report = clean_data_and_report_outliers(master_df)
605
+ if outlier_report:
606
+ report_df = pd.DataFrame(outlier_report)
607
+ st.info(f"Data Cleaning: Found and removed price spikes >100% in {len(outlier_report)} tickers.")
608
+ st.download_button("⬇️ Download Outlier Report", report_df.to_csv(index=False).encode('utf-8'), "outlier_report.csv", "text/csv")
609
+ st.session_state.master_df = master_df
610
+ st.session_state.ticker_list = sorted([col for col in master_df.columns if '_volume' not in str(col).lower()])
611
+
612
+ master_df = st.session_state.master_df
613
+ ticker_list = st.session_state.ticker_list
614
+ defaults = st.session_state.widget_defaults
615
+
616
+ st.sidebar.header("1. Select Test Mode")
617
+ st.sidebar.radio("Mode:", ("Analyse Single Ticker", "Analyse Full List"), key='run_mode', index=1)
618
+ if st.session_state.get('run_mode') == "Analyse Single Ticker":
619
+ st.sidebar.selectbox("Select a Ticker:", ticker_list, key='ticker_select')
620
+ st.sidebar.date_input("Start Date", master_df.index.min().date(), key='start_date')
621
+ st.sidebar.date_input("End Date", master_df.index.max().date(), key='end_date')
622
+ st.markdown("""<style>div[data-testid="stSidebar"] button[kind="primary"] { background-color: #4CAF50; color: white; border-color: #4CAF50;}</style>""", unsafe_allow_html=True)
623
+
624
+ if st.sidebar.button("πŸš€ Run Analysis", type="primary"):
625
+ st.session_state.run_analysis_button = True
626
+ st.rerun()
627
+
628
+ st.sidebar.markdown("---")
629
+
630
+ st.sidebar.header("2. Confidence Score Factors (for Main Signal)")
631
+ st.sidebar.toggle("Use Momentum (RSI)", value=True, key='use_rsi')
632
+ st.sidebar.number_input("RSI Weight", 0.1, 5.0, 1.0, 0.1, key='rsi_w', disabled=not st.session_state.get('use_rsi', True))
633
+ st.sidebar.toggle("Use Volatility", value=True, key='use_vol')
634
+ st.sidebar.number_input("Volatility Weight", 0.1, 5.0, 1.0, 0.1, key='vol_w', disabled=not st.session_state.get('use_vol', True))
635
+ st.sidebar.toggle("Use Trend (200d MA)", value=True, key='use_trend')
636
+ st.sidebar.number_input("Trend Weight", 0.1, 5.0, 1.0, 0.1, key='trend_w', disabled=not st.session_state.get('use_trend', True))
637
+ st.sidebar.toggle("Use Volume Spike", value=True, key='use_volume')
638
+ st.sidebar.number_input("Volume Weight", 0.1, 5.0, 1.0, 0.1, key='volume_w', disabled=not st.session_state.get('use_volume', True))
639
+ st.sidebar.slider("Minimum Confidence Threshold (%)", 0, 100, defaults.get("confidence_threshold", 50), 5, key='confidence_slider')
640
+
641
+ st.sidebar.markdown("---")
642
+ st.sidebar.header("3. Strategy Parameters")
643
+ st.sidebar.number_input("Large MA Period", 10, 200, defaults.get("large_ma_period", 50), 1, key='ma_period')
644
+ st.sidebar.number_input("Bollinger Band Period", 10, 100, defaults.get("bband_period", 20), 1, key='bb_period')
645
+ st.sidebar.number_input("Bollinger Band Std Dev", 1.0, 4.0, defaults.get("bband_std_dev", 2.0), 0.1, key='bb_std')
646
+ st.sidebar.subheader("Long Trade Logic"); st.sidebar.slider("Entry Threshold (%)", 0.0, 10.0, defaults.get("long_entry_threshold_pct", 0.0) * 100, 0.1, key='long_entry'); st.sidebar.slider("Exit MA Threshold (%)", 0.0, 10.0, defaults.get("long_exit_ma_threshold_pct", 0.0) * 100, 0.1, key='long_exit'); st.sidebar.slider("Stop Loss (%)", 0.0, 30.0, defaults.get("long_stop_loss_pct", 0.0) * 100, 0.5, key='long_sl'); st.sidebar.number_input("Delay Entry (days)", 0, 10, defaults.get("long_delay_days", 0), 1, key='long_delay')
647
+ st.sidebar.subheader("Short Trade Logic"); st.sidebar.slider("Entry Threshold (%)", 0.0, 10.0, defaults.get("short_entry_threshold_pct", 0.0) * 100, 0.1, key='short_entry'); st.sidebar.slider("Exit MA Threshold (%)", 0.0, 10.0, defaults.get("short_exit_ma_threshold_pct", 0.0) * 100, 0.1, key='short_exit'); st.sidebar.slider("Stop Loss (%)", 0.0, 30.0, defaults.get("short_stop_loss_pct", 0.0) * 100, 0.5, key='short_sl'); st.sidebar.number_input("Delay Entry (days)", 0, 10, defaults.get("short_delay_days", 0), 1, key='short_delay')
648
+
649
+ st.sidebar.markdown("---")
650
+ st.sidebar.header("4. Find Best Parameters")
651
+ with st.sidebar.expander("Set Optimisation Ranges", expanded=False):
652
+ # Omitted for brevity
653
+ pass
654
+
655
+ st.sidebar.markdown("---")
656
+ st.sidebar.header("5. Find Best/Worst Confidence Setup")
657
+ with st.sidebar.expander("Optimise Confidence Factors", expanded=False):
658
+ # Omitted for brevity
659
+ pass
660
+
661
+ st.sidebar.markdown("---")
662
+ st.sidebar.header("6. Advanced Advisor")
663
+ st.sidebar.info("Uses saved top setups from Section 5. Re-run an optimisation to update them.")
664
+ if st.sidebar.button("πŸ” Generate Advisor Report"):
665
+ st.session_state.run_advanced_advisor = True
666
+ st.rerun()
667
+
668
+ st.sidebar.markdown("---")
669
+ # --- FIX: Correct indentation for this block ---
670
+ if st.session_state.get('veto_setup'):
671
+ st.sidebar.header("Veto Filter")
672
+ st.sidebar.success("Veto filter is ACTIVE.")
673
+ st.sidebar.json(st.session_state.veto_setup)
674
+ if st.sidebar.button("πŸ’Ύ Save Veto as Default"):
675
+ save_veto_setup(st.session_state.veto_setup)
676
+ if st.sidebar.button("Clear Veto Filter"):
677
+ st.session_state.veto_setup = None
678
+ st.rerun()
679
+ st.sidebar.markdown("---")
680
+
681
+ if st.sidebar.button("πŸ’Ύ Save Settings as Default"):
682
+ save_settings({ "large_ma_period": st.session_state.ma_period, "bband_period": st.session_state.bb_period, "bband_std_dev": st.session_state.bb_std, "confidence_threshold": st.session_state.confidence_slider, "long_entry_threshold_pct": st.session_state.long_entry / 100, "long_exit_ma_threshold_pct": st.session_state.long_exit / 100, "long_stop_loss_pct": st.session_state.long_sl / 100, "long_delay_days": st.session_state.long_delay, "short_entry_threshold_pct": st.session_state.short_entry / 100, "short_exit_ma_threshold_pct": st.session_state.short_exit / 100, "short_stop_loss_pct": st.session_state.short_sl / 100, "short_delay_days": st.session_state.short_delay, })
683
+
684
+ # --- Trigger actions based on session state flags ---
685
+ if st.session_state.get('run_analysis_button'):
686
+ st.session_state.confidence_results_df = None
687
+ st.session_state.best_params = None
688
+ st.session_state.advisor_df = None
689
+
690
+ with main_content_placeholder.container():
691
+ veto_to_use = st.session_state.get('veto_setup')
692
+ if veto_to_use: st.info("Veto filter is active for this analysis.")
693
+ else: st.info("πŸ’‘ Tip: You can find and apply a 'Veto Filter' from section 5 in the sidebar.")
694
+
695
+ manual_params = {"large_ma_period": st.session_state.ma_period, "bband_period": st.session_state.bb_period, "bband_std_dev": st.session_state.bb_std, "confidence_threshold": st.session_state.confidence_slider, "long_entry_threshold_pct": st.session_state.long_entry / 100, "long_exit_ma_threshold_pct": st.session_state.long_exit / 100, "long_stop_loss_pct": st.session_state.long_sl / 100, "long_delay_days": st.session_state.long_delay, "short_entry_threshold_pct": st.session_state.short_entry / 100, "short_exit_ma_threshold_pct": st.session_state.short_exit / 100, "short_stop_loss_pct": st.session_state.short_sl / 100, "short_delay_days": st.session_state.short_delay, }
696
+
697
+ # --- FIX: Correct indentation for this block ---
698
+ if st.session_state.run_mode == "Analyse Single Ticker":
699
+ selected_ticker = st.session_state.get('ticker_select', ticker_list[0])
700
+ cols_to_use = [selected_ticker]
701
+ if f'{selected_ticker}_Volume' in master_df.columns: cols_to_use.append(f'{selected_ticker}_Volume')
702
+ data_for_backtest = master_df[cols_to_use].rename(columns={selected_ticker: 'Close', f'{selected_ticker}_Volume': 'Volume'})
703
+ ticker_data_series = data_for_backtest.loc[pd.Timestamp(st.session_state.start_date):pd.Timestamp(st.session_state.end_date)]
704
+
705
+ if not ticker_data_series.empty:
706
+ long_pnl, short_pnl, avg_long_trade, avg_short_trade, results_df, trades, open_trades = run_backtest(ticker_data_series, manual_params, st.session_state.use_rsi, st.session_state.use_vol, st.session_state.use_trend, st.session_state.use_volume, st.session_state.rsi_w, st.session_state.vol_w, st.session_state.trend_w, st.session_state.volume_w, veto_setup=veto_to_use)
707
+ st.session_state.single_ticker_results = {"long_pnl": long_pnl, "short_pnl": short_pnl, "avg_long_trade": avg_long_trade, "avg_short_trade": avg_short_trade, "results_df": results_df, "trades": trades}
708
+ if open_trades: st.session_state.open_trades_df = pd.DataFrame(open_trades)
709
+ else: st.session_state.open_trades_df = pd.DataFrame()
710
+ else: st.warning("No data for this ticker in the selected date range.")
711
+
712
+ elif st.session_state.run_mode == "Analyse Full List":
713
+ summary_results, all_open_trades = [], []
714
+ progress_bar = st.progress(0, text="Starting analysis...")
715
+ for i, ticker_symbol in enumerate(ticker_list):
716
+ progress_bar.progress((i + 1) / len(ticker_list), text=f"Analysing {ticker_symbol}...")
717
+ cols_to_use = [ticker_symbol]
718
+ if f'{ticker_symbol}_Volume' in master_df.columns: cols_to_use.append(f'{ticker_symbol}_Volume')
719
+ data_for_backtest = master_df[cols_to_use].rename(columns={ticker_symbol: 'Close', f'{ticker_symbol}_Volume': 'Volume'})
720
+ ticker_data_series = data_for_backtest.loc[pd.Timestamp(st.session_state.start_date):pd.Timestamp(st.session_state.end_date)]
721
+ if not ticker_data_series.empty:
722
+ long_pnl, short_pnl, avg_long_trade, avg_short_trade, _, trades, open_trades = run_backtest(ticker_data_series, manual_params, st.session_state.use_rsi, st.session_state.use_vol, st.session_state.use_trend, st.session_state.use_volume, st.session_state.rsi_w, st.session_state.vol_w, st.session_state.trend_w, st.session_state.volume_w, veto_setup=veto_to_use)
723
+ long_conf = np.mean([t['confidence'] for t in trades[0]]) if trades[0] else 0
724
+ short_conf = np.mean([t['confidence'] for t in trades[2]]) if trades[2] else 0
725
+ summary_results.append({"Ticker": ticker_symbol, "Cumulative Long P&L": long_pnl, "Avg Long Profit per Trade": avg_long_trade, "Num Long Trades": len(trades[0]), "Avg Long Confidence": long_conf, "Cumulative Short P&L": short_pnl, "Avg Short Profit per Trade": avg_short_trade, "Num Short Trades": len(trades[2]), "Avg Short Confidence": short_conf})
726
+ if open_trades:
727
+ for trade in open_trades:
728
+ trade['Ticker'] = ticker_symbol
729
+ all_open_trades.append(trade)
730
+ progress_bar.empty()
731
+ if summary_results: st.session_state.summary_df = pd.DataFrame(summary_results).set_index('Ticker')
732
+ else: st.warning("No trades found for any ticker with the current settings.")
733
+ if all_open_trades: st.session_state.open_trades_df = pd.DataFrame(all_open_trades)
734
+ else: st.session_state.open_trades_df = pd.DataFrame()
735
+
736
+ st.session_state.run_analysis_button = False
737
+
738
+ if st.session_state.get('run_advanced_advisor'):
739
+ generate_advisor_report(master_df, main_content_placeholder)
740
+ st.session_state.run_advanced_advisor = False
741
+
742
+ # --- Main Display Area ---
743
+ with main_content_placeholder.container():
744
+ if st.session_state.get('advisor_df') is not None:
745
+ st.subheader("πŸ‘¨β€πŸ’Ό Advanced Advisor: Open Positions from Top Setups")
746
+ if not st.session_state.advisor_df.empty:
747
+ st.dataframe(st.session_state.advisor_df.style.format({
748
+ "Current % P/L": "{:.2%}", "Date Open": "{:%Y-%m-%d}",
749
+ "Start Confidence": "{:.0f}%", "Setup G/B Ratio": "{:.2f}",
750
+ "Setup Avg Profit": "{:.2%}"
751
+ }))
752
+ else:
753
+ st.info("No open positions found matching the criteria.")
754
+
755
+ elif st.session_state.get('confidence_results_df') is not None and not st.session_state.confidence_results_df.empty:
756
+ st.subheader("πŸ“Š Confidence Setup Optimisation Results")
757
+ display_df = st.session_state.confidence_results_df.head(60)
758
+ st.dataframe(display_df.style.format({
759
+ "Avg Profit/Trade": "{:.2%}", "Good/Bad Ratio": "{:.2f}",
760
+ "Avg Entry Conf.": "{:.1f}%", "Good Score": "{:.4f}",
761
+ "Bad Score": "{:.4f}", "Norm. Score %": "{:.2f}%"
762
+ }))
763
+
764
+ elif st.session_state.get('single_ticker_results') is not None:
765
+ res = st.session_state.single_ticker_results
766
+ st.subheader(f"Results for {st.session_state.get('ticker_select')}")
767
+ c1, c2, c3, c4 = st.columns(4); c1.metric("Cumulative Long P&L", f"{res['long_pnl']:.2%}"); c2.metric("Avg Long Trade P&L", f"{res['avg_long_trade']:.2%}"); c3.metric("Cumulative Short P&L", f"{res['short_pnl']:.2%}"); c4.metric("Avg Short Trade P&L", f"{res['avg_short_trade']:.2%}")
768
+ if res['results_df'] is not None:
769
+ st.plotly_chart(generate_long_plot(res['results_df'], res['trades'], st.session_state.get('ticker_select')), use_container_width=True)
770
+ st.plotly_chart(generate_short_plot(res['results_df'], res['trades'], st.session_state.get('ticker_select')), use_container_width=True)
771
+
772
+ elif st.session_state.get('summary_df') is not None and not st.session_state.summary_df.empty:
773
+ display_summary_analytics(st.session_state.summary_df)
774
+ st.subheader("Results per Ticker")
775
+ if st.checkbox("Only show tickers with trades", value=True):
776
+ display_df = st.session_state.summary_df[(st.session_state.summary_df['Num Long Trades'] > 0) | (st.session_state.summary_df['Num Short Trades'] > 0)]
777
+ else:
778
+ display_df = st.session_state.summary_df
779
+ st.dataframe(display_df.style.format({"Cumulative Long P&L": "{:.2%}", "Avg Long Profit per Trade": "{:.2%}", "Cumulative Short P&L": "{:.2%}", "Avg Short Profit per Trade": "{:.2%}", "Avg Long Confidence": "{:.0f}%", "Avg Short Confidence": "{:.0f}%"}))
780
+
781
+ if st.session_state.get('open_trades_df') is not None and not st.session_state.open_trades_df.empty:
782
+ st.subheader("πŸ‘¨β€πŸ’Ό Advisor: Currently Open Positions (Manual Run)")
783
+ display_open_df = st.session_state.open_trades_df.copy()
784
+ st.dataframe(display_open_df.style.format({"Date Open": "{:%Y-%m-%d}", "Start Confidence": "{:.0f}%", "Current % P/L": "{:.2%}"}))
785
+
786
+ st.markdown("---")
787
+ st.info("Want to see open trades from a wider range of top strategies?")
788
+ if st.button("Run Advanced Advisor Report"):
789
+ st.session_state.run_advanced_advisor = True
790
+ st.rerun()
791
+
792
+ def apply_best_params_to_widgets():
793
+ bp = st.session_state.get('best_params');
794
+ if not bp: return
795
+ st.session_state.ma_period, st.session_state.bb_period, st.session_state.bb_std = bp.get("large_ma_period"), bp.get("bband_period"), bp.get("bband_std_dev")
796
+ st.session_state.long_sl, st.session_state.short_sl = bp.get("long_stop_loss_pct") * 100, bp.get("short_stop_loss_pct") * 100
797
+ st.session_state.long_delay, st.session_state.short_delay = bp.get("long_delay_days"), bp.get("short_delay_days")
798
+ st.session_state.long_entry, st.session_state.short_entry = bp.get("long_entry_threshold_pct") * 100, bp.get("short_entry_threshold_pct") * 100
799
+ st.session_state.long_exit, st.session_state.short_exit = bp.get("long_exit_ma_threshold_pct") * 100, bp.get("short_exit_ma_threshold_pct") * 100
800
+ st.session_state.confidence_slider = bp.get("confidence_threshold")
801
+ st.session_state.best_params = None
802
+
803
+ if st.session_state.get('best_params'):
804
+ st.button("⬇️ Load Optimal Parameters into Manual Settings", on_click=apply_best_params_to_widgets)
805
+
806
+ if st.session_state.get('worst_confidence_setup'):
807
+ if st.button("Apply Worst Setup as Veto Filter"):
808
+ st.session_state.veto_setup = st.session_state.worst_confidence_setup
809
+ st.session_state.worst_confidence_setup = None
810
+ st.rerun()
811
+
812
+ if __name__ == "__main__":
813
+ main()