laxuu's picture
Update README.md
dd6c3d7 verified
|
Raw
History Blame Contribute Delete
8.55 kB
---
title: LIVE Recommendation Trading AgentandStorestrategy
emoji: πŸ“š
colorFrom: yellow
colorTo: pink
sdk: gradio
sdk_version: 6.18.0
python_version: '3.13'
app_file: app.py
pinned: false
short_description: 'Fully analytical assistant deploying localized LLM '
demo : https://youtu.be/v7obaRRzNnU
post : https://twitter.com/LaxmiTiwari_/status/2066588475567251682
tags:
- track:backyard
- achievement:offgrid
- achievement:welltuned
- achievement:offbrand
- achievement:llama
- achievement:sharing
- achievement:fieldnotes
---
# πŸ€– AI Trading Agent
An end-to-end quantitative trading research platform that combines Local Large Language Models (LLMs), Retrieval-Augmented Generation (RAG), and automated backtesting to streamline strategy development and market analysis.
Most Useful for Individual Trader, it is safe, secure and Local app.
---
## πŸ“‹ Overview
The **AI Trading Agent** enables traders, researchers, and quantitative analysts to seamlessly transition from natural language ideas to backtested trading strategies. By anchoring a local LLM with a specialized trading knowledge base, the system ensures highly relevant code generation and data-driven market insights.
### Key Capabilities
* **Natural Language to Code:** Generate executable trading strategies from plain text.
* **Knowledge Management:** Store, index, and retrieve trading strategies and research.
* **Live Market Analysis:** Run real-time technical analysis on popular assets.
* **Automated Backtesting:** Instantly validate generated strategies using historical data.
* **Persistent Strategy Memory:** Build an incremental, searchable database of quantitative knowledge.
---
## ✨ Features
### πŸ€– AI-Powered Strategy Generation
Generate production-ready **Backtrader** strategies from natural language descriptions.
* **Workflow Example:** *β€œGenerate an RSI mean reversion strategy”*
* **Under the Hood:** The system queries FAISS memory βž” Retrieves the closest matching strategy template βž” Passes context to the Gemma LLM βž” Generates clean Backtrader Python code βž” Executes an automated backtest βž” Returns code, performance metrics, and logs.
### 🧠 Retrieval-Augmented Strategy Memory
Strategies are vectorized and stored inside a high-performance **FAISS** database.
* **Capabilities:** Semantic search, code reuse, knowledge persistence, and sub-millisecond retrieval.
### πŸ“ˆ Live Market Recommendation Engine
Provides real-time technical analysis and actionable trading recommendations based on:
* Trend & Momentum Analysis
* Support & Resistance Detection
* Volatility & Volume Profiles
| Asset Category | Supported Tickers |
| :--- | :--- |
| **Equities** | AAPL, TSLA, MSFT, GOOG, AMZN, NVDA |
| **Crypto** | BTC-USD, ETH-USD |
> **Example Output:**
> * **Recommendation:** BUY (Confidence: 87%)
> * **Reasoning:** Strong macro uptrend, positive short-term momentum, and healthy volume expansion on breakouts.
### πŸ“₯ Strategy Knowledge Ingestion
Populate your vector database by importing quantitative data from multiple sources:
* Web URLs & Manual Text Input
* PDF, Markdown, and Text files
* **Processing Pipeline:** Extracts raw trading logic βž” Detects indicators βž” Generates structural embeddings βž” Stores vectors in FAISS.
### πŸ” Indicator Validation Layer
To maintain data integrity, an automated guardrail scans ingested documents. Files **must** contain references to at least one supported indicator, or they are rejected:
* `RSI` | `SMA` | `EMA` | `MACD` | `Bollinger Bands` | `Stochastic` | `ADX` | `Volume`
---
## πŸ—οΈ System Architecture
```text
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ User Query/Input β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ FAISS Retrieval β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Gemma LLM β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β–Ό β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Code Generation β”‚ β”‚ Market Analysis β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Backtrader β”‚
β”‚ Sandbox Engine β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Results & Logs β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Technology Stack
AI & Retrieval Layer
LLM: Gemma GGUF (gemma-2-2b-it-Q4_K_M.gguf) via llama.cpp
Embeddings: Sentence Transformers (all-MiniLM-L6-v2)
Vector Database: FAISS (using L2 Distance similarity search)
Quantitative Layer
Market Data: Yahoo Finance API (yfinance)
Backtesting Engine: Backtrader
Data Analysis: Pandas & NumPy
UI & Deployment
Interface: Gradio
Deployment Target: Hugging Face Spaces / Local host
πŸ“‚ Project Structure
AI-Trading-Agent/
β”‚
β”œβ”€β”€ app.py # Main Gradio application entrypoint
β”œβ”€β”€ requirements.txt # Python dependencies
β”œβ”€β”€ README.md # Project documentation
β”‚
β”œβ”€β”€ data/ # Local vector storage and ingested documents (still not)
β”œβ”€β”€ logs/ # Backtest execution logs and system errors
└── model/ # Downloaded LLM weights (.gguf)
Getting Started
1. Model Setup
The system utilizes a quantized Gemma-2B model. Download and initialize it using the code snippet below:
from huggingface_hub import hf_hub_download
from llama_cpp import Llama
# Download model from Hugging Face
MODEL_PATH = hf_hub_download(
repo_id="bartowski/gemma-2-2b-it-GGUF",
filename="gemma-2-2b-it-Q4_K_M.gguf"
)
# Initialize local inference engine
llm = Llama(
model_path=MODEL_PATH,
n_ctx=8192,
n_threads=4,
n_gpu_layers=-1 # Set to -1 to offload all layers to GPU
)
2. Installation
Clone the repository and install the required environment dependencies:
# Clone the repository
git clone [https://github.com/yourusername/AI-Trading-Agent.git](https://github.com/yourusername/AI-Trading-Agent.git)
cd AI-Trading-Agent
# Install dependencies
pip install -r requirements.txt
# Run the UI locally
python app.py
Application Modules
πŸ’» Code Engine Studio: Natural language interface to generate, tweak, and instantly backtest Python trading code.
πŸ’¬ Live Recommendation: Real-time dashboard parsing market feeds for high-probability setups.
πŸ“₯ Strategy Encoding & Retrieval: Drag-and-drop workspace to ingest custom research documents into vector memory.
πŸ—ƒοΈ Strategy Database: Administrative view to browse, inspect, and delete keys from your stored vector collections.
πŸ“Š Future Roadmap
[ ] Portfolio Optimization: Markowitz efficient frontier and Black-Litterman integration.
[ ] Pine Script Generation: Support for TradingView script synthesis.
[ ] Reinforcement Learning: Gymnasium-based market agents.
[ ] Multi-Agent Systems: CrewAI/Autogen setups for distinct Analyst vs. Risk Manager roles.
[ ] Production DB: Upgrade FAISS to PostgreSQL (pgvector) for persistent multi-user scaling.
[ ] Live Execution: Execution layer integration via Interactive Brokers or Alpaca APIs.
⚠️ Disclaimer
This project is intended strictly for educational and research purposes. Trading financial instruments involves significant risk
of capital loss. The authors do not guarantee the accuracy of generated code or market recommendations. Always perform thorough
independent verification and paper trading before risking real capital.
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference