TradingAgents/backend
Cursor Agent b34113f420 Add timing tracking, mock LLM, and comprehensive API testing framework
Co-authored-by: zjh08177 <zjh08177@gmail.com>
2025-07-07 07:36:43 +00:00
..
cli v0 iOS app 2025-07-01 23:10:44 -07:00
tradingagents Add timing tracking, mock LLM, and comprehensive API testing framework 2025-07-07 07:36:43 +00:00
FIXES_AND_IMPROVEMENTS_SUMMARY.md Add timing tracking, mock LLM, and comprehensive API testing framework 2025-07-07 07:36:43 +00:00
ISSUE_ANALYSIS_AND_FIX_PLAN.md Add timing tracking, mock LLM, and comprehensive API testing framework 2025-07-07 07:36:43 +00:00
README.md v0 iOS app 2025-07-01 23:10:44 -07:00
SERPAPI_SETUP.md checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
STREAMING_USAGE_EXAMPLES.md v0 iOS app 2025-07-01 23:10:44 -07:00
TEST_DOCUMENTATION.md checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
TEST_SUMMARY.md checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
api.py Add timing tracking, mock LLM, and comprehensive API testing framework 2025-07-07 07:36:43 +00:00
check_setup.py v0 iOS app 2025-07-01 23:10:44 -07:00
main.py v0 iOS app 2025-07-01 23:10:44 -07:00
pyproject.toml v0 iOS app 2025-07-01 23:10:44 -07:00
requirements.txt checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
restart_api.sh v0 iOS app 2025-07-01 23:10:44 -07:00
run_all_tests.py checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
run_api.py v0 iOS app 2025-07-01 23:10:44 -07:00
setup.py v0 iOS app 2025-07-01 23:10:44 -07:00
test_analysis_full_output.json Add timing tracking, mock LLM, and comprehensive API testing framework 2025-07-07 07:36:43 +00:00
test_analysis_output.json Add timing tracking, mock LLM, and comprehensive API testing framework 2025-07-07 07:36:43 +00:00
test_analysis_with_timing.py Add timing tracking, mock LLM, and comprehensive API testing framework 2025-07-07 07:36:43 +00:00
test_api.md v0 iOS app 2025-07-01 23:10:44 -07:00
test_api.py v0 iOS app 2025-07-01 23:10:44 -07:00
test_api_comprehensive.py checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
test_api_comprehensive_final.py Add timing tracking, mock LLM, and comprehensive API testing framework 2025-07-07 07:36:43 +00:00
test_comprehensive_fixes.py checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
test_main_comprehensive.py checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
test_main_simple.py checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
test_output.json Add timing tracking, mock LLM, and comprehensive API testing framework 2025-07-07 07:36:43 +00:00
test_parallel_execution.py checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
test_risk_management_flow.py checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
test_risk_manager_isolated.py checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
test_simple_risk_manager.py checkpoint before checking out cursor/fix-and-refactor-news-analyst-project-5e09 2025-07-06 11:04:02 -07:00
test_with_mock.py Add timing tracking, mock LLM, and comprehensive API testing framework 2025-07-07 07:36:43 +00:00
validate_fixes.py Add SwiftData history tracking and parallel agent execution fixes 2025-07-04 00:06:47 +00:00
verify_api.sh v0 iOS app 2025-07-01 23:10:44 -07:00

README.md

Trading Agents Backend

This directory contains the Python backend for the Trading Agents system.

Structure

  • api.py - FastAPI server that exposes trading analysis endpoints
  • run_api.py - Script to run the FastAPI server
  • main.py - Main entry point for the trading system
  • tradingagents/ - Core trading logic and agents
    • agents/ - Various AI agents (analysts, researchers, traders, risk managers)
    • dataflows/ - Data fetching and processing utilities
    • graph/ - Trading decision graph and workflow
  • cli/ - Command-line interface for the trading system
  • results/ - Output directory for analysis results
  • requirements.txt - Python dependencies
  • setup.py - Package setup configuration
  • pyproject.toml - Modern Python project configuration

Quick Start

  1. Install dependencies:

    pip install -r requirements.txt
    
  2. Run the FastAPI server:

    python run_api.py
    
  3. Or use the CLI:

    python -m cli.main
    

API Endpoints

  • GET / - Root endpoint (API status)
  • GET /health - Health check
  • POST /analyze - Analyze a stock ticker
    • Request: {"ticker": "AAPL"}
    • Response: Comprehensive analysis including market, sentiment, news, and trading decisions
  • GET /docs - Interactive API documentation (Swagger UI)

Testing the API

Quick Test

# Run the test script
python test_api.py

Manual Testing

  1. Using Browser: Navigate to http://localhost:8000/docs for interactive testing
  2. Using curl: See test_api.md for detailed curl commands
  3. Using Python: Use the provided test_api.py script

For detailed testing instructions, see test_api.md.

Environment Variables

Create a .env file in the project root with:

Required

  • OPENAI_API_KEY - Your OpenAI API key for AI agents
  • FINNHUB_API_KEY - For market data (free tier available)

Optional

  • REDDIT_CLIENT_ID - For Reddit sentiment analysis
  • REDDIT_CLIENT_SECRET - For Reddit sentiment analysis
  • TRADINGAGENTS_RESULTS_DIR - Custom results directory (default: backend/results)
  • TRADINGAGENTS_DATA_DIR - Custom data directory (default: backend/data)
  • TRADINGAGENTS_API_HOST - API host (default: localhost)
  • TRADINGAGENTS_API_PORT - API port (default: 8000)

Example .env file:

# API Keys
OPENAI_API_KEY=sk-your-key-here
FINNHUB_API_KEY=your-finnhub-key

# Optional Reddit API
REDDIT_CLIENT_ID=your-client-id
REDDIT_CLIENT_SECRET=your-client-secret

# Optional custom directories
# TRADINGAGENTS_RESULTS_DIR=/custom/path/to/results
# TRADINGAGENTS_API_PORT=3000

Features

Streaming Analysis (NEW!)

The TradingAgents CLI now supports real-time streaming of analysis reports as they're being generated by the AI agents. Instead of waiting for complete sections to be delivered, you can see the analysis content flowing in real-time.

Usage Options

  1. Dedicated Streaming Command:

    python -m cli.main stream
    python -m cli.main stream --advanced  # With advanced configuration
    
  2. Streaming Flag with Analyze Command:

    python -m cli.main analyze --streaming
    python -m cli.main analyze --streaming --advanced
    
  3. Default Command with Streaming:

    python -m cli.main --streaming
    python -m cli.main --streaming --advanced
    

Streaming vs. Regular Analysis

  • Regular Analysis: Shows progress updates and agent statuses, but reports are delivered only when complete sections are finished
  • Streaming Analysis: Shows the same progress updates PLUS real-time streaming of report content as agents generate it

What You'll See in Streaming Mode

  • 🔄 Agent Progress: Same as regular mode - shows which agents are working
  • 📡 Live Streaming Panel: Real-time content from the currently active agent
  • 🔴 Live Indicator: Shows which agent is currently generating content
  • Higher Refresh Rate: Updates 8 times per second for smooth streaming

Benefits

  • Real-time Insights: See analysis developing as it happens
  • Better Understanding: Watch the thought process of each agent
  • Immediate Feedback: Know immediately when agents start working on different aspects
  • Enhanced Experience: More engaging and informative than batch delivery

Technical Details

The streaming implementation:

  • Uses a StreamingMessageBuffer that extends the regular MessageBuffer
  • Detects agent transitions and content generation in real-time
  • Maintains the same final report quality while providing streaming experience
  • Automatically saves all content to report files as in regular mode