//+------------------------------------------------------------------+
//|                                                      MAArrow.mq4 |
//|                                                         BlueRain |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property strict

#property indicator_buffers 2
#property indicator_color1 Yellow  //raw HP data
#property indicator_color2 Green  //uptrend line

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+


extern int period_MA1 = 5;
extern int period_MA2 = 13;
extern ENUM_MA_METHOD MA_Method = MODE_EMA;
extern ENUM_APPLIED_PRICE MA_Price = PRICE_TYPICAL;
extern int SignalCandleShift = 1;

//---- line buffers 
double MA1Buffer[];  //if uptrend.. then it has value, not.. it will be blank
double MA2Buffer[];



int OnInit()
  {
//--- indicator buffers mapping
   
//---
   IndicatorBuffers(2);  
    
   SetIndexBuffer(0,MA1Buffer);
   SetIndexBuffer(1,MA2Buffer); 
   
   ArraySetAsSeries(MA1Buffer, true); 
   ArraySetAsSeries(MA2Buffer, true); 
   
    //line 
    SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
    SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);
    
    
   return(INIT_SUCCEEDED);
  }

int deinit()
{

   ObjectDelete(0,"MA_Signal");

return (0);

}
//+------------------------------------------------------------------+
//| 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[])
  {


   //initial bar count
    int i,counted_bars=IndicatorCounted();
  
   //sets nobs
    if(Bars<=100) return(0);
  //---- check for possible errors
     if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   i=Bars-counted_bars;
   
    int limit=MathMin(Bars-counted_bars,Bars-1);
  
     for(int x = limit; x >= 0; x--)
    {   
        
      MA1Buffer[x] = iMA(NULL,0,period_MA1,0,MA_Method,MA_Price,x);
      MA2Buffer[x] = iMA(NULL,0,period_MA2,0,MA_Method,MA_Price,x);
 
   
   
    }
   
   
   
   
   int BuySellSignal = GetSignal();
   
   double atr = iATR(Symbol(),PERIOD_CURRENT,14,SignalCandleShift);
   if ( ObjectFind(0,"MA_Signal") >= 0 ) ObjectDelete(0,"MA_Signal");
   
   if ( BuySellSignal == 1)
   {
       ObjectCreate("MA_Signal",OBJ_ARROW_UP,0,Time[SignalCandleShift],High[SignalCandleShift] + atr);
   }
   else   if ( BuySellSignal == -1)
   {
   
       ObjectCreate("MA_Signal",OBJ_ARROW_DOWN,0,Time[SignalCandleShift],Low[SignalCandleShift] + atr);
    }
    
    
   

   
   return(rates_total);
  }
//+------------------------------------------------------------------+
int GetSignal()
 { 
  double FastEMA=iMA(NULL,0,period_MA1,0,MA_Method,MA_Price,SignalCandleShift);
  double SlowEMA=iMA(NULL,0,period_MA2,0,MA_Method,MA_Price,SignalCandleShift);
  
  double PrevFastEMA=iMA(NULL,0,period_MA1,0,MA_Method,MA_Price,SignalCandleShift+1);
  double PrevSlowEMA=iMA(NULL,0,period_MA2,0,MA_Method,MA_Price,SignalCandleShift+1);  
 
  int vSig=0;
  //buy
  if(PrevFastEMA<=PrevSlowEMA && FastEMA>SlowEMA  )vSig = 1;
  else
  if(PrevFastEMA>=PrevSlowEMA && FastEMA<SlowEMA )vSig =-1;
  return(vSig); 
 }