# RTH Session Filter - Quick Fix Specification

**For:** Kilocode AI Agent  
**Priority:** URGENT  
**Mode:** Code Mode  
**Estimated Time:** 15-20 minutes  
**Files to Edit:** 1 file (NT8StrategyBase.cs)

---

## 🎯 Objective

Add session time filter to prevent trading during extended hours (ETH).
Only allow trades during Regular Trading Hours (RTH): 9:30 AM - 4:00 PM ET.

---

## 🔧 Fix: Add Session Filter to OnBarUpdate

**File:** `src/NT8.Adapters/Strategies/NT8StrategyBase.cs`

**Find the OnBarUpdate method** (around line 150):

```csharp
protected override void OnBarUpdate()
{
    if (!_sdkInitialized || _sdkStrategy == null)
    {
        if (CurrentBar == 0)
            Print(string.Format("[SDK] Not initialized: sdkInit={0}, strategy={1}", 
                _sdkInitialized, _sdkStrategy != null));
        return;
    }
    
    if (CurrentBar < BarsRequiredToTrade)
    {
        if (CurrentBar == 0)
            Print(string.Format("[SDK] Waiting for bars: current={0}, required={1}", 
                CurrentBar, BarsRequiredToTrade));
        return;
    }
    
    if (Time[0] == _lastBarTime)
        return;
```

**Add this session filter right after the BarsRequiredToTrade check:**

```csharp
protected override void OnBarUpdate()
{
    if (!_sdkInitialized || _sdkStrategy == null)
    {
        if (CurrentBar == 0)
            Print(string.Format("[SDK] Not initialized: sdkInit={0}, strategy={1}", 
                _sdkInitialized, _sdkStrategy != null));
        return;
    }
    
    if (CurrentBar < BarsRequiredToTrade)
    {
        if (CurrentBar == 0)
            Print(string.Format("[SDK] Waiting for bars: current={0}, required={1}", 
                CurrentBar, BarsRequiredToTrade));
        return;
    }
    
    // NEW: Session filter - only trade during RTH (9:30 AM - 4:00 PM ET)
    var currentTime = Time[0];
    var hour = currentTime.Hour;
    var minute = currentTime.Minute;
    
    // Convert to minutes since midnight for easier comparison
    var currentMinutes = (hour * 60) + minute;
    var rthStart = (9 * 60) + 30;  // 9:30 AM = 570 minutes
    var rthEnd = (16 * 60);         // 4:00 PM = 960 minutes
    
    if (currentMinutes < rthStart || currentMinutes >= rthEnd)
    {
        // Outside RTH - skip this bar
        if (CurrentBar == BarsRequiredToTrade)
        {
            Print(string.Format("[SDK] Outside RTH: {0:HH:mm} (RTH is 09:30-16:00)", currentTime));
        }
        return;
    }
    
    if (Time[0] == _lastBarTime)
        return;
```

---

## 🎯 Alternative: Add Property to Enable/Disable RTH Filter

If you want to make it configurable, add this property to the properties section:

```csharp
[NinjaScriptProperty]
[Display(Name = "RTH Only", GroupName = "SDK", Order = 2)]
public bool RthOnly { get; set; }
```

Then in `State.SetDefaults`:

```csharp
EnableSDK = true;
RthOnly = true;  // Default to RTH only
```

And update the session filter:

```csharp
// Session filter - only trade during RTH if enabled
if (RthOnly)
{
    var currentTime = Time[0];
    var hour = currentTime.Hour;
    var minute = currentTime.Minute;
    
    var currentMinutes = (hour * 60) + minute;
    var rthStart = (9 * 60) + 30;  // 9:30 AM
    var rthEnd = (16 * 60);         // 4:00 PM
    
    if (currentMinutes < rthStart || currentMinutes >= rthEnd)
    {
        if (CurrentBar == BarsRequiredToTrade)
        {
            Print(string.Format("[SDK] Outside RTH: {0:HH:mm} (RTH is 09:30-16:00)", currentTime));
        }
        return;
    }
}
```

---

## ✅ Verification

```bash
# Build
dotnet build src\NT8.Adapters\NT8.Adapters.csproj --configuration Release

# Deploy
.\deployment\Deploy-To-NT8.ps1
```

**In NT8 after recompile:**
- Run backtest again
- Check Output window
- Should see: `[SDK] Outside RTH: 22:15 (RTH is 09:30-16:00)`
- Should see intents ONLY during 9:30-16:00
- Should see actual filled trades in results

---

## 📋 Git Commit

```bash
git add src/NT8.Adapters/Strategies/NT8StrategyBase.cs
git commit -m "fix: Add RTH session filter to prevent ETH trading

- Only trade during 9:30 AM - 4:00 PM ET
- Add RthOnly property for configuration
- Log when bars are outside RTH
- Prevents order submission during extended hours

Fixes: Zero trades issue (was trading during ETH)"
```

---

**READY FOR KILOCODE - CODE MODE** ✅

**Time: 15-20 minutes**
