Loading…

Cubic Spline Interpolation

A trader with a handful of pivot points, a volume profile sampled at uneven price levels, or any other small, irregularly spaced series often needs a value between the points that were actually recorded, not just the nearest one. Linear interpolation gives a value but a visibly kinked curve; this library gives a smooth one, in two variants that trade off differently. BuildNaturalSpline fits the classic cubic spline: the curve is twice continuously differentiable everywhere, built by solving a tridiagonal system for the second derivative at each knot with the Thomas algorithm in a single O(n) pass. It is the smoothest possible curve through the points in a least-curvature sense, but "smoothest" does not mean "safest" - between two points where the trend changes sharply, a natural spline can swing above or below the surrounding data. That overshoot is fine for a cosmetic curve and wrong for anything that must stay monotone by definition. BuildMonotoneSpline implements the Fritsch-Carlson method for exactly that case. Each segment's tangent is derived from its immediate neighbours and then limited so the coefficients satisfy alpha^2 + beta^2 <= 9 - the precise condition that guarantees the resulting cubic never overshoots the data on that segment. It gives up being twice differentiable at every knot in exchange for a curve that can be trusted never to say something the data itself did not say. Both evaluators clamp any x value outside the fitted range to the nearest endpoint rather than extrapolating - a cubic projected past the data it was fit to is unreliable and can diverge sharply within a short distance, so holding flat at the edge is the safer default. Notes and limitations - x[] must be strictly increasing for both spline kinds; both Build functions print why and return false rather than producing a curve from bad input. - At least 3 points are required - two points define only a straight line, which neither spline kind adds anything to. - Both evaluators use a linear scan to find the segment containing xValue, which is intentional: the knot counts this library is meant for (pivot points, a handful of curve control points) are small enough that a binary search would not meaningfully outperform it, and the linear scan is easier to verify at a glance. - Neither spline is monotone-guaranteed to reproduce the input data's true underlying shape between sparse points - both are interpolation, not measurement, and a large gap between two knots is still a guess about what happens in between. - Out-of-range x values are clamped to the nearest endpoint's exact y value, not extrapolated along the edge segment's curve.

Script · MT5 · v1.0 · Free

About Cubic Spline Interpolation

Cubic Spline Interpolation is a script for MetaTrader 5. It has 1 setting, such as Resample steps. MQL source code included.

Files in the download: CubicSplineInterpolation.mq5.mqh, CubicSplineInterpolation.txt, CubicSplineInterpolationDemo.ex5, CubicSplineInterpolationDemo.mq5.

Settings

SettingDefaultWhat it does
Resample steps20Number of resampled points printed between the first and last knot

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: Pivot Points · Volume · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria