CoolFace
Apppublic

MaximeSzymanski/TradingAssistant

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
App README

πŸ“ˆ Trader Companion AI

Python Streamlit LangGraph Ollama-black?logo=ollama) Docker

Trader Companion AI is an autonomous financial agent that combines real-time stock analysis with private document retrieval (RAG).

Unlike simple chatbots, this agent uses a cyclic graph architecture to self-correct errors. If it can't find a ticker (e.g., "Boralex"), it searches the web, verifies candidates against official listings, checks for name collisions, and validates data availability before answering.


🧭 High-Level Flow (Agent Graph)

The diagram below illustrates how the agent routes intent, validates tickers, self-corrects failures, and produces analysis or RAG-based answers.

mermaid
graph TD
    Start([User Input]) --> Extractor[Extract Entities]
    Extractor --> Router{RAG or Web?}
    
    Router -- RAG Active --> RagSearch[Query Vector DB]
    RagSearch -->|Success| End([Response])
    RagSearch -->|Fallback| TickerCheck
    
    Router -- Web Mode --> TickerCheck[Validate Ticker]
    
    TickerCheck -->|Invalid| WebSearch[DuckDuckGo Search]
    WebSearch --> TickerCheck
    
    TickerCheck -->|Valid| DateCheck[Validate Dates]
    DateCheck --> Fetcher[Smart Data Fetcher]
    
    Fetcher -->|News Intent| Sentiment[Sentiment Analysis]
    Fetcher -->|Data Intent| Analyst[Tech Analysis]
    
    Analyst --> Forecast[Linear Regression Forecast]
    Forecast --> Viz[Generate Plotly Chart]
    
    Sentiment --> End
    Viz --> End

    classDef default fill:#f9f9f9,stroke:#333,stroke-width:1px
    classDef decision fill:#ffefdb,stroke:#f6b26b,stroke-width:2px
    classDef process fill:#e1f5fe,stroke:#0277bd,stroke-width:2px
    classDef endNode fill:#e8f5e9,stroke:#2e7d32,stroke-width:2px
    
    class Router decision
    class Extractor,RagSearch,TickerCheck,WebSearch,DateCheck,Fetcher,Sentiment,Analyst,Forecast,Viz process
    class Start,End endNode

🧠 System Architecture

The agent is powered by LangGraph, enabling non-linear workflows and state persistence. The system operates through a specialized node architecture:

  • β€”Router Logic: Intelligently decides between pulling real-time market data, searching the web for news, or querying the internal Knowledge Base (PDFs) based on user intent.
  • β€”Self-Correction Loop: If a stock ticker is invalid or ambiguous, the agent enters a fallback loopβ€”searching the web, verifying exchange suffixes (e.g., converting .TSX to .TO), and validating against official company names before proceeding.
  • β€”Hybrid Search: Combines DuckDuckGo for general queries and a local Vector Store (Ollama embeddings) for private document analysis.

✨ Key Capabilities

1. πŸ›‘οΈ Robust Ticker Resolution

  • β€”Self-Correction: Automatically maps informal names (e.g., "Ubisoft") to accurate tickers (UBI.PA) using a multi-step web search and validation loop.
  • β€”Collision Detection: Uses fuzzy matching to distinguish between similar tickers (e.g., BLX for Boralex vs. Banco Latinoamericano).
  • β€”Suffix Handling: Automatically converts exchange suffixes (e.g., .TSX β†’ .TO) for API compatibility.

2. 🧠 Smart Routing

  • β€”Detects intent to route between Fundamental Analysis, Technical Charts, News Sentiment, or Internal RAG queries.
  • β€”Keyword Guards: Bypasses LLM latency for direct data requests (e.g., "Show me the price of Apple").

3. πŸ“Š Interactive Visualization

  • β€”Generates dynamic Plotly charts with zoom/pan.
  • β€”Overlays SMA (Simple Moving Average) and RSI (Relative Strength Index).
  • β€”Projects a 7-day trend forecast using Linear Regression.

4. πŸ“š Local RAG (Retrieval-Augmented Generation)

  • β€”Ingests PDF reports into an in-memory Vector Store.
  • β€”Uses Ollama (nomic-embed-text) for fully local, private document analysis.

πŸ› οΈ Installation & Setup

Option 1: Docker (Recommended)

bash
# Build the image
docker build -t trader-ai .

# Run the container (Exposes port 7860)
docker run -p 7860:7860 trader-ai

Option 2: Local Development

Requires Python 3.11+ and Ollama running locally.

  1. 1.Clone and Install
bash
   git clone https://github.com/yourusername/trader-companion.git
   cd trader-companion
   pip install -r requirements.txt
  1. 1.Start Ollama
bash
   ollama pull qwen2.5:7b
   ollama pull nomic-embed-text
   ollama serve
  1. 1.Run Streamlit
bash
   streamlit run app.py

πŸ§ͺ Testing

bash
# Run all tests
pytest tests/test_agent.py

# Run specific ticker validation tests
pytest -k "validate_ticker"

⚠️ Disclaimer

This project is for educational purposes only. The financial forecasts and analysis provided by the AI are based on simple statistical models and should not be used as financial advice.