Code Library / Grid Trading

MQL5 Hedging Grid Strategy

Hedging grid strategy using pending orders at calculated levels.

Language: MQL5 · Category: Grid Trading · Tags: grid, hedging, MQL5, strategy, pending orders

Source code

#include <Trade/Trade.mqh>
CTrade trade;
input double GridPips=25; input double Lots=0.01; input int Levels=5;
void SetupGrid() {
   double step=GridPips*SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   double bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
   for(int i=1;i<=Levels;i++) {
      trade.BuyLimit(Lots,NormalizeDouble(bid-step*i,_Digits),_Symbol);
      trade.SellLimit(Lots,NormalizeDouble(bid+step*i,_Digits),_Symbol);
   }
}

How to use this snippet

This is a building block, not a finished trading system. Paste it into your own MQL5 project, compile it, and test it on a demo account with your broker's real spreads before relying on it. If MetaEditor reports errors, the compilation error fixer can help.

Related snippets