Skip to content

test: add exchange, strike, and session-flag unit tests - #105

Merged
kunalrbhatia merged 2 commits into
developmentfrom
fix/sensex-hardening-and-regression-tests
Aug 20, 2026
Merged

test: add exchange, strike, and session-flag unit tests#105
kunalrbhatia merged 2 commits into
developmentfrom
fix/sensex-hardening-and-regression-tests

Conversation

@kunalrbhatia

@kunalrbhatia kunalrbhatia commented Aug 20, 2026

Copy link
Copy Markdown
Owner

Incident & Hardening Summary

PR #104 (f395f7a) addressed three core issues encountered during the 20-Aug SENSEX launch:

  1. AB4046 Exchange Bug: doOrder defaulted exchange = 'NFO' while SENSEX options trade on BFO. Angel rejected orders with AB4046 Symbol token not found in scrip master cache for the given exchange. Fixed by resolving exchange from the trading symbol via getExchangeForIndex(getIndexFromSymbol(tradingsymbol)).
  2. Strike-parsing Regex Bug: updateLivePositions and paperTrade used anchored regex matching only NIFTY-style symbols. SENSEX numeric dates (SENSEX2682077400CE) failed to match, setting strike to '0' and triggering continuous straddle rolls every tick. Fixed with regex /(\d{5})([CP]E)$/.
  3. Session-Flag False-Positive: shortStraddle() marked the session opened unconditionally even on rejected orders. Fixed by guarding setStraddleOpenedToday to require both SELL legs to return typeof result === 'object' && result.status === true.

This PR adds complete unit tests, regression verification, code comments, and operational documentation.


Part A — Review PR #104 & Unit Test Coverage

Added robust unit tests in __tests__/helpers/apiService/:

  • Exchange Resolution (orders.test.ts):
    • doOrder without explicit exchange + SENSEX symbol (SENSEX2682077400CE) -> resolves BFO.
    • doOrder without explicit exchange + NIFTY symbol (NIFTY26AUG24200CE) -> resolves NFO.
    • doOrder with explicit exchange (CUSTOM_EXCH) -> preserves explicit exchange.
    • closeParticularTrade for SENSEX position -> sends close BUY order to BFO.
  • Strike Regex Vectors & Limitation Comment (positions.test.ts + positions.ts & paperTrade.ts):
    • NIFTY26AUG24200CE -> 24200 / CE
    • SENSEX2682077400CE -> 77400 / CE
    • SENSEX26AUG76800PE -> 76800 / PE
    • BANKNIFTY26AUG50000CE -> 50000 / CE
    • Added code comments documenting the known limitation for FPI symbols (e.g. NIFTYFPI25AUG261320PE parses as 61320 instead of 1320; algo never trades FPI segment).
  • Session Flag Guard (strategy.test.ts):
    • Both SELL legs { status: true } -> setStraddleOpenedToday called.
    • One SELL leg { status: false } -> flag NOT set.
    • Both SELL legs undefined -> flag NOT set, no error thrown.

Part B — NIFTY Tuesday Regression Evidence

  1. Regex Equivalence on Scrip Master:
    • Run over all 2,758 OPTIDX scrips starting with NIFTY (excluding FPI) in scripMaster.json.
    • 0 mismatches between old and new regexes.
    • All parsed strikes match the scrip master strike field (strike / 100).
  2. Exchange Resolution on NIFTY:
    • getExchangeForIndex('NIFTY') remains strictly 'NFO' (BFO returned only for SENSEX).
  3. Session Flag on NIFTY:
    • Strictly more correct: rejected NIFTY entry retries next tick instead of blocking execution.
  4. Backtest Baseline Sanity:
    • Checked baseline across 6-month NIFTY dataset (expiry-reports/backtest-2026-02-to-2026-08.md): ₹40,188.75 across expiry Tuesdays, 65.2% WR.

Part C — Code Gap Verification

  1. Legacy No-Hedge Roll Path: Confirmed shortStraddle(false) does NOT call setStraddleOpenedToday (flag is entry-only).
  2. placeStoplossForAllSells: Confirmed no hardcoded exchange: 'NFO' remains; doOrder resolves exchange dynamically from symbol.
  3. closeTrade: closeParticularTrade passes trade position to doOrder which resolves SENSEX symbols to BFO. Tested and verified.
  4. Strike Math: Grepped all occurrences; no other location derives SENSEX strike from regex string parsing that could collapse to '0'.

Part D — Gateway Collector Throttling (OPS Note)

Incident Root Cause: The gateway PM2 daemons optionchain-live and sensex-live poll SmartAPI every 10 seconds 24/7 using the same API key (K94372) as the algo, causing rate limit 403 errors during the first trading hour.

Recommended Ops Fix (Gateway Deployment):

  • Option 1 (Recommended): Update src/live.js and src/sensex-live.js sleep duration on gateway server from 10000 (10s) to 300000 (5 minutes).
  • Option 2: Keep daemons stopped during trading hours (09:15–15:40 IST Tue/Thu) and rely on the 5-min collect-once.js cron.

Verification Checklist

  • pnpm verify passed cleanly (prettier, eslint, tsc --noEmit, jest 276/276 tests).
  • Exchange resolution unit tests (4 vectors).
  • Strike regex unit tests (4 vectors + FPI comments).
  • Session flag unit tests (3 scenarios).
  • NIFTY regression verified (0 scrip master regex mismatches).
  • Pushed to fix/sensex-hardening-and-regression-tests.

@kunalrbhatia kunalrbhatia changed the title test: SENSEX hardening unit tests, NIFTY regression verification, and ops notes test: add exchange, strike, and session-flag unit tests Aug 20, 2026
@kunalrbhatia
kunalrbhatia merged commit dbf8255 into development Aug 20, 2026
26 of 28 checks passed
@kunalrbhatia
kunalrbhatia deleted the fix/sensex-hardening-and-regression-tests branch August 20, 2026 06:26
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