AI-Powered Algorithmic Trading Simulator
TradeGraph is a multi-agent system that simulates an algorithmic trading desk. It demonstrates how autonomous agents can cooperate to make financial decisions using Graph Theory and State Machines.
Note: This is a pure simulation using random walk math. No real money or API keys involved.
The system is built on a cyclic graph of three specialized agents:
- Analyst Agent (
agents/analyst.py) 🧠- Role: The "Brain".
- Logic: LLM-Powered. Uses
ChatAnthropicto analyze price history. It calls tools (calculate_sma,calculate_rsi) to form a technical opinion (Buy/Sell/Hold) based on data.
- Risk Manager Agent (
agents/risk.py) 🛡️- Role: The "Brakes".
- Logic: LLM-Powered. A separate persona that strictly enforces capital preservation. It reviews the Analyst's signal against the portfolio using a "Safety First" system prompt.
- Execution Agent (
agents/executor.py) ⚡- Role: The "Hands".
- Logic: Deterministic state updater. Executes the approved order and logs the transaction to the ledger.
Note: Requires an Anthropic API Key for the LLM Agents.
pip install -U langgraph langchain-anthropic yfinance python-dotenvCreate a .env file in the project root:
ANTHROPIC_API_KEY=sk-ant-api03-...Run the simulation. By default, it uses Real World Data (via yfinance) if installed, otherwise falls back to a Random Walk.
# Default (SPY)
python3 main.py
# Trade a specific stock
python3 main.py NVDAFeatures:
- Real Data: Fetches live 1-minute candles from Yahoo Finance.
- Persistence: Saves your portfolio to
data/portfolio.json. You can stop (Ctrl+C) and restart later without losing your virtual money. - LLM Agents: The Analyst and Risk Manager use Claude to reason about the charts.
Test your trading strategy on historical data before running it live.
# Backtest SPY for the last year (default)
python backtest.py SPY
# Custom date range
python backtest.py NVDA --start 2023-01-01 --end 2024-01-01
# Different time intervals
python backtest.py AAPL --interval 1h --period 1mo
# Save detailed results
python backtest.py TSLA --output results/backtest.json- Historical Data: Automatically fetches data from Yahoo Finance
- Performance Metrics: Sharpe ratio, max drawdown, win rate, returns
- Buy-and-Hold Comparison: Compare strategy vs. passive holding
- Trade Log: Detailed record of every trade made by the agents
- Visualization: Generate charts (requires
matplotlib)
Final Equity: $11,234.56
Total Return: +12.35%
Annualized Return: +12.89%
Sharpe Ratio: 1.234
Max Drawdown: -8.45%
Total Trades: 24
Win Rate: 58.3%
Install matplotlib to generate charts:
pip install matplotlib
# In Python
from core.visualize import create_backtest_report
create_backtest_report(results, output_dir="charts")This creates:
- Equity curve over time
- Price chart with buy/sell markers
- Drawdown chart
- Returns distribution histogram
This project demonstrates advanced Computer Science concepts suitable for a Resume:
- Multi-Agent Systems: Coordinating specialized autonomous actors.
- State Machines: Managing complex global state (
TraderState) through defined transitions. - Event-Driven Architecture: Reacting to market tick events in real-time.
- Financial Engineering: Implementing mock market stochastic processes (Geometric Brownian Motion).
- Backtesting: Validating trading strategies on historical data.
core/: Schema definitions, Market Simulator, Backtesting Engine, and Metrics.agents/: Logic for the three bot personas.graph/: The LangGraph workflow definition.main.py: The live simulation entrypoint.backtest.py: The backtesting CLI entrypoint.