GAP-031: fix submitted trade provenance
- serialize grade, score, ORB ticks, and resolved mode from accepted submission data - add exact-value JSONL provenance test - prepare governance for Gate Run 3 Build: clean | Tests: 498 passing | Dual-deployed: yes
This commit is contained in:
@@ -1433,7 +1433,7 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
double atrValue = ATR(Close, AtrPeriod)[0];
|
||||
atrStopTicks = System.Math.Round(atrValue / TickSize * AtrMultiplier);
|
||||
}
|
||||
WriteTradeProvenance(intent, request.Quantity, atrStopTicks);
|
||||
WriteTradeProvenance(intent, request.Quantity, atrStopTicks, resolvedMode);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -1757,7 +1757,7 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
/// <summary>
|
||||
/// Writes trade provenance JSONL line for each trade.
|
||||
/// </summary>
|
||||
protected virtual void WriteTradeProvenance(StrategyIntent intent, int quantity, double atrStopTicks)
|
||||
protected virtual void WriteTradeProvenance(StrategyIntent intent, int quantity, double atrStopTicks, RunnerMode resolvedMode)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -1772,29 +1772,20 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
var dir = LogDirectory;
|
||||
Directory.CreateDirectory(dir);
|
||||
|
||||
var trade = new Dictionary<string, object>
|
||||
{
|
||||
{ "session_id", _sessionId },
|
||||
{ "trade_seq", _tradeSequence },
|
||||
{ "timestamp_utc", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") },
|
||||
{ "symbol", intent.Symbol },
|
||||
{ "side", intent.Side.ToString() },
|
||||
{ "quantity", quantity },
|
||||
{ "stop_ticks", intent.StopTicks },
|
||||
{ "target_ticks", intent.TargetTicks },
|
||||
{ "atr_stop_ticks", atrStopTicks },
|
||||
{ "resolved_runner_mode", GetResolvedRunnerMode().ToString() },
|
||||
{ "confluence_grade", GetConfluenceGrade().ToString() },
|
||||
{ "confluence_score", GetConfluenceScore() },
|
||||
{ "risk_mode", GetRiskMode().ToString() },
|
||||
{ "daily_pnl_at_entry", GetDailyPnL() },
|
||||
{ "session_tag", GetSessionTag() },
|
||||
{ "code_version", GetCodeVersionStamp() }
|
||||
};
|
||||
|
||||
var safeName = Name.Replace(" ", "_").Replace("/", "_").Replace("\\", "_");
|
||||
var path = Path.Combine(LogDirectory, string.Format("trades_{0}_{1}.jsonl", Name.Replace(" ", "_"), DateTime.Now.ToString("yyyyMMdd")));
|
||||
var json = JsonConvert.SerializeObject(trade);
|
||||
var json = TradeProvenanceSerializer.Serialize(
|
||||
_sessionId,
|
||||
_tradeSequence,
|
||||
DateTime.UtcNow,
|
||||
intent,
|
||||
quantity,
|
||||
atrStopTicks,
|
||||
resolvedMode.ToString(),
|
||||
GetRiskMode(),
|
||||
GetDailyPnL(),
|
||||
GetSessionTag(),
|
||||
GetCodeVersionStamp());
|
||||
File.AppendAllText(path, json + Environment.NewLine);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -1857,32 +1848,6 @@ namespace NinjaTrader.NinjaScript.Strategies
|
||||
return testMode ? "TEST MODE ACTIVE" : "LIVE MODE";
|
||||
}
|
||||
|
||||
private RunnerMode GetResolvedRunnerMode()
|
||||
{
|
||||
// This would need to be called in context where request.Quantity is available
|
||||
return ResolveRunnerMode(RunnerActivationMode, 1);
|
||||
}
|
||||
|
||||
private string GetConfluenceGrade()
|
||||
{
|
||||
if (_strategyConfig != null && _strategyConfig.Parameters != null && _strategyConfig.Parameters.ContainsKey("confluence_grade"))
|
||||
{
|
||||
var grade = _strategyConfig.Parameters["confluence_grade"];
|
||||
if (grade != null) return grade.ToString();
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
private double GetConfluenceScore()
|
||||
{
|
||||
if (_strategyConfig != null && _strategyConfig.Parameters != null && _strategyConfig.Parameters.ContainsKey("confluence_weighted_score"))
|
||||
{
|
||||
var score = _strategyConfig.Parameters["confluence_weighted_score"];
|
||||
if (score is double) return (double)score;
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
private string GetRiskMode()
|
||||
{
|
||||
if (_strategyConfig != null && _strategyConfig.Parameters != null && _strategyConfig.Parameters.ContainsKey("risk_mode"))
|
||||
|
||||
100
src/NT8.Core/Logging/TradeProvenanceSerializer.cs
Normal file
100
src/NT8.Core/Logging/TradeProvenanceSerializer.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using NT8.Core.Common.Models;
|
||||
using NT8.Core.Intelligence;
|
||||
|
||||
namespace NT8.Core.Logging
|
||||
{
|
||||
/// <summary>
|
||||
/// Serializes submitted trade provenance from the accepted strategy intent.
|
||||
/// </summary>
|
||||
public static class TradeProvenanceSerializer
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates one JSONL payload for a submitted trade.
|
||||
/// </summary>
|
||||
public static string Serialize(
|
||||
string sessionId,
|
||||
int tradeSequence,
|
||||
DateTime timestampUtc,
|
||||
StrategyIntent intent,
|
||||
int quantity,
|
||||
double atrStopTicks,
|
||||
string resolvedRunnerMode,
|
||||
string riskMode,
|
||||
double dailyPnlAtEntry,
|
||||
string sessionTag,
|
||||
string codeVersion)
|
||||
{
|
||||
if (intent == null)
|
||||
throw new ArgumentNullException("intent");
|
||||
|
||||
string grade = "Unknown";
|
||||
double score = 0.0;
|
||||
double orbRangeTicks = 0.0;
|
||||
ConfluenceScore confluenceScore = null;
|
||||
|
||||
if (intent.Metadata != null)
|
||||
{
|
||||
if (intent.Metadata.ContainsKey("confluence_score"))
|
||||
confluenceScore = intent.Metadata["confluence_score"] as ConfluenceScore;
|
||||
|
||||
if (confluenceScore != null)
|
||||
{
|
||||
grade = confluenceScore.Grade.ToString();
|
||||
score = confluenceScore.WeightedScore;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (intent.Metadata.ContainsKey("trade_grade") && intent.Metadata["trade_grade"] != null)
|
||||
grade = intent.Metadata["trade_grade"].ToString();
|
||||
score = GetDouble(intent.Metadata, "confluence_weighted_score");
|
||||
}
|
||||
|
||||
orbRangeTicks = GetDouble(intent.Metadata, "orb_range_ticks");
|
||||
}
|
||||
|
||||
var trade = new Dictionary<string, object>();
|
||||
trade.Add("session_id", sessionId);
|
||||
trade.Add("trade_seq", tradeSequence);
|
||||
trade.Add("timestamp_utc", timestampUtc.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"));
|
||||
trade.Add("symbol", intent.Symbol);
|
||||
trade.Add("side", intent.Side.ToString());
|
||||
trade.Add("quantity", quantity);
|
||||
trade.Add("stop_ticks", intent.StopTicks);
|
||||
trade.Add("target_ticks", intent.TargetTicks);
|
||||
trade.Add("atr_stop_ticks", atrStopTicks);
|
||||
trade.Add("resolved_runner_mode", resolvedRunnerMode);
|
||||
trade.Add("confluence_grade", grade);
|
||||
trade.Add("confluence_score", score);
|
||||
trade.Add("OrbRangeTicks", orbRangeTicks);
|
||||
trade.Add("risk_mode", riskMode);
|
||||
trade.Add("daily_pnl_at_entry", dailyPnlAtEntry);
|
||||
trade.Add("session_tag", sessionTag);
|
||||
trade.Add("code_version", codeVersion);
|
||||
|
||||
return JsonConvert.SerializeObject(trade);
|
||||
}
|
||||
|
||||
private static double GetDouble(Dictionary<string, object> metadata, string key)
|
||||
{
|
||||
object value;
|
||||
if (!metadata.TryGetValue(key, out value) || value == null)
|
||||
return 0.0;
|
||||
|
||||
if (value is double)
|
||||
return (double)value;
|
||||
if (value is decimal)
|
||||
return (double)(decimal)value;
|
||||
if (value is float)
|
||||
return (double)(float)value;
|
||||
if (value is int)
|
||||
return (double)(int)value;
|
||||
if (value is long)
|
||||
return (double)(long)value;
|
||||
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user