Arrows are not displayed in real time

 
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2


// --- Sell
#property indicator_label1 "Sell"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrAqua
#property indicator_width1 1
input bool SELL_ARROW = true;

// --- Buy
#property indicator_label2 "Buy"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrAqua
#property indicator_width2 1
input bool BUY_ARROW = true;

//--- Indicator Buffer
double SELL[];
double BUY[];

// --- Global Variables
int MA_HANDLE;
int MACD_HANDLE;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   // --- Sell
   SetIndexBuffer(0, SELL, INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_ARROW, 226);
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 0);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0);
   ArraySetAsSeries(SELL, true);
   
   // --- Buy
   SetIndexBuffer(1, BUY, INDICATOR_DATA);
   PlotIndexSetInteger(1, PLOT_ARROW, 225);
   PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, 0);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0);
   ArraySetAsSeries(BUY, true);
   
   // --- Handle
   MA_HANDLE = iMA(NULL, PERIOD_CURRENT, 20, 0, MODE_SMA, PRICE_CLOSE);
   MACD_HANDLE = iMACD(NULL, PERIOD_CURRENT, 5, 25, 5, PRICE_CLOSE);
//---
   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[])
  {
//---
   // --- Bar Calculate
   int limit, to_copy;
   
   if(prev_calculated>rates_total || prev_calculated<=0)
       limit = rates_total;
       else
           limit = rates_total - prev_calculated;
  
  if(prev_calculated>rates_total || prev_calculated<=0)
      to_copy = rates_total;
      else
          {
           to_copy = rates_total - prev_calculated;
           to_copy ++;
          }
   
   // --- MA&MACD
   double ma[], ma_past[], macd[];
   CopyBuffer(MA_HANDLE, 0, 0, to_copy, ma);
   CopyBuffer(MACD_HANDLE, 0, 1, to_copy, ma_past);
   CopyBuffer(MACD_HANDLE, 0, 0, to_copy, macd);
   ArraySetAsSeries(ma, true);
   ArraySetAsSeries(ma_past, true);
   ArraySetAsSeries(macd, true);
   ArraySetAsSeries(open,true);
   ArraySetAsSeries(close, true);
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   
   // --- Arrow 
   for(int i = 0; i < rates_total; i++)
       {
       SELL[i] = EMPTY_VALUE;
       BUY[i] = EMPTY_VALUE;
       
       // --- Alert Time
       static datetime Timehold;
       
       //if(Timehold != iTime(NULL, PERIOD_CURRENT, i))
           {
            if(SELL_ARROW==true
                && ma[i] > close[i] && ma[i] < high[i] && ma[i+1] < high[i+1]
                && macd[i] < 0
               )
                {
                SELL[i] = high[i] + 50*Point();
                Alert("[" + Symbol() + "] Sell");
                Timehold = iTime(NULL, PERIOD_CURRENT, i);
                //Print(high[i+1]);
                }
       
            if(BUY_ARROW == true
               && ma[i] < close[i] && ma[i] > low[i] && ma[i+1] > low[i+1]
               && macd[i] > 0
               )
                {
                BUY[i] = low[i] - 50*Point();
                Alert("[" + Symbol() + "] Buy");
                Timehold = iTime(NULL, PERIOD_CURRENT, i);
                }
            
            else
                {
                SELL[i] = 0;
                BUY[i] = 0;
                }
           }
       }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Arrows are not displayed in real time
It will be displayed arrow when I switch the timeframe or insert an indicator, and an alert will sound. However, after the candlestick is updated on the target timeframe chart, the arrow is not displayed even though the arrow display conditions are met.

 
Attach (please) your code using the button Attach file
 
Vladimir Karputov:
Attach (please) your code using the button

Thank you

Files:
memo5.mq5  5 kb
 

Your code and result (I have put the indicators 'MA' and 'MACD' on the chart manually - for visualization).

MA MACD Test

Files:
 
Vladimir Karputov:

Your code and result (I have put the indicators 'MA' and 'MACD' on the chart manually - for visualization).


thank you very much. Try it with this code.

 
Vladimir Karputov #:

Your code and result (I have put the indicators 'MA' and 'MACD' on the chart manually - for visualization).


If the conditions are met, two arrows will be displayed at the same time. How can I hide the candlestick when the conditions are not met?

Reason: