Loading…

Fractional Differencing Transformer

Plain differencing - price[t] minus price[t-1] - makes a price series stationary, which is what most statistical models need to work correctly. It also throws away almost everything else. Every long-run trend, level and slow structure in the series disappears along with the non-stationarity, which is why models trained on a plain diff often have very little real signal left to find: the transform that fixed the stationarity problem also destroyed most of what made the series predictable in the first place. Fractional differencing generalises "difference order" from the integer 1 to any real number d, and for 0<d<1 it is a genuine compromise: the closer d sits to 0, the more of the original series' memory survives, at the cost of the result being somewhat less perfectly stationary; the closer d sits to 1, the more it behaves like a plain diff. This header implements the actual weight-generation recursion behind that idea: w_0 = 1 w_k = -w_k-1 * (d - k + 1) / k for k = 1, 2, 3, ... These weights decay in magnitude for 0<d<1 but, mathematically, never reach exactly zero over an infinite window. The FIXED-WIDTH version used here truncates that infinite weight series once |w_k| falls below a caller-set threshold, and uses that truncation length as one fixed window applied identically to every point in the output - as opposed to a growing/ expanding window that would use a longer filter later in the series than earlier, making early and late output values not directly comparable. The transformed value at time t is the weighted dot product of the ORIGINAL (undifferenced) price series over that fixed window ending at t. Points near the start of the series, where a full window is not yet available, are left empty rather than computed from a truncated, shorter window - silently averaging over a short window at exactly the point where the filter is least complete would understate how much memory was actually removed there, which is a worse failure than admitting those points have no valid output at all. Choosing d in practice means pairing this transform with a stationarity test - typically an Augmented Dickey-Fuller test - searched over a range of candidate d values to find the smallest d that still yields a stationary series (the point of maximum memory retention subject to the stationarity constraint). That search is genuinely out of scope for this header: no ADF test is implemented here. This library performs the transform only; picking d is left to the caller, with whatever statistical tooling they use for it. Notes and limitations - This library performs the transform only. Selecting d is the caller's responsibility, typically via a stationarity test (e.g. ADF) run across a range of candidate d values - not implemented here. - The window length grows as d moves further from an integer and as threshold shrinks; a very small threshold with a mid-range d can produce a window of hundreds of points. FracDiffGenerateWeights caps the window at 5000 weights as an absolute safety limit regardless of threshold, to guard against a degenerate (d, threshold) pair looping indefinitely. - Leading points without a full window are left at EMPTY_VALUE rather than computed from a partial window - do not treat EMPTY_VALUE as zero when consuming the output array. - The recursion is evaluated exactly as given for any d, including values outside (0, 1); the "closer to the original series" / "closer to a plain diff" intuition in this description and the demo specifically describes the 0<d<1 range this header was designed around. - FracDiffApplyWeights takes the RAW (undifferenced) price series as input at every call - it does not maintain any state between calls, so re- running it on a longer series recomputes the whole output rather than extending a previous run incrementally.

Script · MT4 · v1.0 · Free

About Fractional Differencing Transformer

Fractional Differencing Transformer is a script for MetaTrader 4. It has 2 settings, such as Series length and Threshold. MQL source code included.

Files in the download: FracDiffTransformer.mqh, FracDiffTransformer.txt, FracDiffTransformerDemo.mq4.

Settings

SettingDefaultWhat it does
Series length40synthetic price series length
Threshold0.001weight-truncation threshold

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Buy/Sell Arrows · Trend · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria