# Task 2 — Emergency Kill Switch **Priority:** CRITICAL **Estimated time:** 1.5–2 hours **Depends on:** Task 1 (INT8ExecutionBridge.FlattenAll must exist) **Status:** TODO --- ## Problem There is no way to stop a running strategy and flatten positions from the NinjaTrader UI without killing the entire application. `BasicOrderManager.FlattenAll()` exists in the SDK core but nothing surfaces it as a controllable NT8 strategy parameter. --- ## What Needs to Change ### File: `src/NT8.Adapters/Strategies/NT8StrategyBase.cs` Add two new NinjaScript properties: ```csharp // Kill switch — set to true in NT8 UI to flatten everything and stop trading [NinjaScriptProperty] [Display(Name = "Kill Switch (Flatten & Stop)", GroupName = "Emergency Controls", Order = 1)] public bool EnableKillSwitch { get; set; } // Logging verbosity toggle [NinjaScriptProperty] [Display(Name = "Verbose Logging", GroupName = "Debug", Order = 1)] public bool EnableVerboseLogging { get; set; } ``` Set defaults in `OnStateChange` → `State.SetDefaults`: ```csharp EnableKillSwitch = false; EnableVerboseLogging = false; ``` Add kill switch check at the TOP of `OnBarUpdate()`, BEFORE any strategy logic: ```csharp protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBar < BarsRequiredToTrade) return; // Emergency kill switch — check FIRST, before anything else if (EnableKillSwitch) { if (!_killSwitchTriggered) { _killSwitchTriggered = true; Print(string.Format("[NT8-SDK] KILL SWITCH ACTIVATED at {0}. Flattening all positions.", Time[0])); try { ExitLong("EmergencyFlatten"); ExitShort("EmergencyFlatten"); } catch (Exception ex) { Print(string.Format("[NT8-SDK] Error during emergency flatten: {0}", ex.Message)); } } return; // Do not process any more bar logic } // ... rest of OnBarUpdate } ``` Add the tracking field: ```csharp private bool _killSwitchTriggered = false; ``` Reset in `OnStateChange` → `State.DataLoaded` or `State.Active`: ```csharp _killSwitchTriggered = false; ``` --- ## Acceptance Criteria - [ ] `EnableKillSwitch` appears as a checkbox in the NT8 strategy parameter dialog under "Emergency Controls" - [ ] Setting `EnableKillSwitch = true` on a running strategy causes `ExitLong` and `ExitShort` to fire on the next bar - [ ] Once triggered, no new entries are made (strategy returns early every bar) - [ ] A `Print()` message confirms the activation with timestamp - [ ] Setting kill switch back to `false` does NOT re-enable trading in the same session (once triggered, stays triggered) - [ ] `EnableVerboseLogging` is exposed in parameter dialog under "Debug" - [ ] `verify-build.bat` passes --- ## Files to Modify | File | Action | |---|---| | `src/NT8.Adapters/Strategies/NT8StrategyBase.cs` | Add `EnableKillSwitch`, `EnableVerboseLogging` params; add kill switch logic to `OnBarUpdate()` | | `src/NT8.Adapters/Strategies/SimpleORBNT8.cs` | Ensure `EnableKillSwitch` is inherited (no changes needed if base class handles it) | --- ## Do NOT Change - Any Core layer files - Any test files - Strategy logic in `SimpleORBStrategy.cs`