Runge Kutta ODE Solver
Most trading code has no ODE in it, but the few that do (a smoothed volatility process, a saturation/adoption-style curve, a physical-analogy indicator built on a differential equation) usually get a hand-rolled Euler step, whose error per step is only O(h) - halving the step size only halves the error, so getting acceptable accuracy needs a very small, very slow step. This solver implements classical RK4 instead, whose local error is O(h^5) and global error O(h^4): halving the step size cuts the error by roughly a factor of 16. RK4's four stages genuinely depend on each other in sequence - stage 2 needs stage 1's result, stage 3 needs stage 2's, and so on - so unlike this catalogue's integration and differentiation headers (which can hand out all their evaluation points at once), this one exposes the stage math as individual small steps: it tells you where to evaluate your derivative function next, you evaluate it, and you hand the resulting k-value back in for the next stage. RkStep then combines the four k-values into the update. A step-doubling helper (one full step versus two half steps covering the same interval) gives a genuine local error estimate and a suggested new step size, the standard building block for an adaptive-step integrator, without this header dictating your control loop's structure. Vector-ODE variants of the stage-update and combine functions extend the same approach to a coupled system (e.g. a two-state oscillator or a two-factor model) using arrays instead of a single scalar. Notes and limitations Fixed-step RK4 only in the sense that the header does not run a control loop for you; RkStepDoublingError and RkSuggestStepSize give you the pieces to build an adaptive-step loop yourself, but the loop itself is your code, not this header's. No stiff-equation handling. Classical explicit RK4 can need an impractically small step (or diverge outright) on a stiff ODE (one with widely separated time constants); this library does not detect stiffness and will happily produce a diverging sequence of numbers if fed one. The vector-ODE helpers assume every equation in the system uses the same step size h; there is no per-equation step-size support. Nothing here reads chart or account data itself - VectorF and ScalarF in the demo are ordinary user functions, so a real EA's derivative function is free to read Bid/Ask/indicator values at each evaluation exactly as any other MQL4 function would.
Script · MT4 · v1.0 · Free
About Runge Kutta ODE Solver
Runge Kutta ODE Solver is a script for MetaTrader 4. MQL source code included.
Files in the download: RungeKuttaSolver.mqh, RungeKuttaSolver.txt, RungeKuttaSolverDemo.mq4.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code