Code Library / Bollinger Bands

MQL4 Bollinger %B

Bollinger %B showing price position relative to bands.

Language: MQL4 · Category: Bollinger Bands · Tags: Bollinger %B, MQL4, indicator, oscillator, bands

Source code

double BBPercentB(int p,double d,int s) {
   double u=iBands(Symbol(),0,p,d,0,PRICE_CLOSE,MODE_UPPER,s);
   double l=iBands(Symbol(),0,p,d,0,PRICE_CLOSE,MODE_LOWER,s);
   if(u-l==0) return 0.5;
   return (Close[s]-l)/(u-l);
}
double Bandwidth(int p,double d,int s) {
   double u=iBands(Symbol(),0,p,d,0,PRICE_CLOSE,MODE_UPPER,s);
   double l=iBands(Symbol(),0,p,d,0,PRICE_CLOSE,MODE_LOWER,s);
   double m=iBands(Symbol(),0,p,d,0,PRICE_CLOSE,MODE_MAIN,s);
   return (u-l)/m*100;
}

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