Forecast reconciliation in native MQL5: one projection engine that makes a timeframe hierarchy and a currency-triangle graph coherent, with five weighting schemes and walk-forward Diebold-Mariano evidence.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
ayantrader 71fdcd7c2c Revision after review: reconciled covariance, ridge reporting, guarantee check
- ReconciledCovariance now takes the base error covariance and returns
  P Sigma P^T; the old S A^-1 S^T form was only valid when W equals it.
- RecSpdInverse reports the ridge the normal-equation matrix needed.
- Diebold-Mariano p-value from Student t with n-1 degrees of freedom.
- WeightedDistance added: the metric each projection is orthogonal in.
- Recon_Evidence reports the data span, a per-origin guarantee table and
  how far the realised outcome sits from coherent.
- Last-value forecast is no longer described as a random walk.
- README brought in line with the revised article.
2026-10-07 21:07:45 +05:00
Include/Reconcile Revision after review: reconciled covariance, ridge reporting, guarantee check 2026-10-07 21:07:45 +05:00
Indicators/Reconcile Reconciliation engine, both hierarchies and the walk-forward evidence 2026-08-26 09:58:58 +05:00
Scripts/Reconcile Revision after review: reconciled covariance, ridge reporting, guarantee check 2026-10-07 21:07:45 +05:00
README.md Revision after review: reconciled covariance, ridge reporting, guarantee check 2026-10-07 21:07:45 +05:00

Reconcile

Forecast reconciliation in native MQL5: a projection that makes a set of mutually contradictory forecasts consistent and, in its orthogonal form, provably cannot increase their total squared error while doing it.

Companion code for the MQL5 article: https://www.mql5.com/en/articles/24318

What it does

Run one model per timeframe and the answers disagree. The H1 model describes the next few hours, the H4 model describes the same hours differently, and the D1 model describes a window containing both. All three describe the same stretch of price, so at most one of them can be right. The same thing happens across instruments, where the constraint is arithmetic: in log space EURUSD plus USDJPY is EURJPY. That identity is exact for prices at one instant and only approximate for broker bar closes, and the evidence script measures the gap.

Reconciliation treats that disagreement as measurable error rather than as something to average away. Write the constraints as a summing matrix S, and a coherent truth lies in its column space while your forecasts generally do not. Every component pointing out of that subspace is pure error, so removing it cannot move you further from the truth. Under the orthogonal projection that is the Pythagorean theorem, not a claim about markets. Any other weighting is orthogonal in its own metric instead, which is not squared error.

CReconciler implements the projection and knows nothing about markets. It receives a summing matrix and a weighting, and from then on does arithmetic, which is what lets one object serve two structures with no code in common between them: a temporal hierarchy of sixty nodes over twenty-four free hourly returns, and a currency graph of twenty-one pairs over seven free log prices. Five weightings are available, from OLS through structural and measured WLS to MinT on the sample and Schaefer-Strimmer shrunk covariances.

The numerics are the part that decides whether the guarantee survives contact with real data. W is never inverted. It is factorised by Cholesky with a ridge that escalates until it succeeds, S is pushed through the same factor, and the only explicit inverse in the library is taken on the small m x m normal-equation matrix. Three exact invariants are measured on every inspect run: a coherent input returns unchanged, reconciling twice equals reconciling once, and the output satisfies the constraints, the last checked by an independent projector so it cannot flatter itself.

Recon_Evidence.mq5 then checks the theorem's predictions instead of asserting them, walking forward one top period at a time with residuals entering the error store only after they have been scored. Loss differentials go through a Diebold-Mariano test with a Bartlett kernel, the Harvey-Leybourne-Newbold correction and a Student t p-value. It also counts, at every origin, whether each scheme moved farther from the outcome in squared error and in its own weighted metric, and how far the realised outcome itself sits from coherent. On the temporal hierarchy the orthogonal projection made no origin worse; on the currency graph, whose bar closes are not exactly coherent, it made some hours worse and still cut pooled error. Textbook MinT on an unregularised sample covariance destroys the forecasts it was given at sixty nodes while still getting closer in its own metric at every origin: the failure is in the estimate of W, which was numerically singular most of the time, not in the projection.

The diagnostic that follows sorts origins by how incoherent the base forecasts were and finds the gain tracks the contradiction, monotonically. It also finds the uncomfortable half of the same fact. Reconciliation minimises the trace, the sum across all nodes, and a sum will buy a large reduction in fifty-nine terms with an increase in the sixtieth. In a temporal hierarchy the widest node is the cheapest place to put that error, so if you act on a single forecast you should check that node specifically.

Layout

Include/Reconcile/ReconcileTypes.mqh         enums and the dense linear algebra kernel
Include/Reconcile/Shrinkage.mqh              rolling error store, sample, diagonal and shrunk covariance
Include/Reconcile/Reconcile.mqh              CReconciler: the projection, five weightings, reconciled covariance
Include/Reconcile/Hierarchy.mqh              both summing matrices, connectivity check, time-keyed loader
Include/Reconcile/BaseForecast.mqh           last value, window mean and fitted AR(1)
Scripts/Reconcile/Recon_Inspect.mq5          summing matrix, per-node adjustment, invariants, triangle violations
Scripts/Reconcile/Recon_Evidence.mq5         walk-forward over five weightings, DM tests, guarantee check, buckets
Indicators/Reconcile/ReconciledForecast.mq5  base against reconciled for one node, with the incoherence histogram

Run Recon_Inspect.mq5 first. It prints the structure you are about to trust and the three invariants, and if those are not at machine precision nothing downstream is worth reading. Recon_Evidence.mq5 produces the walk-forward tables. The indicator defaults to the daily node under OLS, which is both the node where base and reconciled visibly disagree and the node the trace is most willing to sacrifice.

Substituting your own models is a single edit: BaseForecast.mqh is the only place that turns a price series into a number, and the engine never asks where the vector it receives came from. Adding a third structure is the same kind of edit at the other end, since nothing downstream of S knows what the constraints mean.

Disclaimer

Educational code. Reconciliation is a forecast-accuracy method, and nothing here demonstrates a trading edge: the directional hit rates it measures are flat before and after. What it improves is squared error, which is a different thing from money. Test on your own data and broker conditions before drawing conclusions.