Problem with automatic indicator removal when running on new charts in MetaTrader 4

 
Hello friends,
I am developing an indicator that initially displays three vertical lines in the middle of the chart. These lines are calculated and drawn based on the number of candles on the chart (i.e. the middle of the chart). The indicator works correctly on fully loaded charts. However, when I run the indicator on a newly opened chart or timeframe where the candles are not yet fully loaded, the indicator is automatically removed from the chart.

The relevant part of my code for calculating and displaying the vertical lines is as follows:

datetime MiddleTime(int offset)
{
    int firstVisibleBar = WindowFirstVisibleBar();  
    int barsOnChart = WindowBarsPerChart();         
    if(firstVisibleBar < barsOnChart) firstVisibleBar = barsOnChart;
    datetime chartStartTime = iTime(NULL, 0, firstVisibleBar);
    datetime chartEndTime = iTime(NULL, 0, firstVisibleBar - barsOnChart);
    datetime midpointTime = chartStartTime + (chartEndTime - chartStartTime) / 2;
    int shift = iBarShift(NULL, 0, midpointTime) - offset;
   
    if (shift < 0 || shift >= Bars) {
        return TimeCurrent();  
    }
    return iTime(NULL, 0, shift);
}

Problem:

When the chart is not yet fully loaded (e.g. when the timeframe changes or a new chart is opened), the indicator is automatically removed instead of waiting for enough data to become available.

Questions:

Is this problem due to insufficient data during initial execution?

How can I configure the indicator to not be removed in such situations and wait for enough data to be loaded?

Do I need to use a special method to delay or check the number of loaded candles?

Any tips or code corrections would be appreciated.

Thanks for your time!