06-24-2018, 02:06 PM
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.
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:
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/