156 satır
6,5 KiloByte
MQL5
//+------------------------------------------------------------------+
| |||
//| ONNX Feature Check Part 4.mq5 |
| |||
//| Copyright 2026, Allan Munene Mutiiria. |
| |||
//| https://t.me/Forex_Algo_Trader |
| |||
//+------------------------------------------------------------------+
| |||
#property copyright "Copyright 2026, Allan Munene Mutiiria."
| |||
#property link "https://t.me/Forex_Algo_Trader"
| |||
#property version "1.00"
| |||
#property strict
| |||
#property description "Prove the terminal builds the same inputs the model was trained on"
| |||
| |||
#include "OMV Features.mqh"
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Inputs |
| |||
//+------------------------------------------------------------------+
| |||
input group "Check"
| |||
input string ProofFile = "ONNX Model Viewer Part 4\\model_proof.csv"; // Proof Path Inside The Shared Files Folder
| |||
input string FeedSymbol = "XAUUSDm"; // Symbol The Model Was Trained On
| |||
input ENUM_TIMEFRAMES FeedPeriod = PERIOD_M15; // Timeframe The Model Was Trained On
| |||
input int FeedBars = 4000; // Bars To Pull For The Comparison
| |||
input double Tolerance = 1e-6; // Largest Difference Treated As A Match
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Global Variables |
| |||
//+------------------------------------------------------------------+
| |||
COmvFeatures builder; // Builds the sixteen numbers from live bars
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Split one comma separated line into its parts |
| |||
//+------------------------------------------------------------------+
| |||
int SplitLine(const string line, string &parts[])
| |||
{
| |||
//--- Break the line wherever a comma appears
| |||
return StringSplit(line, ',', parts);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Compare the terminal's numbers against the written ones |
| |||
//+------------------------------------------------------------------+
| |||
void RunCheck(void)
| |||
{
| |||
//--- Open the numbers the training script wrote
| |||
const int handle = FileOpen(ProofFile, FILE_READ | FILE_TXT | FILE_ANSI | FILE_COMMON);
| |||
if(handle == INVALID_HANDLE)
| |||
{
| |||
Print("cannot open ", ProofFile, ", run the training script first");
| |||
return;
| |||
}
| |||
//--- Step over the header row, which names the columns
| |||
if(!FileIsEnding(handle))
| |||
FileReadString(handle);
| |||
//--- Track how the comparison is going
| |||
int rows = 0, matched = 0, missing = 0, failed = 0;
| |||
double worst = 0.0;
| |||
int worstAt = -1;
| |||
string worstStamp = "";
| |||
double byFeature[OF_INPUTS];
| |||
ArrayInitialize(byFeature, 0.0);
| |||
//--- Walk every written bar
| |||
while(!FileIsEnding(handle))
| |||
{
| |||
//--- Take the next line and break it apart
| |||
const string line = FileReadString(handle);
| |||
if(StringLen(line) < 8)
| |||
continue;
| |||
string parts[];
| |||
if(SplitLine(line, parts) != OF_INPUTS + 1)
| |||
continue;
| |||
rows++;
| |||
//--- Find the bar the terminal holds for this stamp
| |||
const datetime stamp = (datetime)StringToInteger(parts[0]);
| |||
const int at = builder.IndexOf(stamp);
| |||
if(at < 0)
| |||
{
| |||
missing++;
| |||
continue;
| |||
}
| |||
//--- Build the same sixteen numbers here
| |||
OmvFeatures mine;
| |||
if(!builder.Build(at, mine))
| |||
{
| |||
failed++;
| |||
continue;
| |||
}
| |||
//--- Compare them one at a time
| |||
bool same = true;
| |||
for(int k = 0; k < OF_INPUTS; k++)
| |||
{
| |||
//--- Measure how far apart the two are
| |||
const double gap = MathAbs(mine.value[k] - StringToDouble(parts[k + 1]));
| |||
if(gap > byFeature[k])
| |||
byFeature[k] = gap;
| |||
if(gap > worst)
| |||
{
| |||
worst = gap;
| |||
worstAt = k;
| |||
worstStamp = TimeToString(stamp, TIME_DATE | TIME_MINUTES);
| |||
}
| |||
if(gap > Tolerance)
| |||
same = false;
| |||
}
| |||
if(same)
| |||
matched++;
| |||
}
| |||
FileClose(handle);
| |||
//--- Report what the comparison found
| |||
PrintFormat("checked %d written bars against %s %s",
| |||
rows, FeedSymbol, EnumToString(FeedPeriod));
| |||
PrintFormat(" matched %d, missing from the terminal %d, could not build %d",
| |||
matched, missing, failed);
| |||
//--- Name the worst gap, since one bad feature is enough to spoil a prediction
| |||
if(worstAt >= 0)
| |||
PrintFormat(" largest gap %.3g on input %d at %s", worst, worstAt, worstStamp);
| |||
//--- List every input that drifted, so the culprit is named rather than guessed
| |||
for(int k = 0; k < OF_INPUTS; k++)
| |||
if(byFeature[k] > Tolerance)
| |||
PrintFormat(" input %2d differs by up to %.3g", k, byFeature[k]);
| |||
//--- Say plainly whether the model can be trusted with live bars
| |||
const int usable = rows - missing;
| |||
if(usable <= 0)
| |||
Print(" no written bar matched a bar the terminal holds, so check the symbol and timeframe");
| |||
else
| |||
if(matched == usable)
| |||
Print(" every input matches, so the terminal feeds the model what it was trained on");
| |||
else
| |||
PrintFormat(" %d of %d bars disagree, so the model would be fed inputs it never saw",
| |||
usable - matched, usable);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Expert initialization function |
| |||
//+------------------------------------------------------------------+
| |||
int OnInit()
| |||
{
| |||
//--- Take the bars the comparison will be built from
| |||
if(!builder.Load(FeedSymbol, FeedPeriod, FeedBars))
| |||
{
| |||
Print("cannot read bars: ", builder.Error());
| |||
return(INIT_FAILED);
| |||
}
| |||
PrintFormat("holding %d bars of %s %s", builder.Bars(), FeedSymbol, EnumToString(FeedPeriod));
| |||
//--- Compare them against what the training script wrote
| |||
RunCheck();
| |||
return(INIT_SUCCEEDED);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Expert deinitialization function |
| |||
//+------------------------------------------------------------------+
| |||
void OnDeinit(const int reason)
| |||
{
| |||
//--- Release nothing, since nothing was held open
| |||
return;
| |||
}
| |||
//+------------------------------------------------------------------+
|