Modular backtesting environment that follows the four parts outlined in the project brief: data acquisition and preparation, exchange simulation, strategy backtesting, and an Alpaca paper-trading handoff. Everything runs locally in pure Python.
data_pipeline.download_equity_data()pulls equity candles viayfinanceand stores them inraw_data_stock/as CSV files with columnsDatetime, Open, High, Low, Close, Volume.data_pipeline.download_crypto_data()fetches crypto candles from the Binance REST API and stores them inraw_data_crypto/with the same schema.
from data_pipeline import download_equity_data, download_crypto_data
raw_equity_path = download_equity_data("AAPL", period="7d", interval="1m")
raw_crypto_path = download_crypto_data("BTCUSDT", interval="1m", limit=1000)data_pipeline.clean_market_data()removes missing/duplicate rows, enforces a chronologicalDatetimeindex, and adds derived features such as returns, rolling volatility, and momentum.- The cleaned datasets are saved to
clean_data_stock/orclean_data_crypto/and are ready for ingestion by the gateway.
from data_pipeline import clean_market_data
clean_path = clean_market_data(raw_equity_path)
print("Cleaned data:", clean_path)strategy_base.Strategydefines the interface, whilestrategy_base.MovingAverageStrategyimplements a moving average crossover with clear entry/exit rules and position sizing logic.
from strategy_base import MovingAverageStrategy
strategy = MovingAverageStrategy(short_window=20, long_window=60, position_size=15)Reads cleaned CSV files and streams candles row-by-row through an iterator or
the stream() generator. Mimics a live feed by optionally adding delays.
Stores bids and asks in heaps to enforce price-time priority. Supports order addition, modification (by cancel + re-add), cancellation, and matching.
Performs capital sufficiency checks, position-risk checks (long + short limits),
and order rate limiting. OrderLoggingGateway writes a JSONL audit trail of
submitted, rejected, and executed orders.
Randomly decides whether matched orders are filled, partially filled, or cancelled, and returns execution reports for the backtester.
Strategy_Backtesting.Backtester ties together the gateway, strategy, order
book, order manager, matching engine, and logger to simulate execution. The
loop:
- Pulls a new candle from the gateway.
- Runs the strategy to update indicators and generate a signal.
- Builds an order (with configurable position size) when signals fire.
- Validates via
OrderManagerand inserts orders into theOrderBook. - Uses the
MatchingEngineto simulate fills/partials/cancellations. - Tracks portfolio cash, positions, equity, and trade-level PnL.
Strategy_Backtesting.PerformanceAnalyzer computes PnL, Sharpe ratio, max
drawdown, and win rate. plot_equity() visualizes the equity curve.
Quick start
python test_system.py
This generates sample data (if needed), runs the backtest, and prints summary statistics.
- Create an Alpaca account at alpaca.markets and complete identity verification (paper trading only).
- Configure paper trading via the Alpaca dashboard (Paper Overview → Reset virtual funds as needed).
- Obtain API keys from the dashboard and keep them secure. Use only paper trading endpoints.
- Retrieve market data with the Alpaca SDK:
import alpaca_trade_api as tradeapi api = tradeapi.REST(API_KEY, API_SECRET, "https://paper-api.alpaca.markets") data = api.get_bars("AAPL", "1Min", limit=100).df data.to_csv("alpaca_data/AAPL_1m.csv")
- Save market data in flat files (CSV/Parquet) or a database. Organize by asset and timeframe, and handle timezones carefully.
- Use your Part 1 strategy with Alpaca by feeding Alpaca candles through
the same classes (
MarketDataGateway,Backtester, etc.). No new strategy code is required; you can re-use and tune the existing implementation.
clean_data_crypto/
clean_data_stock/
raw_data_crypto/
raw_data_stock/
data_pipeline.py
gateway.py
order_book.py
order_manager.py
matching_engine.py
strategy_base.py
Strategy_Backtesting.py
test_system.py
Readme.md
Create a virtual environment and install dependencies:
pip install -r requirements.txt
Key libraries: pandas, numpy, matplotlib, yfinance, requests,
alpaca-trade-api (for Part 4), and standard Python tooling.