Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import numpy as np
|
|
| 7 |
# Disable Gradio analytics to avoid external API calls
|
| 8 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 9 |
|
| 10 |
-
#
|
| 11 |
strategy_presets = {
|
| 12 |
"Aggressive Prop Trader": {
|
| 13 |
"starting_balance": 2500, "trades_min": 5, "trades_max": 10, "weeks": 12,
|
|
@@ -31,8 +31,6 @@ strategy_presets = {
|
|
| 31 |
}
|
| 32 |
}
|
| 33 |
|
| 34 |
-
|
| 35 |
-
# π Simulation Function
|
| 36 |
def get_scaled_risk_pct(balance, base_risk_pct):
|
| 37 |
if balance < 5000: return base_risk_pct
|
| 38 |
elif balance < 10000: return base_risk_pct * 0.75
|
|
@@ -44,8 +42,7 @@ def simulate_tp_strategy_full(starting_balance, trades_min, trades_max, weeks,
|
|
| 44 |
profit_target=None):
|
| 45 |
sl_prob = 1.0 - tp1_prob - tp2_prob
|
| 46 |
tp1_r = risk_reward_ratio
|
| 47 |
-
tp2_r = risk_reward_ratio * 2
|
| 48 |
-
|
| 49 |
balance = starting_balance
|
| 50 |
peak = balance
|
| 51 |
drawdown = 0
|
|
@@ -83,23 +80,16 @@ def simulate_tp_strategy_full(starting_balance, trades_min, trades_max, weeks,
|
|
| 83 |
peak = max(peak, balance)
|
| 84 |
drawdown = max(drawdown, (peak - balance) / peak * 100)
|
| 85 |
weekly_return = (balance - week_start) / week_start * 100
|
| 86 |
-
log.append({
|
| 87 |
-
"Week": week, "Start Balance": round(week_start, 2),
|
| 88 |
-
"End Balance": round(balance, 2), "Weekly Return (%)": round(weekly_return, 2)
|
| 89 |
-
})
|
| 90 |
-
|
| 91 |
-
summary = {
|
| 92 |
-
"Final Balance": round(balance, 2), "TP1 Hits": tp1_hits,
|
| 93 |
-
"TP2 Hits": tp2_hits, "SL Hits": sl_hits,
|
| 94 |
-
"Max Drawdown %": round(drawdown, 2),
|
| 95 |
-
"Max Win Streak": max_win_streak, "Max Loss Streak": max_loss_streak
|
| 96 |
-
}
|
| 97 |
|
|
|
|
|
|
|
| 98 |
return pd.DataFrame(log), summary
|
|
|
|
| 99 |
def analytics_dashboard():
|
| 100 |
leaderboard = []
|
| 101 |
for name, config in strategy_presets.items():
|
| 102 |
-
df, summary = simulate_tp_strategy_full(**config
|
| 103 |
returns = df['End Balance'].pct_change().dropna()
|
| 104 |
volatility = returns.std() * np.sqrt(52)
|
| 105 |
sharpe = returns.mean() / returns.std() * np.sqrt(52) if returns.std() > 0 else 0
|
|
@@ -108,46 +98,14 @@ def analytics_dashboard():
|
|
| 108 |
max_dd = dd.max() * 100 if not dd.empty else 0
|
| 109 |
final = df['End Balance'].iloc[-1] if not df.empty else config['starting_balance']
|
| 110 |
score = final / (1 + max_dd)
|
| 111 |
-
leaderboard.append({
|
| 112 |
-
"Strategy": name,
|
| 113 |
-
"Final Balance": round(final, 2),
|
| 114 |
-
"Max DD %": round(max_dd, 2),
|
| 115 |
-
"Sharpe": round(sharpe, 2),
|
| 116 |
-
"EdgeCast Score": round(score, 2)
|
| 117 |
-
})
|
| 118 |
return pd.DataFrame(leaderboard).sort_values("EdgeCast Score", ascending=False).reset_index(drop=True)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
# UI
|
| 122 |
-
manual_tab = gr.Interface(
|
| 123 |
-
fn=simulate_tp_strategy_full,
|
| 124 |
-
inputs=[
|
| 125 |
-
gr.Slider(100, 20000, 2500, label="Start Balance"),
|
| 126 |
-
gr.Slider(1, 10, 3, label="Trades Min"),
|
| 127 |
-
gr.Slider(1, 15, 7, label="Trades Max"),
|
| 128 |
-
gr.Slider(1, 52, 12, label="Weeks"),
|
| 129 |
-
gr.Slider(0, 1, 0.3, step=0.05, label="TP1 %"),
|
| 130 |
-
gr.Slider(0, 1, 0.3, step=0.05, label="TP2 %"),
|
| 131 |
-
gr.Slider(0.1, 20, 2.0, step=0.1, label="Risk Reward Ratio"), # Updated
|
| 132 |
-
gr.Slider(0.001, 0.05, 0.01, step=0.001, label="Risk %"),
|
| 133 |
-
gr.Slider(0, 100000, 0, step=500, label="Profit Target π°")
|
| 134 |
-
],
|
| 135 |
-
outputs=["dataframe", "json"],
|
| 136 |
-
title="π οΈ Manual Config",
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
preset_tab = gr.Interface(
|
| 141 |
-
fn=lambda style: simulate_tp_strategy_full(**strategy_presets[style], risk_reward_ratio=2),
|
| 142 |
-
inputs=gr.Dropdown(choices=list(strategy_presets.keys()), label="Choose Preset Strategy"),
|
| 143 |
-
outputs=["dataframe", "json"],
|
| 144 |
-
title="π― Preset Mode"
|
| 145 |
-
)
|
| 146 |
def battle_strategies(style1, style2):
|
| 147 |
p1 = strategy_presets[style1]
|
| 148 |
p2 = strategy_presets[style2]
|
| 149 |
-
df1, s1 = simulate_tp_strategy_full(**p1
|
| 150 |
-
df2, s2 = simulate_tp_strategy_full(**p2
|
| 151 |
|
| 152 |
def enrich_summary(df, summary):
|
| 153 |
returns = df['End Balance'].pct_change().dropna()
|
|
@@ -179,7 +137,7 @@ def battle_strategies(style1, style2):
|
|
| 179 |
plt.figure(figsize=(9, 5))
|
| 180 |
plt.plot(df1['Week'], df1['End Balance'], label=f"{style1} ({badge1})", marker='o')
|
| 181 |
plt.plot(df2['Week'], df2['End Balance'], label=f"{style2} ({badge2})", marker='o')
|
| 182 |
-
plt.title(f"
|
| 183 |
plt.xlabel("Week")
|
| 184 |
plt.ylabel("Account Balance")
|
| 185 |
plt.grid(True)
|
|
@@ -189,27 +147,44 @@ def battle_strategies(style1, style2):
|
|
| 189 |
plt.savefig(plot_path)
|
| 190 |
plt.close()
|
| 191 |
|
| 192 |
-
df_compare = pd.DataFrame({
|
| 193 |
-
"Metric": list(s1.keys()) + ["Badge"],
|
| 194 |
-
style1: list(s1.values()) + [badge1],
|
| 195 |
-
style2: list(s2.values()) + [badge2]
|
| 196 |
-
})
|
| 197 |
-
|
| 198 |
return df_compare, plot_path
|
| 199 |
|
| 200 |
-
|
| 201 |
-
battle_tab = gr.Interface(
|
| 202 |
-
fn=battle_strategies,
|
| 203 |
-
inputs=[gr.Dropdown(choices=list(strategy_presets.keys()), label="Strategy 1"), gr.Dropdown(choices=list(strategy_presets.keys()), label="Strategy 2")],
|
| 204 |
-
outputs=["dataframe", gr.Image(type="filepath")],
|
| 205 |
-
title="π₯ Battle Mode"
|
| 206 |
-
)
|
| 207 |
-
|
| 208 |
-
analytics_tab = gr.Interface(fn=analytics_dashboard, inputs=[], outputs="dataframe", title="π Analytics")
|
| 209 |
-
|
| 210 |
-
# Main App Interface
|
| 211 |
gr.TabbedInterface(
|
| 212 |
-
interface_list=[
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
title="EdgeCast β Strategy Simulation Suite"
|
| 215 |
).launch()
|
|
|
|
| 7 |
# Disable Gradio analytics to avoid external API calls
|
| 8 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 9 |
|
| 10 |
+
# Strategy Presets (risk_reward_ratio included per preset)
|
| 11 |
strategy_presets = {
|
| 12 |
"Aggressive Prop Trader": {
|
| 13 |
"starting_balance": 2500, "trades_min": 5, "trades_max": 10, "weeks": 12,
|
|
|
|
| 31 |
}
|
| 32 |
}
|
| 33 |
|
|
|
|
|
|
|
| 34 |
def get_scaled_risk_pct(balance, base_risk_pct):
|
| 35 |
if balance < 5000: return base_risk_pct
|
| 36 |
elif balance < 10000: return base_risk_pct * 0.75
|
|
|
|
| 42 |
profit_target=None):
|
| 43 |
sl_prob = 1.0 - tp1_prob - tp2_prob
|
| 44 |
tp1_r = risk_reward_ratio
|
| 45 |
+
tp2_r = risk_reward_ratio * 2
|
|
|
|
| 46 |
balance = starting_balance
|
| 47 |
peak = balance
|
| 48 |
drawdown = 0
|
|
|
|
| 80 |
peak = max(peak, balance)
|
| 81 |
drawdown = max(drawdown, (peak - balance) / peak * 100)
|
| 82 |
weekly_return = (balance - week_start) / week_start * 100
|
| 83 |
+
log.append({"Week": week, "Start Balance": round(week_start, 2), "End Balance": round(balance, 2), "Weekly Return (%)": round(weekly_return, 2)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
summary = {"Final Balance": round(balance, 2), "TP1 Hits": tp1_hits, "TP2 Hits": tp2_hits, "SL Hits": sl_hits,
|
| 86 |
+
"Max Drawdown %": round(drawdown, 2), "Max Win Streak": max_win_streak, "Max Loss Streak": max_loss_streak}
|
| 87 |
return pd.DataFrame(log), summary
|
| 88 |
+
|
| 89 |
def analytics_dashboard():
|
| 90 |
leaderboard = []
|
| 91 |
for name, config in strategy_presets.items():
|
| 92 |
+
df, summary = simulate_tp_strategy_full(**config)
|
| 93 |
returns = df['End Balance'].pct_change().dropna()
|
| 94 |
volatility = returns.std() * np.sqrt(52)
|
| 95 |
sharpe = returns.mean() / returns.std() * np.sqrt(52) if returns.std() > 0 else 0
|
|
|
|
| 98 |
max_dd = dd.max() * 100 if not dd.empty else 0
|
| 99 |
final = df['End Balance'].iloc[-1] if not df.empty else config['starting_balance']
|
| 100 |
score = final / (1 + max_dd)
|
| 101 |
+
leaderboard.append({"Strategy": name, "Final Balance": round(final, 2), "Max DD %": round(max_dd, 2), "Sharpe": round(sharpe, 2), "EdgeCast Score": round(score, 2)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
return pd.DataFrame(leaderboard).sort_values("EdgeCast Score", ascending=False).reset_index(drop=True)
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
def battle_strategies(style1, style2):
|
| 105 |
p1 = strategy_presets[style1]
|
| 106 |
p2 = strategy_presets[style2]
|
| 107 |
+
df1, s1 = simulate_tp_strategy_full(**p1)
|
| 108 |
+
df2, s2 = simulate_tp_strategy_full(**p2)
|
| 109 |
|
| 110 |
def enrich_summary(df, summary):
|
| 111 |
returns = df['End Balance'].pct_change().dropna()
|
|
|
|
| 137 |
plt.figure(figsize=(9, 5))
|
| 138 |
plt.plot(df1['Week'], df1['End Balance'], label=f"{style1} ({badge1})", marker='o')
|
| 139 |
plt.plot(df2['Week'], df2['End Balance'], label=f"{style2} ({badge2})", marker='o')
|
| 140 |
+
plt.title(f"π¨ Strategy Battle β {style1} vs {style2}")
|
| 141 |
plt.xlabel("Week")
|
| 142 |
plt.ylabel("Account Balance")
|
| 143 |
plt.grid(True)
|
|
|
|
| 147 |
plt.savefig(plot_path)
|
| 148 |
plt.close()
|
| 149 |
|
| 150 |
+
df_compare = pd.DataFrame({"Metric": list(s1.keys()) + ["Badge"], style1: list(s1.values()) + [badge1], style2: list(s2.values()) + [badge2]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
return df_compare, plot_path
|
| 152 |
|
| 153 |
+
# Interface Setup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
gr.TabbedInterface(
|
| 155 |
+
interface_list=[
|
| 156 |
+
gr.Interface(fn=lambda style: simulate_tp_strategy_full(**strategy_presets[style]),
|
| 157 |
+
inputs=gr.Dropdown(choices=list(strategy_presets.keys()), label="Choose Preset Strategy"),
|
| 158 |
+
outputs=["dataframe", "json"], title="π Preset Mode"),
|
| 159 |
+
|
| 160 |
+
gr.Interface(fn=simulate_tp_strategy_full,
|
| 161 |
+
inputs=[
|
| 162 |
+
gr.Slider(100, 20000, 2500, label="Start Balance"),
|
| 163 |
+
gr.Slider(1, 10, 3, label="Trades Min"),
|
| 164 |
+
gr.Slider(1, 15, 7, label="Trades Max"),
|
| 165 |
+
gr.Slider(1, 52, 12, label="Weeks"),
|
| 166 |
+
gr.Slider(0, 1, 0.3, step=0.05, label="TP1 %"),
|
| 167 |
+
gr.Slider(0, 1, 0.3, step=0.05, label="TP2 %"),
|
| 168 |
+
gr.Slider(0.1, 20, 2.0, step=0.1, label="Risk Reward Ratio"),
|
| 169 |
+
gr.Slider(0.001, 0.05, 0.01, step=0.001, label="Risk %"),
|
| 170 |
+
gr.Slider(0, 100000, 0, step=500, label="Profit Target π°")
|
| 171 |
+
],
|
| 172 |
+
outputs=["dataframe", "json"],
|
| 173 |
+
title="π οΈ Manual Config"),
|
| 174 |
+
|
| 175 |
+
gr.Interface(fn=battle_strategies,
|
| 176 |
+
inputs=[
|
| 177 |
+
gr.Dropdown(choices=list(strategy_presets.keys()), label="Strategy 1"),
|
| 178 |
+
gr.Dropdown(choices=list(strategy_presets.keys()), label="Strategy 2")
|
| 179 |
+
],
|
| 180 |
+
outputs=["dataframe", gr.Image(type="filepath")],
|
| 181 |
+
title="π¨ Battle Mode"),
|
| 182 |
+
|
| 183 |
+
gr.Interface(fn=analytics_dashboard,
|
| 184 |
+
inputs=[],
|
| 185 |
+
outputs="dataframe",
|
| 186 |
+
title="π Analytics")
|
| 187 |
+
],
|
| 188 |
+
tab_names=["Preset", "Manual", "Battle", "Analytics"],
|
| 189 |
title="EdgeCast β Strategy Simulation Suite"
|
| 190 |
).launch()
|