CoolFace
Apppublic

gribsons/quant-tradingbot

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
App README

QuantBot V1

A Python algorithmic trading backtesting system that trades small-cap stocks using a momentum-pullback strategy.

What This Is

QuantBot V1 downloads real stock market data, identifies stocks with strong price momentum that have briefly pulled back, and simulates buying and selling them with realistic costs. It then tells you whether the strategy would have made money historically.

The Strategy (Plain English)

  1. 1.Universe: 20 small-cap stocks
  2. 2.Entry: Buy when a stock is in the top 20% for 20-day momentum AND has pulled back to just below its 5-day moving average AND volume is elevated
  3. 3.Exit: Sell when price rises 3% above the 20-day MA (take profit) OR falls 8% from entry (stop loss)
  4. 4.Sizing: Equal weight across up to 10 positions at once

Setup

bash
pip install -r requirements.txt

Create a .env file (already provided) — the Alpaca keys are not needed for backtesting.

How to Run

Interactive menu (recommended):

bash
python main.py

Individual components:

bash
python data/fetch.py              # Download and clean price data
python strategies/momentum.py     # Generate buy/sell signals
python backtest/run.py            # Run the historical backtest
python simulator/sim.py           # Run the market simulator
python backtest/walk_forward.py   # Validate the edge
python results/charts.py          # Generate performance charts

What Each Component Does

FilePurpose
data/fetch.pyDownloads 3 years of OHLCV data from Yahoo Finance, cleans it
strategies/momentum.pyGenerates dated buy/exit signals and saves to results/signals.csv
backtest/run.pySimulates trading from signals, fills at next-day open, tracks every trade
simulator/sim.pyReplays historical data day by day — the bot never sees the future
backtest/walk_forward.pySplits data 60/20/20 and tests on each window separately
results/charts.pyGenerates 4 PNG charts in results/charts/

How to Read the Results

After running the backtest, check results/metrics.json for:

MetricWhat It Means
Total ReturnHow much the portfolio grew overall
Annualized ReturnWhat the equivalent yearly growth rate would be
Sharpe RatioReturn adjusted for risk. Above 1.0 is good, above 0.5 is acceptable
Max DrawdownThe worst peak-to-trough loss. Below -20% is concerning
Win RatePercentage of trades that were profitable
Profit FactorTotal wins divided by total losses. Above 1.5 is good
Avg Hold DaysHow long the bot typically holds a position

Walk-Forward Verdicts

VerdictMeaning
EDGE LIKELY REALOut-of-sample Sharpe > 0.8. Strategy has real merit
WEAK EDGEOut-of-sample Sharpe 0.4-0.8. Might work, needs refinement
NO EDGE DETECTEDOut-of-sample Sharpe < 0.4. Strategy is likely overfitted

What to Do If Results Are Bad

  1. 1.Sharpe < 0.4 on out-of-sample: The strategy is overfitted to the training period. Try loosening the momentum rank threshold or changing the pullback window.
  2. 2.High max drawdown (>25%): The stop loss may be too wide. Try tightening STOP_LOSS_PCT in backtest/run.py.
  3. 3.Very few trades: Signal conditions are too strict. Try widening PULLBACK_LOW from -3% to -5%.
  4. 4.Win rate < 40%: Exit conditions need tuning. Consider a tighter take-profit target.
  5. 5.Large gap between training and out-of-sample: The strategy is curve-fitted. Use fewer parameters and simpler rules.

Output Files

results/
  signals.csv           -- All buy/exit signals with dates and prices
  trade_log.csv         -- Every completed trade with P&L
  equity_curve.csv      -- Daily portfolio value
  metrics.json          -- All performance statistics
  simulation_results.csv -- Simulator output
  walk_forward.json     -- Walk-forward results and verdict
  charts/
    equity_curve.png    -- Portfolio vs SPY over time
    trade_distribution.png -- Histogram of individual trade returns
    monthly_heatmap.png -- Monthly return calendar
    signal_analysis.png -- Signal frequency and win rate per ticker