Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Bollinger Bands EMA Indicator
#1

The default MT4 Bollinger Bands indicator does not have as option for using a EMA instead of a SMA. I have coded a custom Bollinger Bands indicator that has the EMA option. Below is a screenshot of the Bollinger Bands with EMA. I have chosen 2 standard deviation with EMA 21.

[Image: BBEMA.png]

If you want the MQL4 code for the custom Bollinger Bands with EMA option as well as the SMA option, you can copy that too from below:
Code:
//+------------------------------------------------------------------+
//|                                                       BB_EMA.mq4 |
//|                                                     Ahmad Hassam |
//|                                      https://www.doubledoji.com/ |
//+------------------------------------------------------------------+
#property copyright "Ahmad Hassam"
#property link      "https://www.doubledoji.com/"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 DeepSkyBlue
#property indicator_color3 DeepSkyBlue



// External parameters
//choose the number of bars to use
input int BandsPeriod = 21; //Smoothing Period
input int BandsShift = 0;   //Shift
//---choose which averaging method to use
input ENUM_MA_METHOD BandsMethod = MODE_EMA;  //Smoothing Methods
//---choose which price to use
input ENUM_APPLIED_PRICE priceBB = PRICE_CLOSE; //Applied Price

//---choose the number of standard deviations to use
input int Deviations = 2; // No Of Standard Deviations
extern int K=20; // Standard Deviation Lookback
double StdDev=0.0;
//---- buffers
double EMA[];
double upperBand[];
double lowerBand[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,EMA);
SetIndexLabel(0,"EMA");
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,upperBand);
SetIndexLabel(1,"UpperBand");
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,lowerBand);
SetIndexLabel(2,"LowerBand");
//----

return(0);
}

//+------------------------------------------------------------------+
//| Custom Bollinger Bands Indicator                              |
//+------------------------------------------------------------------+

// start() function
  int start()
 {
 int Count=0;
 int limit=0;
 int counted_bars=IndicatorCounted();
 int CalculateBars = Bars - counted_bars;
//  double StdDev;

/*
  calculate the moving average
  double  iMA(
  string       symbol,           // symbol
  int          timeframe,        // timeframe
  int          ma_period,        // MA averaging period
  int          ma_shift,         // MA shift
  int          ma_method,        // averaging method
  int          applied_price,    // applied price
  int          shift             // shift
  );
 
calculate the standard deviation  
  double  iStdDev(
  string       symbol,           // symbol
  int          timeframe,        // timeframe
  int          ma_period,        // MA averaging period
  int          ma_shift,         // MA shift
  int          ma_method,        // MA averaging method
  int          applied_price,    // applied price
  int          shift             // shift
  );
*/
// NULL means current chart symbol
// 0 means current chart timeframe
if (counted_bars==0)
     {
     limit=Bars-1;
     
     }
  if (counted_bars>0)
     {
     limit=CalculateBars-1;
     
     }

for(Count = limit-40; Count >= 0; Count--)
{
        EMA[Count] = iMA(NULL,0,BandsPeriod,BandsShift,BandsMethod,priceBB,Count);
        StdDev = iStdDev(NULL,0,BandsPeriod,BandsShift,BandsMethod,priceBB,Count);
        upperBand[Count] = EMA[Count] + (StdDev * Deviations);
        lowerBand[Count] = EMA[Count] - (StdDev * Deviations);
       
       
}
//----
  return(0);
 }
//+------------------------------------------------------------------+

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply
#2

This is a slight modification of the standard Bollinger Bands indicator. Instead of calculating the standard deviation of closing price, I have calculated the standard deviation of log returns of closing price and plotted as two bands with mean as EMA. The standard deviation bands are 3 standard deviations away from the EMA. Below is a screenshot of this new indicator!

[Image: ModifiedBB.png]

If you want to the MQL4 code, below I have posted the MQL4 code as well.

Code:
//+------------------------------------------------------------------+
//|                                                   ModifiedBB.mq4 |
//|                                                     Ahmad Hassam |
//|                                      https://www.doubledoji.com/ |
//+------------------------------------------------------------------+
#property copyright "Ahmad Hassam"
#property link      "https://www.doubledoji.com/"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 DeepSkyBlue
#property indicator_color3 DeepSkyBlue


#include <General1.mqh>
// External parameters
//choose the number of bars to use
input int BandsPeriod = 21; //Smoothing Period
input int BandsShift = 0;   //Shift
//---choose which averaging method to use
input ENUM_MA_METHOD BandsMethod = MODE_EMA;  //Smoothing Methods
//---choose which price to use
input ENUM_APPLIED_PRICE priceBB = PRICE_CLOSE; //Applied Price

//---choose the number of standard deviations to use
input int Deviations = 3; // No Of Standard Deviations
extern int K=40; // Standard Deviation Lookback
//---- buffers
double EMA[];
double upperBand[];
double lowerBand[];
double logReturn[];
double std[];



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
 {
//custom indicator buffers mapping
IndicatorBuffers(3);
 
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,EMA);
SetIndexLabel(0,"EMA");
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,upperBand);
SetIndexLabel(1,"UpperBand");
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,lowerBand);
SetIndexLabel(2,"LowerBand");
//----
ArraySetAsSeries(EMA,true);
ArraySetAsSeries(upperBand,true);
ArraySetAsSeries(lowerBand,true);
ArraySetAsSeries(std,true);
ArrayResize(std,Bars);
ArraySetAsSeries(logReturn,true);
ArrayResize(logReturn,Bars);


     
//---
  return(INIT_SUCCEEDED);
 }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate (const int rates_total,      // size of input time series
                const int prev_calculated,  // bars handled in previous call
                const datetime& time[],     // Time
                const double& open[],       // Open
                const double& high[],       // High
                const double& low[],        // Low
                const double& close[],      // Close
                const long& tick_volume[],  // Tick Volume
                const long& volume[],       // Real Volume
                const int& spread[] )        // Spread
 
 
 {      
     
     int bars1 = rates_total - 2;
     if (prev_calculated > 0) bars1 =  rates_total - (prev_calculated - 1);
     for ( int i =  bars1; i >= 0; i--)
     {
     
        EMA[i] = iMA(NULL,0,BandsPeriod,BandsShift,BandsMethod,priceBB,i);
        logReturn[i] = MathLog(Close[i])- MathLog(Close[i+1]);
       
     
     }
     int bars2 = rates_total - K - 1;
     if (prev_calculated > 0) bars2 =  rates_total - (prev_calculated - 1);
     for ( int i =  bars2; i >= 0; i--)
     {
     
        std[i]=standardDeviation(logReturn,K,i);
        upperBand[i] = EMA[i]*(1 + (std[i]*Deviations));
        lowerBand[i] = EMA[i]*(1- (std[i]*Deviations));
     
     }
     
     
     return(rates_total);
 }
//+------------------------------------------------------------------+

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)