6.9 KiB
Article-20856-Graph-Theory-Traversal-Breadth-First-Search-Applied-in-Trading
This repository is an article-derived reference project based on the original MQL5 article. It does not claim to reproduce the full original source code unless files are explicitly attached.
Overview
This project documents a graph-theory-driven trading system centered on the application of Breadth-First Search (BFS) to market structure analysis. The architecture combines:
- an MQL5 Expert Advisor for swing detection, graph construction, BFS traversal, and trade execution
- a directed graph representation of market structure using swing highs and swing lows as nodes
- a level-order traversal engine for market bias evaluation and trading decision support
The article describes how historical price action can be transformed into a directed graph, where each swing point becomes a node and each structural transition becomes an edge. Breadth-First Search is then used to traverse the graph layer by layer, allowing the system to evaluate bullish and bearish structure progressively and generate an objective market bias.
Original Article
Article ID: 20856 Author: Hlomohang John Borotho Publication Date: 2026.01.30 Category: Experts URL: https://www.mql5.com/en/articles/20856
Repository Purpose
This repository should be treated as a reference or reconstruction project derived from the article content. Its purpose is to preserve the article’s technical structure and described file layout for:
- studying graph-theory applications in algorithmic trading
- understanding how market structure can be modeled as a directed graph
- exploring Breadth-First Search traversal for market bias analysis
- reconstructing the BFS-based Expert Advisor architecture
- serving as a foundation for further experimentation with graph-based trading systems
Key Concepts
- Graph Theory
- Breadth-First Search (BFS)
- Level-Order Traversal
- Directed Graphs
- Swing High Detection
- Swing Low Detection
- Market Structure Analysis
- Bullish and Bearish Classification
- Graph Traversal-Based Bias Calculation
- Trading Permission Filtering
- Market Visualization
- Structure-Based Trading
Algorithm / Architecture Summary
Historical Context Selection
The trader selects the historical scope used to build the market graph:
- Previous Bars
- Previous Days
- Yesterday Only
This determines the portion of market structure used for analysis.
Swing Detection
Historical price data is scanned for valid swing highs and swing lows.
A swing high is identified when its high exceeds surrounding highs over a configurable swing period.
A swing low is identified when its low is lower than surrounding lows over the same period.
Each detected swing becomes a graph node.
Graph Construction
Detected swings are transformed into a directed graph.
Each node contains:
- swing price
- swing type
- timestamp
- BFS level
- bullish/bearish classification
Edges are created from older swings to newer swings, ensuring that graph traversal follows the natural progression of time.
Root Selection
The BFS traversal root is selected according to the chosen history mode.
Possible roots include:
- oldest swing within a bar lookback range
- first swing within a multi-day period
- first swing detected during yesterday's session
This root serves as the starting point for traversal.
Breadth-First Search Traversal
A queue-based BFS algorithm traverses the graph level by level.
During traversal:
- each node receives a BFS level
- structural relationships are evaluated
- directional classifications are assigned
This process produces a hierarchical representation of market structure.
Structure Classification
Each swing is compared against previous swings of the same type.
Examples:
- Higher High → Bullish
- Lower High → Bearish
- Higher Low → Bullish
- Lower Low → Bearish
These classifications become the directional attributes of graph nodes.
Market Bias Calculation
Each visited node contributes to a weighted bias score.
Lower BFS levels receive larger weights because they represent structure closer to the traversal root.
The final normalized score becomes the current market bias.
Trading Permissions
Market bias is translated into directional permissions.
Examples:
- Bias > Threshold → Allow Buy
- Bias < -Threshold → Allow Sell
- Otherwise → Neutral
The BFS system acts as a structural filter before trade execution.
Trade Execution
The EA validates:
- directional bias
- swing-break confirmation
- existing positions
- bias reversal conditions
Orders are executed using the MQL5 CTrade framework.
Visualization
The EA visualizes:
- swing nodes
- graph edges
- BFS levels
- bullish and bearish classifications
- current market bias
- trading status
This allows traders to inspect the graph and verify traversal results directly on the chart.
Mentioned or Attached Files
Explicitly Attached Files
The article states that the following files are contained in the archive:
- BFS Trading EA— Main Expert Advisor
- BFS Graph Structures — Node and edge definitions
- BFS Traversal Engine — Queue management and traversal logic
- BFS Visualization — Chart rendering functions
Files Mentioned in the Text
- Graph visualization objects
- Trading logs
- Market structure statistics
Statistics
- Graph Theory Branch: Traversal
- Traversal Algorithm: Breadth-First Search (BFS)
- Graph Type: Directed Graph
- Trading Components: 1 EA
- History Modes: 3
- Node Types: 2 (Swing High, Swing Low)
- Traversal Method: Level-Order
- Market Bias Output: Continuous Score
- Execution Engine: CTrade
Tags
MQL5, MetaTrader5, Graph-Theory, Breadth-First-Search, BFS, Market-Structure, Swing-Trading, Algorithmic-Trading, Expert-Advisor, Directed-Graphs, Graph-Traversal
Difficulty
Advanced
Requires familiarity with:
- MQL5 Expert Advisor development
- Market structure concepts
- Swing high and swing low analysis
- Graph theory fundamentals
- Breadth-First Search traversal
- Queue-based algorithms
- Trading system design
- Risk management principles
Limitations
- The repository is derived from article content and should be treated as a reference reconstruction unless the archive files are actually present.
- BFS evaluates structural relationships and does not predict future prices.
- Results depend on swing detection parameters and historical context selection.
- Market conditions may affect structural interpretations.
- Visualization complexity may increase on lower timeframes with large numbers of detected swings.
- No performance claims beyond the article description should be assumed.
- The article describes code and file roles, but full verification of attached source completeness is not possible from the processed input alone.
Reference
Original MQL5 Article: https://www.mql5.com/en/articles/20856