Skip to content

SebAustin/multi-agent-trading

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI Trading System

A multi-agent stock analysis system built with PydanticAI and Streamlit. Five specialized AI agents collaborate to analyze a stock, form a strategy, assess risk, check regulatory compliance, and produce a final, type-safe trading decision — all from an interactive web dashboard.

Python Streamlit PydanticAI License

Disclaimer: This is an educational project that demonstrates multi-agent AI orchestration. It is not financial advice and should not be used to make real investment decisions.


Overview

The system breaks stock analysis into a pipeline of focused AI agents. Each agent has a narrow responsibility, its own tools, and a strict output schema enforced by Pydantic — so every result is validated, structured data rather than free-form text. Agents run individually from the dashboard and declare dependencies on one another, making the decision flow explicit and auditable.

The Agents

Agent Role Depends on
Market Analyst Analyzes price data and technical indicators (RSI, MACD, Bollinger Bands, Fibonacci levels)
Strategy Agent Develops a trading strategy from the market analysis Market Analyst
Risk Manager Assesses position risk and sizing Market Analyst
Regulatory Agent Checks compliance (e.g. Regulation M) Market Analyst, Strategy Agent
Supervisor Agent Weighs all inputs and issues the final TradingDecision All of the above

Each agent verifies that its prerequisites have run and returns a clear message if they haven't.

Key Features

  • Type-safe agent outputs — every response is a validated Pydantic model. The final decision is a TradingDecision object with an enum signal (BUY / SELL / HOLD), a bounded confidence score, a risk level, and a rationale.
  • Explicit dependency system — agents check that upstream agents have run and report progress in the UI.
  • Manual trade execution — analysis never writes to storage automatically. A separate "Execute Trade" action records the final decision, keeping you in control of what gets logged.
  • Pluggable storage — works out of the box with CSV files; optionally upgrades to PostgreSQL via a single environment variable.
  • Offline-friendly — runs on bundled demo data when live market data is unavailable; only the AI calls require an internet connection.
  • Audit trail — decisions and signals are persisted with timestamps, confidence, and rationale.

Architecture

multi-agent-trading/
├── app/
│   ├── main.py                  # Streamlit dashboard & agent orchestration
│   ├── config.py                # Settings loaded from environment variables
│   ├── agents/
│   │   ├── pydantic_agents.py    # The 5-agent PydanticAI system
│   │   └── smol_agents.py        # Alternative agent implementation
│   ├── models/
│   │   └── trading_models.py     # Pydantic schemas for all agent outputs
│   ├── tools/                    # Market-data & storage tools used by agents
│   ├── data/                     # Market data + demo data handlers
│   ├── storage/                  # CSV storage backend
│   └── db/                       # Optional PostgreSQL backend
├── data_storage/                 # Runtime CSV output (gitignored)
├── requirements.txt
├── run_app.sh                    # Quick-start launcher
├── .env.example                  # Template for your API keys
└── LICENSE

Tech Stack

Python · Streamlit · PydanticAI · Pydantic · OpenAI API · LangChain · yfinance · Plotly · PostgreSQL (optional)

Getting Started

1. Prerequisites

  • Python 3.11 or newer
  • An OpenAI API key (a Vocareum-issued voc- key also works and is routed automatically)

2. Install

git clone https://github.com/SebAustin/multi-agent-trading.git
cd multi-agent-trading

python3 -m venv venv
source venv/bin/activate          # Windows: venv\Scripts\activate

pip install -r requirements.txt

3. Configure your keys

Copy the template and add your key — the .env file is gitignored and never committed:

cp .env.example .env

Then edit .env and set at least OPENAI_API_KEY. All other variables are optional.

4. Run

./run_app.sh

Or directly:

streamlit run app/main.py --server.port 8501

Open http://localhost:8501 in your browser.

Usage

  1. Load stock data — enter a symbol (e.g. AAPL, MSFT) and click Load Stock Data.
  2. Run agents in order — click the agent buttons in the sidebar. Each one shows its analysis and tells you if a prerequisite is missing.
  3. Review the decision — the Supervisor Agent produces the final validated TradingDecision.
  4. Execute — click Execute Trade to persist the decision to storage.

Configuration Reference

All settings are read from environment variables (see .env.example):

Variable Required Purpose
OPENAI_API_KEY Yes OpenAI / Vocareum API key
ANTHROPIC_API_KEY No Use Anthropic Claude instead of OpenAI
TAVILY_API_KEY No News-based sentiment analysis
ALPHA_VANTAGE_API_KEY / QUANDL_API_KEY No Additional market-data sources
DATABASE_URL No PostgreSQL connection string; falls back to CSV storage if unset
DEFAULT_SYMBOL, CACHE_TIMEOUT_HOURS, DEFAULT_AI_MODEL, USE_OPENAI No App behavior tweaks

Troubleshooting

  • "Model initialization issue" — check that OPENAI_API_KEY is set in your .env and that the key is valid with available credits.
  • "Please load stock data first" — click Load Stock Data before running any agent, and confirm the symbol is valid.
  • Database connection errors — the app automatically falls back to CSV storage. To use PostgreSQL, ensure DATABASE_URL is correct and the database is reachable.

License

Released under the MIT License.

Acknowledgements

Built as a hands-on project exploring agentic AI for financial services — multi-agent orchestration, tool use, and structured/type-safe LLM outputs with PydanticAI.

About

Multi-agent stock analysis system with PydanticAI and Streamlit

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors