A professional, reproducible environment for researching, backtesting, and executing algorithmic trading strategies.
This repository bridges the gap between Data Science (Vectorized Backtesting) and Live Execution (Event-Driven Trading). It is designed for developers and researchers who want to move beyond "spaghetti code" scripts into a robust trading infrastructure.
Check out the corresponding blog post:
- Research (Phase 1): VectorBT — High-performance, vectorized backtesting using NumPy/Pandas broadcasting. Used for hypothesis testing and parameter optimization.
- Execution (Phase 2): Lumibot — Event-driven framework for realistic simulation and live trading. Handles order management, slippage, and broker connection.
- Broker: Alpaca — Commission-free API for stock trading (Paper & Live).
- Environment: VSCode Dev Containers — A fully Dockerized Python environment ensuring code runs exactly the same on every machine.
.
├── .devcontainer/ # Docker configuration for VSCode
├── notebooks/ # Phase 1: Research & VectorBT experiments
│ └── example.ipynb # Demo: From "Buy & Hold" to "Golden Cross" optimization
├── strategies/ # Phase 2: Production-ready Strategy Classes
│ └── mag_seven.py # Example: "Magnificent Seven" Rebalancing Strategy
├── run_backtest.py # Script to simulate strategies with Lumibot
├── run_live.py # Script to deploy strategies to Alpaca (Paper/Live)
├── .env.example # Template for API keys
└── requirements.txt # Python dependencies
- Docker Desktop (Running)
- VSCode
- VSCode Extension: Dev Containers (ms-vscode-remote.remote-containers)
- Clone the repository:
git clone https://github.com/cbrincoveanu/algo-trading-template.git cd algo-trading-template - Open in VSCode: Open the folder in VSCode. You should see a popup: "Folder contains a Dev Container configuration file. Reopen to folder to develop in a container."
- Build the Environment:
Click "Reopen in Container".
- Note: This may take a few minutes the first time as it pulls the Docker image and installs Python libraries.
- Get your Paper Trading API Keys from the Alpaca Dashboard.
- Rename
.env.exampleto.env:mv .env.example .env
- Paste your keys into
.env:ALPACA_API_KEY=PKxxxxxxxxxxxxxxxxxx ALPACA_API_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ALPACA_IS_PAPER=True
Use Jupyter Notebooks to rapidly test hypotheses.
- Open
notebooks/example.ipynb. - Select the
base (conda)kernel (or/opt/conda/bin/python). - Run the cells to see how VectorBT can test thousands of parameter combinations in seconds.
Once you have a robust idea, implement it as a class in strategies/.
- See
strategies/mag_seven.pyfor an example of a multi-asset buy-and-hold strategy.
Verify your strategy handles realistic constraints (fees, slippage, sequential data).
python run_backtest.py- Output: A browser window will open with a tear sheet (CAGR, Sharpe, Drawdown).
Deploy the bot to trade in real-time.
python run_live.py- Output: The bot will connect to Alpaca and wait for market open.
This software is for educational and research purposes only.
- Algorithmic trading involves significant risk.
- Past performance is not indicative of future results.
- Do not trade with money you cannot afford to lose.
- The authors and contributors are not financial advisors.