//+------------------------------------------------------------------+ //| Bands M.mq4 | //| Copyright © 2008, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" #property indicator_chart_window #property indicator_buffers 7 #property indicator_color1 White #property indicator_color2 White #property indicator_color3 White #property indicator_color4 White #property indicator_color5 White #property indicator_color6 White #property indicator_color7 White #property indicator_width1 2 #property indicator_style1 0 #property indicator_style2 0 #property indicator_style3 0 #property indicator_style4 2 #property indicator_style5 2 #property indicator_style6 4 #property indicator_style7 4 //---- indicator parameters int BandsPeriod=0; extern ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; extern int MA_Period = 60; extern ENUM_MA_METHOD MA_Mode = MODE_EMA; extern ENUM_APPLIED_PRICE Price_Type= PRICE_CLOSE; extern int BandsShift=0; extern double BandsDeviations1=1.0; extern double BandsDeviations2=0; extern double BandsDeviations3=0; //---- buffers double MovingBuffer[]; double UpperBuffer1[]; double LowerBuffer1[]; double UpperBuffer2[]; double LowerBuffer2[]; double UpperBuffer3[]; double LowerBuffer3[]; double MovingBuffer1[]; string short_name; string short_name1, short_name2, short_name3, short_name4, short_name5, short_name6; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //if (MA_Period != PERIOD_CURRENT ) { int ActivePer = Period(); if (ActivePer == 10080) ActivePer = 7200; if (ActivePer == 40320) ActivePer = 28800; if (ActivePer == 161280) ActivePer = 115200; BandsPeriod = MA_Period/ActivePer; //} //---- indicators SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,MovingBuffer); SetIndexStyle(1,DRAW_LINE); SetIndexBuffer(1,UpperBuffer1); SetIndexStyle(2,DRAW_LINE); SetIndexBuffer(2,LowerBuffer1); SetIndexStyle(3,DRAW_LINE); SetIndexBuffer(3,UpperBuffer2); SetIndexStyle(4,DRAW_LINE); SetIndexBuffer(4,LowerBuffer2); SetIndexStyle(5,DRAW_LINE); SetIndexBuffer(5,UpperBuffer3); SetIndexStyle(6,DRAW_LINE); SetIndexBuffer(6,LowerBuffer3); //---- SetIndexDrawBegin(0,BandsPeriod+BandsShift); SetIndexDrawBegin(1,BandsPeriod+BandsShift); SetIndexDrawBegin(2,BandsPeriod+BandsShift); SetIndexDrawBegin(3,BandsPeriod+BandsShift); SetIndexDrawBegin(4,BandsPeriod+BandsShift); SetIndexDrawBegin(5,BandsPeriod+BandsShift); SetIndexDrawBegin(6,BandsPeriod+BandsShift); //---- name for DataWindow and indicator subwindow label short_name ="KG Bands( "+ BandsPeriod + ", " + IntegerToString(MA_Mode) + ", " + IntegerToString(Price_Type) + " )"; short_name1="Upper Band (M"+BandsPeriod+","+BandsDeviations1+")"; short_name2="Lower Band (M"+BandsPeriod+","+BandsDeviations1+")"; short_name3="Upper Band (M"+BandsPeriod+","+BandsDeviations2+")"; short_name4="Lower Band (M"+BandsPeriod+","+BandsDeviations2+")"; short_name5="Upper Band (M"+BandsPeriod+","+BandsDeviations3+")"; short_name6="Lower Band (M"+BandsPeriod+","+BandsDeviations3+")"; IndicatorShortName(short_name); SetIndexLabel(0,short_name); SetIndexLabel(1,short_name1); SetIndexLabel(2,short_name2); SetIndexLabel(3,short_name3); SetIndexLabel(4,short_name4); SetIndexLabel(5,short_name5); SetIndexLabel(6,short_name6); return(0); } //+------------------------------------------------------------------+ //| Bollinger Bands | //+------------------------------------------------------------------+ int start() { int i,k,counted_bars=IndicatorCounted(); double sum,oldval,newres; //---- if(Bars<=BandsPeriod) return(0); //---- initial zero if(counted_bars<1) for(i=1; i<=BandsPeriod; i++) { MovingBuffer[Bars-i]=EMPTY_VALUE; UpperBuffer1[Bars-i]=EMPTY_VALUE; LowerBuffer1[Bars-i]=EMPTY_VALUE; if(BandsDeviations2>0) UpperBuffer2[Bars-i]=EMPTY_VALUE; if(BandsDeviations2>0) LowerBuffer2[Bars-i]=EMPTY_VALUE; if(BandsDeviations3>0) UpperBuffer3[Bars-i]=EMPTY_VALUE; if(BandsDeviations3>0) LowerBuffer3[Bars-i]=EMPTY_VALUE; } //---- int limit=Bars-counted_bars; if(counted_bars>0) limit++; for(i=0; iBandsPeriod-1) i=Bars-counted_bars-1; while(i>=0) { sum=0.0; k=i+BandsPeriod-1; oldval=MovingBuffer[i]; while(k>=i) { newres=Close[k]-oldval; sum+=newres*newres; k--; } if(BandsDeviations1>0) { double deviation; deviation=BandsDeviations1*MathSqrt(sum/BandsPeriod); UpperBuffer1[i]=oldval+deviation; LowerBuffer1[i]=oldval-deviation; } if(BandsDeviations2>0) { double deviation2; deviation2=BandsDeviations2*MathSqrt(sum/BandsPeriod); UpperBuffer2[i]=oldval+deviation2; LowerBuffer2[i]=oldval-deviation2; } if(BandsDeviations3>0) { double deviation3; deviation3=BandsDeviations3*MathSqrt(sum/BandsPeriod); UpperBuffer3[i]=oldval+deviation3; LowerBuffer3[i]=oldval-deviation3; } i--; } return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+