[GAP-022-FOLLOWUP]: connection-state seam complete
- src/NT8.Adapters/NinjaTrader/ConnectionStateMonitor.cs: added plain testable connection-state monitor - src/NT8.Adapters/Strategies/NT8StrategyBase.cs: wired monitor and added pre-submit ConnectionLost check - tests/NT8.Core.Tests/Scenarios/ScenarioTests.cs: added disconnect scenario coverage - SPRINT_BOARD.md: GAP-022-FOLLOWUP implemented/deployed - docs/00-governance/current_status.md: updated - docs/00-governance/decisions.md: added DEC-20260826-07 Build: clean | Tests: 580 passing | Dual-deployed: yes, backup 20260826_011650
This commit is contained in:
69
src/NT8.Adapters/NinjaTrader/ConnectionStateMonitor.cs
Normal file
69
src/NT8.Adapters/NinjaTrader/ConnectionStateMonitor.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
|
||||
namespace NT8.Adapters.NinjaTrader
|
||||
{
|
||||
/// <summary>
|
||||
/// Tracks broker connection state for entry gating without depending on NinjaScript types.
|
||||
/// </summary>
|
||||
public class ConnectionStateMonitor
|
||||
{
|
||||
private readonly object _lock = new object();
|
||||
private bool _connectionLost;
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the broker connection is currently lost.
|
||||
/// </summary>
|
||||
public bool IsConnectionLost
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return _connectionLost;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Records that the broker connection is currently connected.
|
||||
/// </summary>
|
||||
/// <returns>True when this call changed state from disconnected to connected.</returns>
|
||||
public bool ReportConnected()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
bool changed = _connectionLost;
|
||||
_connectionLost = false;
|
||||
return changed;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Records that the broker connection is currently disconnected.
|
||||
/// </summary>
|
||||
/// <returns>True when this call changed state from connected to disconnected.</returns>
|
||||
public bool ReportDisconnected()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
bool changed = !_connectionLost;
|
||||
_connectionLost = true;
|
||||
return changed;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a new entry may proceed, logging the standard rejection when disconnected.
|
||||
/// </summary>
|
||||
public bool TryAllowNewEntry(DateTime signalTime, Action<string, string, DateTime> rejectLogger)
|
||||
{
|
||||
if (!IsConnectionLost)
|
||||
return true;
|
||||
|
||||
if (rejectLogger != null)
|
||||
rejectLogger("ConnectionLost", "Broker connection lost; new orders halted", signalTime);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -133,7 +133,7 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
private int _ordersSubmittedToday;
|
||||
private DateTime _lastBarTime;
|
||||
private bool _killSwitchTriggered;
|
||||
private bool _connectionLost;
|
||||
private readonly ConnectionStateMonitor _connectionStateMonitor = new ConnectionStateMonitor();
|
||||
private bool _realtimeBarSeen;
|
||||
private bool _breakevenMoved;
|
||||
private string _scalerSignalName;
|
||||
@@ -523,7 +523,7 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
EnableStateTelemetry = true; // DASH-002: additive realtime observability
|
||||
EnableEthTelemetry = false; // Read-only ETH telemetry is opt-in only
|
||||
_killSwitchTriggered = false;
|
||||
_connectionLost = false;
|
||||
_connectionStateMonitor.ReportConnected();
|
||||
_microUpgradeSuggested = false;
|
||||
_microUpgradePersistenceLoaded = false;
|
||||
_trailingDrawdownHalted = false;
|
||||
@@ -743,11 +743,10 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
}
|
||||
|
||||
// Connection loss guard — do not submit new orders if broker is disconnected
|
||||
if (_connectionLost)
|
||||
if (!_connectionStateMonitor.TryAllowNewEntry(Time[0], LogRejectRateLimited))
|
||||
{
|
||||
if (EnableVerboseLogging)
|
||||
Print(string.Format("[NT8-SDK] Bar skipped — connection lost: {0}", Time[0]));
|
||||
LogRejectRateLimited("ConnectionLost", "Broker connection lost; new orders halted", Time[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1415,9 +1414,8 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
|
||||
if (connectionStatusUpdate.Status == ConnectionStatus.Connected)
|
||||
{
|
||||
if (_connectionLost)
|
||||
if (_connectionStateMonitor.ReportConnected())
|
||||
{
|
||||
_connectionLost = false;
|
||||
Print(string.Format("[NT8-SDK] Connection RESTORED at {0} — trading resumed.",
|
||||
DateTime.Now.ToString("HH:mm:ss")));
|
||||
FileLog(string.Format("CONNECTION RESTORED at {0}", DateTime.Now.ToString("HH:mm:ss")));
|
||||
@@ -1426,9 +1424,8 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
else if (connectionStatusUpdate.Status == ConnectionStatus.Disconnected ||
|
||||
connectionStatusUpdate.Status == ConnectionStatus.ConnectionLost)
|
||||
{
|
||||
if (!_connectionLost)
|
||||
if (_connectionStateMonitor.ReportDisconnected())
|
||||
{
|
||||
_connectionLost = true;
|
||||
Print(string.Format("[NT8-SDK] Connection LOST at {0} — halting new orders. Status={1}",
|
||||
DateTime.Now.ToString("HH:mm:ss"),
|
||||
connectionStatusUpdate.Status));
|
||||
@@ -1630,7 +1627,7 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
RiskPerTrade));
|
||||
FileLog(string.Format("Sizing : MinContracts={0} MaxContracts={1}", MinContracts, MaxContracts));
|
||||
FileLog(string.Format("VerboseLog : {0} FileLog: {1}", EnableVerboseLogging, EnableFileLogging));
|
||||
FileLog(string.Format("ConnectionLost : {0}", _connectionLost));
|
||||
FileLog(string.Format("ConnectionLost : {0}", _connectionStateMonitor.IsConnectionLost));
|
||||
WriteHistoricalReadiness("DATA LOADED");
|
||||
FileLog("---");
|
||||
}
|
||||
@@ -2085,6 +2082,9 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_connectionStateMonitor.TryAllowNewEntry(Time[0], LogRejectRateLimited))
|
||||
return;
|
||||
|
||||
if (_trailingDrawdownHalted || (EnableTrailingDrawdown && Account != null && TrailingDrawdownMonitor.Instance.IsHalted(Account.Name)))
|
||||
{
|
||||
_trailingDrawdownHalted = true;
|
||||
|
||||
Reference in New Issue
Block a user