//+------------------------------------------------------------------+ //| Bands.mq4 | //| Copyright © 2005, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //| Modified by Robert Hill to use any MA including LSMA | //| instead of SMA | //+------------------------------------------------------------------+ #property copyright "Copyright © 2005, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" #property indicator_chart_window #property indicator_buffers 5 #property indicator_color1 Yellow #property indicator_color2 LightSeaGreen #property indicator_color3 LightSeaGreen #property indicator_color4 Red #property indicator_color5 Red //---- indicator parameters extern string m = "--Moving Average Types--"; extern string m0 = " 0 = SMA"; extern string m1 = " 1 = EMA"; extern string m2 = " 2 = SMMA"; extern string m3 = " 3 = LWMA"; extern string m4 = " 4 = LSMA"; extern int MA_Type=0; extern string p = "--Applied Price Types--"; extern string p0 = " 0 = close"; extern string p1 = " 1 = open"; extern string p2 = " 2 = high"; extern string p3 = " 3 = low"; extern string p4 = " 4 = median(high+low)/2"; extern string p5 = " 5 = typical(high+low+close)/3"; extern string p6 = " 6 = weighted(high+low+close+close)/4"; extern int MA_AppliedPrice = 0; extern string b = "--Bands Inputs--"; extern int BandsPeriod=20; extern int BandsShift=0; extern double BandsDeviations1 = 1.0; extern double BandsDeviations2 = 2.5; extern bool DisplayMidLine = true; //---- buffers double MovingBuffer[]; double UpperBuffer1[]; double LowerBuffer1[]; double UpperBuffer2[]; double LowerBuffer2[]; //---- variables int MA_Mode; string strMAType; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- 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); //---- SetIndexDrawBegin(0,BandsPeriod+BandsShift); SetIndexDrawBegin(1,BandsPeriod+BandsShift); SetIndexDrawBegin(2,BandsPeriod+BandsShift); SetIndexDrawBegin(3,BandsPeriod+BandsShift); SetIndexDrawBegin(4,BandsPeriod+BandsShift); //---- switch (MA_Type) { case 1: strMAType="EMA"; MA_Mode=MODE_EMA; break; case 2: strMAType="SMMA"; MA_Mode=MODE_SMMA; break; case 3: strMAType="LWMA"; MA_Mode=MODE_LWMA; break; case 4: strMAType="LSMA"; break; default: strMAType="SMA"; MA_Mode=MODE_SMA; break; } IndicatorShortName( strMAType+ " (" +BandsPeriod + ") "); return(0); } //+------------------------------------------------------------------------+ //| LSMA - Least Squares Moving Average function calculation | //| LSMA_In_Color Indicator plots the end of the linear regression line | //+------------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| LSMA with PriceMode | //| PrMode 0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, | //| 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4 | //+------------------------------------------------------------------+ double LSMA(int TimeFrame, int Rperiod, int prMode, int shift) { int i, myShift; double sum, pr; int length; double lengthvar; double tmp; double wt; length = Rperiod; sum = 0; for(i = length; i >= 1 ; i--) { lengthvar = length + 1; lengthvar /= 3; tmp = 0; myShift = length - i + shift; switch (prMode) { case 0: pr = iClose(NULL,TimeFrame,myShift);break; case 1: pr = iOpen(NULL,TimeFrame,myShift);break; case 2: pr = iHigh(NULL,TimeFrame,myShift);break; case 3: pr = iLow(NULL,TimeFrame,myShift);break; case 4: pr = (iHigh(NULL,TimeFrame,myShift) + iLow(NULL,TimeFrame,myShift))/2;break; case 5: pr = (iHigh(NULL,TimeFrame,myShift) + iLow(NULL,TimeFrame,myShift) + iClose(NULL,TimeFrame,myShift))/3;break; case 6: pr = (iHigh(NULL,TimeFrame,myShift) + iLow(NULL,TimeFrame,myShift) + iClose(NULL,TimeFrame,myShift) + iClose(NULL,TimeFrame,myShift))/4;break; } tmp = ( i - lengthvar)*pr; sum+=tmp; } wt = sum*6/(length*(length+1)); wt = MathFloor(wt/Point)*Point; return(wt); } //+------------------------------------------------------------------+ //| Bollinger Bands | //+------------------------------------------------------------------+ int start() { int i,k,counted_bars=IndicatorCounted(); double deviation1, deviation2; 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; UpperBuffer2[Bars-i]=EMPTY_VALUE; LowerBuffer2[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--; } deviation1=BandsDeviations1*MathSqrt(sum/BandsPeriod); deviation2=BandsDeviations2*MathSqrt(sum/BandsPeriod); UpperBuffer1[i]=oldval+deviation1; LowerBuffer1[i]=oldval-deviation1; UpperBuffer2[i]=oldval+deviation2; LowerBuffer2[i]=oldval-deviation2; i--; } if (DisplayMidLine == false) { for(i=1;i<=Bars;i++) MovingBuffer[i]=EMPTY_VALUE; } //---- return(0); } //+------------------------------------------------------------------+