Skip to content

fix: hedge-walk infinite retry for SENSEX caused duplicate hedges - #118

Merged
kunalrbhatia merged 2 commits into
developmentfrom
fix/hedge-loop-infinite-retry
Sep 3, 2026
Merged

kunalrbhatia merged 2 commits into
developmentfrom
fix/hedge-loop-infinite-retry

Conversation

@kunalrbhatia

Copy link
Copy Markdown
Owner

Summary

Fix the SENSEX hedge-walk infinite retry loop that caused duplicate hedge buys on 3-Sep.

Incident (3-Sep 2026, SENSEX expiry)

At 09:30 the algo entered shortStraddle(true) (ATM 76800). The CE hedge (78300, +1500) filled, but the PE hedge (75300) had LTP ~3.5 > 3 → doOrderByStrike returned false → the retry loop re-attempted the SAME strike forever because SENSEX strikeVariance was 0 (only NIFTY had 50). The straddle SELL legs never placed → straddleOpenedToday never set → every 5-min cron tick spawned a NEW process that ran shortStraddle(true) again → each bought ANOTHER CE hedge before blocking again. By 09:50: 5 CE-hedge BUY orders (78300 ×200, 78400 ×300 = 500 qty / 25 lots) instead of 100 qty / 5 lots.

Fix

  1. SENSEX hedge walk step = 100 (strikes are 100 apart; NIFTY stays 50): if the far hedge LTP > 3, walk further OTM to find a cheaper hedge instead of retrying the same strike.
  2. Attempt cap = 5 per hedge: after 5 walk attempts, log and proceed WITHOUT that hedge → the SELL legs place → straddleOpenedToday is set → duplicate entries prevented.
  3. Also resets strikeIncrement for the PE loop (was cumulatively inherited from CE).

Verification

  • Rebuilt dist 09:55; markers MAX_HEDGE_ATTEMPTS + walk logging present.
  • Excess hedges flattened via manual SELLs (78300 ×200, 78400 ×300) — broker verified 0 open SENSEX 03SEP positions.
  • Unit tests updated: SENSEX hedge with LTP > 3 walks to +100 strike and caps at 5 attempts; entry proceeds without hedge after cap.

Notes

  • This is the same class as the 27-Aug PE-hedge LTP issue; the LTP > 3 hedge-cost rule stays (user choice) but can no longer wedge the entry.
  • Next: consider a session-level guard so a single tick's shortStraddle can't be re-entered by the next cron tick if SELLs fail (defense-in-depth beyond straddleOpenedToday).

…ent)

SENSEX had strikeVariance=0 in shortStraddle hedge walk → when the far
PE hedge LTP was > 3, the retry loop re-attempted the SAME strike forever.
The SELL legs never placed → straddleOpenedToday never set → every cron
tick spawned a new process that bought ANOTHER CE hedge before blocking
again. Result: 500 qty (25 lots) of CE hedges instead of 100 (5 lots).

Fix: walk hedge strikes in 100-point steps for SENSEX (50 for NIFTY) and
cap hedge attempts at 5 — proceed with the straddle SELLs even if a hedge
can't be placed, so straddleOpenedToday gets set and duplicate entries
are prevented.

Live hotfix applied 09:55 IST (dist rebuilt); excess hedges flattened via
manual SELLs (broker-verified 0 open). Formalized per PR #104 pattern.
@kunalrbhatia kunalrbhatia changed the title fix: SENSEX hedge-walk infinite retry caused duplicate hedges fix: hedge-walk infinite retry for SENSEX caused duplicate hedges Sep 3, 2026
@kunalrbhatia
kunalrbhatia merged commit 2aa5463 into development Sep 3, 2026
10 of 12 checks passed
@kunalrbhatia
kunalrbhatia deleted the fix/hedge-loop-infinite-retry branch September 3, 2026 04: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