//+------------------------------------------------------------------+ //| Moving Average Crossover.mq4 | //| Copyright © 2009, CompassFX | //| http://www.compassfx.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, CompassFX" #property link "http://www.compassfx.com" #property indicator_separate_window #property indicator_buffers 4 #property indicator_color1 DodgerBlue #property indicator_color2 Red #property indicator_color3 DodgerBlue #property indicator_color4 Red double CrossUp[]; double CrossDown[]; double ExtMapBufferFast[]; double ExtMapBufferSlow[]; extern int Fast_MA_Period = 5; extern int Fast_MA_Method = 1; //0=sma, 1=ema, 2=smma, 3=lwma extern int Fast_MA_Price = 0; extern int Slow_MA_Period = 8; extern int Slow_MA_Method = 1; //0=sma, 1=ema, 2=smma, 3=lwma extern int Slow_MA_Price = 1; extern string MA_Method_Type = "0=SMA, 1=EMA, 2=SMMA, 3=LWMA"; extern string MA_Price_Type = "0=Close, 1=Open, 2=High, 3=Low"; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators IndicatorShortName("Moving Average Crossover | www.compassfx.com "); SetIndexStyle(0, DRAW_ARROW, EMPTY); SetIndexArrow(0, 233); SetIndexBuffer(0, CrossUp); SetIndexStyle(1, DRAW_ARROW, EMPTY); SetIndexArrow(1, 234); SetIndexBuffer(1, CrossDown); SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1, DodgerBlue); SetIndexBuffer(2, ExtMapBufferFast); SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,1, Red); SetIndexBuffer(3, ExtMapBufferSlow); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int limit, i, counter; double Fast_MA_Periodnow, Slow_MA_Periodnow, Fast_MA_Periodprevious, Slow_MA_Periodprevious; int counted_bars=IndicatorCounted(); //---- check for possible errors if(counted_bars<0) return(-1); //---- last counted bar will be recounted if(counted_bars>0) counted_bars--; limit=Bars-counted_bars; for(i = 0; i <= limit; i++) { Fast_MA_Periodnow = iMA(NULL, 0, Fast_MA_Period, 0, Fast_MA_Method, Fast_MA_Price, i); Fast_MA_Periodprevious = iMA(NULL, 0, Fast_MA_Period, 0, Fast_MA_Method, Fast_MA_Price, i+1); Slow_MA_Periodnow = iMA(NULL, 0, Slow_MA_Period, 0, Slow_MA_Method, Slow_MA_Price, i); Slow_MA_Periodprevious = iMA(NULL, 0, Slow_MA_Period, 0, Slow_MA_Method, Slow_MA_Price, i+1); if ((Fast_MA_Periodnow > Slow_MA_Periodnow) && (Fast_MA_Periodprevious < Slow_MA_Periodprevious)) { CrossUp[i] = Fast_MA_Periodprevious - (High[i] - Low[i]) * 0.5; } else if ((Fast_MA_Periodnow < Slow_MA_Periodnow) && (Fast_MA_Periodprevious > Slow_MA_Periodprevious)) { CrossDown[i] = Fast_MA_Periodprevious + (High[i] - Low[i]) * 0.5; } ExtMapBufferFast[i]=Fast_MA_Periodnow; ExtMapBufferSlow[i]=Slow_MA_Periodnow; } return(0); }