learning_ai_invt_trdg/backend/SESSION_RULE_FIX.md

93 lines
2.8 KiB
Markdown

# SessionRule Bug Fix — 2026-02-20
## Summary
A critical logic error in `SessionRule.ts` caused **all profiles configured as "24/7" to silently block trades** during Asian (TOK/SYD) market hours. This was a silent failure — no crash, no alert — the bot simply never entered during those hours.
---
## Root Cause
**File**: `src/strategies/rules/SessionRule.ts`
**Old logic (broken):**
```ts
if (isMajor && hasAllowedSession) {
// pass
}
// else: always fail
```
The old check required **both** conditions simultaneously:
1. `isMajor` — the internal flag marking LDN/NY as "major" sessions.
2. `hasAllowedSession` — the session being in the profile's allowed list.
**The problem:** `isMajor` is `false` during TOK/SYD regardless of how the profile is configured. So even when a profile explicitly set `sessions: "LDN,NY,TOK,SYD"` (24/7 mode), the condition `isMajor && hasAllowedSession` failed during TOK/SYD, producing:
```
❌ Mandatory Rule Failed: SessionRule -> Session TOK | SYD not in allowed set (24/7).
```
This was a misleading error — the session WAS in the allowed set, but `isMajor` overrode the check.
---
## Fix Applied
**New logic — 3-path decision tree:**
```
1. Is allowedSessions = [LDN, NY, TOK, SYD]?
→ YES → 24/7 mode. ALWAYS PASS. Skip time checks.
2. Is current session = OFF (market closed)?
→ YES → BLOCK.
3. Is current session in the profile's allowedSessions list?
→ YES → PASS (restricted schedule, currently within window)
→ NO → BLOCK
```
**Key change:** Removed dependency on `context.isMajorSession` entirely. The session check is now driven purely by the profile's configuration, not by an internal "major session" flag.
---
## Session Mapping (Frontend Wizard → Backend)
| Wizard Selection | `SessionRule.params.sessions` | Backend Behavior |
|:---|:---|:---|
| 24/7 | `LDN,NY,TOK,SYD` | Detected as 24/7 → always passes |
| London + New York | `LDN,NY` | Passes only during LDN/NY hours |
| Asia only | `TOK,SYD` | Passes only during TOK/SYD hours |
---
## Expected Log After Fix
```
✅ Rule Passed: SessionRule -> 24/7 mode: all sessions allowed. Current session: TOK | SYD.
```
vs. the old broken log:
```
❌ Mandatory Rule Failed: SessionRule -> Session TOK | SYD not in allowed set (24/7).
```
---
## Impact Assessment
| Metric | Before Fix | After Fix |
|:---|:---|:---|
| Trades during LDN/NY | ✅ Working | ✅ Working |
| Trades during TOK/SYD (24/7 profiles) | ❌ Silently blocked | ✅ Now allowed |
| Restricted session profiles (LDN,NY only) | ✅ Working | ✅ Working |
| Capital at risk | None (no trades were placed) | Normal |
---
## Files Modified
- `src/strategies/rules/SessionRule.ts` — Core fix