USER (hermes, glm-5.1)
Read the unified strategy source code at /home/ubuntu/bots/Claude_bot/unified_strategy.py and extract the full entry/exit logic. I need to understand:
1. What SMMA crossover conditions trigger an entry (BUY/SELL)?
2. What RSI gap filter conditions are applied?
3. What are the exit conditions (SL, RSI exit, SMMA crossover exit, EOD square-off)?
4. What is the stop-loss / risk management logic?
5. How does MANUAL_EXIT flag work?
6. What lot size / quantity is used per symbol?
7. What are the exact symbol names and exchanges?
Also read /home/ubuntu/bots/Claude_bot/config.py to understand:
- Broker config and symbol mappings
- Strategy parameters (SMMA period, RSI periods, etc.)
Output a clear summary of all the entry/exit rules and parameters.
2026-04-28T23:03
ASSISTANT (hermes, glm-5.1)
Here is the complete summary of the entry/exit logic and parameters extracted from the source code:
---
## STRATEGY OVERVIEW
**Name:** RSI + Monday SMMA Strategy (v4)
**Symbol:** Trades options (CE/PE) on NSE futures underlyings
**Order type:** LIMIT orders only, NRML product type, NFO exchange
---
## 1. SMMA CROSSOVER ENTRY CONDITIONS
There are **two independent triggers** (either can fire a trade):
**Trigger A — SMMA2 Crossover (checked on 5-min candle close):**
- SMMA(2) is calculated on 5-min futures candle closes (rolling Wilder-style smoothing)
- The "line in stone" is the **Monday SMMA(2)** calculated from daily OPEN prices, locked every Monday at 9:16 AM for the entire week
- A **crossover** is detected when SMMA(2) on 5-min crosses from BELOW to ABOVE the Monday SMMA (bullish), or ABOVE to BELOW (bearish)
- If crossover occurs AND RSI directional gap > 3 → immediate BUY_CALL (above) or BUY_PUT (below)
- If crossover occurs but RSI gap <= 3 AND RSI is already in the same direction → sets `smma_crossover_pending=True`, waits for RSI gap to widen (checked on both 5-min and 15-min cycles)
- If crossover occurs but RSI gap <= 3 AND RSI is NOT in same direction → no pending set, entry is blocked
**Trigger B — RSI Direction Crossover (checked on 15-min candle close):**
- When RSI20(closes of LOW prices) crosses RSI120(closes of HIGH prices) from one side to the other
- AND SMMA(2) is already on the **same side** as the new RSI direction
- AND RSI directional gap > 3
- → BUY_CALL (if ABOVE) or BUY_PUT (if BELOW)
**Monday Startup Alignment:**
- On Monday at startup, if both SMMA2 and RSI are already aligned (SMMA2 side matches, gap > 3), the bot auto-enters without waiting for a fresh crossover
- If holding the wrong direction (e.g. bearish but now bullish aligned), it closes and reverses
---
## 2. RSI GAP FILTER CONDITIONS
**RSI Calculation:**
- RSI(20) computed on 15-min candle LOW prices (fast momentum)
- RSI(120) computed on 15-min candle HIGH prices (slow trend)
- Both use Wilder smoothing (exponential)
**Directional Gap:**
- For ABOVE (bullish): gap = RSI20 - RSI120
- For BELOW (bearish): gap = RSI120 - RSI20
**Thresholds:**
- **Entry (fresh):** gap must be > **3** (`RSI_ENTRY_GAP = 3`)
- **RSI gap exit:** gap drops to <= 3 (see below)
- **RSI re-entry after RSI exit:** gap must be > **4** (stricter)
- **SL re-entry:** gap must be > **3** (same as entry)
---
## 3. EXIT CONDITIONS
**A. SMMA2 Cross-Back Exit (highest priority when in position):**
- Checked on every 5-min candle close
- If SMMA(2) crosses to the **wrong side** of Monday SMMA while in position (e.g., holding BULLISH but SMMA2 drops below Monday SMMA) → exit immediately
- On exit: clears `manual_exit_detected`, clears `rsi_exit_pending`, resets `rsi_reentry_count` is NOT reset (stays as is)
- Re-entry after this exit requires a **fresh SMMA2 crossover** (gap > 3)
**B. RSI Gap Exit:**
- Checked on every 15-min candle close while in position
- If directional RSI gap drops to <= 3 → exit
- Sets `rsi_exit_pending=True`, records `rsi_exit_futures_price` (futures LTP at exit time) and direction
- Re-entry threshold is raised to gap > 4 (see re-entry below)
- Max 2 re-entries per original SMMA2 crossover entry
**C. Stop-Loss Exit:**
- Checked **every poll cycle** (60 sec) while in position
- If unrealized P&L < -INR 2200 (`STOP_LOSS_AMOUNT = 2200`) → exit
- Sets `sl_exit_pending=True` with direction
- SL re-entry fires **at most once** per session (`sl_reentry_used` flag)
**D. Friday EOD Square-Off:**
- Every Friday at 15:28 (`FRIDAY_SQUAREOFF_TIME = 15:28`), any open position is closed with reason "friday_eod"
**E. Manual Telegram Square-Off:**
- `/squareoff <name>` or `/squareoff ALL` closes positions immediately
- Sets `manual_exit_detected = True`
---
## 4. STOP-LOSS / RISK MANAGEMENT LOGIC
- **Stop-loss amount:** INR 2,200 per position (`STOP_LOSS_AMOUNT = 2200`)
- Calculated as: `(current_option_LTP - option_entry_price) * quantity`
- If P&L drops below -2200, position is exited cleanly
- After SL exit, the bot watches for **SL re-entry**: if RSI gap re-expands > 3, SMMA2 still on correct side, and RSI hasn't flipped → re-enter (once per session max)
- If RSI flips or SMMA2 crosses wrong side after SL exit, SL re-entry is cancelled
- **2-lot cap:** Before entry, bot checks total positions for the underlying. If combined qty >= lot_size * 2, entry is blocked
- **Max trades per day:** 10 per profile
- **Post-exit cooldown:** Currently set to **0 minutes** (`POST_EXIT_COOLDOWN_MINUTES = 0`), effectively disabled
- **Whipsaw threshold:** 7 minutes (`WHIPSAW_THRESHOLD_MINUTES = 7`) — but cooldown is 0, so it has no effect currently
- **Emergency cover:** If an exit order fails to confirm, the bot buys an opposite-direction option as a hedge, then sends Telegram alerts for manual resolution
---
## 5. MANUAL_EXIT FLAG
- `manual_exit_detected` is set to `True` when the **reconcile()** call discovers the bot thinks it has a position but the broker shows qty=0 (i.e., someone manually closed it)
- Also set on Telegram `/squareoff`
- When `manual_exit_detected = True`:
- **RSI gap re-entry is blocked** (won't auto-re-enter)
- **SL re-entry is blocked**
- The bot requires a **fresh SMMA2 crossover** (gap > 3) to unlock trading again
- When a fresh SMMA2 crossover fires, `manual_exit_detected` is cleared to `False`
---
## 6. LOT SIZE / QUANTITY PER SYMBOL
| Profile | lot_size | max_lots | Quantity (lot_size x max_lots) |
|---|---|---|---|
| DLF_SMMA | 825 | 1 | **825** |
| HAL_SMMA | 150 | 1 | **150** |
| INFY_SMMA | 400 | 1 | **400** |
| INDUSINDBK_SMMA | 700 | 1 | **700** |
All profiles use `max_lots = 1`. Combined position cap is 2 * lot_size per underlying.
Option strike selection: **1 ITM** (`itm_offset = 1`) for all profiles.
---
## 7. EXACT SYMBOL NAMES AND EXCHANGES
| Profile | Broker | Futures Symbol | Underlying | Exchange | Expiry |
|---|---|---|---|---|---|
| DLF_SMMA | FLATTRADE | DLF28APR26FUT | DLF | NFO | 28APR26 |
| HAL_SMMA | FLATTRADE | HAL28APR26FUT | HAL | NFO | 28APR26 |
| INFY_SMMA | SHOONYA | INFY28APR26FUT | INFY | NFO | 28APR26 |
| INDUSINDBK_SMMA | SHOONYA | INDUSINDBK28APR26FUT | INDUSINDBK | NFO | 28APR26 |
Note: HAL_SMMA is **disabled** (`"enabled": False` in config).
---
## RE-ENTRY LOGIC SUMMARY
| Exit Type | Re-entry Condition | Re-entry Threshold | Max Re-entries | Additional Checks |
|---|---|---|---|---|
| SMMA2 cross-back | Needs fresh SMMA2 crossover | gap > 3 | N/A (new sequence) | Resets rsi_reentry_count = 0 |
| RSI gap exit | SMMA2 same side + gap widened + better price | gap > 4 | 2 per SMMA2 entry | Futures price must be more favorable than exit price (higher for CALL, lower for PUT) |
| Stop-loss exit | SMMA2 same side + gap > 3 | gap > 3 | 1 per session | RSI must not have flipped |
| Manual exit | Needs fresh SMMA2 crossover | gap > 3 | N/A | Clears manual_exit_detected flag |
---
## KEY STRATEGY PARAMETERS (constants)
| Parameter | Value | Description |
|---|---|---|
| `SMMA2_PERIOD` | 2 | SMMA period on 5-min closes |
| `RSI_ENTRY_GAP` | 3 | Min RSI directional gap for entry |
| RSI exit gap | 3 (implicit) | Exit when gap <= 3 |
| RSI re-entry gap | 4 (hardcoded) | Re-entry after RSI gap exit needs gap > 4 |
| `STOP_LOSS_AMOUNT` | 2200 | INR stop-loss threshold |
| `LIMIT_BUFFER` | 3.0 | INR buffer on LIMIT orders (buy: LTP+3, sell: LTP-3) |
| `POLL_INTERVAL` | 60 sec | Main loop polling interval |
| `SMMA2_CANDLE_INTERVAL` | 300 sec | 5-min candle interval |
| `CANDLE_FETCH_INTERVAL` | 300 sec | How often to fetch new candles |
| `CANDLE_DAYS_LOOKBACK` | 20 | Calendar days of history |
| `MIN_CANDLES_FOR_RSI` | 122 | Minimum candles needed for RSI(120) calculation |
| `WARMUP_DAYS` | 15 | Daily candle lookback for Monday SMMA |
| `POST_EXIT_COOLDOWN_MINUTES` | 0 | Currently disabled |
| `WHIPSAW_THRESHOLD_MINUTES` | 7 | Reference only (no effect with cooldown=0) |
| `TRADING_START_TIME` | 09:16 | Market open |
| `TRADING_END_TIME` | 15:30 | Market close |
| `FRIDAY_SQUAREOFF_TIME` | 15:28 | Friday forced close |
---
## BROKER CONFIG
- **SHOONYA**: OpenAlgo host at `https://shoonya.openalgo.theworkpc.com`
- **FLATTRADE**: OpenAlgo host at `https://flattrade.openalgo.theworkpc.com`
- Both use API keys defined in config (redacted in this summary)
2026-04-28T23:03