Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TradeGraph 📈

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.

🏗️ Architecture (LangGraph)

The system is built on a cyclic graph of three specialized agents:

  1. Analyst Agent (agents/analyst.py) 🧠
    • Role: The "Brain".
    • Logic: LLM-Powered. Uses ChatAnthropic to analyze price history. It calls tools (calculate_sma, calculate_rsi) to form a technical opinion (Buy/Sell/Hold) based on data.
  2. 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.
  3. 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.

🚀 How to Run

1. Requirements

pip install -U langgraph langchain-anthropic yfinance python-dotenv

2. Configuration

Create a .env file in the project root:

ANTHROPIC_API_KEY=sk-ant-api03-...

3. Start Trading

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 NVDA

Features:

  • 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.

📊 Backtesting

Test your trading strategy on historical data before running it live.

Quick Start

# 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

Features

  • 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)

Example Output

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%

Visualization (Optional)

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

🎓 Key Concepts

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.

📂 Project Structure

  • 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.

About

Simulate or backtest live day-trading using a team of AI agents. Powered by LangGraph.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages