Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Three LLM-driven directional trading modes for MNQ futures on TopstepX.
- All share the same data pipeline, gates, and execution layer — the difference is **how the LLM is prompted and how decisions are made**.
- ```
- ┌─────────────────────────────────────────────────────────────────┐
- │ DIRECTIONAL MODES │
- ├───────────────┬───────────────────┬─────────────────────────────┤
- │ --ai │ Engine Class │ Description │
- │ short-only │ DirectionalEngine│ Bear-only, single LLM │
- │ long-only │ DirectionalEngine│ Bull-only, single LLM │
- │ dual │ DualEngine │ Both prompts, consensus │
- └───────────────┴───────────────────┴─────────────────────────────┘
- ```
- \---
- ## Shared Data Pipeline (All 3 Modes)
- Every 60 seconds, the engine fetches market data and feeds it through indicators:
- ```
- TopstepX WebSocket + REST
- │
- ▼
- ┌───────────────────┐
- │ Bar Builder │
- │ 60× 5-min bars │
- │ 60× 1-min bars │
- │ 12× hourly bars │
- │ 14× daily bars │
- └────────┬──────────┘
- │
- ▼
- ┌───────────────────┐ ┌───────────────────────┐
- │ Technical Calc │─────▶│ Market Context Scorer │
- │ │ │ 7 dimensions: │
- │ EMA 9/21/50 │ │ • Trend (-100→+100) │
- │ MACD + histogram │ │ • Momentum │
- │ RSI │ │ • Mean Reversion │
- │ Bollinger Bands │ │ • Volatility │
- │ VWAP + deviation │ │ • Volume (session) │
- │ ATR(14) │ │ • S/R Proximity │
- │ Volume profile │ │ • Regime │
- └────────┬──────────┘ └──────────┬────────────┘
- │ │
- └────────────┬───────────────┘
- │
- ▼
- ┌────────────────────┐
- │ User Prompt │
- │ │
- │ Dashboard text │
- │ Bar data (OHLCV) │
- │ Today's trades │
- │ Recent hold log │
- │ Account state │
- └────────┬───────────┘
- │
- ▼
- ┌───────────┐
- │ TO LLM │
- └───────────┘
- ```
- \---
- ## Mode 1: Short-Only (`--ai short-only`)
- Single LLM call with bear-regime system prompt. Can only SHORT or HOLD.
- ```
- ┌──────────────────────┐
- User Prompt ─────▶│ LLM (Opus 4.6) │
- │ │
- │ System: SHORT-ONLY │
- │ Bear regime prompt │
- │ Macro: Iran, Fed, │
- │ PPI, credit risk │
- │ "Watch rip exhaust │
- │ before shorting" │
- │ │
- │ Output: │
- │ SHORT / HOLD │
- │ + confidence (0-100) │
- │ + SL/TP points │
- │ + reasoning │
- └──────────┬───────────┘
- │
- ┌──────────▼───────────┐
- │ Direction Gate │
- │ SHORT? ──▶ continue │
- │ LONG? ──▶ BLOCKED │
- │ HOLD? ──▶ skip │
- └──────────┬───────────┘
- │ SHORT
- ▼
- ┌──────────────────────┐
- │ Confidence ≥ 65? │──No──▶ HOLD
- └──────────┬───────────┘
- │ Yes
- ▼
- ┌──────────────────────┐
- │ DOM Gate (±30) │──Blocked──▶ HOLD
- │ Order book opposes? │
- └──────────┬───────────┘
- │ Clear
- ▼
- ┌──────────────────────┐
- │ GEX Gate │──Blocked──▶ HOLD
- │ GEX > +$2Bn? │ (pinned)
- │ TP adjust if neg/pos │
- └──────────┬───────────┘
- │ Clear
- ▼
- ┌──────────────────────┐
- │ ENTER SHORT │
- │ 10 contracts (RTH) │
- │ 5 contracts (ON) │
- │ LLM-set SL/TP │
- │ Min 1.5:1 R:R │
- └──────────────────────┘
- ── While in position (every 60s) ──
- ┌──────────────────────┐
- User Prompt + │ LLM (Opus 4.6) │
- Position Context─▶│ System: SHORT-ONLY │
- (entry, P\\\\\\\&L, │ + EXIT EVALUATION │
- hold time, │ │
- exit history) │ Output: │
- │ HOLD / CLOSE │
- └──────────┬───────────┘
- │
- ┌──────────▼───────────┐
- │ CLOSE? ──▶ market │
- │ close │
- │ HOLD? ──▶ let │
- │ brackets │
- │ work │
- └──────────────────────┘
- ```
- ### Parameters
- |Parameter|Value|
- |-|-|
- |Sizing|10 RTH / 5 overnight|
- |Min confidence|65/100|
- |R:R minimum|1.5:1 (enforced)|
- |SL/TP|LLM-recommended (not fixed)|
- |Cooldown|15 min after each close|
- |Selectivity|2-4 trades/day max|
- \---
- ## Mode 2: Long-Only (`--ai long-only`)
- Mirror of short-only for bull regimes. Same architecture, opposite direction.
- ```
- ┌──────────────────────┐
- User Prompt ─────▶│ LLM (Opus 4.6) │
- │ │
- │ System: LONG-ONLY │
- │ Bull regime prompt │
- │ "Buy dips, VWAP as │
- │ support, momentum │
- │ continuation" │
- │ │
- │ Output: │
- │ LONG / HOLD │
- │ + confidence (0-100) │
- │ + SL/TP points │
- │ + reasoning │
- └──────────┬───────────┘
- │
- ▼
- ┌──────────────────────┐
- │ Direction Gate │
- │ LONG? ──▶ continue │
- │ SHORT? ──▶ BLOCKED │
- │ HOLD? ──▶ skip │
- └──────────┬───────────┘
- │ LONG
- ▼
- (same gate chain)
- Confidence → DOM → GEX
- │
- ▼
- ┌──────────────────────┐
- │ ENTER LONG │
- │ 10 contracts (RTH) │
- │ 5 contracts (ON) │
- │ LLM-set SL/TP │
- │ Min 1.5:1 R:R │
- └──────────────────────┘
- Exit logic identical to short-only (HOLD/CLOSE evaluation)
- ```
- \---
- ## Mode 3: Dual Prompt (`--ai dual`)
- **Both** short-only and long-only prompts called in parallel. Consensus logic determines action.
- ### Entry (When Flat)
- ```
- User Prompt
- │
- ┌─────────────┴─────────────┐
- │ │
- ▼ ▼
- ┌──────────────────────┐ ┌──────────────────────┐
- │ LLM (Opus 4.6) │ │ LLM (Opus 4.6) │
- │ System: SHORT-ONLY │ │ System: LONG-ONLY │
- │ │ │ │
- │ Output: │ │ Output: │
- │ SHORT / HOLD │ │ LONG / HOLD │
- │ + confidence │ │ + confidence │
- │ + SL/TP + reasoning │ │ + SL/TP + reasoning │
- └──────────┬───────────┘ └──────────┬───────────┘
- │ │
- └─────────────┬─────────────┘
- │
- ▼
- ┌──────────────────────────────┐
- │ DECISION MATRIX │
- │ │
- │ SHORT + HOLD ──▶ ENTER SHORT │
- │ HOLD + LONG ──▶ ENTER LONG │
- │ SHORT + LONG ──▶ CONFLICT │──▶ NO TRADE
- │ HOLD + HOLD ──▶ NO SETUP │──▶ NO TRADE
- └──────────────┬───────────────┘
- │ (one trade, one hold)
- ▼
- ┌──────────────────────┐
- │ Gate Chain │
- │ Confidence → DOM │
- │ → GEX → Execute │
- └──────────────────────┘
- ```
- **Key insight:** Entry requires one prompt to signal a trade AND the opposing prompt to confirm there's no setup in the other direction. Both signaling = confused market. Both holding = no edge.
- ### Exit (When In Position)
- Two-stage evaluation every 60 seconds:
- ```
- Current Position: LONG x5 @ 19500.00
- │
- ▼ STAGE 1: Same-Direction Check
- ┌──────────────────────┐
- │ Same-Dir Prompt │
- │ (LONG-ONLY) │
- │ │
- │ + EXIT EVALUATION │
- │ + Position context │
- │ (entry, P\\\\\\\&L, time, │
- │ exit check history) │
- │ │
- │ Output: │
- │ HOLD / CLOSE │
- └──────────┬───────────┘
- │
- ┌───────▼────────┐
- │ CLOSE? │──Yes──▶ Market close (thesis broken)
- │ Bracket fill? │──Yes──▶ Cooldown (SL/TP hit)
- └───────┬────────┘
- │ HOLD
- │
- ▼ STAGE 2: Opposing Direction Check
- ┌──────────────────────┐
- │ Opposing Prompt │
- │ (SHORT-ONLY) │
- │ │
- │ Standard entry mode │
- │ │
- │ Output: │
- │ SHORT / HOLD │
- └──────────┬───────────┘
- │
- ┌───────▼────────┐
- │ SHORT signal? │──No──▶ HOLD position (keep LONG)
- └───────┬────────┘
- │ Yes
- ▼
- ┌──────────────────────┐
- │ Anti-Flip Filter │
- │ │
- │ Need 2 consecutive │
- │ opposing signals │
- │ AND conf ≥ 70 │
- │ │
- │ 1st signal ──▶ │
- │ "building..." │──▶ HOLD (not confirmed yet)
- │ │
- │ 2nd signal + ≥70 ──▶│
- │ CONFIRMED │
- └──────────┬───────────┘
- │ Confirmed
- ▼
- ┌──────────────────────┐
- │ 🔄 REVERSAL │
- │ │
- │ 1. Market close LONG │
- │ 2. Skip cooldown │
- │ 3. Enter SHORT │
- │ (through gates) │
- └──────────────────────┘
- ```
- ### Dual Mode Parameters
- |Parameter|Value|
- |-|-|
- |Sizing|5 RTH / 3 overnight|
- |Min confidence (entry)|65/100|
- |Min confidence (reversal)|70/100|
- |Consecutive signals for reversal|2|
- |Cooldown|15 min (skipped on reversal)|
- |LLM calls per cycle (flat)|2 (parallel)|
- |LLM calls per cycle (in position)|2 (sequential)|
- \---
- ## Shared Gate Chain
- All three modes pass through the same gates before execution:
- ```
- LLM Decision
- │
- ▼
- ┌─────────────────┐
- │ Confidence ≥ 65 │──No──▶ HOLD (low confidence)
- └────────┬────────┘
- │ Yes
- ▼
- ┌─────────────────┐
- │ DOM Gate │
- │ ±30 threshold │
- │ │
- │ LONG + DOM < -30│──▶ BLOCKED (sellers dominate)
- │ SHORT + DOM > +30──▶ BLOCKED (buyers dominate)
- └────────┬────────┘
- │ Clear
- ▼
- ┌─────────────────┐
- │ GEX Gate │
- │ (RTH only, │
- │ 8:30-15:00 CT) │
- │ │
- │ GEX > +$2Bn │──▶ BLOCKED (pinned market)
- │ GEX negative │──▶ TP × 1.3 (amplified moves)
- │ GEX positive │──▶ TP × 0.7 (dampened moves)
- └────────┬────────┘
- │ Clear
- ▼
- ┌─────────────────┐
- │ Risk Manager │
- │ Daily P\\\\\\\&L check │
- │ Trade count │
- └────────┬────────┘
- │ Clear
- ▼
- ┌─────────────────┐
- │ BRACKET ORDER │
- │ Market entry │
- │ + SL stop │
- │ + TP limit │
- └─────────────────┘
- ```
- \---
- ## Position Lifecycle (All Modes)
- ```
- FLAT ──▶ LLM signals entry ──▶ Gates pass ──▶ BRACKET ORDER
- │
- ┌──────────┴──────────┐
- │ │
- ▼ ▼
- ┌───────────┐ ┌───────────┐
- │ SL FILL │ │ TP FILL │
- │ (exchange) │ │ (exchange) │
- └─────┬─────┘ └─────┬─────┘
- │ │
- ▼ ▼
- ┌─────────────────────────────┐
- │ 15-min COOLDOWN │
- │ (persists across restarts) │
- └─────────────┬───────────────┘
- │
- ▼
- FLAT
- │
- ┌───────────┴───────────┐
- │ LLM EXIT (early) │
- │ Thesis broken │
- │ (short/long-only) │
- │ OR reversal signal │
- │ (dual mode only) │
- └───────────────────────┘
- ```
- ### Three Exit Paths
- 1. **SL fill** — Exchange stop-loss triggers → cooldown
- 2. **TP fill** — Exchange take-profit triggers → cooldown
- 3. **LLM exit** — Thesis broken (all modes) or reversal confirmed (dual only)
- \---
- ## Blind Recon (Every 5 Seconds)
- Shared across all modes. Catches bracket fills the engine missed:
- ```
- ┌──────────────────────────────────────────────────┐
- │ Engine State Exchange State │
- │ │
- │ Has position ←→ FLAT │
- │ → SL/TP filled, sync P\\\\\\\&L, clear position │
- │ │
- │ FLAT ←→ Has position │
- │ → Unknown! Alert Discord, BLOCK all orders │
- │ │
- │ Size mismatch ←→ Different size │
- │ → Partial fill, sync to exchange │
- └──────────────────────────────────────────────────┘
- ```
- \---
- ## Mode Comparison
- ```
- ┌───────────────┬──────────────┬──────────────┬───────────────┐
- │ │ SHORT-ONLY │ LONG-ONLY │ DUAL │
- ├───────────────┼──────────────┼──────────────┼───────────────┤
- │ Directions │ SHORT only │ LONG only │ BOTH │
- │ LLM calls/min │ 1 │ 1 │ 2 (parallel) │
- │ Entry logic │ LLM SHORT │ LLM LONG │ One trade + │
- │ │ + gates │ + gates │ one hold │
- │ Exit: thesis │ Same prompt │ Same prompt │ Same prompt │
- │ broken │ HOLD/CLOSE │ HOLD/CLOSE │ HOLD/CLOSE │
- │ Reversal? │ No │ No │ Yes (2×conf≥70│
- │ │ │ │ + anti-flip) │
- │ RTH size │ 10 │ 10 │ 5 │
- │ Overnight size│ 5 │ 5 │ 3 │
- │ Use when │ Bear regime │ Bull regime │ Uncertain / │
- │ │ │ │ transitional │
- └───────────────┴──────────────┴──────────────┴───────────────┘
- ```
- ## Key Files
- |File|Purpose|
- |-|-|
- |`engine/directional\\\\\\\_engine.py`|Short-only / long-only engine (base class)|
- |`engine/dual\\\\\\\_engine.py`|Dual prompt engine (extends DirectionalEngine)|
- |`ai/prompts.py`|`SYSTEM\\\\\\\_PROMPT\\\\\\\_SHORT\\\\\\\_ONLY`, `SYSTEM\\\\\\\_PROMPT\\\\\\\_LONG\\\\\\\_ONLY`|
- |`ai/trader.py`|LLM integration, `raw\\\\\\\_call()` for custom system prompts|
- |`dom/dom\\\\\\\_book.py`|Real-time order book via WebSocket|
- |`dom/dom\\\\\\\_analyzer.py`|DOM scoring + direction block logic|
- |`data/gex\\\\\\\_rh.py`|GEX from Robinhood options (NDX+SPX)|
- |`risk/manager.py`|Daily P\&L + trade gating|
- |`engine/scheduler.py`|Hard close, trading windows, DST-safe|
Add Comment
Please, Sign In to add comment