Code Library / Breakout

MQL4 Channel Breakout EA

Donchian Channel breakout strategy.

Language: MQL4 · Category: Breakout · Tags: channel breakout, Donchian, MQL4, EA, trend following

Source code

input int ChanP=20; input double Lots=0.1; input int Magic=22222;
void OnTick() {
   double upper=High[iHighest(Symbol(),0,MODE_HIGH,ChanP,1)];
   double lower=Low[iLowest(Symbol(),0,MODE_LOW,ChanP,1)];
   bool hasPos=false;
   for(int i=0;i<OrdersTotal();i++)
      if(OrderSelect(i,SELECT_BY_POS)&&OrderMagicNumber()==Magic) hasPos=true;
   if(Close[0]>upper&&!hasPos)
      OrderSend(Symbol(),OP_BUY,Lots,Ask,3,lower,0,"Break",Magic);
   if(Close[0]<lower&&!hasPos)
      OrderSend(Symbol(),OP_SELL,Lots,Bid,3,upper,0,"Break",Magic);
}

How to use this snippet

This is a building block, not a finished trading system. Paste it into your own MQL4 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