This project implements a long-only systematic trading strategy that combines a daily trend filter (EMA-based) with an hourly bullish engulfing candlestick pattern, and then backtests it on individual tickers or on a universe of S&P 500 stocks.[file:7][file:3]
The strategy operates on two timeframes and is designed to enter only in the direction of the prevailing daily uptrend.[file:7][file:3]
-
Daily trend filter
- Uses two EMAs on daily closing prices: a fast EMA and a slow EMA (default 9 and 21 periods).[file:7][file:3]
- Long entries are allowed only when the fast EMA is above the slow EMA, indicating an uptrend.[file:7][file:3]
-
Hourly entry signal
- Uses a bullish engulfing pattern on hourly candles.[file:7][file:3]
- A valid signal requires:
- Previous candle is bearish (close < open).
- Current candle is bullish (close > open).
- Current candle body fully engulfs previous candle body (current open ≤ previous close and current close ≥ previous open).[file:7][file:3]
-
Risk management and exits
- Long-only: the strategy never opens short positions.[file:7][file:3]
- Fixed take profit at a percentage above entry (default 3%).[file:7][file:3]
- 1% trailing stop based on price, implemented via a trailing stop order.[file:7][file:3]
- Position sizing targets a fixed percentage of account equity at risk per trade (default 2%).[file:7][file:3]
-
FINAL.py
Interactive single-ticker backtest script:- Prompts for a ticker symbol.
- Downloads ~2 years of hourly data and 5 years of daily data for that ticker.
- Runs the TrendEngulfingStrategy with analyzers (Sharpe, drawdown, trade stats).
- Prints a performance report and plots a candlestick chart of the backtest.[file:7]
-
FINAL-FOR-DATA-TESTING.PY
Batch backtest engine:- Reads a list of tickers from
SNP500.CSV(expects aSymbolcolumn). - Normalizes tickers (e.g.,
BRK.B→BRK-B) for the data provider. - Runs the same long-only strategy for each ticker.
- Collects metrics (final value, Sharpe ratio, drawdown, win rate, profit factor, returns, etc.).
- Saves results to a CSV file (e.g.
ema921.csvorsp500_backtest_results.csv).[file:3]
- Reads a list of tickers from
-
results.py
Helper/analysis script intended for loading and inspecting the generated result CSVs.[file:2] -
SNP500.CSV
Universe definition:- Contains S&P 500 tickers in a
Symbolcolumn. - Used as input for batch testing.[file:3][file:6]
- Contains S&P 500 tickers in a
-
ema921.csv,ema2150.csv,sp500_backtest_results.csv
Example backtest output files:- Each row corresponds to one ticker.
- Contains per-ticker performance metrics from batch runs.[file:1][file:5][file:4]
-
Clone the repository
git clone <your-repo-url> cd <your-repo-folder>