Indicator MQL4

 

Hello dear community, i wrote the Indicator below and the Array "MA_MX" supposed to find the max price difference with MA and draw a line above it.
However it sometimes gives me over 2Billion as the value and when i change the timeframe "sometimes" works seemingly ok. The problem is when it works ok it is obvious that its not the MAX line.


My question is why is the value is over 2 billion, why does it work when i change timeframe and why does it not calculate the max.

tnx.

Files:
MAjid_1.mq4  5 kb
 
Zolfagharipour: My question is why is the value is over 2 billion,

Unfilled buffer elements default to EMPTY_VALUE (2147483647 (0x7FFFFFFF)).

         BufferData3[i] = BufferData2[ArrayMaximum(BufferData2,period,0)];

Don't you want to only look at filled values? [rates_total-1 … i]

 
William Roeder #:

Unfilled buffer elements default to EMPTY_VALUE (2147483647 (0x7FFFFFFF)).

Don't you want to only look at filled values? [rates_total-1 … i]

ok now i understand why does the value give over 2billion, but how can i prevent it? cuz i feel what i calculate is filled value. 
 
Zolfagharipour # cuz i feel what i calculate is filled value. 

You are calculated bar i. Lower bars have not been calculated yet. Stop looking at them with ArrayMaximum.

 
William Roeder #:

You are calculated bar i. Lower bars have not been calculated yet. Stop looking at them with ArrayMaximum.

my point is finding the highest divergent of price and MA in the last "period" (here set to 100). how else i can find this?
also, when i comment the "BufferData3[i]" it gives me a proper number, yet when i set it in MA_MX it will give me the wrong number.
 
Zolfagharipour #: my point is finding the highest divergent of price and MA in the last "period" (here set to 100). how else i can find this?

What part of looking at uncalculated bars was unclear? There is no "how else". Fix your broken code. I have answered your problem three times.

Reason: