Files
nt8-sdk/TASK_02_EMERGENCY_KILL_SWITCH.md
2026-02-24 15:00:41 -05:00

3.3 KiB
Raw Blame History

Task 2 — Emergency Kill Switch

Priority: CRITICAL
Estimated time: 1.52 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:

// 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 OnStateChangeState.SetDefaults:

EnableKillSwitch = false;
EnableVerboseLogging = false;

Add kill switch check at the TOP of OnBarUpdate(), BEFORE any strategy logic:

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:

private bool _killSwitchTriggered = false;

Reset in OnStateChangeState.DataLoaded or State.Active:

_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