Tensor-Trade-v1

Stars
6
Language
Python
Updated

Multi-Agent Trading Psychology API

AI-Powered Trading Analysis System using Multi-LLM Debate Council & Behavioral Psychology

Version Python FastAPI License


๐Ÿ“– Table of Contents


๐ŸŽฏ Problem Statement

The Trading Psychology Challenge

85% of retail traders lose money - not due to lack of market knowledge, but due to psychological biases and emotional decision-making:

Core Problems:
  1. Revenge Trading - Traders double down after losses, trying to "get even"
  2. FOMO (Fear of Missing Out) - Chasing trades without proper analysis
  3. Overtrading - Excessive trading driven by boredom or addiction
  4. Confirmation Bias - Only seeking information that confirms existing beliefs
  5. Loss Aversion - Holding losing positions too long, cutting winners too early
  6. Lack of Multi-Perspective Analysis - Traders typically analyze markets from one angle (technical OR fundamental)
Information Overload Problem:
  • Traders are bombarded with contradictory opinions from social media, news, analysts
  • No single source provides balanced, multi-perspective analysis
  • Economic events (earnings, Fed meetings, etc.) are scattered across platforms
  • Behavioral patterns go unnoticed until significant damage is done
Existing Solutions are Inadequate:
  • Basic trading journals track trades but don't analyze psychology
  • Traditional analytics tools focus on P&L, not on why traders make bad decisions
  • Financial advisors are expensive and not available 24/7
  • AI chatbots provide generic advice without real-time market context

๐Ÿ’ก Solution Overview

Multi-Agent Trading Psychology API is an AI-powered analysis system that:

What We Do:

โœ… Analyzes trader behavior using 10 psychological pattern detectors
โœ… Provides multi-perspective market analysis via 5 specialized LLM agents debating in real-time
โœ… Validates asset symbols to prevent analysis on invalid tickers
โœ… Aggregates economic calendar data (earnings, Fed meetings, economic indicators)
โœ… Generates personalized narratives tailored to trader psychology & market readiness
โœ… Auto-selects communication personas (Coach, Professional, Casual, Analytical)

How It Works:

  1. Trader submits asset symbol (e.g., AAPL, BTC-USD)
  2. System validates & fetches market data (price, volume, economic calendar)
  3. 5 AI agents debate the market move from different perspectives
  4. Behavioral analyzer scans trade history for psychological patterns
  5. Narrator synthesizes insights into actionable, personalized feedback
  6. Moderator ensures content safety and appropriate tone

Key Innovation:

Instead of a single AI model (which has biases), we use 5 specialized agents with different expertise areas (macro, fundamental, technical, flow, risk) that debate each other to provide balanced analysis - mimicking how professional trading desks operate.


๐Ÿ—๏ธ System Architecture

High-Level Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         CLIENT REQUEST                          โ”‚
โ”‚                   POST /analyze-asset?asset=AAPL                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         FASTAPI SERVER                          โ”‚
โ”‚                           (main.py)                             โ”‚
โ”‚  โ€ข CORS enabled                                                 โ”‚
โ”‚  โ€ข Request validation                                           โ”‚
โ”‚  โ€ข Async orchestration                                          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                โ”‚                โ”‚                โ”‚
                โ–ผ                โ–ผ                โ–ผ
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚ Asset Validator  โ”‚  โ”‚ Market Data  โ”‚  โ”‚ Trade History   โ”‚
    โ”‚ (yfinance API)   โ”‚  โ”‚ Service      โ”‚  โ”‚ Service         โ”‚
    โ”‚ โ€ข Symbol check   โ”‚  โ”‚ โ€ข Price data โ”‚  โ”‚ โ€ข P&L calc      โ”‚
    โ”‚ โ€ข 80+ symbols    โ”‚  โ”‚ โ€ข Volume     โ”‚  โ”‚ โ€ข Win rate      โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    ECONOMIC CALENDAR SERVICE                    โ”‚
โ”‚  โ€ข Earnings dates (via yfinance)                               โ”‚
โ”‚  โ€ข News headlines                                               โ”‚
โ”‚  โ€ข Economic indicators                                          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    5-AGENT LLM DEBATE COUNCIL                   โ”‚
โ”‚                    (llm_council/debate_engine.py)               โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚ ๐Ÿฆ… Macro Hawk โ”‚  โ”‚ ๐Ÿ”ฌ Micro      โ”‚  โ”‚ ๐Ÿ’ง Flow       โ”‚     โ”‚
โ”‚  โ”‚ (OpenRouter)  โ”‚  โ”‚   Forensic    โ”‚  โ”‚   Detective   โ”‚     โ”‚
โ”‚  โ”‚ Mistral-7B    โ”‚  โ”‚ (OpenRouter)  โ”‚  โ”‚ (OpenRouter)  โ”‚     โ”‚
โ”‚  โ”‚               โ”‚  โ”‚ Mythomax-13B  โ”‚  โ”‚ Mistral-7B    โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”‚                                                                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                         โ”‚
โ”‚  โ”‚ ๐Ÿ“Š Tech       โ”‚  โ”‚ ๐Ÿค” Skeptic    โ”‚                         โ”‚
โ”‚  โ”‚   Interpreter โ”‚  โ”‚ (Mistral.ai)  โ”‚                         โ”‚
โ”‚  โ”‚ (OpenRouter)  โ”‚  โ”‚ Large-Latest  โ”‚                         โ”‚
โ”‚  โ”‚ Mythomax-13B  โ”‚  โ”‚               โ”‚                         โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                         โ”‚
โ”‚                                                                 โ”‚
โ”‚  โ€ข Parallel execution (5 agents run simultaneously)            โ”‚
โ”‚  โ€ข Structured debate with confidence levels                    โ”‚
โ”‚  โ€ข Consensus/disagreement detection                            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    BEHAVIORAL ANALYSIS AGENT                    โ”‚
โ”‚                    (agents/behaviour_agent.py)                  โ”‚
โ”‚  โ€ข Detects 10 psychological patterns                           โ”‚
โ”‚  โ€ข Severity classification (High/Medium/Positive)              โ”‚
โ”‚  โ€ข Risk scoring (0-100)                                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    PERSONA SELECTION AGENT                      โ”‚
โ”‚                    (agents/persona.py)                          โ”‚
โ”‚  โ€ข Auto-selects style: Coach | Professional | Casual            โ”‚
โ”‚  โ€ข Based on: win rate, P&L, pattern severity                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         NARRATOR AGENT                          โ”‚
โ”‚                      (agents/narrator.py)                       โ”‚
โ”‚  โ€ข Groq LLM (Mixtral-8x7B)                                     โ”‚
โ”‚  โ€ข Synthesizes all insights                                     โ”‚
โ”‚  โ€ข Generates personalized narrative                             โ”‚
โ”‚  โ€ข Market readiness assessment                                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        MODERATOR AGENT                          โ”‚
โ”‚                     (agents/moderator.py)                       โ”‚
โ”‚  โ€ข Content safety checks                                        โ”‚
โ”‚  โ€ข Tone appropriateness                                         โ”‚
โ”‚  โ€ข Final validation                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                   SHARIAH COMPLIANCE AGENT                      โ”‚
โ”‚                 (agents/shariah_compliance_agent.py)            โ”‚
โ”‚  โ€ข Islamic finance screening                                     โ”‚
โ”‚  โ€ข Haram industry detection                                      โ”‚
โ”‚  โ€ข Riba & Gharar assessment                                     โ”‚
โ”‚  โ€ข Compliance scoring (0-100)                                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        CALLING AGENT                            โ”‚
โ”‚                     (agents/calling_agent.py)                   โ”‚
โ”‚  โ€ข Trade execution calls                                        โ”‚
โ”‚  โ€ข Market prediction calls                                      โ”‚
โ”‚  โ€ข API integration for brokers                                  โ”‚
โ”‚  โ€ข Actionable trade signals                                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         JSON RESPONSE                           โ”‚
โ”‚  {                                                              โ”‚
โ”‚    "trade_history": {...},                                     โ”‚
โ”‚    "economic_calendar": {...},                                 โ”‚
โ”‚    "market_analysis": {5-agent debate},                        โ”‚
โ”‚    "behavioral_analysis": {patterns, risk score},              โ”‚
โ”‚    "narrative": {personalized summary},                        โ”‚
โ”‚    "shariah_compliance": {compliant, score, reason},           โ”‚
โ”‚    "calling_result": {action, details},                        โ”‚
โ”‚    "recommendations": [...]                                    โ”‚
โ”‚  }                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Component Breakdown

1. API Layer (main.py)
  • FastAPI async web framework
  • CORS middleware for frontend integration
  • Request validation via Pydantic
  • Error handling & logging
2. Validation Layer (services/asset_validator.py)
  • Validates asset symbols using yfinance API
  • Supports 80+ symbols (stocks, ETFs, crypto, forex)
  • Rejects invalid tickers instantly
  • Case normalization
3. Market Data Layer (services/economic_calendar.py, services/market_metrics.py)
  • Real-time price data
  • Volume analysis
  • Economic calendar (earnings, Fed meetings, CPI, jobs)
  • News aggregation
4. LLM Council (llm_council/)
  • 5 specialized agents with different expertise
  • Parallel execution for speed (all agents run simultaneously)
  • Structured debate with thesis, supporting points, risks, confidence
  • Model diversity: OpenRouter (4 agents) + Mistral.ai (1 agent)
5. Behavioral Engine (agents/behaviour_agent.py)
  • Pattern matching on trade history
  • Rule-based detection (no ML required)
  • 10 patterns: Revenge trading, FOMO, Overtrading, Loss aversion, etc.
  • Risk scoring algorithm: Weighted by severity
6. Persona System (agents/persona.py)
  • Auto-selection based on trader performance
  • 4 personas: Coach (struggling), Professional (winning), Casual (new), Analytical
  • Tone adaptation: Supportive vs. congratulatory vs. data-driven
7. Narrative Generator (agents/narrator.py)
  • Groq LLM (Mixtral-8x7B) for fast inference
  • Synthesizes all agent outputs into coherent summary
  • Market readiness recommendation (Stop/Caution/Continue/Proceed)
8. Content Moderation (agents/moderator.py)
  • Safety checks on LLM outputs
  • Prevents inappropriate content
  • Ensures professional tone

๐Ÿค– AI Models & Agents

LLM Provider Strategy

We use a multi-provider approach for reliability, cost-efficiency, and model diversity:

AgentProviderModelCostPurpose
๐Ÿฆ… Macro HawkOpenRouterMistral-7B-InstructFreeMacroeconomic analysis
๐Ÿ”ฌ Micro ForensicOpenRouterMythomax-L2-13BFreeFundamental analysis
๐Ÿ’ง Flow DetectiveOpenRouterMistral-7B-InstructFreeMarket microstructure
๐Ÿ“Š Tech InterpreterOpenRouterMythomax-L2-13BFreeTechnical analysis
๐Ÿค” SkepticMistral.aiMistral-Large-LatestPaidRisk assessment
๐Ÿ“– NarratorGroqMixtral-8x7B-InstructFreeSynthesis

Total Cost per Request: ~$0.02 (only Skeptic agent is paid)

The 5-Agent Debate Council

๐Ÿฆ… Macro Hawk - Macroeconomic Strategist

Expertise: Fed policy, interest rates, inflation, sector rotation, currency markets

Analysis Focus:

  • Interest rate expectations (Fed funds futures)
  • Treasury yield movements (10Y, 2Y)
  • Economic indicators (CPI, jobs, PMI, GDP)
  • Dollar strength/weakness impact
  • Sector rotation trends
  • Global macro events

Example Output:

PRIMARY DRIVER: Fed rate cut expectations surged to 65% (up from 45%)
after softer CPI print (2.4% vs. 2.6% expected).

SUPPORTING CATALYSTS:
1. Treasury 10Y yield fell 12bps to 3.45%, benefiting growth stocks
2. Dollar weakened 1.2% vs. basket, bullish for multinational exporters
3. PMI manufacturing beat (52.3 vs. 51.5), indicating economic resilience

MACRO RISKS: Fed could still hold rates if employment remains strong
CONVICTION: High (8/10)
๐Ÿ”ฌ Micro Forensic - Fundamental Analyst

Expertise: Financial statements, earnings, SEC filings, valuation, competitive analysis

Analysis Focus:

  • Revenue growth & quality
  • Earnings per share (EPS) trends
  • Cash flow analysis (FCF, OCF)
  • Balance sheet health (debt, liquidity)
  • Management guidance
  • Competitive positioning
  • Sector-specific metrics

Example Output:

EARNINGS QUALITY ANALYSIS:
- Revenue: $45.2B (+8.2% YoY) vs. consensus $44.8B โ†’ BEAT
- EPS: $4.20 (+15% YoY) vs. $4.10 expected โ†’ BEAT
- Operating Cash Flow: $16.8B (up 22% YoY) โ†’ High quality
- Free Cash Flow: $11.3B (FCF margin = 25%, best in 5 years)
- Guidance: FY25 EPS $16.50-$17.00 (consensus: $16.80) โ†’ RAISED

VALUATION: P/E 28x vs. sector avg 24x โ†’ Premium justified by growth
CONVICTION: Moderate (6/10) - priced in, but solid fundamentals
๐Ÿ’ง Flow Detective - Market Microstructure Expert

Expertise: Order flow, options positioning, institutional flows, dark pools

Analysis Focus:

  • Dark pool activity
  • Options market (Call/Put ratios, IV, open interest)
  • Institutional vs. retail flows
  • Block trades & smart money indicators
  • VWAP positioning
  • Volume profile analysis

Example Output:

SMART MONEY SIGNALS:
- Dark pool buy volume: 2.3M shares (45% of total) vs. avg 1.8M
- Options: Call volume +125%, Put/Call ratio 0.65 (bullish)
- Block trades: 47 blocks today vs. avg 28 โ†’ Institutional accumulation
- VWAP: Price trading above VWAP all day โ†’ Sustained buying

INTERPRETATION: This move is backed by REAL institutional money,
not retail noise. Smart money is accumulating.

CONVICTION: High (9/10) - Strong flow confirmation
๐Ÿ“Š Tech Interpreter - Technical Analyst

Expertise: Chart patterns, support/resistance, indicators, price action

Analysis Focus:

  • Support/resistance levels
  • Moving averages (20, 50, 200-day)
  • Chart patterns (breakouts, reversals)
  • RSI, MACD, volume indicators
  • Fibonacci levels
  • Trend analysis

Example Output:

TECHNICAL SETUP:
- Price broke above $175 resistance (held since August)
- Now trading above 50-day MA ($172) and 200-day MA ($165)
- RSI: 68 (strong momentum, not yet overbought)
- MACD: Bullish crossover 3 days ago
- Volume: 1.5x average โ†’ Conviction behind move

PRICE TARGETS:
- Next resistance: $185 (gap fill from July)
- Support if pullback: $172 (50-day MA)

CONVICTION: Moderate (7/10) - Breakout confirmed but watch overbought
๐Ÿค” Skeptic - Risk Manager

Expertise: Risk assessment, contrarian analysis, bearish scenarios

Analysis Focus:

  • What could go wrong?
  • Overvaluation risks
  • Sentiment extremes (contrarian signals)
  • Historical analogs
  • Black swan scenarios
  • Risk/reward ratio

Example Output:

DEVIL'S ADVOCATE VIEW:
1. VALUATION CONCERN: P/E 28x vs. 5-year avg of 22x
2. SENTIMENT WARNING: Bullish sentiment at 75% (near contrarian sell signal)
3. MACRO RISK: Fed could pause rate cuts if inflation re-accelerates
4. TECHNICAL: RSI approaching 70 (overbought territory)

WHAT COULD TRIGGER REVERSAL:
- Disappointing guidance on earnings call
- Competitor takes market share
- Macro shock (geopolitical event)

RISK/REWARD: 2:1 (potential upside $190, downside $165)
CONVICTION: Low bearish (4/10) - Concerns valid but trend is strong

How the Debate Works

  1. Parallel Execution: All 5 agents receive the same market data simultaneously
  2. Independent Analysis: Each agent analyzes from their unique perspective (no groupthink)
  3. Structured Output: Each agent provides:
    • Thesis (main argument)
    • Supporting Points (3-4 evidence-backed catalysts)
    • Risks (what could invalidate the thesis)
    • Confidence Level (High/Moderate/Low)
  4. Consensus Detection: System identifies areas of agreement/disagreement
  5. Synthesis: Narrator combines insights into actionable summary

๐Ÿš€ Quick Start

Prerequisites

# Python 3.10+
python --version

# pip installed
pip --version

Installation

# Clone repository
git clone <repo-url>
cd deriv

# Create virtual environment (recommended)
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\Activate.ps1

# Install dependencies
pip install -r requirements.txt

Configuration

Create .env file in project root:

# OpenRouter (Free tier - 4 agents)
OPENROUTER_API_KEY=your_key_here

# Mistral.ai (Paid - 1 agent)
MISTRAL_API_KEY=your_key_here

# Groq (Free tier - narrator)
GROQ_API_KEY=your_key_here

# Optional
DATABASE_URL=postgresql://user:pass@localhost/db
REDIS_URL=redis://localhost:6379

Start Server

# Development (auto-reload)
uvicorn main:app --reload --host 0.0.0.0 --port 8000

# Production
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4

Test API

# Health check
curl http://localhost:8000/health

# Analyze asset
curl -X POST "http://localhost:8000/analyze-asset?asset=AAPL&user_id=trader123"

Python Example

import requests

response = requests.post(
    "http://localhost:8000/analyze-asset",
    params={"asset": "TSLA", "user_id": "trader123"}
)

data = response.json()
print(f"P&L: ${data['trade_history']['total_pnl']:.2f}")
print(f"Win Rate: {data['trade_history']['win_rate']}%")
print(f"Risk Score: {data['behavioral_analysis']['risk_score']}/100")

๐Ÿ“‹ Features

โœ… Asset Symbol Validation

  • Validates genuine assets - Rejects invalid/random symbols
  • Comprehensive coverage - 80+ tested symbols (stocks, ETFs, crypto, international)
  • Instant feedback - Clear error messages for users
  • Smart caching - Fast validation for repeated symbols
  • Case normalization - Accepts lowercase input
  • Multiple asset types - Stocks, ETFs, crypto (BTC-USD), forex, international
  • Free & open-source - Uses yfinance API (no registration needed)
  • See ASSET_VALIDATION.md and ASSET_COVERAGE.md

๐Ÿค– 5-Agent LLM Council

  • Diverse perspectives from macro, fundamental, flow, technical, and skeptic experts
  • Parallel execution for 100-120 second response time
  • Structured debate format with confidence levels
  • Consensus & disagreement detection
  • Evidence-backed analysis with citations

๐Ÿง  Behavioral Pattern Detection

Detects 10 trading psychology patterns:

  • High Severity: Revenge trading, Ego trading, Loss aversion, Averaging down
  • Medium Severity: Overtrading, FOMO, Impulsive decisions, Quick profit taking, Hesitation
  • Positive: Calculated risk (good win rate + controlled losses)

๐Ÿ“ˆ Economic Calendar Integration

  • Real-time earnings dates
  • Economic indicators (Fed meetings, CPI, jobs reports)
  • Market news headlines
  • Sector-specific events

๐ŸŽญ Intelligent Persona Selection

Auto-selects communication style based on trader performance:

  • Coach - Supportive (struggling traders, win rate <40%)
  • Professional - Peer-level (winning traders, win rate >60%)
  • Casual - Friendly (new traders, <5 trades)
  • Analytical - Data-focused (default)

๐Ÿ“Š Market Readiness Assessment

  • Risk Score (0-100) calculated from behavioral patterns
  • 4 Readiness Levels:
    • STOP TRADING (Risk 60+)
    • TRADE WITH CAUTION (Risk 40-59)
    • CONTINUE TRADING (Risk <40 + Calculated Risk)
    • PROCEED CAREFULLY (Risk <40)

๐Ÿ•Œ Shariah Compliance Agent

  • Islamic Finance Screening - Validates assets against Shariah principles
  • Haram Industry Detection - Blocks alcohol, tobacco, gambling, weapons
  • Riba & Gharar Assessment - Checks for interest and excessive uncertainty
  • Compliance Scoring - 0-100 score with detailed reasoning
  • LLM-Powered Evaluation - Uses AI for nuanced interpretations
  • Customizable Thresholds - Adjustable for different madhabs/schools

๐Ÿ“ž Calling Agent

  • Trade Execution Calls - Simulates or executes buy/sell orders via APIs
  • Market Prediction Calls - Generates BUY/SELL/HOLD signals
  • Broker API Integration - Connects to trading platforms (Alpaca, etc.)
  • Actionable Signals - Real-time trade recommendations
  • Safety Gates - Only executes Shariah-compliant trades

๐Ÿ› ๏ธ Technical Stack

Core Framework

  • FastAPI - Modern async web framework
  • Pydantic v2.8+ - Data validation
  • Uvicorn - ASGI server
  • Python 3.10+ - Runtime

LLM Providers

  • OpenRouter - 4 agents (Mistral-7B, Mythomax-L2-13B)
  • Mistral.ai - 1 agent (Mistral-Large-Latest)
  • Groq - Narrator (Mixtral-8x7B-Instruct)

Data Sources

  • yfinance - Market data, earnings, economic calendar
  • aiohttp - Async HTTP requests
  • BeautifulSoup4 + lxml - Web scraping (if needed)

Infrastructure (Optional)

  • PostgreSQL - Trade history persistence
  • Redis - Caching layer
  • Docker - Containerization

๐Ÿ“ก API Documentation

Endpoint Overview

EndpointMethodPurposeResponse Time
/analyze-assetPOSTFull market + behavioral analysis100-120s
/run-agentsPOSTLegacy endpoint with manual inputs100-120s
/healthGETService health check<1s
/GETAPI information<1s

POST /analyze-asset (Recommended)

Simplified analysis endpoint with auto-generation of market context.

Request:
POST /analyze-asset?asset=AAPL&user_id=trader123
Query Parameters:
  • asset (required): Stock symbol (e.g., AAPL, MSFT, BTC-USD)
  • user_id (optional): Trader identifier for personalization
Response Structure:
{
  "trade_history": {
    "total_trades": 15,
    "total_pnl": -245.50,
    "win_rate": 40.0,
    "wins": 6,
    "losses": 9,
    "largest_win": 120.00,
    "largest_loss": -85.00,
    "avg_win": 65.50,
    "avg_loss": -45.30
  },
  "economic_calendar": {
    "symbol": "AAPL",
    "earnings_calendar": {
      "next_earnings_date": "2026-04-25",
      "last_reported": "2026-01-28"
    },
    "recent_news": [
      "Apple announces new AI features...",
      "iPhone sales beat expectations..."
    ],
    "economic_events": [
      "Fed Meeting - March 19, 2026",
      "CPI Report - March 12, 2026"
    ]
  },
  "market_analysis": {
    "council_opinions": [
      "๐Ÿฆ… Macro Hawk (High): Fed rate cut expectations surge...",
      "๐Ÿ”ฌ Micro Forensic (Moderate): Earnings beat expectations...",
      "๐Ÿ’ง Flow Detective (High): Strong institutional buying...",
      "๐Ÿ“Š Tech Interpreter (Moderate): Breakout above $175...",
      "๐Ÿค” Skeptic (Low): Valuation concerns remain..."
    ],
    "consensus_points": [
      "Strong earnings performance",
      "Favorable macro environment"
    ],
    "disagreements": [
      "Valuation (Forensic: Fair, Skeptic: Overvalued)"
    ]
  },
  "behavioral_analysis": {
    "flags": [
      {
        "pattern": "Overtrading",
        "severity": "Medium",
        "message": "High trade count (15) for session",
        "details": "Consider reducing frequency"
      },
      {
        "pattern": "Loss Aversion",
        "severity": "High",
        "message": "Holding losing positions too long",
        "details": "Average loss duration: 3.2 days vs. wins: 1.1 days"
      }
    ],
    "risk_score": 55,
    "patterns_detected": 2,
    "positive_patterns": 0
  },
  "narrative": {
    "styled_message": "Hey there - Let's talk about your AAPL session...",
    "persona": "Coach",
    "market_readiness": "TRADE WITH CAUTION",
    "key_recommendations": [
      "Set stricter stop-losses to prevent loss aversion",
      "Reduce trade frequency to avoid overtrading",
      "Wait for clearer setups given current risk profile"
    ]
  },
  "timestamp": "2026-02-07T14:30:00Z",
  "processing_time": 112.5
}

POST /run-agents (Legacy)

Full control over inputs for advanced users.

Request:
{
  "market_event": "AAPL moved up 2.5% on strong earnings",
  "user_trades": [
    {
      "timestamp": "2026-02-07 09:00:00",
      "symbol": "AAPL",
      "action": "BUY",
      "price": 175.50,
      "quantity": 10,
      "pnl": -50.00,
      "status": "CLOSED"
    }
  ],
  "persona_style": "professional"
}

GET /health

Health check endpoint.

Response:
{
  "status": "healthy",
  "version": "2.0.0",
  "services": {
    "llm_council": "operational",
    "economic_calendar": "operational",
    "asset_validator": "operational"
  },
  "timestamp": "2026-02-07T14:30:00Z"
}

Error Responses

Invalid Asset Symbol:
{
  "error": "Invalid asset symbol",
  "message": "Symbol 'INVALIDXYZ' is not a valid asset. Validation failed: No data found.",
  "suggestions": ["Check spelling", "Try common symbols like AAPL, MSFT, TSLA"],
  "status_code": 400
}
Rate Limit Exceeded:
{
  "error": "Rate limit exceeded",
  "message": "Maximum 10 requests per minute",
  "retry_after": 45,
  "status_code": 429
}

๐Ÿ’ผ Use Cases

1. Post-Session Analysis

Scenario: Trader finishes a day of trading and wants feedback

import requests

# After trading session
response = requests.post(
    "http://localhost:8000/analyze-asset",
    params={"asset": "SPY", "user_id": "trader123"}
)

data = response.json()

# Display insights
print(f"๐Ÿ“Š P&L: ${data['trade_history']['total_pnl']:.2f}")
print(f"๐ŸŽฏ Win Rate: {data['trade_history']['win_rate']}%")
print(f"โš ๏ธ Risk Score: {data['behavioral_analysis']['risk_score']}/100")
print(f"\n{data['narrative']['styled_message']}")

Output:

๐Ÿ“Š P&L: -$245.50
๐ŸŽฏ Win Rate: 40.0%
โš ๏ธ Risk Score: 55/100

Hey there - Let's talk about your SPY session. I noticed you hit some
rough patches with a 40% win rate and -$245 P&L. The 5 LLM agents debated
the market move, and here's what they found...

[Full personalized narrative with recommendations]

2. Pre-Trade Market Research

Scenario: Trader researching an asset before entering position

# Get multi-perspective analysis
response = requests.post(
    "http://localhost:8000/analyze-asset",
    params={"asset": "NVDA"}
)

data = response.json()

# Review agent perspectives
for opinion in data['market_analysis']['council_opinions']:
    print(opinion)

# Check economic events
print("\nUpcoming Events:")
for event in data['economic_calendar']['economic_events']:
    print(f"  - {event}")

3. Trading Journal Integration

Scenario: Automated journaling with psychological insights

# Your trading journal system
class TradingJournal:
    def add_session_analysis(self, asset, analysis):
        # Store in database
        self.db.insert({
            "date": datetime.now(),
            "asset": asset,
            "pnl": analysis['trade_history']['total_pnl'],
            "risk_score": analysis['behavioral_analysis']['risk_score'],
            "patterns": [f['pattern'] for f in analysis['behavioral_analysis']['flags']],
            "market_readiness": analysis['narrative']['market_readiness'],
            "recommendations": analysis['narrative']['key_recommendations']
        })

# Get analysis
response = requests.post(f"{API_URL}/analyze-asset", params={"asset": "TSLA"})
data = response.json()

# Save to journal
journal = TradingJournal()
journal.add_session_analysis("TSLA", data)

4. Risk Management Dashboard

Scenario: Monitor trader risk levels across portfolio

import asyncio
import aiohttp

async def analyze_portfolio(assets):
    """Analyze multiple assets concurrently"""
    async with aiohttp.ClientSession() as session:
        tasks = []
        for asset in assets:
            url = f"http://localhost:8000/analyze-asset?asset={asset}"
            tasks.append(session.post(url))
        
        responses = await asyncio.gather(*tasks)
        return [await r.json() for r in responses]

# Analyze portfolio
portfolio = ["AAPL", "MSFT", "GOOGL", "TSLA"]
results = asyncio.run(analyze_portfolio(portfolio))

# Aggregate risk
total_risk = sum(r['behavioral_analysis']['risk_score'] for r in results) / len(results)
print(f"Portfolio Risk Score: {total_risk:.1f}/100")

# Flag high-risk assets
for result in results:
    if result['behavioral_analysis']['risk_score'] > 60:
        print(f"โš ๏ธ HIGH RISK: {result['economic_calendar']['symbol']}")

5. Educational Chatbot Integration

Scenario: Trading education platform with AI feedback

# User asks: "Should I trade AAPL today?"

# Get analysis
response = requests.post(f"{API_URL}/analyze-asset", params={"asset": "AAPL"})
data = response.json()

# Extract key insights for chatbot
chatbot_response = f"""
Based on 5 AI analyst perspectives:

**Market Analysis:**
{data['market_analysis']['council_opinions'][0]}  # Macro view
{data['market_analysis']['council_opinions'][4]}  # Risk view

**Your Readiness:** {data['narrative']['market_readiness']}

**Recommendation:** {data['narrative']['key_recommendations'][0]}
"""

return chatbot_response

6. Algorithmic Trading Integration

Scenario: Use behavioral insights to pause/resume algo trading

class AlgoTradingBot:
    def should_trade_today(self, asset):
        # Check behavioral risk
        response = requests.post(
            f"{API_URL}/analyze-asset",
            params={"asset": asset, "user_id": "algo_bot"}
        )
        data = response.json()
        
        risk_score = data['behavioral_analysis']['risk_score']
        readiness = data['narrative']['market_readiness']
        
        # Pause trading if risk too high
        if risk_score > 60 or readiness == "STOP TRADING":
            self.pause_trading()
            self.send_alert(f"Trading paused due to risk: {risk_score}/100")
            return False
        
        return True

# Before executing trades
bot = AlgoTradingBot()
if bot.should_trade_today("SPY"):
    bot.execute_strategy()
else:
    print("Trading paused - risk too high")

๐Ÿ“‚ Project Structure

deriv/
โ”œโ”€โ”€ main.py                         # FastAPI server & orchestration
โ”œโ”€โ”€ requirements.txt                # Python dependencies
โ”œโ”€โ”€ .env                            # Environment variables (not committed)
โ”œโ”€โ”€ README.md                       # This file
โ”‚
โ”œโ”€โ”€ agents/                         # Behavioral & communication agents
โ”‚   โ”œโ”€โ”€ behaviour_agent.py          # 10 pattern detector
โ”‚   โ”œโ”€โ”€ narrator.py                 # Groq LLM synthesis
โ”‚   โ”œโ”€โ”€ persona.py                  # Auto-persona selection
โ”‚   โ”œโ”€โ”€ moderator.py                # Content safety
โ”‚   โ””โ”€โ”€ market_watcher.py           # Market analysis coordinator
โ”‚
โ”œโ”€โ”€ llm_council/                    # 5-agent debate system
โ”‚   โ”œโ”€โ”€ core/
โ”‚   โ”‚   โ””โ”€โ”€ config.py               # LLM provider settings
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ””โ”€โ”€ schemas.py              # Pydantic data models
โ”‚   โ””โ”€โ”€ services/
โ”‚       โ”œโ”€โ”€ llm_client.py           # Multi-provider abstraction
โ”‚       โ”œโ”€โ”€ agent_prompts.py        # System prompts for 5 agents
โ”‚       โ””โ”€โ”€ debate_engine.py        # Parallel debate execution
โ”‚
โ”œโ”€โ”€ services/                       # Supporting services
โ”‚   โ”œโ”€โ”€ asset_validator.py          # yfinance symbol validation
โ”‚   โ”œโ”€โ”€ economic_calendar.py        # Earnings & economic events
โ”‚   โ”œโ”€โ”€ market_metrics.py           # Price & volume data
โ”‚   โ””โ”€โ”€ trade_history.py            # P&L calculations
โ”‚
โ”œโ”€โ”€ tests/                          # Test suite
โ”‚   โ”œโ”€โ”€ test_api.py
โ”‚   โ”œโ”€โ”€ test_asset_validator.py
โ”‚   โ”œโ”€โ”€ test_market_metrics.py
โ”‚   โ””โ”€โ”€ test_integrated_workflow.py
โ”‚
โ””โ”€โ”€ data/                           # Sample data (for testing)
    โ”œโ”€โ”€ demo_prices/
    โ””โ”€โ”€ demo_trades/

๐Ÿงช Testing

Run Full Test Suite

# Test all components
python test_integrated_workflow.py

# Test asset validation
python test_asset_validator.py

# Test API endpoints
python test_api.py

# Test market metrics
python test_market_metrics.py

Test Specific Features

# Test asset validation
python demo_comprehensive_validation.py

# Test behavioral analysis
python demo_validation.py

Manual API Testing

# Basic request
curl -X POST "http://localhost:8000/analyze-asset?asset=AAPL"

# With user ID
curl -X POST "http://localhost:8000/analyze-asset?asset=TSLA&user_id=trader123"

# Health check
curl http://localhost:8000/health

๐Ÿ”’ Security Considerations

Current Implementation (Development)

  • โš ๏ธ No authentication required
  • โš ๏ธ No rate limiting
  • โœ… CORS enabled for all origins
  • โœ… Content moderation via ModeratorAgent
  • โœ… Input validation via Pydantic

Production Recommendations

1. Add Authentication
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials

security = HTTPBearer()

@app.post("/analyze-asset")
async def analyze_asset(
    credentials: HTTPAuthorizationCredentials = Depends(security)
):
    # Validate JWT token
    token = credentials.credentials
    user = verify_jwt_token(token)
    # ...
2. Implement Rate Limiting
from slowapi import Limiter
from slowapi.util import get_remote_address

limiter = Limiter(key_func=get_remote_address)

@app.post("/analyze-asset")
@limiter.limit("10/minute")
async def analyze_asset():
    # ...
3. Restrict CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://yourdomain.com"],  # Specific origins only
    allow_credentials=True,
    allow_methods=["POST", "GET"],
    allow_headers=["*"],
)
4. Add API Key Authentication
@app.post("/analyze-asset")
async def analyze_asset(api_key: str = Header(...)):
    if api_key != os.getenv("API_KEY"):
        raise HTTPException(status_code=401, detail="Invalid API key")
    # ...

๐Ÿ“Š Performance Optimization

Current Performance

  • Average Response Time: 100-120 seconds (due to LLM calls)
  • Bottleneck: LLM inference (5 agents in parallel)
  • Throughput: ~4-8 requests/minute per worker

Optimization Strategies

1. Increase Workers
# 4 workers for better throughput
uvicorn main:app --workers 4 --host 0.0.0.0 --port 8000
2. Add Redis Caching
import redis

cache = redis.Redis(host='localhost', port=6379, db=0)

# Cache economic calendar (1 hour TTL)
@lru_cache(maxsize=100)
def get_economic_data(symbol):
    cached = cache.get(f"economic:{symbol}")
    if cached:
        return json.loads(cached)
    
    data = fetch_economic_data(symbol)
    cache.setex(f"economic:{symbol}", 3600, json.dumps(data))
    return data
3. Use Faster Models
# Replace with faster models for non-critical agents
"๐Ÿฆ… Macro Hawk": LLMClient(
    provider_type="groq",
    model="mixtral-8x7b-32768"  # Groq is faster
)
4. Stream Responses
@app.post("/analyze-asset-stream")
async def analyze_asset_stream(asset: str):
    async def generate():
        yield json.dumps({"status": "validating"})
        # ... validation
        
        yield json.dumps({"status": "fetching market data"})
        # ... market data
        
        yield json.dumps({"status": "running LLM council"})
        # ... debate
        
        yield json.dumps({"status": "complete", "data": results})
    
    return StreamingResponse(generate(), media_type="application/json")

๐Ÿ›ฃ๏ธ Roadmap

Phase 1: Core Enhancements (Q1 2026) โœ…

  • Asset symbol validation
  • 5-agent LLM debate council
  • Behavioral pattern detection
  • Economic calendar integration
  • Auto-persona selection

Phase 2: Data Persistence (Q2 2026)

  • PostgreSQL integration
  • Redis caching layer
  • Historical trade analysis
  • User profile persistence
  • Trade pattern tracking over time

Phase 3: Advanced Features (Q3 2026)

  • WebSocket streaming responses
  • Real-time trade monitoring
  • Multi-timeframe analysis (1D, 1W, 1M)
  • Portfolio-level analysis
  • Custom pattern definitions

Phase 4: Production Ready (Q4 2026)

  • JWT authentication
  • Rate limiting
  • API key management
  • Usage analytics
  • Monitoring & alerting (Datadog, Sentry)

Phase 5: ML Enhancements (2027)

  • Predictive behavioral modeling
  • Custom LLM fine-tuning
  • Sentiment analysis from social media
  • Market regime detection
  • Automated strategy recommendations

๐Ÿ“ License

MIT License - See LICENSE file for details


๏ฟฝ Product Requirements Document (PRD)

1. Executive Summary

TensorTrade is an AI-powered multi-agent trading psychology platform that combines behavioral analysis, multi-perspective market insights, Shariah compliance, and automated trade execution. Designed for retail traders, it addresses psychological biases, information overload, and ethical trading needs, with a focus on Islamic finance for UAE markets.

Target Audience: Retail traders, Islamic finance practitioners, fintech enthusiasts, hackathon participants.

Key Value Proposition: "Ethical AI trading with psychological insights and Shariah compliance for smarter, halal investing."

2. Objectives

  • Primary: Provide comprehensive trading analysis that mitigates psychological risks and ensures ethical compliance.
  • Secondary: Win UAE hackathons by demonstrating innovative AI agents, real-world applicability, and cultural relevance.
  • Success Metrics: 90%+ compliance accuracy, <2min response time, positive user feedback on ethical features.

3. Features & Requirements

Core Features
  • Multi-Agent LLM Council: 5 AI agents debate market analysis for balanced insights.
  • Behavioral Analysis: Detects 10 psychological patterns with risk scoring.
  • Shariah Compliance: Screens assets for Islamic finance principles (no riba, no haram industries).
  • Calling Agent: Executes trades or generates actionable signals post-compliance check.
  • Economic Calendar: Real-time market events and news integration.
  • Persona Selection: Adaptive communication styles based on trader profile.
Functional Requirements
  • FR1: System must validate asset symbols and reject invalid ones.
  • FR2: LLM Council must provide consensus analysis within 120 seconds.
  • FR3: Behavioral agent must classify patterns with severity levels.
  • FR4: Shariah agent must score compliance 0-100 with reasoning.
  • FR5: Calling agent must execute only compliant trades.
  • FR6: API must handle 100+ concurrent requests.
Non-Functional Requirements
  • NFR1: Response time <120s for full analysis.
  • NFR2: 99% uptime with error handling.
  • NFR3: GDPR/CCPA compliant data handling.
  • NFR4: Scalable to 10,000 users.
  • NFR5: Accessible via web UI and API.

4. User Stories

  • As a trader, I want behavioral analysis so I can avoid revenge trading.
  • As an Islamic investor, I want Shariah screening to ensure halal portfolios.
  • As a developer, I want API integration for automated trading bots.
  • As a hackathon judge, I want innovative AI demos that solve real problems.

5. Technical Architecture

  • Frontend: HTML/CSS/JS for demo UI.
  • Backend: FastAPI with async agents.
  • AI Layer: OpenRouter, Mistral, Groq LLMs.
  • Data Layer: yfinance for market data, PostgreSQL for persistence.
  • Agents: Modular classes in /agents/ directory.

6. Design Considerations

  • UI/UX: Simple web interface for asset input and results display.
  • Security: API key management, input sanitization.
  • Ethics: Bias mitigation in LLMs, transparent compliance scoring.
  • Scalability: Docker containerization, cloud deployment.

7. Implementation Plan

  • Phase 1 (MVP): Core agents + API (2 weeks).
  • Phase 2: Shariah + Calling agents (1 week).
  • Phase 3: UI polish + testing (1 week).
  • Phase 4: Hackathon prep + demos (1 week).

8. Risks & Mitigations

  • Risk: LLM API failures โ†’ Mitigation: Fallback to cached responses.
  • Risk: Shariah interpretation debates โ†’ Mitigation: Configurable thresholds.
  • Risk: High latency โ†’ Mitigation: Parallel agent execution.

9. Success Criteria

  • Technical: All agents functional, API stable.
  • Business: Positive feedback from UAE judges on innovation and ethics.
  • User: Improved trading decisions via psychological insights.

๏ฟฝ๐Ÿ‘ฅ Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“ง Support & Contact

  • Issues: Create an issue in the repository
  • Discussions: Use GitHub Discussions for questions
  • Documentation: See API_REQUEST_FORMAT.md for complete API docs

๐Ÿ™ Acknowledgments

  • OpenRouter - Multi-model LLM access
  • Mistral.ai - High-quality language models
  • Groq - Fast LLM inference
  • yfinance - Market data & economic calendar
  • FastAPI - Modern Python web framework

Version: 2.0.0
Last Updated: February 7, 2026
Server: http://localhost:8000
Docs: http://localhost:8000/docs


Built with โค๏ธ for better trading psychology