Sequence Alignment Tools
A raw "how many events differ" count between two sequences (a day's signal log against a backtest's expected one, or today's fills against yesterday's) hides the actual shape of the difference - it cannot say whether an extra event was inserted, one was dropped, or one was simply replaced by another at the same position. This header answers that properly with two related but distinct algorithms. SatLcsLength()/SatLcsSequence() find the longest common subsequence - the longest run of elements that appears in both sequences in the same relative order, not necessarily contiguously. This is the right tool for "what is the common backbone these two sequences share." SatEditDistance()/SatEditAlignment() compute the minimum number of insert/delete/substitute operations turning one sequence into the other, and SatEditAlignment() additionally returns the full step-by- step trace (MATCH/SUBSTITUTE/DELETE/INSERT per element) rather than just the final count - this is the right tool for "show me exactly where the two sequences diverge." Both DP tables are built into a flat 1D array with manual row*stride+col indexing rather than a genuine 2D array, because MQL4 can only resize the leftmost dimension of a multi- dimensional array at runtime; a table whose row AND column counts are both runtime values (both sequence lengths here) has nowhere to live as a true 2D dynamic array. SatSimilarityRatio() gives a single Dice-style score in [0,1] derived from the LCS length, for cases that just need a threshold check ("are these two sessions similar enough to flag") rather than the full alignment detail. Notes and limitations - Both DP tables cost O(lenA * lenB) time and memory - fine for the short-to-medium event sequences (tens to a few hundred elements) a trading log or signal history realistically produces in one pass, not intended for comparing multi-thousand-element sequences. - Elements are compared with plain string equality (case-sensitive, exact match). Normalise labels (e.g. uppercase) before calling if that matters for your data. - SatEditAlignment()'s traceback tie-break order is fixed (MATCH, SUBSTITUTE, DELETE, INSERT) whenever more than one predecessor cell ties for the optimal value. This makes the output deterministic and repeatable, but a different valid minimum-cost alignment can exist for the same pair of sequences - if your use case cares which specific alignment is chosen among several tied optima, this is not configurable. - All costs are unit (1) per insert/delete/substitute. There is no weighted-cost variant (e.g. making a HOLD<->HOLD substitution cheaper than a BUY<->SELL one) - callers needing that must pre/post-process the alignment output themselves.
Script · MT4 · v1.0 · Free
About Sequence Alignment Tools
Sequence Alignment Tools is a script for MetaTrader 4. MQL source code included.
Files in the download: SequenceAlignmentTools.mqh, SequenceAlignmentTools.txt, SequenceAlignmentToolsDemo.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: Buy/Sell Arrows · MT4 Script · MQL4 Source Code