[GAP-051]: micro upgrade advisory persistence complete

- NT8StrategyBase.cs: persist MicroUpgradeThreshold advisory state per account

- NT8StrategyBaseMicroUpgradeTests.cs: add source guards for persistence, corrupt files, and Account-null behavior

- SPRINT_BOARD.md and decisions.md: record Option A and verification evidence

Build: clean | Tests: 550 passing; Core 461 passing | Dual-deployed: no - skipped per operator instruction
This commit is contained in:
2026-08-25 10:14:23 -04:00
parent 7e82b2ce5a
commit 18877c376c
5 changed files with 302 additions and 1 deletions

View File

@@ -154,6 +154,7 @@ namespace NinjaTrader.NinjaScript.Strategies
private bool _orbRejectSummarySnapshotWritten;
private DateTime _rejectSummarySnapshotTradingDate = DateTime.MinValue;
private bool _microUpgradeSuggested;
private bool _microUpgradePersistenceLoaded;
private bool _trailingDrawdownHalted;
private string _trailingDrawdownAccountName;
@@ -521,6 +522,7 @@ namespace NinjaTrader.NinjaScript.Strategies
_killSwitchTriggered = false;
_connectionLost = false;
_microUpgradeSuggested = false;
_microUpgradePersistenceLoaded = false;
_trailingDrawdownHalted = false;
_trailingDrawdownAccountName = null;
@@ -597,6 +599,8 @@ namespace NinjaTrader.NinjaScript.Strategies
_sdkStrategy.SetParameters(dailyCountParams);
}
InitializeMicroUpgradeSuggestedState();
// TASK-00a: Write session provenance on realtime transition
WriteSessionProvenance();
@@ -853,7 +857,12 @@ namespace NinjaTrader.NinjaScript.Strategies
private void MonitorMicroAccountUpgrade()
{
if (_microUpgradeSuggested || Account == null || MicroUpgradeThreshold <= 0.0 || State != State.Realtime)
if (Account == null || MicroUpgradeThreshold <= 0.0 || State != State.Realtime)
return;
InitializeMicroUpgradeSuggestedState();
if (_microUpgradeSuggested)
return;
try
@@ -865,6 +874,7 @@ namespace NinjaTrader.NinjaScript.Strategies
string message = string.Format("TIER_UPGRADE_SUGGESTED: Balance={0:F2} crossed threshold={1:F2}; review RiskTier setting.", balance, MicroUpgradeThreshold);
Print(message);
FileLog(message);
WriteMicroUpgradeSuggestedFile(balance);
}
}
catch (Exception ex)
@@ -874,6 +884,74 @@ namespace NinjaTrader.NinjaScript.Strategies
}
}
private void InitializeMicroUpgradeSuggestedState()
{
if (_microUpgradePersistenceLoaded || Account == null || State != State.Realtime)
return;
_microUpgradePersistenceLoaded = true;
try
{
string path = GetMicroUpgradeSuggestedFilePath(Account.Name);
if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path))
return;
Dictionary<string, object> payload = JsonConvert.DeserializeObject<Dictionary<string, object>>(System.IO.File.ReadAllText(path));
if (payload != null && payload.ContainsKey("suggested") && payload["suggested"] is bool)
_microUpgradeSuggested = (bool)payload["suggested"];
}
catch (Exception ex)
{
_microUpgradeSuggested = false;
string message = string.Format("MICRO_UPGRADE_SUGGESTED warning: could not read persisted advisory state; advisory may re-fire. Error={0}", ex.Message);
Print(message);
FileLog(message);
}
}
private string GetMicroUpgradeSuggestedFilePath(string accountName)
{
if (string.IsNullOrEmpty(accountName))
return string.Empty;
string safeAccount = MakeSafeFileName(accountName);
string dir = ResolveLogDirectory(false, "micro upgrade advisory state");
return System.IO.Path.Combine(dir, string.Format("micro_upgrade_suggested_{0}.json", safeAccount));
}
private void WriteMicroUpgradeSuggestedFile(double balance)
{
if (Account == null)
return;
try
{
string path = GetMicroUpgradeSuggestedFilePath(Account.Name);
if (string.IsNullOrEmpty(path))
return;
string dir = System.IO.Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(dir))
System.IO.Directory.CreateDirectory(dir);
var payload = new Dictionary<string, object>();
payload.Add("account", Account.Name);
payload.Add("strategy_name", Name);
payload.Add("suggested", true);
payload.Add("suggested_timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
payload.Add("balance", balance);
payload.Add("threshold", MicroUpgradeThreshold);
System.IO.File.WriteAllText(path, JsonConvert.SerializeObject(payload, Formatting.Indented));
}
catch (Exception ex)
{
string message = string.Format("MICRO_UPGRADE_SUGGESTED warning: could not write persisted advisory state; advisory may re-fire after restart. Error={0}", ex.Message);
Print(message);
FileLog(message);
}
}
private bool CheckTrailingDrawdownMonitor()
{
if (!EnableTrailingDrawdown || Account == null || State != State.Realtime)