Skip to content

prakhxr0/quant

Repository files navigation

OpenRouter Quant Terminal - Advanced Edition

A modular, AI-powered autonomous trading agent with historical backtesting, quick-run presets, and multi-stock comparison. Uses OpenRouter's LLM to make intelligent BUY/SELL/HOLD decisions based on technical analysis and learned trading lessons.

Key Features

Intelligent Analysis: 14-period RSI, MACD, Bollinger Bands, ATR, volume analysis
Quick-Run Presets: Ultra-fast (5 days) to deep (60 days) with skip-days mode
🧠 Learning Memory: Relevance-scored past trade outcomes
💼 Portfolio Awareness: LLM sees position, cash, and P&L
🔄 Robust Error Handling: Automatic retries, rate limit aware
📊 Rich UI: Tables, sparklines, live progress
🚀 Multi-Stock: Compare strategies across tickers

Installation

pip install -r requirements.txt

Create .env:

OPENROUTER_KEY=sk-or-v1-xxx...

Usage

Interactive Mode (Recommended)

python main.py

Choose from quick-run presets (5-60 days) or custom settings.

CLI - Single Stock

python autonomous_trader.py --ticker NVDA --days 30 --skip-days 1

CLI - Multi-Stock Comparison

python autonomous_trader.py --tickers AAPL,MSFT,NVDA --days 30 --skip-days 2

Output:

Ticker     Return       Alpha        Sharpe    Max DD      Trades  Final Value
AAPL       +5.23%      +2.15%       0.4521    -8.32%      12      $10523.45
MSFT       +3.87%      +0.79%       0.3210    -6.15%       8      $10387.00
NVDA       +8.45%      +5.37%       0.6234    -5.98%      15      $10845.23

Command-Line Arguments

Argument Default Description
--ticker AAPL Single stock ticker
--tickers None Comma-separated list
--days 30 Historical days
--skip-days 1 Trade every Nth day (higher = faster)
--api-key .env OpenRouter API key

Speed Tip: Use --skip-days 2 for 50% speedup.

Execution Times

With --skip-days 1 (daily):

  • 5 days: ~1-2 min
  • 10 days: ~2-3 min
  • 30 days: ~5-10 min
  • 60 days: ~10-15 min

With --skip-days 2 (every 2 days):

  • 30 days: ~2-5 min
  • 60 days: ~5-8 min

Project Structure

src/
├── analysis.py      → 14-period RSI/MACD, Bollinger Bands, ATR
├── backtest.py      → Core engine with skip-day mode
├── llm.py           → OpenRouter API + retries + portfolio context
├── memory.py        → Relevance-based memory retrieval
├── portfolio.py     → Trade execution & equity tracking
└── __init__.py

main.py              → Interactive UI with presets
autonomous_trader.py → CLI with multi-stock comparison

Architecture

┌──────────────────────────────────┐
│  main.py / autonomous_trader.py  │
└────────────┬────────────────────┘
             │
             ▼
    ┌──────────────────┐
    │  backtest.py     │
    └────┬──────┬─────┬────┐
         ▼      ▼     ▼    ▼
      analysis llm memory portfolio

Per Decision Flow:

  1. Calculate 14-period indicators on 20-day window
  2. Retrieve 3 most relevant past lessons
  3. Get current portfolio state
  4. Send market context + state + memories to LLM
  5. Execute BUY/SELL/HOLD at next price
  6. Reflect on outcome for future learning

Key Improvements

Feature Before After
Indicators 5-day, basic 20-day, 14-period RSI/MACD, Bollinger, ATR
Memory Just last N Relevance-scored by ticker + keywords + recency
Reflection BUY only, hardcoded BUY & SELL with learned lessons
Portfolio Not included Full state (position, cash, P&L, trades)
Error Handling Single attempt 3 retries with exponential backoff
Speed Sequential only Skip-day mode (50%+ faster)
Multi-Stock No Yes, with comparison table
Validation Minimal Complete data checks

Configuration

In code (src/backtest.py, src/analysis.py):

  • Initial balance: $10,000
  • Indicator lookback: 20 days
  • RSI period: 14
  • MACD: 12/26/9
  • Bollinger Bands: 20-period, 2 std dev
  • ATR: 14-period
  • Memory cap: 100 lessons
  • API retries: 3 attempts
  • Rate limit: 0.3s per decision

Dependencies

  • yfinance ≥0.2.28
  • pandas ≥2.0.0
  • stockstats ≥0.5.2
  • numpy ≥1.24.0
  • requests ≥2.31.0
  • python-dotenv ≥1.0.0
  • rich ≥13.0.0

Troubleshooting

Slow execution? Use --skip-days 2 or 3.

API errors? Already retries 3x. If persistent, increase --skip-days.

No trades? LLM may be conservative. Check with --days 10 first.

Insufficient data? Need at least 22 days. Try --days 60.

Roadmap

  • Partial position sizing
  • Stop-loss / take-profit
  • Sentiment analysis
  • Walk-forward optimization
  • Real-time paper trading
  • Web dashboard

Built with OpenRouter AI + Yahoo Finance. Fast, intelligent, and modular trading backtester.

About

AI-based autonomous trading agent with historical backtesting, intelligent BUY/SELL/HOLD decisions via reasoner-agent, technical indicators (RSI, MACD, Bollinger Bands, ATR), learning memory, and multi-stock comparison.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages