Created
May 25, 2023 18:13
-
-
Save brenoperucchi/ea8ef194cdbaf604277b2aa05d7f47a5 to your computer and use it in GitHub Desktop.
Bollinger Band Indicator EMA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| BB.mq5 | | |
//| Copyright 2009, MetaQuotes Software Corp. | | |
//| http://www.mql5.com | | |
//+------------------------------------------------------------------+ | |
#property copyright "2009, MetaQuotes Software Corp." | |
#property link "http://www.mql5.com" | |
#property description "Bollinger Bands" | |
#include <MovingAverages.mqh> | |
//--- | |
#property indicator_chart_window | |
#property indicator_buffers 4 | |
#property indicator_plots 3 | |
#property indicator_type1 DRAW_LINE | |
#property indicator_color1 LightSeaGreen | |
#property indicator_type2 DRAW_LINE | |
#property indicator_color2 LightSeaGreen | |
#property indicator_type3 DRAW_LINE | |
#property indicator_color3 LightSeaGreen | |
#property indicator_label1 "Bands middle" | |
#property indicator_label2 "Bands upper" | |
#property indicator_label3 "Bands lower" | |
//--- input parametrs | |
input int InpBandsPeriod=30; // Period | |
input int InpBandsShift=0; // Shift | |
input double InpBandsDeviations=2.4; // Deviation | |
//--- global variables | |
int ExtPlotBegin=0; | |
//---- indicator buffer | |
double middleBand[]; | |
double upperBand[]; | |
double lowerBand[]; | |
double DeviationBuffer[]; | |
//+------------------------------------------------------------------+ | |
//| Custom indicator initialization function | | |
//+------------------------------------------------------------------+ | |
void OnInit() | |
{ | |
//--- check for input values | |
//--- define buffers | |
SetIndexBuffer(0,middleBand); | |
SetIndexBuffer(1,upperBand); | |
SetIndexBuffer(2,lowerBand); | |
SetIndexBuffer(3,DeviationBuffer,INDICATOR_CALCULATIONS); | |
//--- set index labels | |
PlotIndexSetString(0,PLOT_LABEL,"Bands("+string(InpBandsPeriod)+") Middle"); | |
PlotIndexSetString(1,PLOT_LABEL,"Bands("+string(InpBandsPeriod)+") Upper"); | |
PlotIndexSetString(2,PLOT_LABEL,"Bands("+string(InpBandsPeriod)+") Lower"); | |
//--- indicator name | |
IndicatorSetString(INDICATOR_SHORTNAME,"Bollinger Bands"); | |
//--- indexes draw begin settings | |
ExtPlotBegin=InpBandsPeriod-1; | |
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpBandsPeriod); | |
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpBandsPeriod); | |
PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,InpBandsPeriod); | |
//--- indexes shift settings | |
PlotIndexSetInteger(0,PLOT_SHIFT,InpBandsShift); | |
PlotIndexSetInteger(1,PLOT_SHIFT,InpBandsShift); | |
PlotIndexSetInteger(2,PLOT_SHIFT,InpBandsShift); | |
//--- number of digits of indicator value | |
IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); | |
//---- OnInit done | |
} | |
//+------------------------------------------------------------------+ | |
//| Custom indicator iteration function | | |
//+------------------------------------------------------------------+ | |
int OnCalculate(const int rates_total, | |
const int prev_calculated, | |
const int begin, | |
const double &price[]) | |
{ | |
//--- variables | |
int pos; | |
//--- indexes draw begin settings, when we've recieved previous begin | |
if(ExtPlotBegin!=InpBandsPeriod+begin) | |
{ | |
ExtPlotBegin=InpBandsPeriod+begin; | |
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPlotBegin); | |
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtPlotBegin); | |
PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtPlotBegin); | |
} | |
//--- check for bars count | |
if(rates_total<ExtPlotBegin) | |
return(0); | |
//--- starting calculation | |
if(prev_calculated>1) pos=prev_calculated-1; | |
else pos=0; | |
//--- main cycle | |
for(int i=pos;i<rates_total && !IsStopped();i++) | |
{ | |
//--- middle line | |
//middleBand[i]=SimpleMA(i,InpBandsPeriod,price); | |
int handleEma = iMA(Symbol(),PERIOD_CURRENT, InpBandsPeriod, 0, MODE_EMA, PRICE_CLOSE); | |
CopyBuffer(handleEma, 0, 0, rates_total, middleBand); | |
//--- calculate and write down StdDev | |
DeviationBuffer[i]=StdDev_Func(i,price,middleBand,InpBandsPeriod); | |
//--- upper line | |
upperBand[i]=middleBand[i]+InpBandsDeviations*DeviationBuffer[i]; | |
//--- lower line | |
lowerBand[i]=middleBand[i]-InpBandsDeviations*DeviationBuffer[i]; | |
//--- | |
} | |
//---- OnCalculate done. Return new prev_calculated. | |
return(rates_total); | |
} | |
//+------------------------------------------------------------------+ | |
//| Calculate Standard Deviation | | |
//+------------------------------------------------------------------+ | |
double StdDev_Func(int position,const double &price[],const double &MAprice[],int period) | |
{ | |
//--- variables | |
double StdDev_dTmp=0.0; | |
//--- check for position | |
if(position<period) return(StdDev_dTmp); | |
//--- calcualte StdDev | |
for(int i=0;i<period;i++) StdDev_dTmp+=MathPow(price[position-i]-MAprice[position],2); | |
StdDev_dTmp=MathSqrt(StdDev_dTmp/period); | |
//--- return calculated value | |
return(StdDev_dTmp); | |
} | |
//+------------------------------------------------------------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment