Recovery

Partial Fill Recovery

When a box spread aborts mid-way with partial fills, instead of blindly reversing every filled leg (paying the bid-ask spread twice on each one), the bot predicts the short-term market direction from recent underlying spot prices and makes a smart decision:

Operational audit: /audit_positions is a read-only command that compares tracked bot trades with Dhan broker positions. It reports tracked legs that appear flat or missing, broker orphan positions, and quantity mismatches without placing orders. During the normal scan cycle, ReconciliationService consumes the same audit output: tracked-flat trades are removed only after two consecutive flat cycles, known orphan strategy shapes can be adopted into open_trades, and quantity mismatches block new entries until /confirm_recon <tag> acknowledges them.
📈 Market going UP
Keep BUY legs with trailing stop & take profit.
Close SELL legs immediately at market price.
📉 Market going DOWN
Keep SELL legs with trailing stop & take profit.
Close BUY legs at market. If no SELL legs → fresh ATM PUT.

Direction signal — two-gate model

Spot price is recorded every 30 s scan cycle. predict_direction() applies two gates in sequence. Both must pass to issue an UP or DOWN signal.

Gate 1 — Point-to-point

Compare spot_now vs spot_10_ago. Move must exceed RECOVERY_MIN_TREND_PCT (0.15%). Fast to compute — eliminates obviously flat markets.

Gate 2 — Momentum consistency

Count how many of the last 9 tick intervals moved in the same direction. At least RECOVERY_CONSISTENCY_THRESHOLD (60%) must agree. Filters spikes that fool the point-to-point gate. When rejected → cheaper full reversal at mid instead of wrong directional bet.

# Gate 1: point-to-point move
change = (spot_now − spot_10_ago) / spot_10_ago   # RECOVERY_DIRECTION_LOOKBACK = 10
if abs(change) < 0.15%: return "FLAT"            # RECOVERY_MIN_TREND_PCT = 0.0015

# Gate 2: momentum consistency
ticks   = [spot[i+1] − spot[i] for i in last_9_intervals]
up_frac = count(t > 0) / len(ticks)
if change > 0 and up_frac >= 0.60: return "UP"   # RECOVERY_CONSISTENCY_THRESHOLD
if change < 0 and (1−up_frac) >= 0.60: return "DOWN"
return "FLAT"  # trend exists but ticks inconsistent → don't bet
Backtest result (11 real trades, Apr 16–17) OLD (LTP+0.4% abort): −₹2,034  ·  NEW (anchor + delta recovery): +₹6,519  ·  MOMENTUM (+ consistency gate): +₹6,755 — ₹236 better than NEW
The consistency gate adds ~₹20–26 per partial-fill trade by avoiding wrong directional bets on noisy signals.

Recovery Simulation Demo

All 9 permutations — pick any combination of partial fill state × market direction:

📈 UP
📉 DOWN
➡ FLAT
1/4
[BUY CE]
2/4
[BUY CE + SELL CE]
3/4
[BUY CE + SELL CE + BUY PE]

Decision flowchart

Partial fill detected (N/4 legs filled) │ ├── Predict direction from spot history │ ├── UP (spot rose ≥ 0.15% over last 10 readings) │ │ ├── Keep BUY legs → register RECOVERY trade │ │ └── Close SELL legs at MARKET immediately │ │ │ ├── DOWN (spot fell ≥ 0.15% over last 10 readings) │ │ ├── Keep SELL legs → register RECOVERY trade │ │ ├── Close BUY legs at MARKET │ │ └── No SELL legs? → Fresh ATM PUT at MARKET │ │ │ └── FLAT (move < 0.15%) │ └── Full _auto_reverse (safe default, no position) │ └── RECOVERY trade monitored every 30 s scan cycle ├── Update peak LTP each cycle (trailing stop ratchets up) ├── Close if LTP < peak × (1 − trailing_stop%) └── Close if LTP > entry × (1 + take_profit%)