A Python-based algorithmic trading framework designed for both backtesting and live/paper trading. This project uses free financial APIs, modular code, and supports both stock and crypto trading.
- Multi-API support: Alpha Vantage, Alpaca, Yahoo Finance
- Backtesting: Backtrader integration for strategy testing
- Live Trading: Support for paper and live trading with Alpaca
- Forex Support: Data fetching for major currency pairs via Alpha Vantage
- Technical Indicators: RSI, MACD, Bollinger Bands, Moving Averages, and more
- Modular Design: Clean, maintainable code structure
| Layer | Tool | Purpose |
|---|---|---|
| Language | Python | Core logic, data, and execution |
| Data APIs | yfinance, alpha_vantage, finnhub, ccxt |
Market data (stocks, forex, crypto) |
| Analysis | pandas, numpy, matplotlib, seaborn |
Data cleaning, analysis, plotting |
| Indicators | TA-Lib, pandas_ta |
Technical indicators |
| Backtesting | backtrader |
Strategy simulation |
| Live Trading | Alpaca (stocks) / ccxt (crypto) |
Real or paper trade execution |
| Visualization | matplotlib, plotly |
Charts and dashboards |
| Environment | .env, python-dotenv |
Store API keys and config safely |
algo_trader/
│
├── data/
│ ├── raw/ # raw downloaded data
│ └── processed/ # cleaned data
│
├── strategies/
│ └── moving_average.py # example strategy file
│
├── backtest/
│ └── backtest_engine.py # backtest runner
│
├── live_trading/
│ └── trader.py # live/paper trading logic
│
├── utils/
│ ├── data_loader.py # fetch market data
│ ├── indicators.py # technical indicator functions
│ └── config.py # load .env and global settings
│
├── main.py # entry point
├── requirements.txt # dependencies
├── .env # environment variables (ignored by Git)
└── README.md
git clone <repository-url>
cd algo_trader# Create the environment from the file
conda env create -f environment.yml
# Activate the environment
conda activate algo_trader- Get an API key from Alpha Vantage
- Get API keys from Alpaca (for live/paper trading)
- Edit
.envwith your keys and desired mode:
# Trading Mode
MODE=backtest # or 'live' or 'paper'
# Alpaca API Keys (get from https://alpaca.markets/)
ALPACA_API_KEY=your_alpaca_api_key
ALPACA_API_SECRET=your_alpaca_api_secret
ALPACA_BASE_URL=https://paper-api.alpaca.markets # Use for paper trading
# Alpha Vantage API Key
ALPHA_VANTAGE_API_KEY=your_alpha_vantage_api_keySet MODE=backtest in your .env file and run:
python main.pyThis will run the default moving average strategy on AAPL stock data.
Set MODE=paper in your .env file and run:
python main.pyThis will run the trading bot in paper trading mode using Alpaca's paper trading API.
Set MODE=live in your .env file and run:
python main.pyThe framework comes with two built-in strategies:
- Moving Average Crossover: Buys when short MA crosses above long MA, sells when short MA crosses below long MA
- RSI Strategy: Buys when RSI is below 30 (oversold), sells when RSI is above 70 (overbought)
You can create additional strategies by extending the bt.Strategy class in the strategies/ directory.
The utils/indicators.py file contains many built-in technical indicators. You can add your own by following the same pattern:
def calculate_your_indicator(data: pd.Series, parameter: int = 14) -> pd.Series:
# Your indicator calculation here
return resultTo run a different strategy in backtesting, modify the main.py file:
run_backtest(
symbol="AAPL",
strategy_class=YourCustomStrategy,
start="2022-01-01",
end="2023-01-01",
initial_cash=10000
)- Store all API keys in the
.envfile, which is git-ignored - Never commit your
.envfile to version control - Use paper trading mode to test strategies before going live
- Regularly monitor and audit your trading activity
- Start with paper trading to test your strategies
- Use stop-losses to limit potential losses
- Backtest extensively with historical data before live trading
- Monitor your bot regularly when running live
- Keep your API keys secure
- Add more sophisticated strategies
- Implement risk management features
- Create a dashboard for real-time monitoring
- Add support for cryptocurrency trading with CCXT
- Implement advanced technical indicators
This project is licensed under the MIT License - see the LICENSE file for details.
This is for educational purposes only. Trading financial instruments is risky, and past performance is not indicative of future results. The authors are not responsible for any losses incurred from using this software.