Trading Rule DSL Interpreter
Most EAs bake their entry logic directly into code: an if-statement here, a boolean flag there, one more input parameter every time someone wants a slightly different combination of conditions. Every variant needs a recompile, and a user who wants "RSI above 70 and the 20 MA below price, otherwise if RSI is below 30 buy, otherwise do nothing" has no way to express that without editing source. This library parses that sentence directly. The rule text IF RSI(14) > 70 AND MA(20) < CLOSE THEN SELL ELSE IF RSI(14) < 30 THEN BUY ELSE HOLD is tokenized and parsed into a real decision tree with a documented grammar: standard AND/OR/NOT boolean logic with parenthesised grouping, comparators (> < >= <= = <>), and an ELSE IF chain evaluated top to bottom exactly like a nested if/else-if/else in ordinary code, just expressed as data instead. The first branch whose condition is true wins; a trailing plain ELSE (no IF) catches whatever falls through; and if no branch matches and there is no trailing ELSE, the rule reports "no branch matched" rather than guessing. Values inside a condition are either bare identifiers (CLOSE, SPREAD, BALANCE, or any name you invent) resolved through a SetVariable() table, or period-parameterised indicator readings (RSI(14), MA(20), ATR(14)) resolved through a separate SetIndicatorValue(name, period, value) table. MQL4 has no function-pointer type to hand the parser a live callback into iRSI/iMA directly, so the caller computes the values it cares about once per tick and registers them before calling Interpret() - the same practical substitute used elsewhere in this catalogue for the same underlying MQL4 limitation. Every malformed rule - missing IF/THEN, an unregistered variable or indicator/period pair, unbalanced parentheses, an action that is not a plain identifier - is caught and reported through GetLastError(), which matters as soon as the rule text comes from a user input box rather than something you typed and tested yourself. Notes and limitations - The action returned is whatever identifier followed THEN/ELSE in the rule text - the library does not restrict it to BUY/SELL/HOLD, so it can carry any vocabulary your EA defines (e.g. CLOSE_ALL, TIGHTEN_STOP, DO_NOTHING). Compare the returned string against the actions your caller understands. - Comparators only compare two VALUES (numbers, variables, or IDENT(period) readings) - there is no arithmetic inside a condition (no "RSI(14)*2 > 140"). Combine this with FormulaExpressionEvaluator.mqh (a separate library in this catalogue) if a condition needs arithmetic first: compute the number there, register it as a variable here. - IDENT(period) values must be registered via SetIndicatorValue() before Interpret() is called - this library never calls iRSI/iMA/etc itself, it only interprets already-computed readings you hand it. - Keywords (IF THEN ELSE AND OR NOT) are reserved and cannot be used as variable names, indicator names or action names. - If no branch's condition is true and the rule has no trailing plain ELSE, Interpret() reports success with matched=false rather than an error - a well-formed rule simply had nothing to say on that tick.
Script · MT4 · v1.0 · Free
About Trading Rule DSL Interpreter
Trading Rule DSL Interpreter is a script for MetaTrader 4. It has 1 setting, such as Custom rule. MQL source code included.
Files in the download: TradingRuleDsl.mqh, TradingRuleDsl.txt, TradingRuleDslDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Custom rule | IF RSI(14) > 70 AND MA(20) < CLOSE THEN |
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: RSI · Moving Average · MT4 Script · MQL4 Source Code