Loading…

Formula Expression Evaluator

Every EA that wants a user-configurable combination of indicators ends up either hardcoding one fixed formula, or growing a pile of boolean input flags trying to cover every combination a user might want ("use RSI? weight? use MA? weight? combine how?"). Neither scales, and both need a recompile the moment someone wants a shape the flags did not anticipate. This library parses an actual formula string - something like "MAX(RSI, MA) - ABS(SPREAD)*2" - at runtime, with a genuine tokenizer and a recursive-descent parser that gets operator precedence right: standard precedence order (unary minus, then power, then multiply/divide, then add/subtract), power is right-associative (2^3^2 = 2^(3^2) = 512, not (2^3)^2), and unary minus binds LOOSER than power, matching ordinary maths and every mainstream language (-2^2 = -4, i.e. -(2^2), not (-2)^2 - a detail a naive hand-rolled evaluator gets wrong more often than it gets right). Named variables (RSI, MA, SPREAD, or any identifier the caller invents) are resolved through a name/value table set up with SetVariable() before Evaluate() is called - MQL4 has no function-pointer/delegate type to hand the parser a lookup callback, so a parallel table is the natural substitute, and it doubles as a place to keep a formula's inputs fresh tick to tick without re-parsing the string itself. A handful of built-in functions (ABS, SQRT, MIN, MAX, ROUND) cover the common cases without this header trying to be a general-purpose scripting language. Every error path - a truncated expression, an unknown variable, an unknown function, wrong argument count, unbalanced parentheses, division by zero, a negative SQRT argument - is caught and reported through GetLastError() rather than silently returning 0.0 or crashing, which matters enormously the moment the formula string comes from a user input box rather than a value you typed and tested yourself. Notes and limitations - Variable and function names are matched case-insensitively (RSI, rsi and Rsi are the same lookup). - Only arithmetic is supported - no comparison operators (>, <, ==), no boolean logic (AND/OR), no conditionals (IF). A formula here always produces one number; combine this with TradingRuleDsl.mqh (a separate library in this catalogue) for conditional decision-making. - Functions take a fixed argument count (ABS/SQRT/ROUND take exactly 1, MIN/MAX take exactly 2) - the wrong count is a reported error, not a silently ignored extra argument. - Division by zero and a negative SQRT argument are reported errors, not an MQL4 division warning or a NaN propagating silently through the rest of the formula. - Each Evaluate() call re-tokenizes and re-parses the formula string from scratch - there is no compiled/cached AST reuse across calls. For a formula evaluated every tick, this is a handful of microseconds per call on a short expression, not a meaningful cost, but it is not designed for evaluating the same formula thousands of times in a tight inner loop.

Script · MT4 · v1.0 · Free

About Formula Expression Evaluator

Formula Expression Evaluator is a script for MetaTrader 4. It has 1 setting, such as Custom formula. MQL source code included.

Files in the download: FormulaExpressionEvaluator.mqh, FormulaExpressionEvaluator.txt, FormulaExpressionEvaluatorDemo.mq4.

Settings

SettingDefaultWhat it does
Custom formulaMAX(RSI, MA) - ABS(SPREAD)*2Try your own formula here

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: RSI · Moving Average · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria