Kalman.mqh is a Kalman filter and smoother for linear Gaussian state space models with one observation per bar. It starts with the exact diffuse initialization of Durbin and Koopman, so no first state and no large "starting variance" has to be guessed, it accepts missing values, and it estimates the variances by maximum likelihood for three ready models: a local level (a level that drifts, seen with noise), a local linear trend, and a regression whose coefficients drift, for example the hedge ratio of a pair. No DLL.
#include <Kalman\Kalman.mqh>
vector y, x; // for example log prices of two symbols on the same bars
// ... fill y and x ...
matrix X(x.Size(), 1);
X.Col(x, 0);
CKalman k;
if(k.FitDynamicRegression(y, X, true)) // intercept and hedge ratio follow random walks
{
const int last = k.Observations() - 1;
double beta = k.Filtered(last, 1); // hedge ratio now, from the data up to now
double sd = k.FilteredStd(last, 1); // its standard deviation
double z = k.Innovation(last) / MathSqrt(k.InnovationVariance(last)); // last forecast error in sd
}
CKalman ll;
if(ll.FitLocalLevel(y)) // y = level + noise
{
double now = ll.Filtered(ll.Observations() - 1, 0);
vector mean, var;
ll.Forecast(10, mean, var);
} Any other model y(t) = Z(t) a(t) + e(t), a(t+1) = T a(t) + w(t) can be set with SetModel(Z, T, Q, H), then Filter(y) and Smooth(). SetDynamicRegression() runs the regression with variances you choose instead of fitting them.
The script KalmanHedge.mq5 fits the regression of one symbol on another (log prices by default), prints the fitted variances, the hedge ratio now and 20, 60, 250 and 500 bars ago as it was known then, with its standard deviation and its smoothed value, the z-scores of the last forecast errors (the signal of a Kalman pair trade) and the share of errors beyond 2 standard deviations (picture 1). It can write the hedge ratio of every bar to a CSV file (picture 2).


Picture 3: the hedge ratio of the Kalman filter, known each day from the past only, against an ordinary regression on the last 60 days. The rolling regression jumps from below 0 to above 4, and its day-to-day changes have a standard deviation of 0.060, against 0.009 for the filter. The filter does not need a window length: it estimates from the data how fast the ratio really drifts.

Picture 4: log silver against log gold, one shade per year. A single regression line over the six years (slope 1.14) misses whole years, above it in 2021 and 2026, below it in 2025. This is why the hedge ratio is modelled as a value that moves slowly instead of a constant.

On six series (simulated local level, the same with 10% missing values, simulated local linear trend, the log price of gold, a simulated regression and silver on gold, H4), against the state space models of statsmodels 0.15 with exact diffuse initialization and against a 40-digit computation of the same recursions:
Kalman.mqh goes to MQL5\Include\Kalman, KalmanHedge.mq5 to MQL5\Scripts\Kalman.
RegimeHMM: market regimes with a hidden Markov model
Gaussian hidden Markov model fitted by Baum-Welch: regime probabilities without look-ahead, most likely path, expected durations and regime forecasts.
CME equity index futures holiday calendar
Holidays and early halts of the CME equity index futures (S&P 500, Nasdaq-100, Dow, Russell 2000, E-mini and Micro), computed from the US holiday rules for any year, with a script that checks the calendar against your broker's own M1 history.
Cointegration: ADF, Engle-Granger and Johansen tests with a pair scanner
Unit root and cointegration tests (ADF, Engle-Granger, Johansen) with MacKinnon p-values, hedge ratio, half-life and z-score of the spread, and a pair scanner.
Basket Pair Closer
Position manager for MT5 that closes the best winner together with the worst loser when their combined result reaches your minimum. Basket take profit and stop loss in money, magic filter, simple panel. Works with grid, martingale and manual trades. Open source code.