forked from animatedread/Warrior_EA
The //| box blocks were excluded from0b06f8eand5efdb48and were what remained: 160 of them ran to 10+ lines, the longest to 88. Compressed to their leading topic sentences - 5 lines for a function header, 8 for a file header - keeping the box format and the standard MQL5 name/author lines verbatim. Verified at the BYTE level this time, across every in-scope file: the list of non-comment lines is byte-identical to HEAD and braces balance. The first check compared a locale-decoded 'git show' against a UTF-8 read and flagged 25 files that had not changed at all - every BOM and every non-ASCII line mismatched. 47,696 -> 40,665 lines in scope; comment share 38% -> 26%. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
595 lines
32 KiB
MQL5
595 lines
32 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| Warrior_EA |
|
|
//| AnimateDread |
|
|
//| |
|
|
//| Live continual learning, the EMA shadow net, and the OOS continu |
|
|
//+------------------------------------------------------------------+
|
|
#ifndef WARRIOR_AIBASE_ONLINELEARNING_MQH
|
|
#define WARRIOR_AIBASE_ONLINELEARNING_MQH
|
|
//+------------------------------------------------------------------+
|
|
//| Clones the just-converged Net into a separate CNet (m_simOosNet) |
|
|
//| and arms a chunked bar-by-bar walk through the OOS window - see |
|
|
//| AdvanceOosSimulationChunk(). Evaluation-only: the clone's learned |
|
|
//| weights are never written back to Net or any persisted file. |
|
|
//+------------------------------------------------------------------+
|
|
void CExpertSignalAIBase::StartOosContinualSimulation(int bars, int oosCutoff)
|
|
{
|
|
if(m_simOosRunActive)
|
|
{
|
|
delete m_simOosNet;
|
|
m_simOosNet = NULL;
|
|
m_simOosRunActive = false;
|
|
}
|
|
if(oosCutoff <= 0)
|
|
return; // nothing to walk this run
|
|
//--- Clone via the full Save()/Load() pair. Load() calls InitOpenCL()/InitDirectML() before
|
|
//--- reconstructing layers, so a bare "new CNet(NULL)" ends up with a GPU/DirectML backend
|
|
//--- matching production.
|
|
string simFile = m_activeFileName + "_simoos.tmp";
|
|
int simFlags = m_activeFileCommon ? FILE_COMMON : 0;
|
|
double ip[];
|
|
if(!Net.Save(simFile, 0.0, 0.0, 0.0, dtStudied, m_activeFileCommon, m_eraCount, m_trainingComplete, ip))
|
|
return;
|
|
m_simOosNet = new CNet(NULL);
|
|
double loadE, loadU, loadF;
|
|
datetime loadTime;
|
|
long loadEra;
|
|
bool loadComplete;
|
|
double loadIp[];
|
|
bool loaded = m_simOosNet.Load(simFile, loadE, loadU, loadF, loadTime, m_activeFileCommon, loadEra, loadComplete, loadIp, true /*quiet: this evaluation-only sim is optional - on a miss it simply doesn't run*/);
|
|
FileDelete(simFile, simFlags);
|
|
if(!loaded)
|
|
{
|
|
delete m_simOosNet;
|
|
m_simOosNet = NULL;
|
|
return;
|
|
}
|
|
m_simOosCutoff = oosCutoff;
|
|
m_simOosBarIndex = oosCutoff - 1;
|
|
m_simOosForecast = 0;
|
|
m_simOosSamples = 0;
|
|
m_simOosRunActive = true;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Advances the evaluation-only continual-learning OOS walk by up |
|
|
//| to TRAIN_TIME_BUDGET_MS of work, then yields (same chunking |
|
|
//| pattern as the real era loop's m_eraResumePending). |
|
|
//+------------------------------------------------------------------+
|
|
void CExpertSignalAIBase::AdvanceOosSimulationChunk(void)
|
|
{
|
|
const uint SIM_TIME_BUDGET_MS = 80;
|
|
uint chunkStartTick = GetTickCount();
|
|
//--- Mirror OnlineLearnStep()'s pinned rate for the duration of this chunk, and hand the shared
|
|
//--- global back on BOTH exit paths - this simulation is only a valid forecast of live continual
|
|
//--- learning if it steps at the same size, and `g_eta` is shared by every signal instance.
|
|
double savedEta = g_eta;
|
|
g_eta = m_modelEta * ONLINE_LEARN_ETA_SCALE;
|
|
int i;
|
|
for(i = m_simOosBarIndex; i >= 0; i--)
|
|
{
|
|
if(GetTickCount() - chunkStartTick >= SIM_TIME_BUDGET_MS)
|
|
{
|
|
m_simOosBarIndex = i;
|
|
g_eta = savedEta;
|
|
return;
|
|
}
|
|
if(i >= ArraySize(m_labelCacheHasValue) || !m_labelCacheHasValue[i])
|
|
continue; // no cached label for this bar (e.g. right at a window edge) - nothing to learn from
|
|
//--- Window ends AT (includes) bar i - see Train()'s matching r declaration comment for why.
|
|
int r = i;
|
|
if(!BuildFeatureWindow(r))
|
|
continue;
|
|
m_simOosNet.feedForward(TempData);
|
|
m_simOosNet.getResults(TempData);
|
|
double simSignal = (m_outputNeuronsCount == 3) ? ApplyClassificationSoftmax() : TempData[0];
|
|
//--- Pre-update softmax probabilities, read before TempData is rebuilt as the target vector -
|
|
//--- feeds the same alpha-balanced focal weight the live path applies (OnlineSampleWeight).
|
|
double sBuy = (TempData.Total() > 0) ? TempData.At(0) : 0.0;
|
|
double sSell = (TempData.Total() > 1) ? TempData.At(1) : 0.0;
|
|
double sNeutral = (TempData.Total() > 2) ? TempData.At(2) : 0.0;
|
|
bool buy = m_labelCacheBuy[i];
|
|
bool sell = m_labelCacheSell[i];
|
|
ENUM_SIGNAL trueSignal = buy ? Buy : (sell ? Sell : Neutral);
|
|
bool hit = (DoubleToSignal(simSignal) == trueSignal);
|
|
m_simOosSamples++;
|
|
if(hit)
|
|
m_simOosForecast += (100 - m_simOosForecast) / Net.recentAverageSmoothingFactor;
|
|
else
|
|
m_simOosForecast -= m_simOosForecast / Net.recentAverageSmoothingFactor;
|
|
TempData.Clear();
|
|
if(m_outputNeuronsCount == 1)
|
|
TempData.Add(buy && !sell ? 1 : !buy && sell ? -1 : 0);
|
|
else
|
|
if(m_outputNeuronsCount == 3)
|
|
{
|
|
TempData.Add(buy ? LABEL_SMOOTH_HIGH : LABEL_SMOOTH_LOW);
|
|
TempData.Add(sell ? LABEL_SMOOTH_HIGH : LABEL_SMOOTH_LOW);
|
|
TempData.Add((!buy && !sell) ? LABEL_SMOOTH_HIGH : LABEL_SMOOTH_LOW);
|
|
}
|
|
m_simOosNet.backProp(TempData, OnlineSampleWeight(trueSignal, sBuy, sSell, sNeutral));
|
|
}
|
|
g_eta = savedEta;
|
|
delete m_simOosNet;
|
|
m_simOosNet = NULL;
|
|
m_simOosRunActive = false;
|
|
Print(ID + ": continual-learning OOS simulation complete - " + IntegerToString(m_simOosSamples) + " samples, accuracy " + DoubleToString(m_simOosForecast, 1) + "%");
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Arms the one-shot pattern-database backfill (see the declaration |
|
|
//| comment). Called right after FinalizeTrainRun() has restored the |
|
|
//| DEPLOYED checkpoint, so the walk below scores with the exact |
|
|
//| weights that are about to trade live - not the last era's, which |
|
|
//| the plateau ladder may have superseded. |
|
|
//+------------------------------------------------------------------+
|
|
void CExpertSignalAIBase::StartPatternDatabaseBackfill(int bars, int totalIter, int oosCutoff)
|
|
{
|
|
if(m_dbBackfillDone || m_dbBackfillActive)
|
|
return;
|
|
if(!UseDatabaseRanking || MQLInfoInteger(MQL_TESTER) || MQLInfoInteger(MQL_OPTIMIZATION) || MQLInfoInteger(MQL_FORWARD))
|
|
return;
|
|
if(GetFilterID() == "NULL" || oosCutoff <= 0 || CheckPointer(Net) == POINTER_INVALID)
|
|
return;
|
|
//--- READS THE CALIBRATION BAND - never the window pass 3 grades.
|
|
int calibLo = CalibLoIndex(oosCutoff);
|
|
int calibHi = CalibHiIndex(totalIter, oosCutoff);
|
|
if(CalibBandBars(totalIter, oosCutoff) <= 0 || calibHi <= calibLo)
|
|
{
|
|
m_dbBackfillDone = true;
|
|
Print(ID + ": pattern-database backfill SKIPPED - this era carved no calibration band (study"
|
|
" window too short for OOS + two " + IntegerToString(CalibPurgeBars()) + "-bar purges + a"
|
|
" band). Filter weights will build from real fills instead. Lengthen the study period or"
|
|
" lower the OOS split % to enable it.");
|
|
return;
|
|
}
|
|
//--- ONE-SHOT ACROSS ATTACHES, not merely across this object's lifetime. m_dbBackfillDone is an
|
|
//--- in-memory flag, so every later attach that trains this configuration through to convergence
|
|
//--- again would walk the same OOS bars and write a second full set of rows - RegisterSignal()
|
|
//--- (Expert\ExpertSignalCustom.mqh) inserts unconditionally, with no key and no duplicate check.
|
|
//--- The ranking would then be counting the SAME bar several times, once per model that ever
|
|
//--- deployed here, weighting a superseded model's opinion exactly as heavily as the live one's.
|
|
//--- The marker stamps the era whose weights were used, so a redeploy of the same era is skipped
|
|
//--- and a genuinely retrained model (different era) is allowed through; reset-weights deletes it
|
|
//--- alongside the other sidecars (see LoadAndCompareTopologyConfiguration's discard block).
|
|
long deployedEra = (m_ensembleMember && g_ensBestEra >= 0) ? g_ensBestEra : (long)m_eraCount;
|
|
int markerFlags = m_activeFileCommon ? FILE_COMMON : 0;
|
|
string markerFile = m_activeFileName + ".dbfill";
|
|
if(FileIsExist(markerFile, markerFlags))
|
|
{
|
|
//--- FILE_SHARE_READ|FILE_SHARE_WRITE on every open, without exception - a sibling chart holding
|
|
//--- this file open must not turn a skip-check into a hard failure (see the optimizer-cache
|
|
//--- corruption this rule came from).
|
|
int mh = FileOpen(markerFile, markerFlags | FILE_TXT | FILE_READ | FILE_SHARE_READ | FILE_SHARE_WRITE);
|
|
if(mh != INVALID_HANDLE)
|
|
{
|
|
long stampedEra = StringToInteger(FileReadString(mh));
|
|
FileClose(mh);
|
|
if(stampedEra == deployedEra)
|
|
{
|
|
m_dbBackfillDone = true;
|
|
PrintVerbose(ID + ": pattern-database backfill already done for era " +
|
|
IntegerToString((int)deployedEra) + " - skipping (its rows are still in the DB).");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
m_dbBackfillEra = deployedEra;
|
|
dbm.OpenDatabase();
|
|
//--- Frozen batch-norm statistics, exactly like pass 3's OOS scoring walk - see its comment for why
|
|
//--- an unfrozen forward pass would let the running stats drift while scoring.
|
|
Net.SetBatchNormFrozen(true);
|
|
m_dbBackfillBars = bars;
|
|
//--- Walks [calibLo, calibHi) from its OLDEST bar down to its newest - i.e. oldest -> newest in
|
|
//--- TIME, which is the order ProcessSignal's outdated-row guard requires.
|
|
m_dbBackfillStartIndex = (int)MathMin(calibHi - 1, bars - MathMax(m_historyBars, 0) - 2);
|
|
m_dbBackfillStopIndex = (int)MathMax(2, calibLo);
|
|
m_dbBackfillIndex = m_dbBackfillStartIndex;
|
|
m_dbBackfillFired = 0;
|
|
m_dbBackfillActive = (m_dbBackfillStartIndex >= m_dbBackfillStopIndex);
|
|
if(!m_dbBackfillActive)
|
|
m_dbBackfillDone = true; // OOS window too short to walk - nothing to backfill, don't retry forever
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Time-boxed slice of the backfill walk - same chunking doctrine as |
|
|
//| every other long walk in this file (AdvanceOosSimulationChunk, |
|
|
//| AdvanceChartSignalRescan): a real forward pass per bar is genuine |
|
|
//| compute, so this yields on a wall-clock budget rather than running |
|
|
//| the whole OOS window in one call. Walks OLDEST -> NEWEST (mirrors |
|
|
//| pass 3's own m_oosScoreIndex descent) because ProcessSignal()'s |
|
|
//| outdated-row guard rejects a registration OLDER than a row its |
|
|
//| table already holds - inserting newest-first would have every |
|
|
//| older row rejected the instant the first one landed. |
|
|
//+------------------------------------------------------------------+
|
|
void CExpertSignalAIBase::AdvancePatternDatabaseBackfill(void)
|
|
{
|
|
const uint DB_BACKFILL_TIME_BUDGET_MS = 80;
|
|
uint chunkStartTick = GetTickCount();
|
|
dbm.BeginTransaction();
|
|
//--- ConfidenceTier() reads the live dPrevSignal field (the panel/RefreshLatestSignal's source of
|
|
//--- truth) - borrowed per bar below to get the SAME tier bucketing a live vote would have used,
|
|
//--- then restored so this backfill walk never leaks into the live-facing signal.
|
|
double savedPrevSignal = dPrevSignal;
|
|
for(; m_dbBackfillIndex >= m_dbBackfillStopIndex; m_dbBackfillIndex--)
|
|
{
|
|
if(GetTickCount() - chunkStartTick >= DB_BACKFILL_TIME_BUDGET_MS)
|
|
break;
|
|
int oi = m_dbBackfillIndex;
|
|
if(!(oi < (int)(m_dbBackfillBars - MathMax(m_historyBars, 0) - 1) &&
|
|
oi < ArraySize(m_labelCacheHasValue) && m_labelCacheHasValue[oi]))
|
|
continue;
|
|
if(!BuildFeatureWindow(oi) || !Net.feedForward(TempData))
|
|
continue;
|
|
Net.getResults(TempData);
|
|
double oSignal = (m_outputNeuronsCount == 3) ? ApplyClassificationSoftmax() : TempData[0];
|
|
double oDeploySignal = (m_outputNeuronsCount == 3) ? AdjustedSignalFromSoftmax() : oSignal;
|
|
ENUM_SIGNAL dir = DoubleToSignal(oDeploySignal);
|
|
if(dir != Buy && dir != Sell)
|
|
continue; // Neutral/abstained - live voting would not have buffered a row for this bar either
|
|
dPrevSignal = oDeploySignal;
|
|
int tier = ConfidenceTier();
|
|
bool winLong = (oi < ArraySize(m_winLongCache)) ? m_winLongCache[oi] : false;
|
|
bool winShort = (oi < ArraySize(m_winShortCache)) ? m_winShortCache[oi] : false;
|
|
bool tradeWon = (dir == Buy) ? winLong : winShort;
|
|
double atr = m_ATR.Main(oi);
|
|
double closeAt = m_Close.GetData(oi);
|
|
if(!MathIsValidNumber(atr) || atr <= 0.0 || !MathIsValidNumber(closeAt) || closeAt <= 0.0)
|
|
continue;
|
|
//--- Same fill/exit convention TripleBarrierLabel() uses: long fills at close+spread and its
|
|
//--- target/stop are entry+reward/entry-risk; short fills at close and mirrors the two.
|
|
double spread = (double)m_symbol.Spread() * m_symbol.Point();
|
|
double slMult, tpMult;
|
|
BarrierMultiples(slMult, tpMult);
|
|
double entryPrice, exitPrice;
|
|
if(dir == Buy)
|
|
{
|
|
entryPrice = closeAt + spread;
|
|
exitPrice = tradeWon ? entryPrice + tpMult * atr : entryPrice - slMult * atr;
|
|
}
|
|
else
|
|
{
|
|
entryPrice = closeAt;
|
|
exitPrice = tradeWon ? entryPrice - tpMult * atr : entryPrice + slMult * atr;
|
|
}
|
|
MqlDateTime t;
|
|
TimeToStruct(m_Time.GetData(oi), t);
|
|
string pattern = "Pattern_" + IntegerToString(tier);
|
|
string dirStr = (dir == Buy) ? "Buy" : "Sell";
|
|
string tableName = PatternTableName(GetFilterID(), pattern, dirStr);
|
|
double netVote = (dir == Buy) ? PatternWeightForTier(tier) : -PatternWeightForTier(tier);
|
|
RegisterSignal(t.year, t.mon, t.day, t.day_of_week, t.hour, t.min, tableName, pattern, dirStr,
|
|
entryPrice, exitPrice, tradeWon ? "Profit" : "Loss", netVote);
|
|
m_dbBackfillFired++;
|
|
}
|
|
dPrevSignal = savedPrevSignal;
|
|
dbm.CommitTransaction();
|
|
if(m_dbBackfillIndex >= m_dbBackfillStopIndex)
|
|
return; // more slices to come
|
|
Net.SetBatchNormFrozen(false);
|
|
m_dbBackfillActive = false;
|
|
m_dbBackfillDone = true;
|
|
g_forcePatternWeightsRefresh = true;
|
|
//--- Stamp the marker only now, on completion: a walk interrupted half way (EA removed mid-chunk)
|
|
//--- leaves NO marker, so the next attach redoes it in full rather than ranking on a partial window.
|
|
//--- The duplicate rows that costs are the lesser error - a half-filled table is silently biased
|
|
//--- toward whichever end of the OOS window happened to finish.
|
|
{
|
|
int markerFlags = m_activeFileCommon ? FILE_COMMON : 0;
|
|
int mh = FileOpen(m_activeFileName + ".dbfill",
|
|
markerFlags | FILE_TXT | FILE_WRITE | FILE_SHARE_READ | FILE_SHARE_WRITE);
|
|
if(mh != INVALID_HANDLE)
|
|
{
|
|
FileWriteString(mh, IntegerToString((int)m_dbBackfillEra));
|
|
FileClose(mh);
|
|
}
|
|
}
|
|
Print(ID + ": pattern database backfilled from " + IntegerToString(m_dbBackfillFired) + " calls on the"
|
|
" held-out CALIBRATION band (bars " + IntegerToString(m_dbBackfillStopIndex) + ".." +
|
|
IntegerToString(m_dbBackfillStartIndex) + ", era " + IntegerToString((int)m_dbBackfillEra) +
|
|
") - this IS the deploy-time warm-up: it runs with the weights FinalizeTrainRun just restored,"
|
|
" so the per-pattern win-rate history describes exactly what is about to trade and no separate"
|
|
" backtest is needed first. Those bars were never trained on, never graded by pass 3 and never"
|
|
" seen by the deploy gate. Two honest caveats: they are SIMULATED triple-barrier outcomes at"
|
|
" today's spread rather than realised fills, and m_dirConfThreshold was fitted on this same"
|
|
" band, so coverage here is mildly optimistic. Small tiers are shrunk toward the pooled rate"
|
|
" before they become weights (see WinRateFromCounts).");
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Alpha-balanced focal weight for one streamed bar - the cost-level |
|
|
//| imbalance correction used by BOTH the live continual-learning path |
|
|
//| and its OOS simulation. See the declaration comment and the |
|
|
//| ONLINE_LEARN_* block's CLASS IMBALANCE note for the derivation. |
|
|
//+------------------------------------------------------------------+
|
|
double CExpertSignalAIBase::OnlineSampleWeight(ENUM_SIGNAL trueSignal, double pBuy, double pSell, double pNeutral)
|
|
{
|
|
//--- Regression head has no class structure to balance.
|
|
if(m_outputNeuronsCount != 3)
|
|
return 1.0;
|
|
double weight = 1.0;
|
|
//--- alpha_c: inverse class frequency from the measured, persisted priors, normalised so the
|
|
//--- MAJORITY class is exactly 1.0 (a majority bar is never down-weighted below parity) and only
|
|
//--- minority bars are ever up-weighted.
|
|
double priorMax = MathMax(m_priorNeutral, MathMax(m_priorBuy, m_priorSell));
|
|
bool priorsUsable = (priorMax > 0.0 && m_priorBuy > 0.0 && m_priorSell > 0.0 && m_priorNeutral > 0.0);
|
|
if(priorsUsable && trueSignal != Neutral)
|
|
{
|
|
double truePrior = (trueSignal == Buy) ? m_priorBuy : m_priorSell;
|
|
//--- Measured ratio scaled by ONLINE_LEARN_PARITY, then capped so a single rare bar can never
|
|
//--- deliver an outsized kick to an already-validated deployed model.
|
|
weight = MathMin(MathMax(1.0, (priorMax / truePrior) * ONLINE_LEARN_PARITY), ONLINE_LEARN_ALPHA_CAP);
|
|
}
|
|
//--- gamma: down-weights bars the model already gets right (the overwhelming Neutral majority), so
|
|
//--- the update concentrates on genuinely informative confirmations. Constant since 2026-07-31.
|
|
if(ONLINE_LEARN_FOCAL_GAMMA > 0.0)
|
|
{
|
|
double pt = (trueSignal == Buy) ? pBuy : (trueSignal == Sell) ? pSell : pNeutral;
|
|
pt = MathMax(0.0, MathMin(1.0, pt));
|
|
weight *= MathPow(1.0 - pt, ONLINE_LEARN_FOCAL_GAMMA);
|
|
}
|
|
return weight;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Online continual-learning step - LIVE CHART ONLY. Once a model |
|
|
//| is deployed (m_trainingComplete) it keeps learning from real |
|
|
//| market structure the same supervised way it was trained: |
|
|
//| predicting the TRIPLE-BARRIER outcome for each bar. |
|
|
//+------------------------------------------------------------------+
|
|
void CExpertSignalAIBase::OnlineLearnStep(void)
|
|
{
|
|
//--- Hard gates. m_inferenceOnly covers BOTH the single backtest and every optimization pass (see
|
|
//--- its declaration comment): in the tester the model is held FIXED, so continual learning is a
|
|
//--- live-chart-only behaviour (forward-test it on a demo account, not the Strategy Tester).
|
|
if(!m_enableOnlineLearning || m_inferenceOnly || m_trainRunActive)
|
|
return;
|
|
//--- Meta target: online continual learning is direction-shaped (3-slot targets, bar labels) and
|
|
//--- the meta head's live path does not exist until S3 - hold the model fixed.
|
|
if(IsMetaTarget())
|
|
return;
|
|
if(!m_trainingComplete || m_trainingStopRequested || m_trainingPaused)
|
|
return;
|
|
if(MQLInfoInteger(MQL_TESTER) || MQLInfoInteger(MQL_OPTIMIZATION) || MQLInfoInteger(MQL_FORWARD))
|
|
return; // belt-and-braces: never adapt weights inside any tester context
|
|
if(CheckPointer(Net) == POINTER_INVALID || Net.CpuInference())
|
|
return; // DLL-free inference build has no backend to backprop through
|
|
if(CheckPointer(m_shadowNet) == POINTER_INVALID)
|
|
return; // nothing deployed to blend into yet (RefreshLatestSignal bootstraps it first)
|
|
if(m_outputNeuronsCount != 1 && m_outputNeuronsCount != 3)
|
|
return;
|
|
int conf = MathMax(m_barrierHorizonBars, 1);
|
|
int barsAvail = Bars(m_symbol.Name(), PERIOD_CURRENT);
|
|
//--- Need the frontier bar (now-relative index conf) plus a full feature window BEHIND it, plus a
|
|
//--- little slack so a short catch-up walk stays in-bounds.
|
|
int need = conf + (int)m_historyBars + 2;
|
|
if(barsAvail < need)
|
|
return;
|
|
//--- Load enough history for the frontier window and a bounded catch-up; RefreshConvergedSignal()
|
|
//--- only sized buffers relative to dtStudied (newest bars), which is too shallow to reach the
|
|
//--- confirmation frontier.
|
|
int wantBars = MathMin(need + ONLINE_LEARN_MAX_CATCHUP, barsAvail);
|
|
//--- HOLD RATHER THAN LEARN ON A SHORT WINDOW. `need` is the minimum that reaches the
|
|
//--- confirmation frontier WITH a full feature window behind it; below it the swing block
|
|
//--- silently degrades and the features stop matching the ones the model was fitted on.
|
|
int servable = ServableBars(wantBars, "online learning");
|
|
if(servable < need)
|
|
return;
|
|
wantBars = servable;
|
|
if(!ResizeBuffers(wantBars) || !RefreshData())
|
|
return;
|
|
//--- Same now-relative invalidation RefreshConvergedSignal() does, and needed independently of
|
|
//--- it: this runs on a DEEPER bar grid (wantBars reaches the confirmation frontier, that one
|
|
//--- only reaches the newest feature window), so the two legitimately disagree about `bars` and
|
|
//--- each must re-key the cache for the grid it is about to read.
|
|
EnsureBarCachesCapacity(wantBars);
|
|
datetime frontierTime = m_Time.GetData(conf);
|
|
if(frontierTime <= 0)
|
|
return;
|
|
//--- First step of this deployment (or a model that never online-learned): DON'T retroactively
|
|
//--- backfill the whole history through backprop in one shot - that could shift the just-
|
|
//--- validated deployed model materially before any live confirmation.
|
|
if(m_onlineLearnedUpToTime <= 0)
|
|
{
|
|
m_onlineLearnedUpToTime = frontierTime;
|
|
return;
|
|
}
|
|
if(frontierTime <= m_onlineLearnedUpToTime)
|
|
return; // no bar has matured past the watermark since last time
|
|
//--- Seed the guardrail EMA from the model's deploy-time OOS accuracy the first time we actually
|
|
//--- learn, so the floor is meaningful from the very first update (not a cold 0 that would trip it).
|
|
if(m_onlineRollingAcc < 0.0)
|
|
m_onlineRollingAcc = (dForecast > 0.0 && dForecast <= 100.0) ? dForecast : 100.0;
|
|
//--- Guardrail floor: deploy baseline minus a margin, never below the absolute minimum.
|
|
double baseline = (dForecast > 0.0 && dForecast <= 100.0) ? dForecast : 100.0;
|
|
double accFloor = MathMax(ONLINE_LEARN_MIN_ACC, baseline - ONLINE_LEARN_ACC_MARGIN);
|
|
//--- Find the oldest not-yet-learned confirmed bar: walk from the frontier (index conf) toward older
|
|
//--- bars (increasing index) until we pass the watermark or hit the catch-up cap, then learn newest-
|
|
//--- ward from there so bars are consumed in strict chronological (oldest->newest) order.
|
|
int oldestIdx = conf;
|
|
while(oldestIdx < barsAvail - 1
|
|
&& oldestIdx < conf + ONLINE_LEARN_MAX_CATCHUP
|
|
&& m_Time.GetData(oldestIdx) > m_onlineLearnedUpToTime)
|
|
oldestIdx++;
|
|
//--- oldestIdx now points at the first bar whose time is <= watermark (already learned) or the
|
|
//--- cap; the newest UNLEARNED bar is one step newer (idx-1). Learn from idx = oldestIdx-1 down
|
|
//--- to conf.
|
|
double savedEta = g_eta;
|
|
g_eta = m_modelEta * ONLINE_LEARN_ETA_SCALE;
|
|
int learned = 0;
|
|
for(int idx = oldestIdx - 1; idx >= conf; idx--)
|
|
{
|
|
datetime bt = m_Time.GetData(idx);
|
|
if(bt <= m_onlineLearnedUpToTime)
|
|
continue; // already learned (defensive; the walk above should exclude it)
|
|
//--- Build this bar's feature window - IDENTICAL to Train()/RefreshLatestSignal(): ends AT bar idx
|
|
//--- and extends m_historyBars into the past. No lookahead (all bars are older than idx).
|
|
//--- "Identical" is now enforced rather than asserted - all three go through BuildFeatureWindow().
|
|
if(!BuildFeatureWindow(idx))
|
|
{
|
|
//--- window not buildable this bar (e.g. an indicator hole) - advance the watermark past it so
|
|
//--- we don't wedge re-trying the same bar forever, but learn nothing from it.
|
|
m_onlineLearnedUpToTime = bt;
|
|
continue;
|
|
}
|
|
//--- Predict with the CURRENT (pre-update) weights, then score against the confirmed label for the
|
|
//--- rolling guardrail - exactly AdvanceOosSimulationChunk()'s predict-before-learn measurement.
|
|
Net.feedForward(TempData);
|
|
Net.getResults(TempData);
|
|
double predSignal = (m_outputNeuronsCount == 3) ? ApplyClassificationSoftmax() : TempData[0];
|
|
//--- Same target rule training used. `idx` is at or beyond the confirmation frontier (conf ==
|
|
//--- m_barrierHorizonBars, enforced above), so the forward window this reads is fully closed.
|
|
ENUM_SIGNAL trueSignal = TripleBarrierLabel(idx);
|
|
bool hit = (DoubleToSignal(predSignal) == trueSignal);
|
|
m_onlineRollingAcc += (100.0 * (hit ? 1.0 : 0.0) - m_onlineRollingAcc) / ONLINE_ACC_SMOOTH;
|
|
//--- Per-class softmax probabilities as of THIS bar's pre-update prediction.
|
|
double pBuy = (TempData.Total() > 0) ? TempData.At(0) : 0.0;
|
|
double pSell = (TempData.Total() > 1) ? TempData.At(1) : 0.0;
|
|
double pNeutral = (TempData.Total() > 2) ? TempData.At(2) : 0.0;
|
|
//--- Build the target vector - identical encoding to Train()/AdvanceOosSimulationChunk().
|
|
bool buy = (trueSignal == Buy);
|
|
bool sell = (trueSignal == Sell);
|
|
TempData.Clear();
|
|
if(m_outputNeuronsCount == 1)
|
|
TempData.Add(buy && !sell ? 1 : (!buy && sell ? -1 : 0));
|
|
else
|
|
{
|
|
TempData.Add(buy ? LABEL_SMOOTH_HIGH : LABEL_SMOOTH_LOW);
|
|
TempData.Add(sell ? LABEL_SMOOTH_HIGH : LABEL_SMOOTH_LOW);
|
|
TempData.Add((!buy && !sell) ? LABEL_SMOOTH_HIGH : LABEL_SMOOTH_LOW);
|
|
}
|
|
Net.backProp(TempData, OnlineSampleWeight(trueSignal, pBuy, pSell, pNeutral));
|
|
m_onlineSamples++;
|
|
//--- Deploy the improvement ONLY while accuracy holds. Warmup: allow the first few blends (the
|
|
//--- model was just validated at deploy, steps are tiny) until the EMA has enough samples to judge.
|
|
bool blendOk = (m_onlineSamples <= ONLINE_LEARN_WARMUP) || (m_onlineRollingAcc >= accFloor);
|
|
if(blendOk)
|
|
{
|
|
m_shadowNet.BlendWeightsFrom(Net, SHADOW_WEIGHT_TAU);
|
|
if(m_onlineBlendFrozen)
|
|
{
|
|
m_onlineBlendFrozen = false;
|
|
Print(ID + ": online-learning deployment RESUMED - rolling accuracy recovered to "
|
|
+ DoubleToString(m_onlineRollingAcc, 1) + "% (floor " + DoubleToString(accFloor, 1) + "%)");
|
|
}
|
|
}
|
|
else if(!m_onlineBlendFrozen)
|
|
{
|
|
m_onlineBlendFrozen = true;
|
|
Print(ID + ": online-learning deployment FROZEN - rolling accuracy " + DoubleToString(m_onlineRollingAcc, 1)
|
|
+ "% fell below floor " + DoubleToString(accFloor, 1) + "%; live keeps trading the last-good model while it adapts");
|
|
}
|
|
m_onlineLearnedUpToTime = bt;
|
|
learned++;
|
|
m_onlineBarsSincePersist++;
|
|
}
|
|
//--- Hand the shared global back exactly as found, on BOTH exit paths below - see the matching
|
|
//--- savedEta assignment above for why this must not leak out of this function.
|
|
g_eta = savedEta;
|
|
if(learned <= 0)
|
|
return;
|
|
//--- Periodic durable persistence so a crash loses at most ONLINE_LEARN_PERSIST_EVERY bars of
|
|
//--- adaptation (shutdown also persists via PersistOnShutdown()).
|
|
if(m_onlineBarsSincePersist >= ONLINE_LEARN_PERSIST_EVERY)
|
|
{
|
|
double ip[];
|
|
m_indicatorTuner.Flatten(ip);
|
|
bool saveOk = Net.Save(m_activeFileName + ".nnw", dError, dUndefine, dForecast, dtStudied, m_activeFileCommon, m_eraCount, m_trainingComplete, ip);
|
|
if(!saveOk)
|
|
Print(ID + ": ERROR - online-learning Net.Save failed for " + m_activeFileName + ".nnw. Retrying next persist interval instead of resetting the bars-since-persist counter.");
|
|
SaveShadowNet(ip);
|
|
if(!SaveModelStats(m_activeFileName, m_activeFileCommon))
|
|
Print(ID + ": ERROR - online-learning SaveModelStats failed for " + m_activeFileName + ".");
|
|
// Only reset the counter on a successful weight save - resetting unconditionally on a
|
|
// transient failure would silently double the effective data-loss window on the NEXT failure too.
|
|
if(saveOk)
|
|
{
|
|
m_onlineBarsSincePersist = 0;
|
|
PrintVerbose(ID + ": online-learning checkpoint saved (" + IntegerToString((int)m_onlineSamples)
|
|
+ " total updates, rolling acc " + DoubleToString(m_onlineRollingAcc, 1) + "%)");
|
|
}
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void CExpertSignalAIBase::SaveShadowNet(const double &indicatorParams[])
|
|
{
|
|
if(CheckPointer(m_shadowNet) == POINTER_INVALID)
|
|
return;
|
|
m_shadowNet.Save(m_activeFileName + "_shadow.nnw", dError, dUndefine, dForecast, dtStudied, m_activeFileCommon, m_eraCount, m_trainingComplete, indicatorParams);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void CExpertSignalAIBase::EnsureShadowNet(void)
|
|
{
|
|
if(CheckPointer(m_shadowNet) != POINTER_INVALID)
|
|
return;
|
|
//--- Pure-MQL5 inference (DLL-free backtest): no era blending happens, so the shadow would just be a
|
|
//--- copy of Net - and bootstrapping one via Save/Load would spin a compute backend up on the clone,
|
|
//--- defeating the DLL-free goal. Skip it; RefreshLatestSignal() falls back to Net directly.
|
|
if(CheckPointer(Net) != POINTER_INVALID && Net.CpuInference())
|
|
return;
|
|
string shadowFile = m_activeFileName + "_shadow.nnw";
|
|
if(FileIsExist(shadowFile, m_activeFileCommon ? FILE_COMMON : 0))
|
|
{
|
|
CNet *loaded = new CNet(NULL);
|
|
if(CheckPointer(loaded) != POINTER_INVALID)
|
|
{
|
|
double loadE, loadU, loadF;
|
|
datetime loadTime;
|
|
long loadEra;
|
|
bool loadComplete;
|
|
double loadIp[];
|
|
if(loaded.Load(shadowFile, loadE, loadU, loadF, loadTime, m_activeFileCommon, loadEra, loadComplete, loadIp, true /*quiet: a miss just falls through to the clone bootstrap below*/))
|
|
{
|
|
m_shadowNet = loaded;
|
|
//--- This second CNet spins up its OWN compute backend, so on a fresh attach the log shows a
|
|
//--- second backend-init block right after the main model's. Name it here (verbose) so it
|
|
//--- reads as "the shadow net came up" rather than "the EA started twice".
|
|
PrintVerbose(ID + ": EMA shadow net restored from " + shadowFile + " (its own network instance - hence a second compute-backend init)");
|
|
return;
|
|
}
|
|
delete loaded;
|
|
}
|
|
}
|
|
//--- No compatible persisted shadow - bootstrap from Net's current weights.
|
|
if(CheckPointer(Net) == POINTER_INVALID)
|
|
return;
|
|
//--- Attempt the clone bootstrap at most once per topology (see m_shadowBootstrapAttempted). On the
|
|
//--- tester's CPU-DLL fallback a second full-net clone can fail to load; retrying every bar would
|
|
//--- rebuild the compute backend each tick and crawl. Falling back to Net is correct and lossless here.
|
|
if(m_shadowBootstrapAttempted)
|
|
return;
|
|
m_shadowBootstrapAttempted = true;
|
|
//--- Co-locate the ephemeral clone temp with the active model (COMMON on a live chart, LOCAL in the
|
|
//--- tester sandbox) instead of always LOCAL.
|
|
string cloneFile = m_activeFileName + "_shadowclone.tmp";
|
|
int cloneFlags = m_activeFileCommon ? FILE_COMMON : 0;
|
|
double ip[];
|
|
if(!Net.Save(cloneFile, 0.0, 0.0, 0.0, dtStudied, m_activeFileCommon, m_eraCount, m_trainingComplete, ip))
|
|
return;
|
|
CNet *clone = new CNet(NULL);
|
|
if(CheckPointer(clone) == POINTER_INVALID)
|
|
{
|
|
FileDelete(cloneFile, cloneFlags);
|
|
return;
|
|
}
|
|
double loadE, loadU, loadF;
|
|
datetime loadTime;
|
|
long loadEra;
|
|
bool loadComplete;
|
|
double loadIp[];
|
|
bool loaded = clone.Load(cloneFile, loadE, loadU, loadF, loadTime, m_activeFileCommon, loadEra, loadComplete, loadIp, true /*quiet: best-effort clone, the miss is handled gracefully below*/);
|
|
FileDelete(cloneFile, cloneFlags);
|
|
if(!loaded)
|
|
{
|
|
delete clone;
|
|
//--- Best-effort: without a shadow, live signals read the main Net directly
|
|
//--- (RefreshLatestSignal's deployNet fallback), which is correct and lossless - so one calm
|
|
//--- line, not an error.
|
|
PrintVerbose(ID + ": EMA shadow net could not be bootstrapped - live signals use the main model directly (lossless fallback).");
|
|
return;
|
|
}
|
|
m_shadowNet = clone;
|
|
//--- See the matching note on the restore path above: a second CNet means a second compute-backend init
|
|
//--- in the log, which is expected, not a duplicated EA.
|
|
PrintVerbose(ID + ": EMA shadow net bootstrapped from the main model's current weights (its own network instance - hence a second compute-backend init)");
|
|
}
|
|
#endif // WARRIOR_AIBASE_ONLINELEARNING_MQH
|