mfn

architecture llm polymarket 2 feeding

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