//+------------------------------------------------------------------+
//|                                         MA_Cross_Arrows_v1.0.mq4 |
//|                                         Copyright 2020, NickBixy |
//|                           https://www.mql5.com/en/users/nickbixy |
//+------------------------------------------------------------------+

#property copyright "NickBixy"
#property link      "https://www.mql5.com/en/users/nickbixy"
//#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2


input string MAHeader="-----------------MA Settings------------------------------------------";//----- MA Settings
input string MAFastHeader="--Fast MA Settings";//--Fast MA Settings
input int ma_period_fast=1;//Fast MA Averaging Period
input ENUM_MA_METHOD ma_method_fast=MODE_SMA;//Fast MA Averaging Method
input ENUM_APPLIED_PRICE applied_price_fast=PRICE_TYPICAL;//Fast MA Applied Price

input string MASlowHeader="--Slow MA Settings";//--Slow MA Settings
input int ma_period_slow=3;//Slow MA Averaging Period
input ENUM_MA_METHOD ma_method_slow=MODE_SMA;//Slow MA Averaging Method
input ENUM_APPLIED_PRICE applied_price_slow=PRICE_TYPICAL;//Slow MA Applied Price

input string ArrowHeader="-----------------Arrows Settings------------------------------------------";//----- Arrows Settings
input int shiftMultiplier=2;//Shift Multipilier Arrows up or down more
input string info="https://docs.mql4.com/constants/objectconstants/wingdings";//Windings font symbols
input color bullishArrowColor=clrGreen;//Bullish Arrow Color
input int bullishWinding=233;//Arrow Wingding Character
input int bullishArrowSize=0;//Bullish Arrow Size 0-5
input color bearishArrowColor=clrMaroon;//Bearish Arrow Color
input int bearishWinding=234;//Arrow Wingding Character
input int bearishArrowSize=0;//Bearish Arrow Size 0-5

double BEARISH[];
double BULLISH[];

int    nShift;//for shifting arrows

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorShortName("MA_Arrows");

//Bearish Set up
   SetIndexLabel(0,"MA_Arrows Bearish");
   SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,bearishArrowSize,bearishArrowColor);//sell
   SetIndexArrow(0,bearishWinding);
   SetIndexBuffer(0,BEARISH);

//Bullish Set up
   SetIndexLabel(1,"MA_Arrows Bullish");
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,bullishArrowSize,bullishArrowColor);//buy
   SetIndexArrow(1,bullishWinding);
   SetIndexBuffer(1,BULLISH);

   switch(Period())//each timeframe has own nShift value for arrow distance
     {
      case     1:
         nShift = 5;
         break;
      case     5:
         nShift = 7;
         break;
      case    15:
         nShift = 9;
         break;
      case    30:
         nShift = 14;
         break;
      case    60:
         nShift = 19;
         break;
      case   240:
         nShift = 24;
         break;
      case  1440:
         nShift = 84;
         break;
      case 10080:
         nShift = 104;
         break;
      case 43200:
         nShift = 204;
         break;
     }

   nShift=nShift*shiftMultiplier;//shift arrows by multipling
   ArrayInitialize(BEARISH,0.0);
   ArrayInitialize(BULLISH,0.0);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void deinit()
  {
   ObjectsDeleteAll(0,"MA_Arrows",0,OBJ_ARROW) ;
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0)
      return(-1);
//---- the last counted bar will be recounted
   if(counted_bars>0)
      counted_bars--;
   limit=Bars-counted_bars;
//---- main loop
   for(int i=0; i<limit; i++)
     {

      if(MACheck(i)==1)//BUY ARROWS
        {
         BULLISH[i]=Low[i]-nShift*Point;
         BEARISH[i]=0.0;
        }
      else
         if(MACheck(i)==-1)//SELL ARROWS
           {
            BEARISH[i]=High[i]+nShift*Point;
            BULLISH[i]=0.0;
           }
         else//NO ARROWS
           {
            BEARISH[i]=0.0;
            BULLISH[i]=0.0;
           }
     }
   return(0);
  }
int MACheck(int index)
  {
   int result=0;

   int r1=0;
   int r2=0;
   
   /*
   double  iMA( 
   string       symbol,           // symbol 
   int          timeframe,        // timeframe 
   int          ma_period,        // MA averaging period 
   int          ma_shift,         // MA shift 
   int          ma_method,        // averaging method 
   int          applied_price,    // applied price 
   int          shift             // shift 
   );
   */

   double fast=iMA(NULL,0,ma_period_fast,0,ma_method_fast,applied_price_fast,index);
   double slow=iMA(NULL,0,ma_period_slow,0,ma_method_slow,applied_price_slow,index);

   double fastP=iMA(NULL,0,ma_period_fast,0,ma_method_fast,applied_price_fast,index+1);
   double slowP=iMA(NULL,0,ma_period_slow,0,ma_method_slow,applied_price_slow,index+1);

   if(fast>slow)//current check
     {
      r1=1;
     }
   else
     {
      r1=-1;
     }

   if(fastP>slowP)//prev check
     {
      r2=1;
     }
   else
     {
      r2=-1;
     }

   if(r1==1 && r2!=1)//get state
     {
      result=1;
     }
   else
      if(r1==-1 && r2!=-1)
        {
         result=-1;
        }
      else
        {
         result=0;
        }



   return result;
  }
//+------------------------------------------------------------------+
