Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,6 +53,140 @@ def get_strategy_presets():
|
|
| 53 |
|
| 54 |
strategy_presets = get_strategy_presets()
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# === CORE SIMULATION ===
|
| 57 |
def simulate_tp_strategy_full(starting_balance, trades_min, trades_max, weeks,
|
| 58 |
tp1_prob, tp2_prob, tp1_r, tp2_r, base_risk_pct,
|
|
|
|
| 53 |
|
| 54 |
strategy_presets = get_strategy_presets()
|
| 55 |
|
| 56 |
+
# === CORE SIMULATION ===
|
| 57 |
+
def simulate_tp_strategy_full(starting_balance, trades_min, trades_max, weeks,
|
| 58 |
+
tp1_prob, tp2_prob, tp1_r, tp2_r, base_risk_pct,
|
| 59 |
+
profit_target=None, fatigue=0.0, trump_vol=0.0):
|
| 60 |
+
if tp1_prob + tp2_prob >= 1.0:
|
| 61 |
+
return pd.DataFrame(), {"Error": "Invalid probability config. TP1 + TP2 must be < 1.0"}
|
| 62 |
+
|
| 63 |
+
sl_prob = 1.0 - tp1_prob - tp2_prob
|
| 64 |
+
balance, peak, drawdown = starting_balance, starting_balance, 0
|
| 65 |
+
tp1_hits = tp2_hits = sl_hits = 0
|
| 66 |
+
max_win_streak = max_loss_streak = cur_win_streak = cur_loss_streak = 0
|
| 67 |
+
fatigue_multiplier = 1.0 - fatigue * 0.4
|
| 68 |
+
trump_vol_factor = np.random.normal(1.0, 0.2 * trump_vol)
|
| 69 |
+
log = []
|
| 70 |
+
|
| 71 |
+
for week in range(1, weeks + 1):
|
| 72 |
+
if profit_target and balance >= profit_target: break
|
| 73 |
+
week_start = balance
|
| 74 |
+
num_trades = np.random.randint(trades_min, trades_max + 1)
|
| 75 |
+
|
| 76 |
+
for _ in range(num_trades):
|
| 77 |
+
risk_amount = balance * base_risk_pct * np.random.uniform(0.9, 1.1)
|
| 78 |
+
risk_amount *= trump_vol_factor
|
| 79 |
+
if fatigue > 0.6 and cur_loss_streak >= 3 and np.random.rand() < fatigue * 0.25:
|
| 80 |
+
outcome = "SL"
|
| 81 |
+
else:
|
| 82 |
+
outcome = np.random.choice(["TP1", "TP2", "SL"], p=[tp1_prob, tp2_prob, sl_prob])
|
| 83 |
+
|
| 84 |
+
if outcome == "TP1":
|
| 85 |
+
balance += risk_amount * tp1_r * fatigue_multiplier
|
| 86 |
+
tp1_hits += 1
|
| 87 |
+
cur_win_streak += 1
|
| 88 |
+
cur_loss_streak = 0
|
| 89 |
+
elif outcome == "TP2":
|
| 90 |
+
balance += risk_amount * tp2_r * fatigue_multiplier
|
| 91 |
+
tp2_hits += 1
|
| 92 |
+
cur_win_streak += 1
|
| 93 |
+
cur_loss_streak = 0
|
| 94 |
+
else:
|
| 95 |
+
balance -= risk_amount
|
| 96 |
+
sl_hits += 1
|
| 97 |
+
cur_loss_streak += 1
|
| 98 |
+
cur_win_streak = 0
|
| 99 |
+
|
| 100 |
+
max_win_streak = max(max_win_streak, cur_win_streak)
|
| 101 |
+
max_loss_streak = max(max_loss_streak, cur_loss_streak)
|
| 102 |
+
peak = max(peak, balance)
|
| 103 |
+
drawdown = max(drawdown, (peak - balance) / peak * 100)
|
| 104 |
+
|
| 105 |
+
log.append({
|
| 106 |
+
"Week": week, "Start Balance": round(week_start, 2),
|
| 107 |
+
"End Balance": round(balance, 2),
|
| 108 |
+
"Weekly Return (%)": round((balance - week_start) / week_start * 100, 2)
|
| 109 |
+
})
|
| 110 |
+
|
| 111 |
+
df = pd.DataFrame(log)
|
| 112 |
+
returns = df["End Balance"].pct_change().dropna()
|
| 113 |
+
sharpe = returns.mean() / returns.std() * np.sqrt(52) if returns.std() > 0 else 0
|
| 114 |
+
score = balance / (1 + drawdown)
|
| 115 |
+
summary = {
|
| 116 |
+
"Final Balance": round(balance, 2),
|
| 117 |
+
"TP1 Hits": tp1_hits,
|
| 118 |
+
"TP2 Hits": tp2_hits,
|
| 119 |
+
"SL Hits": sl_hits,
|
| 120 |
+
"Max Drawdown %": round(drawdown, 2),
|
| 121 |
+
"Max Win Streak": max_win_streak,
|
| 122 |
+
"Max Loss Streak": max_loss_streak,
|
| 123 |
+
"Sharpe Ratio": round(sharpe, 2),
|
| 124 |
+
"EdgeCast Score": round(score, 2)
|
| 125 |
+
}
|
| 126 |
+
return df, summary
|
| 127 |
+
|
| 128 |
+
# === VISUALIZATION ===
|
| 129 |
+
def equity_curve_plot(df, label="Equity Curve"):
|
| 130 |
+
fig = go.Figure()
|
| 131 |
+
fig.add_trace(go.Scatter(x=df["Week"], y=df["End Balance"], mode="lines+markers", name=label))
|
| 132 |
+
fig.update_layout(title=f"📈 {label}", xaxis_title="Week", yaxis_title="Balance ($)", height=400)
|
| 133 |
+
return fig
|
| 134 |
+
|
| 135 |
+
import os
|
| 136 |
+
import gradio as gr
|
| 137 |
+
import pandas as pd
|
| 138 |
+
import numpy as np
|
| 139 |
+
import plotly.graph_objs as go
|
| 140 |
+
import plotly.express as px
|
| 141 |
+
|
| 142 |
+
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 143 |
+
try:
|
| 144 |
+
gr.analytics_enabled = False
|
| 145 |
+
except:
|
| 146 |
+
pass
|
| 147 |
+
|
| 148 |
+
# === STRATEGY PRESETS ===
|
| 149 |
+
def get_strategy_presets():
|
| 150 |
+
return {
|
| 151 |
+
"Aggressive Prop Trader": {
|
| 152 |
+
"starting_balance": 2500, "trades_min": 5, "trades_max": 10, "weeks": 12,
|
| 153 |
+
"tp1_prob": 0.25, "tp2_prob": 0.4, "tp1_r": 1.2, "tp2_r": 2.4,
|
| 154 |
+
"base_risk_pct": 0.015, "profit_target": None,
|
| 155 |
+
"fatigue": 0.0, "trump_vol": 0.0,
|
| 156 |
+
"description": "High-frequency, high-risk with strong upside potential."
|
| 157 |
+
},
|
| 158 |
+
"Conservative Swing Trader": {
|
| 159 |
+
"starting_balance": 2500, "trades_min": 2, "trades_max": 5, "weeks": 12,
|
| 160 |
+
"tp1_prob": 0.35, "tp2_prob": 0.25, "tp1_r": 0.9, "tp2_r": 1.8,
|
| 161 |
+
"base_risk_pct": 0.01, "profit_target": None,
|
| 162 |
+
"fatigue": 0.0, "trump_vol": 0.0,
|
| 163 |
+
"description": "Lower frequency, prioritizes preservation and steady returns."
|
| 164 |
+
},
|
| 165 |
+
"Momentum Scalper": {
|
| 166 |
+
"starting_balance": 2500, "trades_min": 4, "trades_max": 8, "weeks": 12,
|
| 167 |
+
"tp1_prob": 0.3, "tp2_prob": 0.35, "tp1_r": 1.0, "tp2_r": 2.2,
|
| 168 |
+
"base_risk_pct": 0.012, "profit_target": None,
|
| 169 |
+
"fatigue": 0.0, "trump_vol": 0.0,
|
| 170 |
+
"description": "Intraday momentum strategy for fast-paced trading windows."
|
| 171 |
+
},
|
| 172 |
+
"Swing Sniper": {
|
| 173 |
+
"starting_balance": 2500, "trades_min": 2, "trades_max": 4, "weeks": 12,
|
| 174 |
+
"tp1_prob": 0.2, "tp2_prob": 0.5, "tp1_r": 1.1, "tp2_r": 3.0,
|
| 175 |
+
"base_risk_pct": 0.008, "profit_target": None,
|
| 176 |
+
"fatigue": 0.0, "trump_vol": 0.0,
|
| 177 |
+
"description": "Selective entries with high RR setups. Less frequent."
|
| 178 |
+
},
|
| 179 |
+
"Intraday Prop Mode": {
|
| 180 |
+
"starting_balance": 2500, "trades_min": 3, "trades_max": 7, "weeks": 12,
|
| 181 |
+
"tp1_prob": 0.3, "tp2_prob": 0.3, "tp1_r": 1.0, "tp2_r": 2.0,
|
| 182 |
+
"base_risk_pct": 0.02, "profit_target": None,
|
| 183 |
+
"fatigue": 0.0, "trump_vol": 0.0,
|
| 184 |
+
"description": "Intraday consistency with a balanced reward profile."
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
strategy_presets = get_strategy_presets()
|
| 189 |
+
|
| 190 |
# === CORE SIMULATION ===
|
| 191 |
def simulate_tp_strategy_full(starting_balance, trades_min, trades_max, weeks,
|
| 192 |
tp1_prob, tp2_prob, tp1_r, tp2_r, base_risk_pct,
|