An AI-driven multi-agent trading platform for options trading and stock trends analysis. This project leverages advanced machine learning, real-time market data, and a modular multi-agent framework.
Alpha-Agent
Overview
Alpha-Agent is a sophisticated multi-agent trading system that leverages machine learning, alpha factor selection, and real-time market data to generate trading signals for stocks and options.Features
- Multi-Agent Architecture: Specialized agents for data, prediction, trading, risk management, and sentiment analysis
- Alpha Factor Selection: Implementation of 101 alphas and custom factors with dynamic evaluation
- Machine Learning Models: LSTM, XGBoost, and Prophet models for price prediction
- Options Strategies: Black-Scholes pricing and advanced options strategy generation
- Polygon.io Integration: Real-time and historical data from Polygon.io API
- Backtesting Framework: Comprehensive backtesting with detailed performance metrics
- Agent Communication: Robust communication protocol inspired by metaGPT
Installation
# Clone the repository
git clone https://github.com/yourusername/alpha-agent.git
cd alpha-agent
Install dependencies
pip install -r requirements.txt
Set environment variables for API keys
export POLYG
export OPENAIAPIKEY="youropenaiapi_key" # If using sentiment analysis
Configuration
Edit the configuration file at configs/config.yaml to customize:
- Default tickers to monitor
- Data update intervals
- Model hyperparameters
- Trading parameters
- Risk management settings
Usage
Basic Usage
# Start the Alpha-Agent system
from alphaagent.main import startsystem
Start the system with default configuration
start_system()
Or specify a custom configuration file
start_system(c)
Running Individual Agents
# Run the prediction agent in standalone mode
from agents.prediction_agent import PredictionAgent
from utils.communication.unified_communication import UnifiedCommunicationManager
Initialize communication manager
communicator = UnifiedCommunicationManager()
communicator.start()
Create and start prediction agent
predictionagent = PredictionAgent("predictionagent", communicator)
prediction_agent.start()
Running Backtests
# Run a backtest
from backtest.backtest_engine import BacktestEngine
from backtest.historicaldatafetcher import HistoricalDataFetcher
Fetch historical data
datafetcher = HistoricalDataFetcher(apikey="yourpolygonapi_key")
dataset = datafetcher.fetchcomplete_dataset(
tickers=["AAPL", "MSFT", "GOOGL"],
start_date="2022-01-01",
end_date="2022-12-31"
)
Run backtest
backtester = BacktestEngine(initial_capital=100000)
results = backtester.runbacktest(dataset["stocks"], yourstrategy_function)
Plot results
backtester.plotportfolioperformance()
backtester.plottradeanalysis()
Evaluating Alpha Factors
# Evaluate alpha factors
from models.signals.alpha_factors import AlphaFactors
from utils.factorevaluation.factoranalyzer import FactorAnalyzer
Calculate alpha factors
alpha_factors = AlphaFactors()
dfwithalphas = alphafactors.calculatealphafactors(historicaldata)
Evaluate factor performance
analyzer = FactorAnalyzer()
metrics = analyzer.calculatefactormetrics(
data=dfwithalphas,
factors=["alpha1", "alpha12", "alpha101"],
forwardreturnsperiods=[1, 5, 10]
)
Get best factors
bestfactors = analyzer.getbest_factors(metric="ic", n=5)
print(f"Best factors: {best_factors}")
Adding Custom Alpha Factors
from models.signals.alpha_factors import AlphaFactors
class ExtendedAlphaFactors(AlphaFactors): def custom_momentum(self, df): """Custom momentum alpha factor.""" return df['close'].pct_change(20) # 20-day momentum
Use your custom implementation
my_factors = ExtendedAlphaFactors()
dfwithalphas = myfactors.calculatealpha_factors(data)
System Architecture
+------------------+ +------------------+ +-------------------+
| | | | | |
| Data Agent |<-->| Prediction Agent |<-->| Trading Agent |
| | | | | |
+------------------+ +------------------+ +-------------------+
^ ^ ^
| | |
v v v
+------------------+ +------------------+ +-------------------+
| | | | | |
| Sentiment Agent |<-->| Communication |<-->| Risk Agent |
| | | Manager | | |
+------------------+ +------------------+ +-------------------+
Evaluation Metrics
The system provides comprehensive evaluation metrics for both individual agents and the overall system:
- Prediction Accuracy: RMSE, MAE, Directional Accuracy
- Trading Performance: Returns, Sharpe, Sortino, Maximum Drawdown
- Factor Performance: IC, Turnover, Half-life, Factor correlation
- System Performance: Agent communication efficiency, runtime metrics
Documentation
Detailed documentation is available in the docs/ directory:
- System architecture and design:
docs/README.md - API reference documentation:
docs/api_reference.md - Factor analysis guide:
docs/factor_analysis.md - Backtesting guide:
docs/backtesting.md
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.