← Back to Portfolio
Python
TradingView
Pine Script
Backtesting
TopStepX
Trading Strategy Optimization
Full backtest optimization of an EMA 89 Flip strategy on E-mini S&P 500 — parameter sweep across 1,260 combinations, PDF report with actionable insights, and a production-ready Pine Script strategy.
Private (Client Strategy)
Overview
A prop firm trader on TopStepX had a promising EMA 89 crossover strategy with a 67% win rate — but was losing money overall due to uncapped losses. They needed data-driven parameter optimization to find the right TP/SL settings and trading windows.
I pulled historical price data, built a custom Renko-based backtesting engine in Python, ran a full parameter sweep across 1,260 configurations, and delivered a professional PDF report with clear recommendations. Then converted their indicator into a production-ready Pine Script strategy with the optimized settings built in.
Key Features
- Custom Renko construction engine from 1-minute price data — matching TradingView's Renko behavior
- EMA 89 calculated on Renko closes with flip detection (crossover signals)
- Full parameter sweep: 7 TP values x 6 SL values x 5 session windows x 3 brick sizes = 1,260 combinations
- 25 trading days of E-mini S&P 500 data (27,389 price bars)
- Professional PDF report with executive summary, KPI cards, optimization tables, and key insights
- Pine Script v5 strategy with configurable TP/SL, session filter, and real-time alerts
- Client's actual trade history analysis (228 trades across 6 accounts) to diagnose the root cause of losses
Results
- Best configuration: TP 3pt / SL 2pt / 9:00-16:00 ET — +$26,822 net profit, 67.7% win rate, profit factor 3.01
- Identified the root cause: client's 67% win rate was solid, but average loss was 3x average win (no stop loss)
- Adding a 2pt stop loss alone would have turned the same entries from -$6,504 to profitable
- Extended backtest from 15 to 25 trading days to validate consistency — results held up
- Client verified on TradingView: +$21,550 profit, 82.67% win rate, profit factor 7.15 over 11 months
Technical Details
- Python backtesting engine with numpy arrays for fast Renko construction and EMA calculation
- TopStepX API integration for pulling real trade history and account data across all 6 accounts
- Yahoo Finance API for extended historical 1-minute price data when TopStepX data was limited
- HTML/CSS report with dark theme, responsive KPI cards, and Chrome headless PDF generation
- Pine Script v5 strategy with strategy.entry/exit, session filtering, and alert() integration
- Fees ($2.80/round-trip) included in all PnL calculations for realistic performance estimates
Tech Stack
Python
numpy
pandas
TradingView Pine Script v5
TopStepX API
Yahoo Finance API
Chrome Headless PDF
HTML/CSS
Outcome
The client loaded the strategy on TradingView, confirmed it matched expectations, and released payment the same day. They left a 5-star review: "absolutely amazing, goes above and beyond to help his clients." The extended backtest and code update were delivered as a bonus — building trust that led to immediate repeat engagement potential.