-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstrategy.py
More file actions
239 lines (192 loc) · 9.38 KB
/
Copy pathstrategy.py
File metadata and controls
239 lines (192 loc) · 9.38 KB
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
"""
Final Optimized Strategy V5 — BTC 1H Price Action Trading System
V1 WINNER: 1H MS+FVG, No Trailing → +49.7%, Sharpe 1.81, DD 10.3%
V2 IMPROVEMENTS:
A. Suppress SL for first 19 bars → stops early shakeouts
B. Breakeven + 0.3 ATR after TP1 → locks partial profits
C. Min signal score 0.60 → filters weak trades
V2 RESULT: +95.5%, Sharpe 3.36, Max DD 5.9%, WR 71.1%, PF 2.06
V3 IMPROVEMENTS:
A. TP Rebalance: tp1_close 40%→20%, tp3_atr 8→6x
D. Tiered Sizing: (was NO-OP — all scores 0.65, mapped to 1% risk)
F. Extended SL suppression: 19→30 bars
V3 RESULT: +173.2%, Sharpe 4.66, Max DD 4.1%, WR 75.9%, PF 3.24
V4 IMPROVEMENTS (fix broken tiered sizing, extend parameter ranges):
A. Risk fix: disable broken tiered sizing, flat 2% risk (was 1% effective)
B. Extended SL suppression: 30→50 bars (monotonic improvement continues)
C. TP1 rebalance: tp1_close 20%→5%, tp3_close 50%→65% (maximize TP3 capture)
V4 RESULT: +250.5%, Sharpe 4.29, Max DD 6.6%, WR 75.8%, PF 4.06
V5 IMPROVEMENTS (return maximization — tighter SL/TP + unlocked position sizing):
A. Tighter SL: 2.0→1.0x ATR (tighter stop = less $ lost per loss)
B. Earlier TP1: 2.0→1.0x ATR (lock profits sooner → WR 75.8%→84.3%)
C. Risk increase: 2%→3% with position cap 50%→80% (was cap-bound at 2%)
V5 RESULT: +949.7%, Sharpe 5.40, Max DD 8.55%, WR 84.3%, PF 12.46
Expectancy: $114→$426/trade
Walk-forward: 5/5 folds profitable (avg OOS +48.72%)
Usage:
python3 strategy.py # backtest with default params
python3 strategy.py --validate # walk-forward validation
"""
import pandas as pd
import numpy as np
from pathlib import Path
from datetime import datetime, timezone
from data_fetcher import fetch_klines, load_csv
from backtester import Backtester, format_metrics, walk_forward_test
from stake_manager import StakeManager, SizingMethod, Direction
from indicators import add_atr
# ═══════════════════════════════════════════════════════════════
# STRATEGY CONFIGURATION
# ═══════════════════════════════════════════════════════════════
STRATEGY_PARAMS = {
# Capital & Risk
"initial_capital": 10_000,
"risk_pct": 0.03, # V5-C: 3% risk (was 2%, cap-bound)
"sizing_method": SizingMethod.FIXED_FRACTIONAL,
"max_position_pct": 0.80, # V5-C: 80% position cap (was 50%, bottleneck)
# Entry
"use_market_structure": True,
"use_fvg": True,
"use_smc": False,
"use_classic_pa": False,
"min_confluence": 0.50, # require both MS + FVG agreement
# Stop Loss
"sl_atr_mult": 1.0, # V5-A: 1x ATR stop (was 2x — tighter = less $ lost)
# Take Profits (progressive, ATR-based)
"tp1_atr_mult": 1.0, # V5-B: TP1 at 1x ATR (was 2x — locks profit sooner)
"tp2_atr_mult": 4.0, # TP2 at 4x ATR
"tp3_atr_mult": 6.0, # TP3 at 6x ATR
"tp1_close_pct": 0.05, # close 5% at TP1
"tp2_close_pct": 0.30, # close 30% at TP2
"tp3_close_pct": 0.65, # close remaining 65% at TP3
# Trailing Stop — DISABLED (key optimization finding)
"use_trailing": False,
"trailing_atr_mult": 0,
"trailing_activation_atr": 0,
# ── V2 Improvements ──
# B: Move SL to breakeven + 0.3 ATR buffer after TP1
"be_after_tp1": True,
"be_buffer_atr": 0.30,
# C: Filter weak signals (require score >= 0.60)
"min_signal_score": 0.60,
# ── V3+V4 Improvements ──
# V4-B: Extended SL suppression — 50 bars (was 30)
"min_bars_before_sl": 50,
# V4-A: Tiered sizing DISABLED
"use_tiered_sizing": False,
# Execution
"warmup_bars": 50,
"commission_pct": 0.075, # Binance taker fee
"slippage_pct": 0.02, # realistic BTC 1H slippage
# Realism patches (2026-05-08). See docs/launch-gates.md.
"tp_require_close": True, # P1: TP fills only when bar closes beyond the level
"slippage_atr_frac": 0.05, # P2: extra ATR-fraction slippage on every fill
"spread_atr_frac": 0.03, # P3: bid/ask spread as fraction of ATR
# Hard stop — catastrophic protection during SL suppression.
# P4: tightened from 15× ATR to 8× ATR (2026-05-08). Old floor was wide
# enough to mask real risk-of-ruin during the 50-bar SL suppression window.
"use_hard_stop": True,
"hard_stop_atr_mult": 8.0,
}
DATA_DIR = Path(__file__).parent / "data"
REPORT_DIR = Path(__file__).parent / "reports"
# ═══════════════════════════════════════════════════════════════
# MAIN BACKTEST
# ═══════════════════════════════════════════════════════════════
def run_backtest(
symbol: str = "BTCUSDT",
interval: str = "1h",
start_date: str = "2023-01-01",
end_date: str = None,
params: dict = None,
) -> dict:
"""Run the optimized strategy on historical data."""
if params is None:
params = STRATEGY_PARAMS.copy()
# Load or fetch data
pattern = f"{symbol}_{interval}_*.csv"
files = list(DATA_DIR.glob(pattern))
if files:
filepath = max(files, key=lambda f: f.stat().st_size)
df = pd.read_csv(filepath)
df["date"] = pd.to_datetime(df["date"], utc=True)
print(f"Loaded {len(df)} bars from {filepath.name}")
else:
print("Fetching fresh data...")
df = fetch_klines(symbol=symbol, interval=interval,
start_date=start_date, end_date=end_date)
# Run backtest
bt = Backtester(**params)
result = bt.run(df)
return result
def run_full_report(result: dict, title: str = "BTCUSDT 1H Strategy"):
"""Print comprehensive report from backtest result."""
print(format_metrics(result["metrics"], title))
# Trade distribution by direction
trades = result["trades"]
if trades:
long_t = [t for t in trades if t.direction == "LONG"]
short_t = [t for t in trades if t.direction == "SHORT"]
long_pnl = sum(t.pnl for t in long_t if t.pnl)
short_pnl = sum(t.pnl for t in short_t if t.pnl)
print(f"\n DIRECTION BREAKDOWN")
print(f" {'Long PnL:':<30} ${long_pnl:,.2f} ({len(long_t)} trades)")
print(f" {'Short PnL:':<30} ${short_pnl:,.2f} ({len(short_t)} trades)")
# Monthly returns
print(f"\n MONTHLY RETURNS")
monthly = {}
for t in trades:
if t.pnl is None:
continue
month_key = t.exit_date[:7] if t.exit_date else "Unknown"
if month_key not in monthly:
monthly[month_key] = 0
monthly[month_key] += t.pnl
for month, pnl in sorted(monthly.items()):
bar = "+" * int(min(abs(pnl) / 50, 30)) if pnl > 0 else "-" * int(min(abs(pnl) / 50, 30))
print(f" {month}: ${pnl:>+8.2f} {bar}")
def run_validation(params: dict = None):
"""Run walk-forward validation."""
if params is None:
params = STRATEGY_PARAMS.copy()
pattern = "BTCUSDT_1h_*.csv"
files = list(DATA_DIR.glob(pattern))
filepath = max(files, key=lambda f: f.stat().st_size)
df = pd.read_csv(filepath)
df["date"] = pd.to_datetime(df["date"], utc=True)
print(f"\n{'='*60}")
print(f" WALK-FORWARD VALIDATION")
print(f"{'='*60}")
wf = walk_forward_test(df, params, n_folds=5, train_pct=0.6)
print(f"\n Pass Rate: {wf['passing_folds']}/{wf['total_folds']} "
f"({'ROBUST' if wf['pass_rate'] >= 0.6 else 'WEAK'})")
print(f" Avg Test Return: {wf['avg_test_return_pct']:.2f}%")
print()
for fold in wf["folds"]:
status = "PASS" if fold["test_profitable"] else "FAIL"
fm = fold["full_metrics"]
print(f" Fold {fold['fold']}: "
f"train ${fold['train_pnl']:>+8.1f} ({fold['train_trades']}t) | "
f"test ${fold['test_pnl']:>+8.1f} ({fold['test_trades']}t) [{status}]")
return wf
# ═══════════════════════════════════════════════════════════════
# CLI
# ═══════════════════════════════════════════════════════════════
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="BTC 1H Price Action Strategy")
parser.add_argument("--validate", action="store_true", help="Run walk-forward validation")
parser.add_argument("--symbol", default="BTCUSDT")
parser.add_argument("--interval", default="1h")
parser.add_argument("--start", default="2023-01-01")
parser.add_argument("--end", default=None)
args = parser.parse_args()
result = run_backtest(
symbol=args.symbol,
interval=args.interval,
start_date=args.start,
end_date=args.end,
)
run_full_report(result)
if args.validate:
run_validation()