Arrow not printing after the line cross

 

Hi team, I got a custom code. The arrow seems to follow the next candle open and it will not reflect after the 2 MA cross correctly without me refreshing the chart. PLease advise how to fix this thank you!!

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 2
#property indicator_color1 0x0000FF
#property indicator_label1 "Sell"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 2
#property indicator_color2 0x00FF37
#property indicator_label2 "Buy"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | Binary Options @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 234);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 233);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 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 limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(iMA(NULL, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, i) > iMA(NULL, PERIOD_CURRENT, 2, -1, MODE_EMA, PRICE_CLOSE, i)
      && iMA(NULL, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, i+1) < iMA(NULL, PERIOD_CURRENT, 2, -1, MODE_EMA, PRICE_CLOSE, i+1) //Moving Average crosses above Moving Average
      )
        {
         Buffer1[i] = High[i]; //Set indicator value at Candlestick High
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(iMA(NULL, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, i) < iMA(NULL, PERIOD_CURRENT, 2, -1, MODE_EMA, PRICE_CLOSE, i)
      && iMA(NULL, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, i+1) > iMA(NULL, PERIOD_CURRENT, 2, -1, MODE_EMA, PRICE_CLOSE, i+1) //Moving Average crosses below Moving Average
      )
        {
         Buffer2[i] = Low[i]; //Set indicator value at Candlestick Low
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
aksofficial:

Hi team, I got a custom code. The arrow seems to follow the next candle open and it will not reflect after the 2 MA cross correctly without me refreshing the chart. PLease advise how to fix this thank you!!

Try to add ChartRedraw() operation in the end of your code.

Like this....

   
   ChartRedraw();
   //----
   return(rates_total);
  }
 
Nikolaos Pantzos:

Try to add ChartRedraw() operation in the end of your code.

Like this....

Thank you for helping! 

However, the issue is the same..when the MA cross each other it shows a red arrow on the current candle even if it's in an upward direction and I still need to manually refresh to get the arrow.

Reason: