//+------------------------------------------------------------------+ //| StandardDeviationChannel.mq5 | //| Copyright 2014,fxMeter. | //| https://www.mql5.com/en/users/fxmeter | //+------------------------------------------------------------------+ //2017-07-21 15:37:03 an error found. Updated. /* line 137: midBuffer[i]=A*(CalcBars-i-1)+B; should be midBuffer[i]=A*(StarBar+CalcBars-i-1)+B; */ //2017-05-17 17:44:17 coded #property copyright "Copyright 2017,fxMeter." #property link "https://www.mql5.com/en/users/fxmeter" #property version "1.00" #property indicator_chart_window #property indicator_buffers 5 #property indicator_plots 5 //--- plot mid #property indicator_label1 "mid" #property indicator_type1 DRAW_LINE #property indicator_color1 clrDeepSkyBlue #property indicator_style1 STYLE_DOT #property indicator_width1 1 //--- plot top #property indicator_label2 "top" #property indicator_type2 DRAW_LINE #property indicator_color2 clrDeepSkyBlue #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- plot btm #property indicator_label3 "btm" #property indicator_type3 DRAW_LINE #property indicator_color3 clrDeepSkyBlue #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //--- plot top2 #property indicator_label4 "top2" #property indicator_type4 DRAW_LINE #property indicator_color4 clrDeepSkyBlue #property indicator_style4 STYLE_SOLID #property indicator_width4 1 //--- plot bmt2 #property indicator_label5 "bmt2" #property indicator_type5 DRAW_LINE #property indicator_color5 clrDeepSkyBlue #property indicator_style5 STYLE_SOLID #property indicator_width5 1 input int InputStarBar = 0;//StarBar input int InputCalcBars = 120;//Bars for Calculation input double f1=1.0;//Inner Channel Multiplier input double f2=2.0;//Outer Channel Multiplier //--- indicator buffers double midBuffer[]; double topBuffer[]; double btmBuffer[]; double topBuffer2[]; double btmBuffer2[]; double sample[];//sample data for calculating linear regression //--- int StarBar = 0; int CalcBars = 0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,midBuffer,INDICATOR_DATA); SetIndexBuffer(1,topBuffer,INDICATOR_DATA); SetIndexBuffer(2,btmBuffer,INDICATOR_DATA); SetIndexBuffer(3,topBuffer2,INDICATOR_DATA); SetIndexBuffer(4,btmBuffer2,INDICATOR_DATA); for(int i=0;i<5;i++)PlotIndexSetDouble(i,PLOT_EMPTY_VALUE,0.0); IndicatorSetInteger(INDICATOR_DIGITS,_Digits); ArraySetAsSeries(midBuffer,true); ArraySetAsSeries(topBuffer,true); ArraySetAsSeries(btmBuffer,true); ArraySetAsSeries(topBuffer2,true); ArraySetAsSeries(btmBuffer2,true); StarBar = InputStarBar; CalcBars = InputCalcBars; if(CalcBars<2)CalcBars=120; if(StarBar<0)StarBar=0; ArrayResize(sample,CalcBars); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- int i; double A=0.0,B=0.0,stdev=0.0; if(StarBar+CalcBars>rates_total-1)return(0); //explicitly initialize array buffer to zero if(prev_calculated==0) { ArrayInitialize(midBuffer,0.0); ArrayInitialize(topBuffer,0.0); ArrayInitialize(btmBuffer,0.0); ArrayInitialize(topBuffer2,0.0); ArrayInitialize(btmBuffer2,0.0); } //--- copy close data to sample array if(CopyClose(Symbol(),PERIOD_CURRENT,StarBar,CalcBars,sample)!=CalcBars)return(0); //--- use sample data to calculate linear regression,to get the coefficient a and b CalcAB(sample,CalcBars,A,B); for(i=StarBar;i