SMC Trading Bot - MQL5 Core

MQL5 implementation of the Smart Money Concepts strategy

Core Features

Structural Analysis

Real-time detection of BOS/CHoCH patterns using N=2 pivots on 5M timeframe with customizable sensitivity.

OB/FVG Detection

Automated identification of Order Blocks and Fair Value Gaps with strict confluence requirements.

Risk Management

Built-in R:R 1:2 ratio with adjustable TP/SL levels and position sizing based on account risk parameters.

MQL5 Implementation

//+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Set up indicators and parameters int handleMA = iMA(NULL, PERIOD_M5, 50, 0, MODE_SMA, PRICE_CLOSE); // Validate initialization if(handleMA == INVALID_HANDLE) { Print("Failed to create MA indicator"); return(INIT_FAILED); } return(INIT_SUCCEEDED); }
//+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { static datetime lastBarTime = 0; datetime currentBarTime = iTime(NULL, PERIOD_M5, 0); if(currentBarTime == lastBarTime) return; // Wait for new bar lastBarTime = currentBarTime; // 1. Detect Pivots (N=2) int pivots[]; DetectPivots(pivots, 2); // 2. Analyze Market Structure string bias = AnalyzeStructure(); // 3. Check for Trade Setup if(bias != "Accumulative") { CheckTradeSetup(bias); } }

System Architecture

SMC Bot Architecture
1. Data Layer

MT5 Price Feed & Indicators

2. Analysis Layer

Pivot Detection & Structure Analysis

3. Strategy Layer

OB/FVG Detection & Entry Logic

4. Execution Layer

Order Management & Risk Control

Key Functions

1. DetectPivots()

bool DetectPivots(int &pivots[], int N) { int totalBars = Bars(NULL, PERIOD_M5); ArrayResize(pivots, totalBars); ArrayInitialize(pivots, 0); for(int i = N; i < totalBars - N; i++) { double high = iHigh(NULL, PERIOD_M5, i); double low = iLow(NULL, PERIOD_M5, i); bool isSwingHigh = true, isSwingLow = true; for(int j = i - N; j <= i + N; j++) { if(i == j) continue; if(iHigh(NULL, PERIOD_M5, j) >= high) isSwingHigh = false; if(iLow(NULL, PERIOD_M5, j) <= low) isSwingLow = false; } if(isSwingHigh) pivots[i] = 1; // Swing High else if(isSwingLow) pivots[i] = -1; // Swing Low } return true; }

2. AnalyzeStructure()

string AnalyzeStructure() { int pivots[]; if(!DetectPivots(pivots, 2)) return "Accumulative"; int swingCount = 0; int swingIndices[3]; double swingPrices[3]; string swingTypes[3]; for(int i = ArraySize(pivots)-1; i >= 0 && swingCount < 3; i--) { if(pivots[i] != 0) { swingIndices[swingCount] = i; swingPrices[swingCount] = (pivots[i] == 1) ? iHigh(NULL, PERIOD_M5, i) : iLow(NULL, PERIOD_M5, i); swingTypes[swingCount] = (pivots[i] == 1) ? "H" : "L"; swingCount++; } } if(swingCount < 3) return "Accumulative"; string P1 = swingTypes[0], P2 = swingTypes[1], P3 = swingTypes[2]; double P1_price = swingPrices[0], P2_price = swingPrices[1], P3_price = swingPrices[2]; double close = iClose(NULL, PERIOD_M5, 0); string bias = "Accumulative"; if(P2 == "H" && close > P2_price) { if(P3 == "L" && P2_price > P3_price) bias = "Bullish"; else if(P3 == "L" && P2_price < P3_price) bias = "Bullish"; } else if(P2 == "L" && close < P2_price) { if(P3 == "H" && P2_price < P3_price) bias = "Bearish"; else if(P3 == "H" && P2_price > P3_price) bias = "Bearish"; } return bias; }

3. CheckTradeSetup()

void CheckTradeSetup(string bias) { if(IsTradeContextBusy()) return; bool isBullish = (bias == "Bullish"); double entry, sl, tp; double lotSize = OptimalLotSize(); // 1. Try OB/FVG Setup (Priority 1) if(FindObFvgSetup(isBullish, entry, sl, tp)) { PlaceOrder(isBullish ? ORDER_TYPE_BUY : ORDER_TYPE_SELL, lotSize, entry, sl, tp, "OB/FVG Confluence"); } // 2. Fallback to Fib 30% (Priority 2) else if(FindFibSetup(isBullish, entry, sl, tp)) { PlaceOrder(isBullish ? ORDER_TYPE_BUY : ORDER_TYPE_SELL, lotSize, entry, sl, tp, "30% Fib Retracement"); } }
SMC Trading Bot

MQL5 implementation of Smart Money Concepts

© 2023 Forex AutoPilot. All rights reserved.