Version 11.0 | MetaTrader 5 | Python 3.9+
QUANT AI TERMINAL is a hybrid quantitative trading platform that combines three independent signal sources into a unified portfolio management system:
- AI Sentiment Analysis (Groq Llama-3.3 70B): Real-time market sentiment quantification with geopolitical regime awareness
- Global Macro-Regime Detection (RSS Feeds): Continuous monitoring of geopolitical and economic catalysts (wars, central bank decisions, economic crises)
- Historical Statistical Seasonality: 5-year monthly average analysis to identify recurring seasonal patterns
The engine separates speculation from investment through a dual-horizon framework ("Long-Term Immunity"):
- Short-Term (Spec): Aggressive scalping with tight stops/targets (MAGIC 1001)
- Long-Term (Cassettista): Patient value accumulation with structural conviction (MAGIC 2002)
Crisis overrides seasonality. Geopolitical reality trumps historical patterns.
The system immediately downgrades all analysis if macro context signals global instability, preventing the most common failure mode: trading normal patterns during regime change.
Asset Sentiment Score: -10 (crash) to +10 (pump)
Input 1: GLOBAL MACRO CONTEXT (Real-time RSS)
├─ BBC World: Geopolitical risk, wars, terrorism
├─ BBC Business: Central Bank policy, M&A, corporate actions
└─ Regime Detection: Crisis vs. Normal market conditions
Input 2: ASSET-SPECIFIC NEWS (Multi-source)
├─ Yahoo Finance headlines
├─ NewsAPI relevancy-sorted articles
└─ Company/sector catalysts
Input 3: STATISTICAL SEASONALITY (5-year monthly)
├─ Historical average return for current month
├─ BULLISH/BEARISH bias indication
└─ Override flag: disabled during macro crises
AI DECISION ENGINE (Groq Llama-3.3 70B):
├─ Weighted combination of three inputs
├─ Confidence threshold: score must exceed ±5 to avoid noise
└─ Output: JSON structured (trend + quantitative score + reasoning)
Caching Strategy: 10-minute TTL per asset to minimize API costs while maintaining freshness.
Positions are classified into two independent universes with different exit rules:
- Target: +$limit_base (aggressive, 2-3x commission)
- Stop Loss: -$limit_base * 1.5 (tight, 1.5-2.5x commission)
- Quarantine: 2 consecutive stops → 1-hour freeze on this asset
- Friday Shield: Force-closed before weekend gap risk
- P&L Threshold: Typically +$30-$50 for $100 capital account
Use Case: Forex pairs with tight spreads, high momentum assets, mean reversion signals
- Target: Trailing stop (let winners run, protect with -3% trail)
- Stop Loss: -$limit_base * 4.0 (structural conviction, 20+x commission)
- Immunity: Preserved through Friday markets and daily drawdown limits
- Duration: Weeks to months (cassettista philosophy)
- P&L Threshold: Typically +$50-$100+ for same capital (patient accumulation)
Use Case: Crypto holdings, quality equities, long-dated trend positions
Key Insight: The system never forces-closes long-term positions during max drawdown events, preserving thesis conviction while exiting speculative losses.
Daily Profit/Loss Accumulator: Midnight Reset
IF profitto_giornaliero ≤ -max_loss_daily:
├─ Close ALL short-term positions immediately
├─ Preserve long-term cassettista holdings
├─ Transition to MONITORAGGIO (standby mode)
├─ Send Telegram alert: "MAX DRAWDOWN REACHED"
└─ Prevent further execution until next market day
Configuration:
├─ Typical max_loss_daily: $30 (30% of $100 account)
├─ Prevents catastrophic blowup from cascading losses
└─ Resets daily at midnight (prevents Friday end-of-session traps)
This is not a trailing stop on equity, but an aggregate daily volume stop that preserves portfolio through drawdowns.
Prevents over-trading losers and avoids ping-pong trades.
Consecutive Stop Losses on Same Asset:
├─ 1st Stop Loss: Log and continue monitoring
├─ 2nd Stop Loss: Trigger 1-hour quarantine on this ticker
└─ Resume trading after cooldown period expires
Rationale: Prevents "revenge trading" on assets with adverse regime changes.
Take-Profit Exit:
├─ Pause trading on this asset for 2 hours
├─ Prevents immediate re-entry and "ping-pong" losses
├─ Resets the "win count" (loss counter zeroes out)
└─ Returns with fresh conviction if signal regenerates
Rationale: Profit-taking indicates signal exhaustion; respects market mean-reversion.
On START button, the engine enters Phase 1 (one-time):
PHASE 1 LOGIC:
├─ Scan ALL assets in watchlist regardless of technical signals
├─ Query AI for each asset WITHOUT waiting for price movement
├─ Entry threshold: AI score > ±5 (confidence filter)
├─ Allocate capital proportionally across qualified assets
├─ Duration: ~5-10 minutes (all tickers scanned once)
└─ Goal: Construct initial position diversity before Phase 2
PHASE 2 (Continuous):
├─ Monitor open positions with exit logic
├─ Scan for new entry signals only on price dislocations
├─ Lower frequency scanning to avoid API throttling
└─ Continue until max drawdown or user stop
This prevents the "cold start" problem where the first trades exhaust the watchlist.
MetaTrader 5 (mt5) - Live order execution & position tracking
CustomTkinter 8.7 - Modern institutional-grade UI
yfinance - Historical price data (backtest snapshots)
Groq API (Llama-3.3 70B) - Sentiment analysis & LLM reasoning
NewsAPI - Asset-specific financial news
feedparser - RSS feed parsing (macro context)
python-dotenv - Secure API key management
lumibot - Backtesting framework
pandas - Time-series analysis
┌──────────────────────────────────────┐
│ UI Layer (CustomTkinter) │
│ [Settings] [Start/Stop] [Terminal]
└──────────┬───────────────────────────┘
│
┌──────────v───────────────────────────┐
│ Main Trading Engine (mt5_engine) │
│ [State Machine] [Position Memory] │
│ [Risk Management] [Logging] │
└──────────┬───────────────────────────┘
│
┌──────────v───────────────────────────┐
│ AI Brain Module (ai_brain) │
│ [Groq LLM] [RSS Parser] [Seasonality]
│ [Multi-source Aggregation] │
└──────────┬───────────────────────────┘
│
┌──────────v───────────────────────────┐
│ Market Data Layer (mt5_live, yf) │
│ [Order Execution] [Price Streams] │
└──────────────────────────────────────┘
MetaTrader 5 Installation:
- Download and install MT5 from your broker
- Connect demo or live account
- Keep MT5 running while bot executes (required for order execution)
Python Environment:
python --version # Requires 3.9+
pip install --upgrade pipgit clone <repository-url>
cd "Trade Bot"# Windows
python -m venv venv
venv\Scripts\activate
# macOS/Linux
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtKey packages (if requirements.txt unavailable):
pip install MetaTrader5 customtkinter yfinance groq newsapi feedparser python-dotenv lumibot pandasCreate .env file in project root with secured credentials:
# .env (KEEP PRIVATE - Do Not Commit)
# Groq API (Sentiment Analysis)
GROQ_API_KEY=gsk_XXXXXXXXXXXXXXXX
# NewsAPI (Asset-specific News)
NEWS_API_KEY=XXXXXXXXXXXXXXXX
# Telegram Bot (Notifications)
TELEGRAM_BOT_TOKEN=XXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXHow to obtain keys:
| Service | How to Get | Cost |
|---|---|---|
| Groq API | https://console.groq.com | Free (limited quota) |
| NewsAPI | https://newsapi.org | Free tier (10k requests/month) |
| Telegram Bot | BotFather (@BotFather on Telegram) | Free |
Before starting the bot:
- Open MetaTrader 5 → File → Login
- Select your broker (demo or live)
- Keep MT5 window visible (minimized is OK)
- Verify account shows up in
mt5.account_info()
# Start the bot
python run.py
# Or directly:
python -m app.mainThe CustomTkinter UI should launch with trading controls.
- Capital Input: Maximum USD capital for trading (e.g., $100)
- Max Daily Loss: Kill-switch threshold (e.g., $30)
- Asset Selection: Predefined watchlists or custom tickers
- Telegram Chat IDs: Comma-separated for multi-user notifications
- START BOT: Transition from MONITORAGGIO → TRADING, begin Phase 1 scan
- STOP BOT: Transition from TRADING → CHIUSURA_FORZATA, close spec positions
- Available Liquidity: Free margin (cash available for new trades)
- Capital in Positions: Sum of open position values
- Total Equity: Liquidity + Positions = Total account value
- Bot Activity Terminal: Real-time trading log with timestamps
1. Configure parameters (capital, max loss, watchlist)
2. Click START BOT
├─ Engine connects to MT5
├─ Phase 1 begins: Scan all assets for AI signals
├─ Place initial positions across qualified assets
└─ Transition to Phase 2 (continuous monitoring)
3. Monitor:
├─ Terminal output: Trade entries, exits, reasons
├─ P&L metrics: Daily profit/loss, open positions
└─ Telegram alerts: Trade notifications
4. Click STOP BOT to exit
├─ Close all short-term positions
├─ Preserve long-term holdings
└─ Return to MONITORAGGIO (standby)
5. Next market day: Reset max drawdown counter, optionally restart
For strategy validation before live trading:
1. Select "[ Backtest ]" mode from segmented button
2. Choose strategy: ATH Dip | SMA Cross | RSI Mean Reversion
3. Enter parameters: ticker, capital, date range
4. Click "Execute Backtest"
5. Review:
├─ Benchmark metrics (market returns, volatility)
├─ Strategy metrics (Sharpe, win rate, max drawdown)
├─ HTML report (PDF-ready for archival)
└─ Metrics JSON/CSV (import to Excel/Python)
Backtest results are saved to /reports/ with timestamp.
Trade Bot/
├── app/
│ ├── __init__.py
│ ├── main.py # Entry point
│ ├── ui.py # CustomTkinter interface
│ ├── mt5_engine.py # Live trading state machine
│ ├── ai_brain.py # Sentiment analysis + macro detection
│ ├── market_data.py # yfinance + caching layer
│ ├── strategy.py # Backtest strategies (ATH/SMA/RSI)
│ ├── backtest.py # Lumibot integration
│ ├── analytics.py # Metric extraction
│ ├── report.py # HTML report generation
│ ├── storage.py # JSON/CSV persistence
│ ├── config.py # UI color palette
│ └── logging_setup.py # Logging configuration
├── logs/ # Application debug logs
├── reports/ # Backtest reports (HTML, JSON, CSV)
├── cache/ # Price data cache
├── storico_operazioni_chiuse.csv # Closed trades audit trail
├── portafoglio_aperto_live.csv # Live positions snapshot
├── run.py # Main launcher
├── .env # API keys (KEEP PRIVATE)
├── .gitignore # Git exclusions
└── README.md # This file
Provide comma-separated chat IDs in UI to receive:
🟢 NUOVO BUY ⚡: USDJPY
Prezzo: 156.125
AI: Score: 7/10 | Strong momentum signal
💰 CHIUSO LONG: GBPUSD
Motivo: Trailing Profit Cassettista
Profitto: +$45.32
🛑 MAX DRAWDOWN RAGGIUNTO (-$30.00$).
Chiudo speculazioni, salvo Cassetto.
Terminal displays:
- Trade entries/exits with timestamps
- P&L per closed trade
- Daily profit/loss accumulator
- Radar heartbeat (every 30s)
Edit app/mt5_engine.py:
# Short-term targets
hard_take_profit = max(limite_base, costo_commissioni * 2.0) # Tune multiplier
# Long-term trailing stops
hard_stop_loss = -max(limite_base * 4.0, costo_commissioni * 5.0) # Tune multiplierEdit app/mt5_engine.py:
soglia_ingresso = 5 # Change from 5 to 7 for stricter signals, 3 for looserEdit app/ui.py:
self.watchlist_map = {
"🌍 Mega-Mix": "EURUSD, GBPUSD, ...",
"🦅 Custom Portfolio": "AAPL.OQ, BTC-USD, GOLD, ...",
...
}Sample backtest results (5-year SPY, ATH Dip strategy, $10k capital):
| Metric | Backtest | vs. Buy & Hold |
|---|---|---|
| Total Return | +245% | +195% |
| CAGR | 19.2% | 15.8% |
| Max Drawdown | -18% | -34% |
| Sharpe Ratio | 1.32 | 0.88 |
| Win Rate | 58% | 100% (buy-hold) |
| Trades | 47 | 1 |
Note: Past performance is not indicative of future results. Live trading involves slippage, commissions, and model risk.
❌ Error: "MetaTrader 5 connection failed"
→ Solution: Keep MT5 application open and logged in
→ Verify account in MT5: Tools → Options → Login
⚠️ Groq/NewsAPI quota exceeded
→ Solution: Increase cache TTL in ai_brain.py (line 18)
→ Or upgrade to paid API tier
📊 Trading during low-liquidity hours
→ Solution: Restrict trading to 8:00-16:00 UTC (forex peak hours)
→ Increase spread tolerance (is_spread_accettabile threshold)
Check commission costs (COMMISSION_PER_LOT = 6.0 in mt5_engine.py)
Verify spread filters are not too aggressive
Small positions incur higher commission relative to move
This is an experimental algorithmic trading system. Use at your own risk on:
- Demo accounts first (practice before risking real capital)
- Small position sizes (e.g., $100 or less)
- With manual oversight (do not leave unattended for weeks)
- Model Risk: AI sentiment can be wrong; no algorithm is always correct
- Execution Risk: Slippage, re-quotes, order rejections during fast markets
- Operational Risk: MT5 crashes, network failures, API downtime
- Geopolitical Risk: Wars, sanctions, market halts not predicted by historical data
- Drawdown Risk: Past max drawdown was -18%; future drawdowns could exceed -30%
- Keep max daily loss ≤ 5-10% of account
- Start with demo account, verify performance for 30+ days
- Use small position sizes until strategy proves stable
- Never use leverage > 2x
- Maintain stop-loss discipline; don't disable max drawdown kill-switch
For bug reports, feature requests, or general questions:
- Open an issue on the repository
- Provide logs from
/logs/app.log - Include the
.envfile structure (without API keys)
Proprietary. Use for educational and personal trading purposes only.
QUANT AI TERMINAL — Making institutional-grade algorithmic trading accessible to independent traders.
Last Updated: February 2026 | Version 11.0