Files
nt8-sdk/tests/NT8.Core.Tests/Adapters/SimpleORBNT8DefaultsTests.cs

165 lines
10 KiB
C#

using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace NT8.Core.Tests.Adapters
{
[TestClass]
public class SimpleORBNT8DefaultsTests
{
[TestMethod]
public void SetDefaults_DirectionFlags_EnableLongAndShortTrades()
{
string source = File.ReadAllText(FindSourceFile());
string setDefaults = ExtractSetDefaultsBlock(source);
Assert.IsTrue(setDefaults.Contains("EnableLongTrades = true;"), "SetDefaults must enable long trades by default.");
Assert.IsTrue(setDefaults.Contains("EnableShortTrades = true;"), "SetDefaults must enable short trades by default per DEC-20260814-04.");
Assert.IsFalse(setDefaults.Contains("EnableShortTrades = false;"), "SetDefaults must not disable short trades by default.");
}
[TestMethod]
public void OnStateChange_BaseRunsBeforeOrbSetDefaultsAssignments()
{
string source = File.ReadAllText(FindSourceFile());
string method = ExtractMethodBlock(source, "protected override void OnStateChange");
int baseCall = method.IndexOf("base.OnStateChange();", StringComparison.Ordinal);
Assert.IsTrue(baseCall >= 0, "OnStateChange must invoke the base implementation.");
Assert.IsTrue(baseCall < method.IndexOf("RunnerTrailTicks = 20;", StringComparison.Ordinal), "Base defaults must run before the subclass trail default.");
Assert.IsTrue(baseCall < method.IndexOf("MinTradeGrade = 5;", StringComparison.Ordinal), "Base defaults must run before the subclass grade default.");
Assert.IsTrue(baseCall < method.IndexOf("MaxOpenPositions = 2;", StringComparison.Ordinal), "Base defaults must run before the subclass position default.");
Assert.IsTrue(baseCall < method.IndexOf("BarsRequiredToTrade = 50;", StringComparison.Ordinal), "Base defaults must run before the subclass warm-up default.");
}
[TestMethod]
public void BuildStateTelemetryIntent_DailyContext_UsesDirectionCorrectClone()
{
string source = File.ReadAllText(FindSourceFile());
string method = ExtractMethodBlock(source, "protected override StrategyIntent BuildStateTelemetryIntent");
Assert.IsTrue(method.Contains("CloneDailyBarContextForTelemetry((DailyBarContext)dailySource, side, barData, context)"), "Telemetry must clone daily context and set clone-only fields.");
Assert.IsTrue(method.Contains("metadata.Add(\"daily_bars\", telemetryDaily);"), "Telemetry must attach the cloned daily context.");
Assert.IsTrue(method.Contains("metadata.Add(\"orb_range_ticks\", telemetryDaily.OrbRangeTicks);"), "Telemetry must expose ORB range metadata for OrbRangeVsAtr.");
Assert.IsFalse(method.Contains("metadata.Add(\"daily_bars\", _strategyConfig.Parameters[\"daily_bars\"]);"), "Telemetry must not attach the shared daily context directly.");
}
[TestMethod]
public void CloneDailyBarContextForTelemetry_UsesTelemetrySideAndSessionOpen()
{
string source = File.ReadAllText(FindSourceFile());
string method = ExtractMethodBlock(source, "private static DailyBarContext CloneDailyBarContextForTelemetry");
Assert.IsTrue(method.Contains("daily.TradeDirection = side == OrderSide.Buy ? 1 : -1;"), "Telemetry clone must derive direction from the synthetic intent side.");
Assert.IsTrue(method.Contains("context.CustomData.ContainsKey(\"session_open_price\")"), "Telemetry clone must prefer session_open_price over current bar open.");
Assert.IsTrue(method.Contains("daily.TodayOpen = (double)sessionOpen;"), "Telemetry clone must apply the session open value.");
Assert.IsTrue(method.Contains("context.CustomData.ContainsKey(\"avg_volume\")"), "Telemetry clone must mirror trade-path avg_volume context.");
Assert.IsTrue(method.Contains("daily.BreakoutBarVolume = (double)barData.Volume;"), "Telemetry clone must use current bar volume for breakout-volume scoring.");
}
[TestMethod]
public void OnBarUpdate_DailyContextOrbRange_UsesSdkOpeningRangeTracker()
{
string source = File.ReadAllText(FindSourceFile());
string method = ExtractMethodBlock(source, "protected override void OnBarUpdate");
Assert.IsTrue(method.Contains("SdkSimpleORB sdkOrb = _sdkStrategy as SdkSimpleORB;"), "Daily context must read the live SDK-side ORB tracker.");
Assert.IsTrue(method.Contains("double orbHigh = (sdkOrb != null && sdkOrb.OpeningRangeReady) ? sdkOrb.OpeningRangeHigh : 0.0;"), "Daily context must use SDK ORB high only after the range is ready.");
Assert.IsTrue(method.Contains("double orbLow = (sdkOrb != null && sdkOrb.OpeningRangeReady) ? sdkOrb.OpeningRangeLow : 0.0;"), "Daily context must use SDK ORB low only after the range is ready.");
Assert.IsTrue(method.Contains("orbRangeTicks = Math.Round((orbHigh - orbLow) / tickSize);"), "Daily context must compute ORB range ticks from SDK high/low.");
Assert.IsFalse(source.Contains("_openingRangeHigh"), "Legacy adapter ORB high field must stay removed because the SDK tracker owns this state.");
Assert.IsFalse(source.Contains("_openingRangeLow"), "Legacy adapter ORB low field must stay removed because the SDK tracker owns this state.");
}
[TestMethod]
public void StateTelemetryContext_IncludesAvwapTrendInputsWithRequiredFormatting()
{
string source = File.ReadAllText(FindSourceFile());
string method = ExtractMethodBlock(source, "protected override Dictionary<string, string> GetStateTelemetryContextFields");
Assert.IsTrue(method.Contains("AddStateContextDouble(fields, context, \"Avwap\", \"avwap\", \"F2\");"), "STATE_CONTEXT must include avwap as Avwap with F2 formatting.");
Assert.IsTrue(method.Contains("AddStateContextDouble(fields, context, \"AvwapSlope\", \"avwap_slope\", \"F4\");"), "STATE_CONTEXT must include avwap_slope as AvwapSlope with F4 formatting.");
Assert.IsTrue(method.Contains("AddStateContextDouble(fields, context, \"TrendConfirm\", \"trend_confirm\", \"F1\");"), "STATE_CONTEXT must include trend_confirm as TrendConfirm with F1 formatting.");
Assert.IsFalse(method.Contains("TrendConfirm") && method.Contains("0.0"), "STATE_CONTEXT must not add placeholder TrendConfirm values inside the context method.");
}
[TestMethod]
public void StateTelemetryContext_OmitsAvwapTrendInputsWhenCustomDataKeysMissing()
{
string source = File.ReadAllText(FindSourceFile());
string helper = ExtractMethodBlock(source, "private static void AddStateContextDouble");
Assert.IsTrue(helper.Contains("context.CustomData == null"), "Telemetry helper must tolerate missing CustomData.");
Assert.IsTrue(helper.Contains("!context.CustomData.ContainsKey(customDataKey)"), "Telemetry helper must omit missing CustomData keys.");
Assert.IsTrue(helper.Contains("return;"), "Telemetry helper must return without placeholders for missing keys.");
Assert.IsFalse(helper.Contains("fields.Add(outputKey, \""), "Telemetry helper must not emit placeholder string values.");
}
[TestMethod]
public void StateTelemetryContext_DoesNotChangeScoringOrStateLineFormats()
{
string source = File.ReadAllText(FindSourceFile());
string method = ExtractMethodBlock(source, "protected override Dictionary<string, string> GetStateTelemetryContextFields");
Assert.IsFalse(source.Contains("context.CustomData[\"trend_confirm\"] ="), "SimpleORBNT8 telemetry must not change trend_confirm calculation.");
Assert.IsFalse(method.Contains("STATE:"), "GAP-061 must not change STATE line formatting.");
Assert.IsFalse(method.Contains("STATE_DETAIL:"), "GAP-061 must not change STATE_DETAIL line formatting.");
}
private static string FindSourceFile()
{
DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
while (dir != null)
{
string candidate = Path.Combine(dir.FullName, "src", "NT8.Adapters", "Strategies", "SimpleORBNT8.cs");
if (File.Exists(candidate))
return candidate;
dir = dir.Parent;
}
throw new FileNotFoundException("Could not locate src\\NT8.Adapters\\Strategies\\SimpleORBNT8.cs from test output directory.");
}
private static string ExtractSetDefaultsBlock(string source)
{
const string marker = "if (State == State.SetDefaults)";
int start = source.IndexOf(marker, StringComparison.Ordinal);
if (start < 0)
Assert.Fail("SimpleORBNT8.cs does not contain the SetDefaults state block.");
int end = source.IndexOf("else if (State == State.Configure)", start, StringComparison.Ordinal);
if (end < 0)
Assert.Fail("SimpleORBNT8.cs does not contain the Configure state block after SetDefaults.");
return source.Substring(start, end - start);
}
private static string ExtractMethodBlock(string source, string signature)
{
int start = source.IndexOf(signature, StringComparison.Ordinal);
if (start < 0)
Assert.Fail("SimpleORBNT8.cs does not contain method: " + signature);
int brace = source.IndexOf("{", start, StringComparison.Ordinal);
if (brace < 0)
Assert.Fail("SimpleORBNT8.cs method has no opening brace: " + signature);
int depth = 0;
for (int i = brace; i < source.Length; i++)
{
if (source[i] == '{')
depth++;
else if (source[i] == '}')
{
depth--;
if (depth == 0)
return source.Substring(start, i - start + 1);
}
}
Assert.Fail("SimpleORBNT8.cs method has no closing brace: " + signature);
return string.Empty;
}
}
}