//+------------------------------------------------------------------+
//|                                                MACrossSignal.mq4 |
//|                                       Copyright 2020, Trader1971 |
//|                          https://www.forexfactory.com/trader1971 |
//+------------------------------------------------------------------+
#property copyright  "Copyright 2020, Trader1971"
#property link       "https://www.forexfactory.com/trader1971"
//#property version    "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 clrDarkGreen
#property indicator_color2 clrRed
#property indicator_color3 clrAqua
#property indicator_color4 clrAqua
//#property indicator_color5 clrAqua
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
//#property indicator_width5 2

//External Settings
extern int fast=15;//Fast Moving Average Period
extern int slow=30;//Slow Moving Average Period
extern ENUM_MA_METHOD method=MODE_EMA;//Select Moving Average Method
extern ENUM_APPLIED_PRICE appliedtofast=PRICE_CLOSE;//Select Price Applied To Fast MA
extern ENUM_APPLIED_PRICE appliedtoslow=PRICE_OPEN;//Select Price Applied To Slow MA
extern bool show=true;//Show moving averages?
extern int ArrowSpacing=100;//Arrow Spacing from candle in points
double pips;

double fastma[];
double slowma[];
//double ema200[];
double uparrow[];
double downarrow[];
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Comment("");
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,fastma);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexLabel(0,"fast Ma");
//---
   SetIndexBuffer(1,slowma);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexLabel(1,"slow Ma");
//---
   SetIndexBuffer(2,uparrow);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,233);
   SetIndexLabel(2,"Moving averages crossed up.");
//---
   SetIndexBuffer(3,downarrow);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,234);
   SetIndexLabel(3,"Moving averages crossed down.");
//---
   //SetIndexBuffer(4,ema200);
   //SetIndexStyle(4,DRAW_LINE);
   //SetIndexLabel(4,"200 EMA"); 
// Determine what a pip is.
   pips=Point; //.00001 or .0001. .001 .01.
   if(Digits==3 || Digits==5)
      pips*=10;   
   
   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 counted_bars=IndicatorCounted();
   int limit= Bars-counted_bars;
   for(int i=0;i<limit;i++)
     {
      double currentFastMa=iMA(NULL,0,fast,0,method,appliedtofast,i);
      double previousFastMa= iMA(NULL,0,fast,0,method,appliedtofast,i+1);
      double currentSlowMa = iMA(NULL,0,slow,0,method,appliedtoslow,i);
      double previousSlowMa= iMA(NULL,0,slow,0,method,appliedtoslow,i+1);
      //double Ema_200= iMA(NULL,0,200,0,method,PRICE_CLOSE,i);
      
      //Comment distance from price to 15 EMA on M15 TF
      double ema15 = iMA( NULL, PERIOD_M15, 15, 0, MODE_EMA, PRICE_CLOSE,i);
      //double openPriceM15 = iOpen(NULL,PERIOD_M15,i);      
      double distance = ( Bid - ema15 )/Point;
      Comment("\n\nDistance between price and 15 EMA on M15 TF = "+DoubleToStr(distance,2)+"Ticks");
      
      if(show)
        {
         fastma[i]=currentFastMa;
         slowma[i]=currentSlowMa;
         //ema200[i]=Ema_200;
        }
      if(currentFastMa>currentSlowMa && previousFastMa<previousSlowMa)
        {
         uparrow[i]=Low[i]-ArrowSpacing*pips;
        }
      else if(currentFastMa<currentSlowMa && previousFastMa>previousSlowMa)
        {
         downarrow[i]=High[i]+ArrowSpacing*pips;
        }
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
