//+------------------------------------------------------------------+
//|                                2_MA_Candle_Close_Arrows_v1.0.mq4 |
//|                                         Copyright 2021, NickBixy |
//|             https://www.forexfactory.com/showthread.php?t=904734 |
//+------------------------------------------------------------------+
#property copyright "NickBixy"
#property link      "https://www.forexfactory.com/showthread.php?t=904734"
//#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 4

input string Header="----------------- MA Settings------------------------------------------";//-----  MA Settings
input int MA1_Period=3;//MA 1 Period
input ENUM_MA_METHOD MA1_Method=MODE_SMA;//MA 1 METHOD
input ENUM_APPLIED_PRICE MA1_Price=PRICE_TYPICAL;//MA 1 APPLIED_PRICE
input int MA1_Shift=0;//MA 1 Shift
input ENUM_LINE_STYLE MA1_Style=STYLE_SOLID;//MA 1 Line Style
input int MA1_LineWidth=1;//MA 1 Line Width
input color MA1_Color=clrGold;//MA 1 Line Color

input int MA2_Period=7;//MA 2 Period
input ENUM_MA_METHOD MA2_Method=MODE_SMA;//MA 2 METHOD
input ENUM_APPLIED_PRICE MA2_Price=PRICE_TYPICAL;//MA 2 APPLIED_PRICE
input int MA2_Shift=0;//MA 2 Shift
input ENUM_LINE_STYLE MA2_Style=STYLE_SOLID;//MA 2 Line Style
input int MA2_LineWidth=1;//MA 2 Line Width
input color MA2_Color=clrSilver;//MA 2 Line Color

input string ArrowHeader="-----------------Arrows Settings------------------------------------------";//----- Arrows Settings
input double shiftMultiplier=.40;//Shift Multipilier Arrows up or down more
input int atrValue=21;//ATR value for arrow draw distance
input string info="https://docs.mql4.com/constants/objectconstants/wingdings";//Windings font symbols
input color bullishArrowColor=clrLimeGreen;//Bullish Arrow Color
input int bullishWinding=217;//Arrow Wingding Character
input int bullishArrowSize=1;//Bullish Arrow Size 0-5
input color bearishArrowColor=clrCrimson;//Bearish Arrow Color
input int bearishWinding=218;//Arrow Wingding Character
input int bearishArrowSize=1;//Bearish Arrow Size 0-5

double BEARISH[];
double BULLISH[];

double MA1[];
double MA2[];
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorShortName("2MA_Arrows");

//Bearish Set up
   SetIndexLabel(0,"2MA_Arrows Bearish");
   SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,bearishArrowSize,bearishArrowColor);//sell
   SetIndexArrow(0,bearishWinding);
   SetIndexBuffer(0,BEARISH);

//Bullish Set up
   SetIndexLabel(1,"2MA_Arrows Bullish");
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,bullishArrowSize,bullishArrowColor);//buy
   SetIndexArrow(1,bullishWinding);
   SetIndexBuffer(1,BULLISH);

//MA1 Set up
   SetIndexLabel(2,"2MA_Arrows MA1");
   SetIndexStyle(2,DRAW_LINE,MA1_Style,MA1_LineWidth,MA1_Color);//MA 1
   SetIndexBuffer(2,MA1);

//MA2 Set up
   SetIndexLabel(3,"2MA_Arrows MA2");
   SetIndexStyle(3,DRAW_LINE,MA2_Style,MA2_LineWidth,MA2_Color);//MA 2
   SetIndexBuffer(3,MA2);

   ArrayInitialize(BEARISH,0.0);
   ArrayInitialize(BULLISH,0.0);
   ArrayInitialize(MA1,0.0);
   ArrayInitialize(MA2,0.0);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void deinit()
  {
   ObjectsDeleteAll(0,"2MA_Arrows",0,OBJ_ARROW);
   ArrayInitialize(BEARISH,0.0);
   ArrayInitialize(BULLISH,0.0);
   ArrayInitialize(MA1,0.0);
   ArrayInitialize(MA2,0.0);
  }
//+------------------------------------------------------------------+
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++)
     {
      MA1[i]=iMA(NULL,PERIOD_CURRENT,MA1_Period,MA1_Shift,MA1_Method,MA1_Price,i);
      MA2[i]=iMA(NULL,PERIOD_CURRENT,MA2_Period,MA2_Shift,MA2_Method,MA2_Price,i);

      if(MACheck(i)==1)//BUY ARROWS
        {
         BULLISH[i]=Low[i]-iATR(NULL,0,atrValue,i)*shiftMultiplier;
         BEARISH[i]=0.0;
        }
      else
         if(MACheck(i)==-1)//SELL ARROWS
           {
            BEARISH[i]=High[i]+iATR(NULL,0,atrValue,i)*shiftMultiplier;
            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 MA1Value=iMA(NULL,PERIOD_CURRENT,MA1_Period,MA1_Shift,MA1_Method,MA1_Price,index);
   double MA2Value=iMA(NULL,PERIOD_CURRENT,MA2_Period,MA2_Shift,MA2_Method,MA2_Price,index);

   double MA1PValue=iMA(NULL,PERIOD_CURRENT,MA1_Period,MA1_Shift,MA1_Method,MA1_Price,index+1);
   double MA2PValue=iMA(NULL,PERIOD_CURRENT,MA2_Period,MA2_Shift,MA2_Method,MA2_Price,index+1);

   double closePrice=iClose(NULL,PERIOD_CURRENT,index);
   double closePriceP=iClose(NULL,PERIOD_CURRENT,index+1);

   if(closePrice>MA1Value && closePrice>MA2Value)//current check
     {
      r1=1;
     }
   else
      if(closePrice<MA1Value && closePrice<MA2Value)//current check
        {
         r1=-1;
        }
      else
        {
         r1=0;
        }
   if(closePriceP>MA1PValue && closePriceP>MA2PValue)//prev check
     {
      r2=1;
     }
   else
      if(closePriceP<MA1PValue && closePriceP<MA2PValue)//prev check
        {
         r2=-1;
        }
      else
        {
         r2=0;
        }
   if(r1==1 && r2!=1)//get state
     {
      result=1;
     }
   else
      if(r1==-1 && r2!=-1)
        {
         result=-1;
        }
      else
        {
         result=0;
        }
   return result;
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
