Indicator fail to calculate at specific time and fail to load sometime

Le Minh Duc  

Hello, I'm building a zigzag base on MACD indicator. There are 2 problems i'm dealing with it:

1. It fail to calculate from 7:50AM 13/09/2023 on EURUSD M1, and other symbols.

2. Sometime it crash when i try to load it to chart or refresh the window. 

I have no clue about both of them

//+------------------------------------------------------------------+
//|                                                  ZigzagColor.mq5 |
//|                             Copyright 2000-2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   1
#property indicator_type1   DRAW_COLOR_ZIGZAG
#property indicator_color1  clrBlue, clrRed

//+------------------------------------------------------------------+
//| Includes & Defines                                               |
//+------------------------------------------------------------------+
#define indexZZPeak     0
#define indexZZBottom   1
#define indexZZColor    2

#define indexMACD       3
#define MACDBuffer      0
#define MACDIndicatorPath "MACD.ex5"

//+------------------------------------------------------------------+
//| Inputs and variables                                             |
//+------------------------------------------------------------------+
// Buffer
double bufferMACD[];
double bufferZZPeak[];
double bufferZZBottom[];
double bufferZZColor[];

// Handle
int handleMACD;

// Others
int longestPeriod;
static int extremeBar = -1;

static int lastPos = -1;
static double lastVal = 0;

input group "_________MACD_________";
input ushort              inpMaFastPeriod   = 05;               // MA Fast Period
input ushort              inpMASlowPeriod   = 30;               // MA Slow Period
input ENUM_APPLIED_PRICE  inpAppliedPrice   = PRICE_MEDIAN;     // Apply to
//input ENUM_MA_METHOD      inpMAMode         = MODE_EMA;         // Method


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit() {
  longestPeriod = MathMax(inpMASlowPeriod, inpMaFastPeriod);
  ZigZagInit();
  MACDInit();
}

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
  ZigZagDeinit();
  MACDDeinit();
}

//+------------------------------------------------------------------+
//| ZigZag calculation                                               |
//+------------------------------------------------------------------+
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[]) {
  if(rates_total < longestPeriod) return 0;
  
  //--- initializing
  ArraySetAsSeries(bufferMACD, true);
  ArraySetAsSeries(bufferZZPeak, true);
  ArraySetAsSeries(bufferZZBottom, true);
  ArraySetAsSeries(bufferZZColor, true);
  ArraySetAsSeries(high, true);
  ArraySetAsSeries(low, true);
  
  int copyBars = 0;
  
  if(prev_calculated > rates_total || prev_calculated <= 0) copyBars = rates_total - longestPeriod;
  else copyBars = rates_total - prev_calculated;
  
  //--- Copy MACD
  if(!CopyBuffer(handleMACD, MACDBuffer, 0, copyBars, bufferMACD)) return 0;
  
  for(int bar = 0; bar < copyBars; ++bar) {
    if(extremeBar == -1) {
      extremeBar = bar + 1;
      lastPos = extremeBar;
    }
    if((bufferMACD[bar] > 0 && high[bar] > high[extremeBar]) || // Dương
       (bufferMACD[bar] < 0 && low[bar] < low[extremeBar]) ) {  // Âm
      extremeBar = bar;
      UpdateBuffers1(high, low, bar);
    }
  }
  
  ChartRedraw(0);
  //--- return value of prev_calculated for the next call
  return rates_total;
}

//+------------------------------------------------------------------+
//+==================================================================+
//|  Zig-Zag                                                         |
//+==================================================================+
//+-----------------------+
// Initialize
//+-----------------------+
void ZigZagInit() {
  //--- indicator buffers mapping
  SetIndexBuffer(indexZZPeak, bufferZZPeak, INDICATOR_DATA);
  SetIndexBuffer(indexZZBottom, bufferZZBottom, INDICATOR_DATA);
  SetIndexBuffer(indexZZColor, bufferZZColor, INDICATOR_COLOR_INDEX);


  PlotIndexSetString(0, PLOT_LABEL, "Peak;Bottom");

  //--- set accuracy
  IndicatorSetInteger(INDICATOR_DIGITS, _Digits);

  //--- set an empty value
  PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);
}

void MACDInit() {
  SetIndexBuffer(indexMACD, bufferMACD, INDICATOR_CALCULATIONS);
  //handleMACD = iCustom(NULL, PERIOD_CURRENT, MACDIndicatorPath, inpMaFastPeriod, inpMASlowPeriod, inpAppliedPrice, inpMAMode);
  handleMACD = iMACD(NULL, PERIOD_CURRENT, inpMaFastPeriod, inpMASlowPeriod, 1, inpAppliedPrice);
}

//+-----------------------+
// ZigZag Handling
//+-----------------------+
void UpdateBuffers1(const double &high[],
                   const double &low[],
                   int bar) {
  if(bufferMACD[bar] < 0) {  
    bufferZZBottom[extremeBar] = low[extremeBar];
    bufferZZBottom[lastPos] = 0;
    bufferZZColor[extremeBar] = 1;
    lastPos = extremeBar;
  }
  else if(bufferMACD[bar] > 0) { 
    bufferZZPeak[extremeBar] = high[extremeBar];
    bufferZZPeak[lastPos] = 0;
    bufferZZColor[extremeBar] = 0;
    lastPos = extremeBar;  
  }
}

//+-----------------------+
// DeInitialize
//+-----------------------+
void ZigZagDeinit() {
  ArrayFree(bufferZZPeak);
  ArrayFree(bufferZZBottom);
  ArrayFree(bufferZZColor);
}

void MACDDeinit() {
  ArrayFree(bufferMACD);
  IndicatorRelease(handleMACD);
}


Reason: