Loading…

Graph Algorithms Toolkit

An adjacency list is inherently ragged - one node might have two neighbours, another nine - and neither platform allows anything but the leftmost dimension of a true 2D array to be dynamic, which rules out int neighbours[][] directly on either. This header uses the struct-of-dynamic-arrays pattern instead: each SGraphNode carries its own neighbours[] and weights[] arrays that resize independently, which is exactly the ragged shape a graph needs. Two shortest-path algorithms are included. Dijkstra covers the ordinary non-negative-weight case with a simple O(V^2) node selection - fine for the tens of nodes a currency or watchlist graph actually has; BinaryHeap.mqh elsewhere in this catalogue is the building block for a heap-based version if a graph ever grows large enough to need O((V+E) log V) instead. Bellman-Ford is included specifically for triangular arbitrage: take -log(exchange rate) as an edge's weight, and compounding rates around a loop becomes SUMMING weights around that loop, because log turns multiplication into addition. A profitable arbitrage loop - rates that multiply to more than 1.0 - is then exactly a negative-weight cycle, which Bellman-Ford already detects as a side effect of running one relaxation pass past normal convergence. No separate arbitrage-specific search is needed once the weights are framed this way, and unlike a bespoke three-currency check it is not limited to triangles - any loop length is covered by the same detection. Topological sort (Kahn's algorithm) and cycle detection (three-colour DFS) round the header out for dependency-ordering problems, such as sequencing a chain of correlated positions or validating that a set of derived indicator dependencies has no circular reference. Notes and limitations - Dijkstra requires non-negative weights; a negative edge silently produces wrong distances rather than an error. Use GraphBellmanFord() whenever a weight can be negative, which -log(rate) edges always can be for a rate below 1.0. - Node lookup by label (GraphFindByLabel) is a linear scan - fine for a handful of currencies or symbols, not for a graph with thousands of nodes. - Bellman-Ford's negative-cycle flag reports that one exists somewhere reachable from source; it does not extract the cycle's node sequence - only that an arbitrage opportunity exists, not automatically which three (or more) legs to trade. Reconstructing the exact loop is a reasonable next step for a caller that wants to act on the signal automatically. - Nothing here executes a trade or sizes a position - this is graph theory only. Any strategy that acts on a detected arbitrage loop still owns its own execution, spread, latency and slippage assumptions on either platform.

Script · MT5 · v1.0 · Free

About Graph Algorithms Toolkit

Graph Algorithms Toolkit is a script for MetaTrader 5. MQL source code included.

Files in the download: GraphAlgorithms.mq5.mqh, GraphAlgorithms.txt, GraphAlgorithmsDemo.ex5, GraphAlgorithmsDemo.mq5.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: Arbitrage · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria