Root Finding Methods
Every trader eventually needs to invert a function that has no closed-form inverse: what price makes this position's profit zero once a nonlinear cost model is included, what rate makes this cashflow schedule net to zero, what input makes an indicator cross a threshold. The naive approach is a fixed step loop that either overshoots or takes forever. This header gives you four real solvers instead. Neither MQL4 nor MQL5 has function pointers, so a generic solver cannot be handed an arbitrary f(x) the way it could in most other languages. Every solver here uses an ask-tell pattern instead: NextX() tells you which x to evaluate, you evaluate your own function however you like, and Update() reports the result back. The solver never touches your function, only its values, so it works identically whether f(x) is a one-line formula or a call into a broker API. Bisection is the one that cannot fail, provided you hand it a genuine bracket - two x values where f has opposite sign. It is also the slowest, halving the interval one step at a time. Secant and Newton converge much faster near a well-behaved root but are not guaranteed: a bad starting guess, a flat derivative, or an oscillating function can send either of them somewhere useless, and both solvers detect the specific failure (a zero denominator, a near-zero derivative) and stop rather than returning a garbage answer. Brent is the one to reach for by default. It keeps a valid bracket at all times like bisection, but on every step it tries a faster interpolated jump first and only falls back to a plain bisection step when that jump would land outside the shrinking bracket or fails to make real progress. That fallback rule - inherited from the real Brent's method, not simplified away here - is what makes it converge in single-digit iterations on well-behaved problems while still being unable to diverge on a genuine bracket. Notes and limitations - Always check .state before trusting Root(). ROOTFIND_FAILED means exactly that - a bad bracket, an undefined slope, or the iteration budget ran out. Never treat the last x reached before failure as an answer. - Bisection and Brent require the caller to supply a real bracket. Passing two points on the same side of the root is refused, not silently accepted. - Newton needs an analytic or numerically-approximated derivative supplied by the caller each step. If you only have f(x), use Brent instead. - None of the four solvers know anything about trading. They operate purely on the numbers you feed them - the breakeven example in the demo is one possible use, not a built-in feature. - Not thread-relevant: each solver is a plain struct instance with no shared or global state, so multiple solves can run side by side safely.
Script · MT5 · v1.0 · Free
About Root Finding Methods
Root Finding Methods is a script for MetaTrader 5. It has 3 settings, such as Open price, Lots and Spread points. MQL source code included.
Files in the download: RootFinding.mq5.mqh, RootFinding.txt, RootFindingDemo.ex5, RootFindingDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Open price | 1.10000 | Position open price for the breakeven example |
| Lots | 0.50 | Position size for the breakeven example |
| Spread points | 18 | Base spread in points, widens with distance travelled |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Trailing Stop & Break-even · Oil · MT5 Script · MQL5 Source Code