Skip to content

feat: implement real-time websocket exit engine daemon - #115

Merged
kunalrbhatia merged 7 commits into
developmentfrom
feat/websocket-real-time-exit
Sep 1, 2026
Merged

kunalrbhatia merged 7 commits into
developmentfrom
feat/websocket-real-time-exit

Conversation

@kunalrbhatia

Copy link
Copy Markdown
Owner

Motivation & Problem

  • 403 Rate-Limit Wars (20-Aug, 25-Aug): REST LTP polling bursts hit rate limits on shared API keys. Angel SmartStream WebSocket LTP push does NOT consume the REST rate window.
  • 5-Minute Exit Latency: 1-Sep incident demonstrated that a 5-minute cron poll can miss rapid premium spikes (e.g. NIFTY 24000 CE breached 130.8 trigger sub-second, but REST cron only closed it at 131.65 five minutes later).
  • CAS Pre-Auction Window (15:15–15:35): Premium doubling can occur between 5-minute REST ticks.

Key Changes

  • WebSocket Feed (src/helpers/apiService/marketFeed.ts): Connects to Angel WebSocketV2 using login feedToken, auto-reconnects with exponential backoff (2s, 4s, 8s, up to 60s), and normalizes raw tokens (normalizeToken).
  • Real-Time Exit Daemon (src/marketFeedDaemon.ts): PM2-supervised background process that subscribes to all position tokens + index spot (NIFTY 99926000 or SENSEX spot), tracks LTPs in memory, recomputes MTM on every tick, and triggers closeBreachedLegs immediately on per-leg stoploss breach (reusing PR fix: guard shouldExitDueToStoploss against zero entry price and ltp #111 zero-guard and PR feat: close only breached leg on stoploss #113 selective leg exit logic).
  • Fast-Path MTM (src/helpers/apiService/positions.ts): Extended getMtm with an optional latestPrices map for sub-millisecond in-memory MTM evaluation without REST calls.
  • PM2 & CI Integration (ecosystem.config.cjs, .github/workflows/ci.yml): Added smart-api-feed PM2 app with automatic startOrReload during deployment.
  • Watchdog Fallback: Retained the existing 5-minute cron watchdog as a secondary safety net.

Verification

  • Full verification suite (pnpm format; pnpm lint; pnpm typecheck; pnpm test; pnpm build) passed cleanly.
  • Unit tests added in __tests__/helpers/apiService/marketFeed.test.ts and __tests__/marketFeedDaemon.test.ts (306/306 tests passing).
  • Smoke test verified against WebSocketV2 API connection and token normalization.

@kunalrbhatia
kunalrbhatia merged commit 36abf17 into development Sep 1, 2026
14 checks passed
@kunalrbhatia
kunalrbhatia deleted the feat/websocket-real-time-exit branch September 1, 2026 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant