Loading…

Signal Combinator Algebra

Most languages let you compose signals by storing "the function that evaluates this node" directly - a closure, a delegate, a function pointer. MQL4 has none of those. This header's whole reason for existing is the workaround: every signal is a SignalNode struct carrying an ENUM_SIGNAL_KIND tag (SIG_CONST / SIG_EXTERNAL / SIG_AND / SIG_OR / SIG_NOT / SIG_VOTE), stored in a small fixed-size pool array, and SignalEvaluate() recurses through the tree by SWITCHING on that tag rather than calling anything indirectly. It is more verbose than a real closure and the pool size is a compile-time constant (SIGNAL_MAX_NODES, 64 by default) rather than unbounded - but it is the entire vocabulary MQL4 actually offers for "compose objects that get evaluated later", and every function in this header exists to make that pattern usable rather than hand-rolled fresh in every EA that needs it. A tree is built once from small pieces - SignalConst() for a fixed true/false, SignalExternal() for a leaf whose truth is supplied at evaluation time (a moving-average cross, an RSI level, anything the EA already computes), then SignalAnd()/SignalOr()/SignalNot() combine two existing nodes into a new one, and SignalVote() takes up to eight children with individual weights and a threshold for a genuine weighted-majority signal rather than a plain unweighted AND/OR. SignalEvaluate(root, externalValues[]) walks the whole tree in one call, re-evaluating it fresh every tick from the current external readings - the tree's SHAPE is built once, but its RESULT is never cached or stale. Notes and limitations - SIGNAL_MAX_NODES (64) and SIGNAL_MAX_CHILDREN (8) are compile-time constants at the top of the header - raise them there if a tree genuinely needs more nodes or a vote with more than 8 children. - SignalAnd/SignalOr take exactly two children each; chain several calls (SignalAnd(a, SignalAnd(b, c))) for more than two, or use SignalVote with every weight set to 1 and the threshold set to the required count for an N-way AND/OR without nesting. - externalValues[] is indexed by the externalId given to SignalExternal() when the tree was built - keep those ids as named #define constants (as the demo does) rather than magic numbers, so the array size and the indices used to fill it stay in sync as the tree grows. - The pool is a set of global arrays; SignalReset() clears the node COUNT, not any nodes already referenced by index elsewhere - do not hold node indices across a SignalReset() call.

Script · MT4 · v1.0 · Free

About Signal Combinator Algebra

Signal Combinator Algebra is a script for MetaTrader 4. MQL source code included.

Files in the download: SignalCombinatorAlgebra.mqh, SignalCombinatorAlgebra.txt, SignalCombinatorAlgebraDemo.mq4.

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: Buy/Sell Arrows · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria