A modular algorithmic trading system that supports multiple strategies, portfolio management, and real-time trading capabilities. The system is designed with service-oriented architecture in mind, allowing components to be extracted into independent services when needed.
- Multiple trading strategies:
- Moving Average Crossover
- Random Forest ML-based strategy
- Portfolio-based trading
- Advanced portfolio management with position tracking
- Signal aggregation for multi-strategy portfolios
- Support for multiple data providers (Polygon, Yahoo Finance)
- Feature engineering and caching system
- Comprehensive metrics and visualization
- Historical data backtesting
- Real-time trading capabilities
The system is designed with a service-oriented architecture approach, where each component can potentially be extracted into an independent service. This includes:
- Strategy Services: Individual trading strategies that can be deployed independently
- Portfolio Services: Portfolio management and aggregation services
- Data Services: Market data and feature engineering services
- Execution Services: Trade execution and risk management services
Each component is designed with:
- Clean interfaces for inter-service communication
- Minimal dependencies
- Independent deployment capability
- Service extraction readiness
src/
├── data/ # Data fetching and processing
│ ├── vendors/ # Data providers (Polygon, Yahoo Finance)
│ └── data_loader.py # Data loading and caching
├── execution/ # Trade execution and portfolio management
│ ├── portfolio_manager.py
│ ├── trade_executor.py
│ └── signal_aggregation/
├── features/ # Feature engineering
│ ├── feature_store.py
│ └── technical_indicators.py
├── strategies/ # Trading strategies
│ ├── SingleStock/ # Single stock strategies
│ └── portfolio/ # Portfolio strategies
├── visualization/ # Results visualization
└── run_trading_system.py # Main system driver
- Create a virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate- Install dependencies:
pip install -r requirements.txtPYTHONPATH=. python src/run_trading_system.py --symbol AAPL --strategy ma_crossover --start-date 2023-01-01 --end-date 2023-12-31PYTHONPATH=. python src/run_trading_system.py --portfolio default_portfolio --start-date 2023-01-01 --end-date 2023-12-31PYTHONPATH=. python src/scripts/backtest.py --strategy ma_crossover
# or
PYTHONPATH=. python src/scripts/backtest.py --strategy random_forestTo add a new strategy:
- Create a new strategy class that inherits from
BaseStrategy - Implement the required methods:
prepare_data(): Prepare data for the strategygenerate_signals(): Generate trading signalsupdate(): Update strategy state with new data
Example:
from src.strategies.base_strategy import BaseStrategy
class MyStrategy(BaseStrategy):
def __init__(self):
super().__init__(name="MyStrategy")
def prepare_data(self, data):
# Prepare data for your strategy
return processed_data
def generate_signals(self, data, symbol, timestamp):
# Generate trading signals
return signalThe system can be configured through:
- Command line arguments in
run_trading_system.py - Strategy-specific configuration files
- Portfolio configuration through
PortfolioTradingExecutionConfigFactory
Run tests using pytest:
pytest tests/For slow tests:
pytest tests/ -m "slow"- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
This application backtests a moving average crossover strategy on multiple stocks and ETFs. The strategy generates buy signals when the 10-day moving average crosses above the 50-day moving average, and sell signals when it crosses below.
- Tests the strategy on 10 different stocks/ETFs
- Analyzes performance over the last 1000 trading days
- Generates interactive visualizations of results
- Calculates and displays performance metrics
- Shows buy/sell signals on the price chart
- Clone this repository
- Install the required packages:
pip install -r requirements.txtSimply run the backtest script:
PYTHONPATH=. python src/scripts/backtest.py --strategy ma_crossover OR
PYTHONPATH=. python src/scripts/backtest.py --strategy random_forestThe script will:
- Download historical data for the selected symbols
- Calculate moving averages and generate trading signals
- Calculate strategy returns
- Display an interactive plot showing the results
- Print summary statistics for each symbol
The script tests the strategy on the following symbols:
- Tech Stocks: AAPL, MSFT, GOOGL, AMZN, META
- ETFs: SPY, QQQ, IWM, DIA, VTI
You can modify the following parameters in the code:
- Moving average windows (currently 10 and 50 days)
- List of symbols to test
- Date range for backtesting
- Visualization settings
To run the strategy and generate results, use the following command:
PYTHONPATH=. python3 src/run_trading_system.py --symbol AAPL --strategy ml --start-date 2025-01-01 --end-date 2025-04-10This script will execute the strategy and save the results in the results directory.
This project contains algorithmic trading models and strategies.
- Updated workflow to use Python 3.9, 3.10, and 3.11 for CI/CD.
- The driver file is now
run_trading_system.py.
- Weighted average signal aggregation
- Multi-strategy portfolio support
- Signal validation and normalization
- Portfolio-level signal generation
- Risk management and position sizing
- Smart order routing and execution
- Portfolio optimization and rebalancing
- Advanced order management
The system's Trade Execution Orchestrator is designed to work seamlessly across different usage scenarios:
-
Backtesting
- End-to-end strategy testing
- Historical performance analysis
- Strategy validation
-
Strategy Benchmarking
- Performance comparison
- Strategy evaluation
- Risk-adjusted returns analysis
-
Live Trading
- Real-time execution
- Market data processing
- Portfolio management
The orchestrator maintains a clean separation between core business logic and scenario-specific implementations, ensuring consistent behavior across all usage scenarios.