This project implements a robust algorithmic trading engine in Python, capable of both historical backtesting and live trading using the Alpaca API. It features a modular architecture with abstract history providers, corporate action adjustments, and a containerized live trading loop.
Here is where to find the key components of the project:
systrade/: The core Python package.alpaca.py: (New) Live Feed and Broker implementations usingalpaca-py.history.py: (New) AbstractHistoryProviderwith implementations for CSV files and QuestDB (including split/dividend adjustments).feed.py: (Refactored)HistoricalFeednow accepts generic providers and handles sparse data gaps.strategies.py: (New) Contains theMomentumStrategylogic that is used in live trading. This can be adapted to fit another trading strategy.engine.py&broker.py: Core event loop and backtesting logic.
run_live.py: The main entry point for the live trading application.Dockerfile: Configuration for building the application container.tests/: Unit tests for the new components (Alpaca Connection, Corporate Action Adjustments, Feeds).
This application is containerized in Docker for easy deployment through background processes. To install locally instead of through a container (container is recommended), run
pip install .in the root directory. Add -e flag for editable mode.
Run the following command in the project root directory:
docker build -t systrade .Run the following command to start the application:
docker run --name systrade-final --restart unless-stopped -e ALPACA_API_KEY=YOUR_ALPACA_API_KEY -e ALPACA_SECRET=YOUR_ALPACA_API_SECRET systrade Optional Parameters:
- Pass in
-dflag to run in daemon mode (don't need to keep terminal open) - Pass in
--intervalflag (set to 1m for minute and 1d for daily bars), default is 1m - Pass in
--symbolflag to set symbol you want to trade, default is SPY
To view trading logs and activity, run the following command:
docker logs -f systrade-finaland
docker logs systrade-final > trading_log.txtto save the logs to a file.
To stop the bot, run the following command:
docker stop systrade-final- Abstract History Providers: Able to accept CSV or QuestDB (new!) database as inputs for backtesting engine
- Corporate Actions:
adjustedparameter in feed setup in backtest, which automatically calculates adjusted prices - Refactored Feed: Now supports multiple providers and handles sparse data (forward-filling gaps).
- Alpaca Integration: Polls the API for 1-minute bars via IEX and handles order submission/fill reporting. Set
paper=Falseinrun_live.pyif you are not paper trading.
To run the unit tests:
docker run --rm systrade-final pytestif running containerized, otherwise
pytestif running locally.
Note: You will need a Tiingo API key to run the corporate action adjustment verification test. If so, add it to your environment under TIINGO_API_KEY.