//+------------------------------------------------------------------+ //| Custom Moving Average.mq4 | //| Copyright © 2004, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //| | //| Color Fill MA1 + MA2: Copyleft © 2013, File45 | //| http://codebase.mql4.com/en/author/file45 | //+------------------------------------------------------------------+ // ======================== DEFAULT PARAMETERS - START #property indicator_chart_window #property indicator_buffers 4 #property indicator_color1 LimeGreen #property indicator_color2 Red #property indicator_color3 Teal #property indicator_style3 STYLE_DOT #property indicator_color4 HotPink #property indicator_style4 STYLE_DOT extern int MA_Color_Fill_1_2_3_or_4=3; extern int MA1_Period = 50; extern int MA2_Period = 100; extern int MA1_Shift = 0; extern int MA2_Shift = 0; extern int MA1_Method = 1; extern int MA2_Method = 1; extern int MA1_Price = 0; extern int MA2_Price = 0; extern string MA_Method_Key= "0=SMA, 1=EMA, 2=SMMA, 3=LWMA"; extern string MA_Price_Key = "0=Close, 1=Open, 2=High, 3=Low"; extern string MA_Price_Key_cont="4=Median (High + Low) / 2"; extern bool Show_MA1 = true; extern bool Show_MA2 = true; extern int Label_Size= 12; extern bool Label_Bold=false; extern color MA1_Label_Color = indicator_color1; extern color MA2_Label_Color = indicator_color2; extern int Label_Corner=3; extern int Label_Left_Right=20; extern int Label_Up_Down= 20; extern bool Show_Labels = true; // ======================== DEFAULT PARAMRTERS - END // ======================== indicator buffers double ExtMapBuffer_1[]; double ExtMapBuffer_2[]; double ExtMapBuffer_A1[]; double ExtMapBuffer_B1[]; int Color_Fill_ma; int ma_1_meth,ma_2_meth; int draw_ma_1,draw_ma_2; double Fill_A_Latch,Fill_B_Latch; int ExtCountedBars=0; int a_begin; string Tcf; string ObjLabMA1="labma1"; string ObjLabMA2="labma2"; string textma1; string textma2; string Font_Type; int MA1_Price_Latch,MA2_Price_Latch; int Y_Dist_MA1,Y_Dist_MA2; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int init() { // ======================== Fill Alert / Reset switch(Period()) { case 1: Tcf = "M1"; break; case 5: Tcf = "M5"; break; case 15: Tcf = "H15"; break; case 30: Tcf = "M30"; break; case 60: Tcf = "H1"; break; case 240: Tcf = "H4"; break; case 1440: Tcf = "D1"; break; case 10080: Tcf = "W1"; break; case 43200: Tcf = "M4"; break; } switch(MA_Color_Fill_1_2_3_or_4) { case 1: Color_Fill_ma = 1; break; case 2: Color_Fill_ma = 2; break; case 3: Color_Fill_ma = 3; break; case 4: Color_Fill_ma = 4; break; default: Alert(Symbol()+" - "+Tcf+" : "+"MA_Color_Fill out of bouns, please enter 1, 2 , 3 or 4."); Color_Fill_ma=3; } // ======================== Show / Hide MA1 / MA2 switch(Show_MA1) { case 1: draw_ma_1 = DRAW_LINE; break; case 0: draw_ma_1 = DRAW_NONE; } switch(Show_MA2) { case 1: draw_ma_2 = DRAW_LINE; break; case 0: draw_ma_2 = DRAW_NONE; } // ======================== MA1 Price Latch if(MA1_Price==0) { MA1_Price_Latch=0; } else if(MA1_Price==1) { MA1_Price_Latch=1; } else if(MA1_Price==2) { MA1_Price_Latch=2; } else if(MA1_Price==3) { MA1_Price_Latch=3; } else if(MA1_Price==4) { MA1_Price_Latch=4; } else if(MA1_Price<0 || MA1_Price>4) { Alert(Symbol()+" - "+Tcf+" : "+"MA1_Price out of bounds, please enter 0, 1, 2, 3 or 4."); MA1_Price_Latch=0; } // ======================== MA2 Price Latch if(MA2_Price==0) { MA2_Price_Latch=0; } else if(MA2_Price==1) { MA2_Price_Latch=1; } else if(MA2_Price==2) { MA2_Price_Latch=2; } else if(MA2_Price==3) { MA2_Price_Latch=3; } else if(MA2_Price==4) { MA2_Price_Latch=4; } else if(MA2_Price<0 || MA2_Price>4) { Alert(Symbol()+" - "+Tcf+" : "+"MA2_Price out of bounds, please enter 0, 1, 2, 3 or 4."); MA2_Price_Latch=0; } // ======================== Labek Bold switch(Label_Bold) { case 1: Font_Type = "Arial Bold"; break; case 0: Font_Type = "Arial"; break; } // ======================== Label Corner YDISTANCE if(Label_Corner==0 || Label_Corner==1) { Y_Dist_MA1 = Label_Up_Down; Y_Dist_MA2 = Label_Up_Down + Label_Size*1.7; } else { Y_Dist_MA1 = Label_Up_Down + Label_Size*1.7; Y_Dist_MA2 = Label_Up_Down; } // ======================== drawing settings & buffers mapping SetIndexStyle(0,draw_ma_1); SetIndexBuffer(0,ExtMapBuffer_1); SetIndexDrawBegin(0,MA1_Period); SetIndexShift(0,MA1_Shift); SetIndexStyle(1,draw_ma_2); SetIndexBuffer(1,ExtMapBuffer_2); SetIndexDrawBegin(1,MA2_Period); SetIndexShift(1,MA2_Shift); a_begin=MA2_Period; if(a_begin0) ExtCountedBars--; switch(MA1_Method) { case 0 : sma_1(); break; case 1 : ema_1(); break; case 2 : smma_1(); break; case 3 : lwma_1(); break; default: Alert(Symbol() + " - " + Tcf + " : " + "MA1_Method out of bounds, please enter 0, 1, 2 or 3."); ema_1();break; } switch(MA2_Method) { case 0 : sma_2(); break; case 1 : ema_2(); break; case 2 : smma_2(); break; case 3 : lwma_2(); break; default: Alert(Symbol() + " - " + Tcf + " : " + "MA2_Method out of bounds, please enter 0, 1, 2 or 3."); ema_2();break; } // ======================== FILL A i=Bars-a_begin+1; if(counted_bars>a_begin-1) i=Bars-counted_bars-1; while(i>=0) { if(Color_Fill_ma==1 || Color_Fill_ma==2) { Fill_A_Latch=Close[i]; } else if(Color_Fill_ma==3) { Fill_A_Latch=ExtMapBuffer_1[i+MA1_Shift]; } else if(Color_Fill_ma==4) { Fill_A_Latch=NULL; } ExtMapBuffer_A1[i]=Fill_A_Latch; if(Show_Labels==true && Show_MA1==true) { string PLMA1=DoubleToStr(ExtMapBuffer_1[i],Digits); textma1="ma1 - "; } ObjectCreate(ObjLabMA1,OBJ_LABEL,0,0,0); ObjectSetText(ObjLabMA1,textma1+PLMA1,Label_Size,Font_Type,MA1_Label_Color); ObjectSet(ObjLabMA1,OBJPROP_CORNER,Label_Corner); ObjectSet(ObjLabMA1,OBJPROP_XDISTANCE,Label_Left_Right); ObjectSet(ObjLabMA1,OBJPROP_YDISTANCE,Y_Dist_MA1); i--; } // ======================== FILL B i=Bars-0; counted_bars=IndicatorCounted(); if(counted_bars < 0) return(-1); if(counted_bars>0) counted_bars--; int limit=Bars-counted_bars; if(counted_bars==0) limit-=1+MA1_Shift; i=limit; while(i>=0) { if(Color_Fill_ma==1) { Fill_B_Latch=ExtMapBuffer_1[i+MA1_Shift]; } else if(Color_Fill_ma==2 || Color_Fill_ma==3) { Fill_B_Latch=ExtMapBuffer_2[i+MA2_Shift]; } else if(Color_Fill_ma==4) { Fill_B_Latch=NULL; } ExtMapBuffer_B1[i]=Fill_B_Latch; if(Show_Labels==true && Show_MA2==true) { string PLMA2=DoubleToStr(ExtMapBuffer_2[i],Digits); textma2="ma2 - "; } ObjectCreate(ObjLabMA2,OBJ_LABEL,0,0,0); ObjectSetText(ObjLabMA2,textma2+PLMA2,Label_Size,Font_Type,MA2_Label_Color); ObjectSet(ObjLabMA2,OBJPROP_CORNER,Label_Corner); ObjectSet(ObjLabMA2,OBJPROP_XDISTANCE,Label_Left_Right); ObjectSet(ObjLabMA2,OBJPROP_YDISTANCE,Y_Dist_MA2); i--; } return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int deinit() { ObjectDelete("Labma1"); ObjectDelete("Labma2"); } // ======================== MA1 TYPES & PRICE void sma_1() { double sum=0; int i,pos=Bars-ExtCountedBars-1; //---- initial accumulation if(pos=0) { if(MA1_Price_Latch==0) { sum+=Close[pos]; } else if(MA1_Price_Latch==1) { sum+=Open[pos]; } else if(MA1_Price_Latch==2) { sum+=High[pos]; } else if(MA1_Price_Latch==3) { sum+=Low[pos]; } else if(MA1_Price_Latch==4) { sum+=0.5*High[pos]+0.5*Low[pos]; } ExtMapBuffer_1[pos]=sum/MA1_Period; if(MA1_Price_Latch==0) { sum-=Close[pos+MA1_Period-1]; } else if(MA1_Price_Latch==1) { sum-=Open[pos+MA1_Period-1]; } else if(MA1_Price_Latch==2) { sum-=High[pos+MA1_Period-1]; } else if(MA1_Price_Latch==3) { sum-=Low[pos+MA1_Period-1]; } else if(MA1_Price_Latch==4) { sum-=0.5*High[pos+MA1_Period-1]+0.5*Low[pos+MA1_Period-1]; } pos--; } // ======================== zero initial bars if(ExtCountedBars<1) for(i=1;i2) pos=Bars-ExtCountedBars-1; // ======================== main calculation loop while(pos>=0) { if(pos==Bars-2) { if(MA1_Price_Latch==0) { ExtMapBuffer_1[pos+1]=Close[pos+1]; } else if(MA1_Price_Latch==1) { ExtMapBuffer_1[pos+1]=Open[pos+1]; } else if(MA1_Price_Latch==2) { ExtMapBuffer_1[pos+1]=High[pos+1]; } else if(MA1_Price_Latch==3) { ExtMapBuffer_1[pos+1]=Low[pos+1]; } else if(MA1_Price_Latch==4) { ExtMapBuffer_1[pos+1]=0.5*High[pos+1]+0.5*Low[pos+1]; } } if(MA1_Price_Latch==0) { ExtMapBuffer_1[pos]=Close[pos]*pr+ExtMapBuffer_1[pos+1]*(1-pr); } else if(MA1_Price_Latch==1) { ExtMapBuffer_1[pos]=Open[pos]*pr+ExtMapBuffer_1[pos+1]*(1-pr); } else if(MA1_Price_Latch==2) { ExtMapBuffer_1[pos]=High[pos]*pr+ExtMapBuffer_1[pos+1]*(1-pr); } else if(MA1_Price_Latch==3) { ExtMapBuffer_1[pos]=Low[pos]*pr+ExtMapBuffer_1[pos+1]*(1-pr); } else if(MA1_Price_Latch==4) { ExtMapBuffer_1[pos]=(0.5*High[pos]+0.5*Low[pos])*pr+ExtMapBuffer_1[pos+1]*(1-pr); } pos--; } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void smma_1() { double sum=0; int i,k,pos=Bars-ExtCountedBars+1; // ======================== main calculation loop pos=Bars-MA1_Period; if(pos>Bars-ExtCountedBars) pos=Bars-ExtCountedBars; { while(pos>=0) { if(pos==Bars-MA1_Period) { // ======================== initial accumulation for(i=0,k=pos;i=0) { ExtMapBuffer_1[pos]=sum/weight; if(pos==0) break; pos--; i--; if(MA1_Price_Latch==0) { price=Close[pos]; } else if(MA1_Price_Latch==1) { price=Open[pos]; } else if(MA1_Price_Latch==2) { price=High[pos]; } else if(MA1_Price_Latch==3) { price=Low[pos]; } else if(MA1_Price_Latch==4) { price=0.5*High[pos]+0.5*Low[pos]; } sum=sum-lsum+price*MA1_Period; if(MA1_Price_Latch==0) { lsum-=Close[i]; } else if(MA1_Price_Latch==1) { lsum-=Open[i]; } else if(MA1_Price_Latch==2) { lsum-=High[i]; } else if(MA1_Price_Latch==3) { lsum-=Low[i]; } else if(MA1_Price_Latch==4) { lsum-=0.5*High[i]+0.5*Low[i]; } lsum+=price; } // ======================== zero initial bars if(ExtCountedBars<1) for(i=1;i=0) { if(MA2_Price_Latch==0) { sum+=Close[pos]; } else if(MA2_Price_Latch==1) { sum+=Open[pos]; } else if(MA2_Price_Latch==2) { sum+=High[pos]; } else if(MA2_Price_Latch==3) { sum+=Low[pos]; } else if(MA2_Price_Latch==4) { sum+=0.5*High[pos]+0.5*Low[pos]; } ExtMapBuffer_2[pos]=sum/MA2_Period; if(MA2_Price_Latch==0) { sum-=Close[pos+MA2_Period-1]; } else if(MA2_Price_Latch==1) { sum-=Open[pos+MA2_Period-1]; } else if(MA2_Price_Latch==2) { sum-=High[pos+MA2_Period-1]; } else if(MA2_Price_Latch==3) { sum-=Low[pos+MA2_Period-1]; } else if(MA2_Price_Latch==4) { sum-=0.5*High[pos+MA2_Period-1]+0.5*Low[pos+MA2_Period-1]; } pos--; } // ======================== zero initial bars if(ExtCountedBars<1) for(i=1;i2) pos=Bars-ExtCountedBars-1; // ======================== main calculation loop while(pos>=0) { if(pos==Bars-2) { if(MA2_Price_Latch==0) { ExtMapBuffer_2[pos+1]=Close[pos+1]; } else if(MA2_Price_Latch==1) { ExtMapBuffer_2[pos+1]=Open[pos+1]; } else if(MA2_Price_Latch==2) { ExtMapBuffer_2[pos+1]=High[pos+1]; } else if(MA2_Price_Latch==3) { ExtMapBuffer_2[pos+1]=Low[pos+1]; } else if(MA2_Price_Latch==4) { ExtMapBuffer_2[pos+1]=0.5*High[pos+1]+0.5*Low[pos+1]; } } if(MA2_Price_Latch==0) { ExtMapBuffer_2[pos]=Close[pos]*pr+ExtMapBuffer_2[pos+1]*(1-pr); } else if(MA2_Price_Latch==1) { ExtMapBuffer_2[pos]=Open[pos]*pr+ExtMapBuffer_2[pos+1]*(1-pr); } else if(MA2_Price_Latch==2) { ExtMapBuffer_2[pos]=High[pos]*pr+ExtMapBuffer_2[pos+1]*(1-pr); } else if(MA2_Price_Latch==3) { ExtMapBuffer_2[pos]=Low[pos]*pr+ExtMapBuffer_2[pos+1]*(1-pr); } else if(MA2_Price_Latch==4) { ExtMapBuffer_2[pos]=(0.5*High[pos]+0.5*Low[pos])*pr+ExtMapBuffer_2[pos+1]*(1-pr); } pos--; } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void smma_2() { double sum=0; int i,k,pos=Bars-ExtCountedBars+1; // ======================== main calculation loop pos=Bars-MA2_Period; if(pos>Bars-ExtCountedBars) pos=Bars-ExtCountedBars; { while(pos>=0) { if(pos==Bars-MA2_Period) { //---- initial accumulation for(i=0,k=pos;i=0) { ExtMapBuffer_2[pos]=sum/weight; if(pos==0) break; pos--; i--; if(MA2_Price_Latch==0) { price=Close[pos]; } else if(MA2_Price_Latch==1) { price=Open[pos]; } else if(MA2_Price_Latch==2) { price=High[pos]; } else if(MA2_Price_Latch==3) { price=Low[pos]; } else if(MA2_Price_Latch==4) { price=0.5*High[pos]+0.5*Low[pos]; } sum=sum-lsum+price*MA2_Period; if(MA2_Price_Latch==0) { lsum-=Close[i]; } else if(MA2_Price_Latch==1) { lsum-=Open[i]; } else if(MA2_Price_Latch==2) { lsum-=High[i]; } else if(MA2_Price_Latch==3) { lsum-=Low[i]; } else if(MA2_Price_Latch==4) { lsum-=0.5*High[i]+0.5*Low[i]; } lsum+=price; } // ======================== zero initial bars if(ExtCountedBars<1) for(i=1;i