Skip to content

test: add SENSEX hardening unit tests and fix market close exit window - #108

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

test: add SENSEX hardening unit tests and fix market close exit window#108
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 Summary & Hardening Review

This PR adds unit tests, closes remaining operational and code gaps, and provides regression verification for the 20-Aug-2026 SENSEX expiry launch incident.

Key Gaps Closed & Improvements Made:

  1. Market Close Exit Window Bug (isMarketClosed):

    • isMarketClosed() previously hardcoded { hours: 15, minutes: 30 } as market close cutoff.
    • On 20-Aug, when EXIT_TIME was set past 15:30 (e.g. 15:35), isMarketClosed() evaluated to true, causing isTradeAllowed() to fail with Conditions not right... Reasons: Market closed. Because executeTrade() was gated behind isTradeAllowed(), closeTrade() never ran automatically.
    • Fix: isMarketClosed() now evaluates upper bound using appConfig.exitTime (defaulting to 15:40 F&O close bound). checkMarketConditionsAndExecuteTrade also allows execution if past exit time so trade close is never blocked by market entry gates.
  2. Roll Decision Logging & Strikeprice Data-Integrity Guard (repeatShortStraddle):

    • Added comprehensive roll decision input logging (difference, strikeDiff, isSameStrikeAlreadyTraded, cepe_present, previousTradeStrikePrice, atmStrike) before any branching decisions.
    • Added explicit data-integrity guards: if any open position carries strikeprice === '0' or unparseable strike, or if previousTradeStrikePrice === 0, the roll decision is skipped with a warning.
    • Updated updateLivePositions so existing positions also refresh strikeprice and optiontype if previously invalid.
  3. NODE_ENV / Jest Execution Gate for File Writes:

    • Gated writeToFile in src/helpers/logger.ts and scrip master cache file writes in src/helpers/apiService/marketData.ts on process.env.NODE_ENV !== 'test'.
    • Running Jest tests locally or in CI no longer corrupts production log files or overwrites scripMaster.json.
  4. Exchange Resolution & Code Commenting:

    • Confirmed doOrder exchange resolution (exchange || getExchangeForIndex(...)) is applied only as fallback when exchange is omitted.
    • Documented the known FPI regex limitation (NIFTYFPI25AUG261320PE parsing strike as 61320 in FPI segment) in code comments.

NIFTY Regression Evidence

  1. Regex Equivalence on NIFTY:
    • Verified across all OPTIDX symbols starting with NIFTY (excluding FPI) against scripMaster.json (156,812 scrips):
    • 0 mismatches found across 2,758 NIFTY option symbols between the old anchored regex /^([A-Z]+)(\d{2}[A-Z]{3}\d{2})(\d+\.?\d*)([CP]E)$/ and the new suffix regex /(\d{5})([CP]E)$/.
  2. Exchange Resolution on NIFTY:
    • getExchangeForIndex('NIFTY') returns 'NFO'.
  3. Session Flag on NIFTY:
    • Behavior is strictly more accurate: failed NIFTY SELL legs leave straddleOpenedToday as false to enable retry on the next tick.
  4. Full Test Suite Verification:
    • pnpm verify suite (pnpm format; pnpm lint; pnpm typecheck; pnpm test; pnpm build) runs 100% green (24 test suites, 277 tests passing).

Collector Throttling (Ops / Deployment Note)

  • Issue: optionchain-live and sensex-live PM2 daemons poll SmartAPI every 10s using API key K94372, causing rate limit 403 HTTP errors during live algo order placement.
  • Ops Recommendation: Increase gateway collector polling sleep in src/live.js and src/sensex-live.js from 10s (10000) to 5 minutes (300000), or rely on the 5-minute collect-once.js cron during expiry day trading windows (9:15–15:40 IST).

@kunalrbhatia
kunalrbhatia merged commit 5561ddc into development Aug 20, 2026
14 checks passed
@kunalrbhatia
kunalrbhatia deleted the fix/sensex-hardening-and-regression-tests branch August 20, 2026 14:48
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