feat(oneshot): add Anti-Flip 5m microstructure execution engine
Introduces a complete, event-driven execution engine for 5-minute
Polymarket UP/DOWN markets, implementing the Anti-Flip strategy spec.
Architecture
────────────
• EventBus — central pub/sub bus connecting all services
• MarketFeedService — discovers 5m/15m markets via Gamma API, polls
CLOB orderbooks every 200–500ms, emits snapshots
• FeatureEngine — maintains a 15s rolling buffer per market and
computes midSlope6s, retrace3s, imbalance, spread,
depthTop3 for both UP and DOWN sides
• SignalEngine — hard gate checks (TTE, spread, depth, stale) then
weighted score (imbalance 35%, slope 35%,
spread 20%, retrace 10%) + trend confirmation
• ExecutionEngine — limit-marketable FOK buy, market-sell FOK exit,
GTC limit-sell for TP; dry-run short-circuits
• RiskEngine — consecutive loss cooldown, daily USDC loss cap,
session halt; all via explicit canTrade() gate
• PositionEngine — per-market position state, TP/adverse/slope/time
exit evaluation on every snapshot tick
• StateMachine — explicit state graph with guarded transitions:
IDLE → SETUP_READY → ORDER_PENDING → POSITION_OPEN
→ REDUCE_ONLY → IDLE | COOLDOWN | HALTED
• Telemetry — structured JSONL logger (data/oneshot_telemetry.jsonl)
recording decisions, orders, exits, and transitions
Runtime sequence (per market, per tick)
────────────────────────────────────────
A Ingest snapshot (MarketFeedService)
B Build features — rolling slope, retrace, imbalance (FeatureEngine)
C Hard gate check — TTE [25,120]s, spread ≤ 0.02, depth ≥ minTopSize
D Score + trend confirm → emit ENTER_LONG / ENTER_SHORT / NO_TRADE
E Submit FOK limit-marketable at bestAsk
F Fill handling — full fill / partial (reduce if TTE ≤ 25s) / timeout
G Position management — TP, adverse (2-tick), slope drop (4s), time exits
H Risk enforcement — P&L accounting, cooldown, daily halt
New scripts
───────────
npm run oneshot — live trading (DRY_RUN=false)
npm run oneshot-sim — simulation (DRY_RUN=true)
npm run oneshot-dev — sim + nodemon
New .env variables
──────────────────
ONESHOT_ASSETS, ONESHOT_DURATION, ONESHOT_POLL_INTERVAL_MS,
ONESHOT_BASE_RISK_USDC, ONESHOT_TP_TICKS, ONESHOT_SCORE_THRESHOLD,
ONESHOT_MIN_TOP_SIZE, ONESHOT_MAX_CONSEC_LOSSES,
ONESHOT_COOLDOWN_ROUNDS, ONESHOT_DAILY_LOSS_CAP, ONESHOT_FILL_TIMEOUT_MS
Co-Authored-By: direkturcrypto <direkturcrypto.x@mail3.me>
This commit is contained in:
@@ -124,3 +124,45 @@ SNIPER_PRICE=0.01
|
||||
# Shares per side — minimum Polymarket order size is 5 shares
|
||||
# At $0.01/share: 5 shares = $0.05 per side, $0.10 per market
|
||||
SNIPER_SHARES=5
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# ONESHOT ENGINE (oneshot.js / npm run oneshot-sim)
|
||||
# Anti-Flip 5m microstructure execution engine.
|
||||
# Evaluates book features on every tick and enters only when
|
||||
# momentum, depth, and spread conditions align.
|
||||
# ALWAYS test with DRY_RUN=true before going live.
|
||||
# ─────────────────────────────────────────────
|
||||
|
||||
# Comma-separated assets to monitor (e.g. btc,eth,sol)
|
||||
ONESHOT_ASSETS=btc
|
||||
|
||||
# Market duration: "5m" (5-minute) or "15m" (15-minute)
|
||||
ONESHOT_DURATION=5m
|
||||
|
||||
# Book poll interval in milliseconds (200–500ms recommended)
|
||||
ONESHOT_POLL_INTERVAL_MS=300
|
||||
|
||||
# USDC risk per trade — size = floor(ONESHOT_BASE_RISK_USDC / entryPrice), min 5 shares
|
||||
ONESHOT_BASE_RISK_USDC=5
|
||||
|
||||
# Take-profit in ticks above entry price (1 tick = tickSize, e.g. 0.01)
|
||||
ONESHOT_TP_TICKS=1
|
||||
|
||||
# Minimum composite score to trigger entry (0–1, higher = more selective)
|
||||
ONESHOT_SCORE_THRESHOLD=0.60
|
||||
|
||||
# Minimum shares at the best bid AND best ask for the depth hard gate
|
||||
ONESHOT_MIN_TOP_SIZE=10
|
||||
|
||||
# ── Risk settings ──────────────────────────────────────────────────────
|
||||
# Number of consecutive losses before entering cooldown
|
||||
ONESHOT_MAX_CONSEC_LOSSES=2
|
||||
|
||||
# Number of market slots to skip during cooldown
|
||||
ONESHOT_COOLDOWN_ROUNDS=3
|
||||
|
||||
# Maximum cumulative daily loss in USDC before halting all trading
|
||||
ONESHOT_DAILY_LOSS_CAP=20
|
||||
|
||||
# Maximum milliseconds to wait for a FOK fill ack (timeout → cancel → IDLE)
|
||||
ONESHOT_FILL_TIMEOUT_MS=800
|
||||
|
||||
Reference in New Issue
Block a user