Buffers as series or not?

 

I have made an indicator that will show me valid highs and lows following a certain logic. 
But when after a while (maybe some hours) when i have it on the chart it will start to do weird things, draw lines that shouldn't be drawn, or delete certain line without any logic behind it.
I have used the buffers in my indicator with increasing indexes (so not set as series).

Is this something that I have to do when using buffers? Since I need to update them then on each new bar created and now i don't have to do that.
attached is the indicator file.

 
  1. int OnInit()
      {
       ArrayInitialize(HighsBuffer, EMPTY_VALUE);
       ArrayInitialize(LowsBuffer, EMPTY_VALUE);
       ArrayInitialize(LineBuffer, EMPTY_VALUE);
       ArrayInitialize(ProtectedHighsBuffer, EMPTY_VALUE);
       ArrayInitialize(ProtectedLowsBuffer, EMPTY_VALUE);
       ArrayInitialize(LineToFlipBullish, EMPTY_VALUE);
       ArrayInitialize(LineToFlipBearish, EMPTY_VALUE);=
       ArrayInitialize(LineForNewZone, EMPTY_VALUE);

    Your arrays (they are not buffers yet) have no size. Your calls do nothing. Must be done in OnCalculate.

  2. void OnDeinit(const int reason)
      {
       ArrayResize(HighsBuffer, 0);
       ArrayResize(LowsBuffer, 0);
       ArrayResize(LineBuffer, 0);
       ArrayResize(LineToFlipBullish, 0);
       ArrayResize(LineToFlipBearish, 0);
       ArrayResize(ProtectedHighsBuffer, 0);
       ArrayResize(ProtectedLowsBuffer, 0);

    Your buffers are handled by the terminal. You can't resize buffers.

  3.    int start = MathMax(rates_total - int(daysback * 24 * 60 / (PeriodSeconds(_Period) / 60)), 0);
       start = MathMax(start, prev_calculated - 2);
    

    Why are you processing 60*daysback worth of bars, each and every tick. If you don't have that many bars, array exceed (negative index) and your indicator crashes.
              How to do your lookbacks correctly #9#14 & #19 (2016)

  4.    for(int i = 0; i < rates_total- int(daysback * 24 * 60); i++)
    

    Loop does nothing unless you have 1440*dayback worth of bars. Why are you updating the earliest bars?

  5.       while(continueProcessing);
          LineBuffer[i] = currentVC;//The line that will validate a high/low
          LineForNewZone[i]= currentLineForNewZone;//The line that when breached a new zone will be determined
          LineToFlipBullish[i] = currentLineToFlipBullish;//The line that when breached, structure will shift bullish
          LineToFlipBearish[i] = currentLineToFlipBearish ;//The line that when breached, structure will shift bearish
         }

    Infinite loop.

  6. Gentleman26 Mattias: I have used the buffers in my indicator with increasing indexes (so not set as series).

    Where do you set your buffers to non-series?

  7.          continueProcessing = processBar(i, rates_total, high, low, close, open);
             continueProcessing = updateProtectedValues(i, rates_total, high, low, 

    Where do you set the direction of those arrays? In MT5, you must set the direction.

    To define the indexing direction in the time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[] arrays, call the ArrayGetAsSeries() function. In order not to depend on defaults, call the ArraySetAsSeries() function for the arrays to work with.
              Event Handling / OnCalculate - Reference on algorithmic/automated trading language for MetaTrader 5
 
William Roeder #:
  1. Your arrays (they are not buffers yet) have no size. Your calls do nothing. Must be done in OnCalculate.

  2. Your buffers are handled by the terminal. You can't resize buffers.

  3. Why are you processing 60*daysback worth of bars, each and every tick. If you don't have that many bars, array exceed (negative index) and your indicator crashes.
              How to do your lookbacks correctly #9#14 & #19 (2016)

  4. Loop does nothing unless you have 1440*dayback worth of bars. Why are you updating the earliest bars?

  5. Infinite loop.

  6. Where do you set your buffers to non-series?

  7. Where do you set the direction of those arrays? In MT5, you must set the direction.

Thanks for you comprehensive reply! 
I ‘learned’ coding via ChatGPT 4 and as it seems it makes good code but not the most solid code. I will read your sources and will try to correct where I can. 


 
William Roeder #:
  1. Your arrays (they are not buffers yet) have no size. Your calls do nothing. Must be done in OnCalculate.

  2. Your buffers are handled by the terminal. You can't resize buffers.

  3. Why are you processing 60*daysback worth of bars, each and every tick. If you don't have that many bars, array exceed (negative index) and your indicator crashes.
              How to do your lookbacks correctly #9#14 & #19 (2016)

  4. Loop does nothing unless you have 1440*dayback worth of bars. Why are you updating the earliest bars?

  5. Infinite loop.

  6. Where do you set your buffers to non-series?

  7. Where do you set the direction of those arrays? In MT5, you must set the direction.

I have updated my mq5 file. I do still have some questions about some of the feedback you gave.
1: removed that whole part as indeed it didn't do anything
2: Also removed the 'resizing' as mt5 does it automatically when deleting the indicator from the chart
3: I think I now made sure that only when a new candle has formed the logic will run
4: If you are referring to the fact that i'm emptying buffers before the lookback bars, if i don't do that the indicator will plot 0 values on all bars before the lookback bars.
5: I have a logic in that needs to be run again when for instance a block of code that is in the middle part is influencing the top part. but since that has already run i need to run the whole block of code one more time for it to have incorporated the change that was made in the middle part. (i hope i make myself clear here)
6: I have now set the buffer explicitly to non-series


Reason: