Skip to content

kechprog/trading_strat

Repository files navigation

Trading Strategy Backtesting Framework

This project implements a trading strategy backtesting system using the Nautilus Trader platform. Backtest simulates a full venue with order book and slippage. The strategy uses trend divergence analysis with volume confirmation to identify trading opportunities.

Strategy Overview

The strategy is based on the concept of Trend Divergence with Volume Confirmation:

  1. Dual Trend Analysis:

    • Long-term trend: Linear regression on EMA(100 hours)
    • Short-term trend: Recent price momentum (6-12 days period)
  2. Volume as Confirmation Signal:

    • Big volume jumps indicate genuine trend changes
    • Moderate volume increases suggest temporary deviations with continuation
  3. Trading Logic:

    • When short-term and long-term trends diverge:
      • Big volume jump → Follow short-term trend (new trend forming)
      • Moderate/low volume → Expect reversion to long-term trend

Project Structure

├── backtest.py              # Main backtest configuration and execution
├── breakout.py              # Core strategy implementation
├── indicators/              # Custom trading indicators
│   ├── trend_div_vol_spike.py  # Main indicator logic
│   └── high_low_hist.py     # High/low level tracking
├── visualize.py             # Visualization tools for backtest results
└── requirements.txt         # Project dependencies

Implementation Details

Core Components

  1. Breakout Strategy (breakout.py):

    • Uses VOO (S&P 500 ETF) as the main instrument and SH (Short S&P 500 ETF) as the reverse instrument
    • Entry/exit rules based on daily high/low levels and indicator signals
    • Risk management with position sizing based on account balance
  2. Trend Divergence Indicator (indicators/trend_div_vol_spike.py):

    • Calculates long-term and short-term trends
    • Analyzes volume patterns to confirm trend changes
    • Generates continuous signals for the strategy
  3. High/Low Historical Indicator (indicators/high_low_hist.py):

    • Tracks historical high/low levels for entry and exit points
    • Provides dynamic support/resistance levels

Backtest Configuration

The current backtest configuration runs from December 1, 2023 to November 27, 2025 with:

  • Initial capital: $100,000
  • Instruments: VOO.NASDAQ (long) and SH.NASDAQ (short)
  • Timeframe: 1-minute bars
  • Indicator: MomentumMeanReversionNautilusIndicator by default

Results

Latest smoke-test results after the Alpha Vantage refresh:

  • Total Profit: $30,807.81 (30.81% return)
  • Sharpe Ratio: 1.21
  • Sortino Ratio: 2.10
  • Win Rate: 37.4%
  • Profit Factor: 1.61

The strategy generated 406 entry/exit markers over the available December 1, 2023 through November 26, 2025 data.

Usage

  1. Create a virtual environment and install dependencies:

    python -m venv .venv
    .\.venv\Scripts\activate
    pip install -r requirements.txt
  2. Download Alpha Vantage data:

    python download_alpha_vantage.py --symbols VOO SH --start 2023-12-01 --end 2025-11-27
  3. Build the Nautilus catalog:

    python create_catalog.py
  4. Run the backtest:

    python backtest.py
  5. Visualize results:

    python visualize.py

Order Book Simulation

The system uses Nautilus Trader's advanced market simulator which provides:

  • Full order book reconstruction
  • Realistic market dynamics
  • Accurate transaction cost modeling
  • Proper handling of market orders, limit orders, and partial fills

This ensures that backtest results closely reflect how the strategy would perform in real market conditions.

Data

The strategy uses minute-level data for VOO and SH ETFs. Raw Alpha Vantage CSVs are written to raw_data, then create_catalog.py converts them into the parquet catalog directory.

The downloader reads the API key from ALPHA_KEY in .env by default.

Visualization

The backtest generates two CSV files:

  • backtest_1m_log.csv: Per-minute snapshots with price, indicator values, and equity
  • backtest_events.csv: Entry/exit events with details

These can be visualized using the visualize.py script which creates interactive plots showing:

  • Price action with entry/exit markers
  • Cumulative indicator values
  • Equity curve

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors