Indicator issue - Plotted Arrows disappear unintentionally when a new tick comes in

 

While converting my MT4 indicator code to MT5, I came across a strange issue where the arrows disappeared when a new tick came in. Could anyone help with this?

When I hit [z] key to activate indicator display, peak high and low arrows appear on chart, but soon after when a new tick comes in, they disappear unintentionally.

//+------------------------------------------------------------------+
//|                                                   PeakFinder.mq5 |
//|                                    Copyright 2020, Orchard Forex |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Orchard Forex"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_label1  "Peak_High"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_width1  1
#property indicator_label2  "Peak_Low"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrAqua
#property indicator_width2  1

input int PeakCount = 5;         // Total display count of high and low peaks
input int shoulder = 10;         // Shoulder bar count to find peak

double Peak_High[];
double Peak_Low[];
int bar_high = 0;;
int bar_low = 0;
bool showPF = false;
string IndicatorName = "pf";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, Peak_High, INDICATOR_DATA);
   SetIndexBuffer(1, Peak_Low, INDICATOR_DATA);
   ArraySetAsSeries(Peak_High, true);
   ArraySetAsSeries(Peak_Low, true);
   PlotIndexSetInteger(0, PLOT_ARROW, 159);
   PlotIndexSetInteger(1, PLOT_ARROW, 159);
   PlotIndexSetInteger(0, PLOT_ARROW_SHIFT, 0);
   PlotIndexSetInteger(1, PLOT_ARROW_SHIFT, 0);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectsDeleteAll(ChartID(), IndicatorName);
  }

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(id == CHARTEVENT_KEYDOWN)
     {
      if(lparam == 90)  // z key
        {
         if(showPF)
           {
            ArrayInitialize(Peak_High, EMPTY_VALUE);
            ArrayInitialize(Peak_Low, EMPTY_VALUE);
            showPF = false;
            GlobalVariableSet("pf_showPF", false);
           }
         else
           {
            bar_high = 0;
            bar_low = 0;
            for(int i = 0; i < PeakCount; i ++)
              {
               bar_high = FindPeak(Symbol(), Period(), MODE_HIGH, shoulder, bar_high + 1);
               Peak_High[bar_high] = iHigh(Symbol(), Period(), bar_high);
               bar_low = FindPeak(Symbol(), Period(), MODE_LOW, shoulder, bar_low + 1);
               Peak_Low[bar_low] = iLow(Symbol(), Period(), bar_low);
              }
            showPF = true;
            GlobalVariableSet("pf_showPF", true);
           }
        }
     }
   Comment("showPF = ", showPF);
  }

//+------------------------------------------------------------------+
//| 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[])
  {
   showPF = GlobalVariableGet("pf_showPF");
   Print("showPF = ", showPF);
   if(showPF)
     {
      ArrayInitialize(Peak_High, EMPTY_VALUE);
      ArrayInitialize(Peak_Low, EMPTY_VALUE);
      bar_high = 0;
      bar_low = 0;
      for(int i = 0; i < PeakCount; i ++)
        {
         bar_high = FindPeak(Symbol(), Period(), MODE_HIGH, shoulder, bar_high + 1);
         Peak_High[bar_high] = high[bar_high];
         bar_low = FindPeak(Symbol(), Period(), MODE_LOW, shoulder, bar_low + 1);
         Peak_Low[bar_low] = low[bar_low];
         PrintFormat("bar_high = %d  bar_low = %d", bar_high, bar_low);
         PrintFormat("PeakHigh[bar_high] = %f  PeakLow[bar_low] = %f", Peak_High[bar_high], Peak_Low[bar_low]);
        }
     }
   return(rates_total);
  }

//+---------------------------------------------------------------------------------------------+
//|  find highest or lowest bar standing out from specified number(count) of pre and post bars  |
//+---------------------------------------------------------------------------------------------+
int FindPeak(string symbol, ENUM_TIMEFRAMES period, ENUM_SERIESMODE mode, int count, int startBar)
  {
   if(mode != MODE_HIGH && mode != MODE_LOW)
      return(-1);
   int currentBar = startBar;
   int foundBar = FindNextPeak(symbol, period, mode, count * 2 + 1, currentBar - count);
   while(foundBar != currentBar)
     {
      currentBar = FindNextPeak(symbol, period,mode, count, currentBar + 1);
      foundBar = FindNextPeak(symbol, period,mode, count * 2 + 1, currentBar - count);
     }
   return(currentBar);
  }

//+------------------------------------------------------------------+
//|  calling from FindPeak to make logic simple and compact          |
//+------------------------------------------------------------------+
int FindNextPeak(string symbol, ENUM_TIMEFRAMES period, ENUM_SERIESMODE mode, int count, int startBar)
  {
   if(startBar < 0)
     {
      count  += startBar;
      startBar = 0;
      MessageBox("Start Bar is negative = " + (string)startBar);
     }
   return((mode == MODE_HIGH) ?
          iHighest(symbol, period, MODE_HIGH, count, startBar) :
          iLowest(symbol, period, MODE_LOW, count, startBar)
         );
  }

//+------------------------------------------------------------------+
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2026.04.09
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions