Custom BBs instead of MTF-BB for MT5
#property copyright "2010, Young. K. Han."#property link "http://www.mql5.com"#property description "BB7-1"#include <MovingAverages.mqh>#property indicator_chart_window#property indicator_buffers 6#property indicator_plots 5#property indicator_type1 DRAW_LINE#property indicator_color1 Orange#property indicator_type2 DRAW_LINE#property indicator_color2 Orange#property indicator_type3 DRAW_LINE#property indicator_color3 Orange#property indicator_width3 2#property indicator_type4 DRAW_LINE#property indicator_color4 Orange#property indicator_type5 DRAW_LINE#property indicator_color5 Orange#property indicator_label1 "BB Upper3"#property indicator_label2 "BB Upper2"#property indicator_label3 "BB Middle"#property indicator_label4 "BB Lower2"#property indicator_label5 "BB Lower3"//--- input parametrsinput int InpBandsPeriod=25;input int InpBandsShift=0;input double InpBandsDeviations=1.0;//--- global variablesint ExtBandsPeriod,ExtBandsShift;double ExtBandsDeviations;int ExtPlotBegin=0;//---- indicator bufferdouble ExtMLBuffer[];double ExtTLBuffer3[],ExtTLBuffer2[];double ExtBLBuffer3[],ExtBLBuffer2[];double ExtStdDevBuffer[];//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+void OnInit() {//--- check for input values if(InpBandsPeriod<2) { ExtBandsPeriod=25; printf("Incorrect value for input variable InpBandsPeriod=%d. Indicator will use value=%d for calculations.",InpBandsPeriod,ExtBandsPeriod); } else ExtBandsPeriod=InpBandsPeriod; if(InpBandsShift<0) { ExtBandsShift=0; printf("Incorrect value for input variable InpBandsShift=%d. Indicator will use value=%d for calculations.",InpBandsShift,ExtBandsShift); } else ExtBandsShift=InpBandsShift; if(InpBandsDeviations==0.0) { ExtBandsDeviations=2.0; printf("Incorrect value for input variable InpBandsDeviations=%f. Indicator will use value=%f for calculations.",InpBandsDeviations,ExtBandsDeviations); } else ExtBandsDeviations=InpBandsDeviations;//--- define buffers SetIndexBuffer(0,ExtTLBuffer3); SetIndexBuffer(1,ExtTLBuffer2); SetIndexBuffer(2,ExtMLBuffer); SetIndexBuffer(3,ExtBLBuffer2); SetIndexBuffer(4,ExtBLBuffer3); SetIndexBuffer(5,ExtStdDevBuffer,INDICATOR_CALCULATIONS);//--- set index labels PlotIndexSetString(0,PLOT_LABEL,"Bands("+string(ExtBandsPeriod)+") Upper3"); PlotIndexSetString(1,PLOT_LABEL,"Bands("+string(ExtBandsPeriod)+") Upper2"); PlotIndexSetString(2,PLOT_LABEL,"Bands("+string(ExtBandsPeriod)+") Middle"); PlotIndexSetString(3,PLOT_LABEL,"Bands("+string(ExtBandsPeriod)+") Lower2"); PlotIndexSetString(4,PLOT_LABEL,"Bands("+string(ExtBandsPeriod)+") Lower3");//--- indicator name IndicatorSetString(INDICATOR_SHORTNAME,"BB7-1");//--- indexes draw begin settings ExtPlotBegin=ExtBandsPeriod-1; PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtBandsPeriod); PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtBandsPeriod); PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtBandsPeriod); PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,ExtBandsPeriod); PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,ExtBandsPeriod);//--- indexes shift settings PlotIndexSetInteger(0,PLOT_SHIFT,ExtBandsShift); PlotIndexSetInteger(1,PLOT_SHIFT,ExtBandsShift); PlotIndexSetInteger(2,PLOT_SHIFT,ExtBandsShift); PlotIndexSetInteger(3,PLOT_SHIFT,ExtBandsShift); PlotIndexSetInteger(4,PLOT_SHIFT,ExtBandsShift);//--- number of digits of indicator value IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); }//+------------------------------------------------------------------+//| 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 received previous begin if(ExtPlotBegin!=ExtBandsPeriod+begin) { ExtPlotBegin=ExtBandsPeriod+begin; PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPlotBegin); PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtPlotBegin); PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtPlotBegin); PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,ExtPlotBegin); PlotIndexSetInteger(4,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;i++) { //--- middle line ExtMLBuffer[i]=SimpleMA(i,ExtBandsPeriod,price); //--- calculate and write down StdDev ExtStdDevBuffer[i]=StdDev_Func(i,price,ExtMLBuffer,ExtBandsPeriod); //--- upper line ExtTLBuffer3[i]=ExtMLBuffer[i]+3*ExtBandsDeviations*ExtStdDevBuffer[i]; ExtTLBuffer2[i]=ExtMLBuffer[i]+2*ExtBandsDeviations*ExtStdDevBuffer[i]; //--- lower line ExtBLBuffer2[i]=ExtMLBuffer[i]-2*ExtBandsDeviations*ExtStdDevBuffer[i]; ExtBLBuffer3[i]=ExtMLBuffer[i]-3*ExtBandsDeviations*ExtStdDevBuffer[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); }//+------------------------------------------------------------------+#property copyright "2009-2017, MetaQuotes Software Corp."#property link "http://www.mql5.com"#property description "BB7-2"#include <MovingAverages.mqh>#property indicator_chart_window#property indicator_buffers 4#property indicator_plots 3#property indicator_type1 DRAW_LINE#property indicator_color1 Orange#property indicator_style1 STYLE_DOT#property indicator_type2 DRAW_LINE#property indicator_color2 Orange#property indicator_type3 DRAW_LINE#property indicator_color3 Orange#property indicator_label1 "Bands middle"#property indicator_label2 "Bands upper"#property indicator_label3 "Bands lower"//--- input parametrsinput int InpBandsPeriod=21; // Periodinput int InpBandsShift=0; // Shiftinput double InpBandsDeviations=1.0; // Deviation//--- global variablesint ExtBandsPeriod,ExtBandsShift;double ExtBandsDeviations;int ExtPlotBegin=0;//---- indicator bufferdouble ExtMLBuffer[];double ExtTLBuffer[];double ExtBLBuffer[];double ExtStdDevBuffer[];//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+void OnInit() {//--- check for input values if(InpBandsPeriod<2) { ExtBandsPeriod=21; printf("Incorrect value for input variable InpBandsPeriod=%d. Indicator will use value=%d for calculations.",InpBandsPeriod,ExtBandsPeriod); } else ExtBandsPeriod=InpBandsPeriod; if(InpBandsShift<0) { ExtBandsShift=0; printf("Incorrect value for input variable InpBandsShift=%d. Indicator will use value=%d for calculations.",InpBandsShift,ExtBandsShift); } else ExtBandsShift=InpBandsShift; if(InpBandsDeviations==0.0) { ExtBandsDeviations=1.0; printf("Incorrect value for input variable InpBandsDeviations=%f. Indicator will use value=%f for calculations.",InpBandsDeviations,ExtBandsDeviations); } else ExtBandsDeviations=InpBandsDeviations;//--- define buffers SetIndexBuffer(0,ExtMLBuffer); SetIndexBuffer(1,ExtTLBuffer); SetIndexBuffer(2,ExtBLBuffer); SetIndexBuffer(3,ExtStdDevBuffer,INDICATOR_CALCULATIONS);//--- set index labels PlotIndexSetString(0,PLOT_LABEL,"Bands("+string(ExtBandsPeriod)+") Middle"); PlotIndexSetString(1,PLOT_LABEL,"Bands("+string(ExtBandsPeriod)+") Upper"); PlotIndexSetString(2,PLOT_LABEL,"Bands("+string(ExtBandsPeriod)+") Lower");//--- indicator name IndicatorSetString(INDICATOR_SHORTNAME,"BB7-2");//--- indexes draw begin settings ExtPlotBegin=ExtBandsPeriod-1; PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtBandsPeriod); PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtBandsPeriod); PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtBandsPeriod);//--- indexes shift settings PlotIndexSetInteger(0,PLOT_SHIFT,ExtBandsShift); PlotIndexSetInteger(1,PLOT_SHIFT,ExtBandsShift); PlotIndexSetInteger(2,PLOT_SHIFT,ExtBandsShift);//--- 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!=ExtBandsPeriod+begin) { ExtPlotBegin=ExtBandsPeriod+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 ExtMLBuffer[i]=SimpleMA(i,ExtBandsPeriod,price); //--- calculate and write down StdDev ExtStdDevBuffer[i]=StdDev_Func(i,price,ExtMLBuffer,ExtBandsPeriod); //--- upper line ExtTLBuffer[i]=ExtMLBuffer[i]+ExtBandsDeviations*ExtStdDevBuffer[i]; //--- lower line ExtBLBuffer[i]=ExtMLBuffer[i]-ExtBandsDeviations*ExtStdDevBuffer[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); }//+------------------------------------------------------------------+