Skip to content
Maintained
Data Science
Open Source
Jan 2024

trading-bot

An Alpaca paper-trading bot built to answer one question — can a retail momentum system beat the market? — and the measurement work that showed the answer is no. Over 2020–2024 the best validated strategy returned +16.3% against SPY's +95.3%.

+16.3%
Best strategy
+95.3%
SPY, same window
None — paper only
Real capital
backtest-audit
Audited by
trading-bot project visual

The constraint

The first backtests said +646%, then +53%, then +42.9%. Every one was a measurement bug rather than an edge, and a backtest that flatters you is more dangerous than one that fails, because it is the one you trade.

The approach

Fix the measurement first. The four defects behind those numbers — exits that could never fire, a strategy blind to its own positions, short sales that booked no liability, and sizing off idle cash — were fixed, then turned into mechanical checks in a separate tool, backtest-audit, which now audits every committed result.

Architecture
Momentum signals and a risk engine on paper; every backtest audited before it is believed.
High level
BacktestSlippage + spreadMarket DataPrice + volumeAuditbacktest-auditIndicatorsRSI, MACD, ADXRisk EngineLimits + sizingExecutionAlpaca papertrade logcomputeverdictfeaturesorders
Process
  1. 1
    Make exits reachable

    Found that the signal generator never emitted a sell and the engine never called the method holding exit logic, so every early run measured enter-and-hold.

  2. 2
    Book every liability

    Fixed position detection and naked shorts, which together had invented tens of thousands of dollars of cash in the backtest.

  3. 3
    A/B the filter

    Turned the Bollinger filter off to reach a statistically meaningful trade count, and found the unfiltered signal loses money.

  4. 4
    Make the lesson mechanical

    Extracted the four defects into backtest-audit so they are checked on every result instead of remembered.

Outcomes

  • An honest headline: +16.3% (Sharpe 0.16) for the best validated strategy against +95.3% (0.75) for SPY buy-and-hold, 2020–2024
  • Strategy and benchmark compared at matched gross exposure, so the comparison is like-for-like
  • Superseded runs still audit as NOT TRUSTWORTHY, reconstructing their defects from the trade log alone
  • Unvalidated research moved off main to an archive branch rather than left looking finished
What I'd do next
  • Revisit pairs trading, the one archived research thread with a case for a second look.
  • Push trade counts past the 50-trade significance bar before quoting any new result.
Details

Built an Alpaca paper-trading bot in async Python with a backtest engine modelling slippage and spread, a risk manager (VaR, correlation, sizing) and a daily-loss circuit breaker.

Traced three inflated backtest results (+646%, +53%, +42.9%) to measurement bugs, published the honest result that the strategy does not beat buy-and-hold, and extracted the defects into an audit tool.