Code Library / Bollinger Bands

MQL5 BB Mean Reversion

Mean reversion using Bollinger Bands for ranging markets.

Language: MQL5 · Category: Bollinger Bands · Tags: Bollinger Bands, mean reversion, MQL5, range trading, strategy

Source code

#include <Trade/Trade.mqh>
CTrade trade;
void BBMeanRev(int p,double d,double lots) {
   double u[],l[]; int h=iBands(_Symbol,PERIOD_CURRENT,p,0,d,PRICE_CLOSE);
   CopyBuffer(h,1,0,1,u); CopyBuffer(h,2,0,1,l);
   double bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
   if(PositionsTotal()==0) {
      if(bid<=l[0]) trade.Buy(lots,_Symbol);
      if(bid>=u[0]) trade.Sell(lots,_Symbol);
   }
   IndicatorRelease(h);
}

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