Skip to content

RickyChavan/BAcktesting90

Repository files navigation

Trend + Engulfing S&P 500 Backtester

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]

Strategy Overview

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]

Repository Contents

  • 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 a Symbol column).
    • Normalizes tickers (e.g., BRK.BBRK-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.csv or sp500_backtest_results.csv).[file:3]
  • 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 Symbol column.
    • Used as input for batch testing.[file:3][file:6]
  • 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]

Installation

  1. Clone the repository

    git clone <your-repo-url>
    cd <your-repo-folder>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages