mfn

architecture llm polymarket 2 feeding

mfn
Apr 9th, 2026
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.87 KB | None | 0 0
  1. Three LLM-driven directional trading modes for MNQ futures on TopstepX.
  2. All share the same data pipeline, gates, and execution layer — the difference is **how the LLM is prompted and how decisions are made**.
  3.  
  4. ```
  5. ┌─────────────────────────────────────────────────────────────────┐
  6. │ DIRECTIONAL MODES │
  7. ├───────────────┬───────────────────┬─────────────────────────────┤
  8. │ --ai │ Engine Class │ Description │
  9. │ short-only │ DirectionalEngine│ Bear-only, single LLM │
  10. │ long-only │ DirectionalEngine│ Bull-only, single LLM │
  11. │ dual │ DualEngine │ Both prompts, consensus │
  12. └───────────────┴───────────────────┴─────────────────────────────┘
  13. ```
  14.  
  15. \---
  16.  
  17. ## Shared Data Pipeline (All 3 Modes)
  18.  
  19. Every 60 seconds, the engine fetches market data and feeds it through indicators:
  20.  
  21. ```
  22. TopstepX WebSocket + REST
  23. ┌───────────────────┐
  24. │ Bar Builder │
  25. │ 60× 5-min bars │
  26. │ 60× 1-min bars │
  27. │ 12× hourly bars │
  28. │ 14× daily bars │
  29. └────────┬──────────┘
  30. ┌───────────────────┐ ┌───────────────────────┐
  31. │ Technical Calc │─────▶│ Market Context Scorer │
  32. │ │ │ 7 dimensions: │
  33. │ EMA 9/21/50 │ │ • Trend (-100→+100) │
  34. │ MACD + histogram │ │ • Momentum │
  35. │ RSI │ │ • Mean Reversion │
  36. │ Bollinger Bands │ │ • Volatility │
  37. │ VWAP + deviation │ │ • Volume (session) │
  38. │ ATR(14) │ │ • S/R Proximity │
  39. │ Volume profile │ │ • Regime │
  40. └────────┬──────────┘ └──────────┬────────────┘
  41. │ │
  42. └────────────┬───────────────┘
  43. ┌────────────────────┐
  44. │ User Prompt │
  45. │ │
  46. │ Dashboard text │
  47. │ Bar data (OHLCV) │
  48. │ Today's trades │
  49. │ Recent hold log │
  50. │ Account state │
  51. └────────┬───────────┘
  52. ┌───────────┐
  53. │ TO LLM │
  54. └───────────┘
  55. ```
  56.  
  57. \---
  58.  
  59. ## Mode 1: Short-Only (`--ai short-only`)
  60.  
  61. Single LLM call with bear-regime system prompt. Can only SHORT or HOLD.
  62.  
  63. ```
  64. ┌──────────────────────┐
  65. User Prompt ─────▶│ LLM (Opus 4.6) │
  66. │ │
  67. │ System: SHORT-ONLY │
  68. │ Bear regime prompt │
  69. │ Macro: Iran, Fed, │
  70. │ PPI, credit risk │
  71. │ "Watch rip exhaust │
  72. │ before shorting" │
  73. │ │
  74. │ Output: │
  75. │ SHORT / HOLD │
  76. │ + confidence (0-100) │
  77. │ + SL/TP points │
  78. │ + reasoning │
  79. └──────────┬───────────┘
  80. ┌──────────▼───────────┐
  81. │ Direction Gate │
  82. │ SHORT? ──▶ continue │
  83. │ LONG? ──▶ BLOCKED │
  84. │ HOLD? ──▶ skip │
  85. └──────────┬───────────┘
  86. │ SHORT
  87. ┌──────────────────────┐
  88. │ Confidence ≥ 65? │──No──▶ HOLD
  89. └──────────┬───────────┘
  90. │ Yes
  91. ┌──────────────────────┐
  92. │ DOM Gate (±30) │──Blocked──▶ HOLD
  93. │ Order book opposes? │
  94. └──────────┬───────────┘
  95. │ Clear
  96. ┌──────────────────────┐
  97. │ GEX Gate │──Blocked──▶ HOLD
  98. │ GEX > +$2Bn? │ (pinned)
  99. │ TP adjust if neg/pos │
  100. └──────────┬───────────┘
  101. │ Clear
  102. ┌──────────────────────┐
  103. │ ENTER SHORT │
  104. │ 10 contracts (RTH) │
  105. │ 5 contracts (ON) │
  106. │ LLM-set SL/TP │
  107. │ Min 1.5:1 R:R │
  108. └──────────────────────┘
  109.  
  110. ── While in position (every 60s) ──
  111.  
  112. ┌──────────────────────┐
  113. User Prompt + │ LLM (Opus 4.6) │
  114. Position Context─▶│ System: SHORT-ONLY │
  115. (entry, P\\\\\\\&L, │ + EXIT EVALUATION │
  116. hold time, │ │
  117. exit history) │ Output: │
  118. │ HOLD / CLOSE │
  119. └──────────┬───────────┘
  120. ┌──────────▼───────────┐
  121. │ CLOSE? ──▶ market │
  122. │ close │
  123. │ HOLD? ──▶ let │
  124. │ brackets │
  125. │ work │
  126. └──────────────────────┘
  127. ```
  128.  
  129. ### Parameters
  130.  
  131. |Parameter|Value|
  132. |-|-|
  133. |Sizing|10 RTH / 5 overnight|
  134. |Min confidence|65/100|
  135. |R:R minimum|1.5:1 (enforced)|
  136. |SL/TP|LLM-recommended (not fixed)|
  137. |Cooldown|15 min after each close|
  138. |Selectivity|2-4 trades/day max|
  139.  
  140. \---
  141.  
  142. ## Mode 2: Long-Only (`--ai long-only`)
  143.  
  144. Mirror of short-only for bull regimes. Same architecture, opposite direction.
  145.  
  146. ```
  147. ┌──────────────────────┐
  148. User Prompt ─────▶│ LLM (Opus 4.6) │
  149. │ │
  150. │ System: LONG-ONLY │
  151. │ Bull regime prompt │
  152. │ "Buy dips, VWAP as │
  153. │ support, momentum │
  154. │ continuation" │
  155. │ │
  156. │ Output: │
  157. │ LONG / HOLD │
  158. │ + confidence (0-100) │
  159. │ + SL/TP points │
  160. │ + reasoning │
  161. └──────────┬───────────┘
  162. ┌──────────────────────┐
  163. │ Direction Gate │
  164. │ LONG? ──▶ continue │
  165. │ SHORT? ──▶ BLOCKED │
  166. │ HOLD? ──▶ skip │
  167. └──────────┬───────────┘
  168. │ LONG
  169. (same gate chain)
  170. Confidence → DOM → GEX
  171. ┌──────────────────────┐
  172. │ ENTER LONG │
  173. │ 10 contracts (RTH) │
  174. │ 5 contracts (ON) │
  175. │ LLM-set SL/TP │
  176. │ Min 1.5:1 R:R │
  177. └──────────────────────┘
  178.  
  179. Exit logic identical to short-only (HOLD/CLOSE evaluation)
  180. ```
  181.  
  182. \---
  183.  
  184. ## Mode 3: Dual Prompt (`--ai dual`)
  185.  
  186. **Both** short-only and long-only prompts called in parallel. Consensus logic determines action.
  187.  
  188. ### Entry (When Flat)
  189.  
  190. ```
  191. User Prompt
  192. ┌─────────────┴─────────────┐
  193. │ │
  194. ▼ ▼
  195. ┌──────────────────────┐ ┌──────────────────────┐
  196. │ LLM (Opus 4.6) │ │ LLM (Opus 4.6) │
  197. │ System: SHORT-ONLY │ │ System: LONG-ONLY │
  198. │ │ │ │
  199. │ Output: │ │ Output: │
  200. │ SHORT / HOLD │ │ LONG / HOLD │
  201. │ + confidence │ │ + confidence │
  202. │ + SL/TP + reasoning │ │ + SL/TP + reasoning │
  203. └──────────┬───────────┘ └──────────┬───────────┘
  204. │ │
  205. └─────────────┬─────────────┘
  206. ┌──────────────────────────────┐
  207. │ DECISION MATRIX │
  208. │ │
  209. │ SHORT + HOLD ──▶ ENTER SHORT │
  210. │ HOLD + LONG ──▶ ENTER LONG │
  211. │ SHORT + LONG ──▶ CONFLICT │──▶ NO TRADE
  212. │ HOLD + HOLD ──▶ NO SETUP │──▶ NO TRADE
  213. └──────────────┬───────────────┘
  214. │ (one trade, one hold)
  215. ┌──────────────────────┐
  216. │ Gate Chain │
  217. │ Confidence → DOM │
  218. │ → GEX → Execute │
  219. └──────────────────────┘
  220. ```
  221.  
  222. **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.
  223.  
  224. ### Exit (When In Position)
  225.  
  226. Two-stage evaluation every 60 seconds:
  227.  
  228. ```
  229. Current Position: LONG x5 @ 19500.00
  230. ▼ STAGE 1: Same-Direction Check
  231. ┌──────────────────────┐
  232. │ Same-Dir Prompt │
  233. │ (LONG-ONLY) │
  234. │ │
  235. │ + EXIT EVALUATION │
  236. │ + Position context │
  237. │ (entry, P\\\\\\\&L, time, │
  238. │ exit check history) │
  239. │ │
  240. │ Output: │
  241. │ HOLD / CLOSE │
  242. └──────────┬───────────┘
  243. ┌───────▼────────┐
  244. │ CLOSE? │──Yes──▶ Market close (thesis broken)
  245. │ Bracket fill? │──Yes──▶ Cooldown (SL/TP hit)
  246. └───────┬────────┘
  247. │ HOLD
  248. ▼ STAGE 2: Opposing Direction Check
  249. ┌──────────────────────┐
  250. │ Opposing Prompt │
  251. │ (SHORT-ONLY) │
  252. │ │
  253. │ Standard entry mode │
  254. │ │
  255. │ Output: │
  256. │ SHORT / HOLD │
  257. └──────────┬───────────┘
  258. ┌───────▼────────┐
  259. │ SHORT signal? │──No──▶ HOLD position (keep LONG)
  260. └───────┬────────┘
  261. │ Yes
  262. ┌──────────────────────┐
  263. │ Anti-Flip Filter │
  264. │ │
  265. │ Need 2 consecutive │
  266. │ opposing signals │
  267. │ AND conf ≥ 70 │
  268. │ │
  269. │ 1st signal ──▶ │
  270. │ "building..." │──▶ HOLD (not confirmed yet)
  271. │ │
  272. │ 2nd signal + ≥70 ──▶│
  273. │ CONFIRMED │
  274. └──────────┬───────────┘
  275. │ Confirmed
  276. ┌──────────────────────┐
  277. │ 🔄 REVERSAL │
  278. │ │
  279. │ 1. Market close LONG │
  280. │ 2. Skip cooldown │
  281. │ 3. Enter SHORT │
  282. │ (through gates) │
  283. └──────────────────────┘
  284. ```
  285.  
  286. ### Dual Mode Parameters
  287.  
  288. |Parameter|Value|
  289. |-|-|
  290. |Sizing|5 RTH / 3 overnight|
  291. |Min confidence (entry)|65/100|
  292. |Min confidence (reversal)|70/100|
  293. |Consecutive signals for reversal|2|
  294. |Cooldown|15 min (skipped on reversal)|
  295. |LLM calls per cycle (flat)|2 (parallel)|
  296. |LLM calls per cycle (in position)|2 (sequential)|
  297.  
  298. \---
  299.  
  300. ## Shared Gate Chain
  301.  
  302. All three modes pass through the same gates before execution:
  303.  
  304. ```
  305. LLM Decision
  306. ┌─────────────────┐
  307. │ Confidence ≥ 65 │──No──▶ HOLD (low confidence)
  308. └────────┬────────┘
  309. │ Yes
  310. ┌─────────────────┐
  311. │ DOM Gate │
  312. │ ±30 threshold │
  313. │ │
  314. │ LONG + DOM < -30│──▶ BLOCKED (sellers dominate)
  315. │ SHORT + DOM > +30──▶ BLOCKED (buyers dominate)
  316. └────────┬────────┘
  317. │ Clear
  318. ┌─────────────────┐
  319. │ GEX Gate │
  320. │ (RTH only, │
  321. │ 8:30-15:00 CT) │
  322. │ │
  323. │ GEX > +$2Bn │──▶ BLOCKED (pinned market)
  324. │ GEX negative │──▶ TP × 1.3 (amplified moves)
  325. │ GEX positive │──▶ TP × 0.7 (dampened moves)
  326. └────────┬────────┘
  327. │ Clear
  328. ┌─────────────────┐
  329. │ Risk Manager │
  330. │ Daily P\\\\\\\&L check │
  331. │ Trade count │
  332. └────────┬────────┘
  333. │ Clear
  334. ┌─────────────────┐
  335. │ BRACKET ORDER │
  336. │ Market entry │
  337. │ + SL stop │
  338. │ + TP limit │
  339. └─────────────────┘
  340. ```
  341.  
  342. \---
  343.  
  344. ## Position Lifecycle (All Modes)
  345.  
  346. ```
  347. FLAT ──▶ LLM signals entry ──▶ Gates pass ──▶ BRACKET ORDER
  348. ┌──────────┴──────────┐
  349. │ │
  350. ▼ ▼
  351. ┌───────────┐ ┌───────────┐
  352. │ SL FILL │ │ TP FILL │
  353. │ (exchange) │ │ (exchange) │
  354. └─────┬─────┘ └─────┬─────┘
  355. │ │
  356. ▼ ▼
  357. ┌─────────────────────────────┐
  358. │ 15-min COOLDOWN │
  359. │ (persists across restarts) │
  360. └─────────────┬───────────────┘
  361. FLAT
  362. ┌───────────┴───────────┐
  363. │ LLM EXIT (early) │
  364. │ Thesis broken │
  365. │ (short/long-only) │
  366. │ OR reversal signal │
  367. │ (dual mode only) │
  368. └───────────────────────┘
  369. ```
  370.  
  371. ### Three Exit Paths
  372.  
  373. 1. **SL fill** — Exchange stop-loss triggers → cooldown
  374. 2. **TP fill** — Exchange take-profit triggers → cooldown
  375. 3. **LLM exit** — Thesis broken (all modes) or reversal confirmed (dual only)
  376.  
  377. \---
  378.  
  379. ## Blind Recon (Every 5 Seconds)
  380.  
  381. Shared across all modes. Catches bracket fills the engine missed:
  382.  
  383. ```
  384. ┌──────────────────────────────────────────────────┐
  385. │ Engine State Exchange State │
  386. │ │
  387. │ Has position ←→ FLAT │
  388. │ → SL/TP filled, sync P\\\\\\\&L, clear position │
  389. │ │
  390. │ FLAT ←→ Has position │
  391. │ → Unknown! Alert Discord, BLOCK all orders │
  392. │ │
  393. │ Size mismatch ←→ Different size │
  394. │ → Partial fill, sync to exchange │
  395. └──────────────────────────────────────────────────┘
  396. ```
  397.  
  398. \---
  399.  
  400. ## Mode Comparison
  401.  
  402. ```
  403. ┌───────────────┬──────────────┬──────────────┬───────────────┐
  404. │ │ SHORT-ONLY │ LONG-ONLY │ DUAL │
  405. ├───────────────┼──────────────┼──────────────┼───────────────┤
  406. │ Directions │ SHORT only │ LONG only │ BOTH │
  407. │ LLM calls/min │ 1 │ 1 │ 2 (parallel) │
  408. │ Entry logic │ LLM SHORT │ LLM LONG │ One trade + │
  409. │ │ + gates │ + gates │ one hold │
  410. │ Exit: thesis │ Same prompt │ Same prompt │ Same prompt │
  411. │ broken │ HOLD/CLOSE │ HOLD/CLOSE │ HOLD/CLOSE │
  412. │ Reversal? │ No │ No │ Yes (2×conf≥70│
  413. │ │ │ │ + anti-flip) │
  414. │ RTH size │ 10 │ 10 │ 5 │
  415. │ Overnight size│ 5 │ 5 │ 3 │
  416. │ Use when │ Bear regime │ Bull regime │ Uncertain / │
  417. │ │ │ │ transitional │
  418. └───────────────┴──────────────┴──────────────┴───────────────┘
  419. ```
  420.  
  421. ## Key Files
  422.  
  423. |File|Purpose|
  424. |-|-|
  425. |`engine/directional\\\\\\\_engine.py`|Short-only / long-only engine (base class)|
  426. |`engine/dual\\\\\\\_engine.py`|Dual prompt engine (extends DirectionalEngine)|
  427. |`ai/prompts.py`|`SYSTEM\\\\\\\_PROMPT\\\\\\\_SHORT\\\\\\\_ONLY`, `SYSTEM\\\\\\\_PROMPT\\\\\\\_LONG\\\\\\\_ONLY`|
  428. |`ai/trader.py`|LLM integration, `raw\\\\\\\_call()` for custom system prompts|
  429. |`dom/dom\\\\\\\_book.py`|Real-time order book via WebSocket|
  430. |`dom/dom\\\\\\\_analyzer.py`|DOM scoring + direction block logic|
  431. |`data/gex\\\\\\\_rh.py`|GEX from Robinhood options (NDX+SPX)|
  432. |`risk/manager.py`|Daily P\&L + trade gating|
  433. |`engine/scheduler.py`|Hard close, trading windows, DST-safe|
  434.  
  435.  
  436.  
Add Comment
Please, Sign In to add comment