//+------------------------------------------------------------------+ //| StandardDeviationChannel.mq4 | //| Copyright 2016, fxMeter. | //| https://www.mql5.com/en/users/fxmeter | //+------------------------------------------------------------------+ //2017-07-21 15:28:04 an error found. Updated. /* line 131: midBuffer[i]=A*(CalcBars-i-1)+B; should be: midBuffer[i]=A*(StarBar+CalcBars-i-1)+B; */ //2017-05-17 15:01:00 Standard Deviation Channel.Publish to MQL5.com codebase //2016-03-08 10:56:49 coded #property copyright "Copyright 2016, fxMeter." #property link "https://www.mql5.com/en/users/fxmeter" #property version "1.00" #property strict #property indicator_chart_window #property indicator_buffers 5 #property indicator_plots 5 //--- plot top #property indicator_label1 "mid" #property indicator_type1 DRAW_LINE #property indicator_color1 clrDeepSkyBlue #property indicator_style1 STYLE_DOT #property indicator_width1 1 //--- plot mid #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 mid #property indicator_label4 "top2" #property indicator_type4 DRAW_LINE #property indicator_color4 clrDeepSkyBlue #property indicator_style4 STYLE_SOLID #property indicator_width4 1 //--- plot btm #property indicator_label5 "btm2" #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 topBuffer[]; double midBuffer[]; 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); SetIndexBuffer(1,topBuffer); SetIndexBuffer(2,btmBuffer); SetIndexBuffer(3,topBuffer2); SetIndexBuffer(4,btmBuffer2); for(int i=0;i<5;i++)SetIndexEmptyValue(i,0.0); IndicatorDigits(_Digits); 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