Custom Indicator! Hight & Low - Help!

 

Hello Everyone!,

I can't make this work!, debugger show me "Array out of range", surely must be the main loop but where?

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(prev_calculated == 0)
      count = 0;
   else
      count = prev_calculated - 1;

   for(i = count; i < rates_total - 1; i ++)
     {

      if((high[rates_total - i - 1] >= high[rates_total - i - 2] && high[rates_total - i - 3] && high[rates_total - i - 4]) &&
         (low[rates_total - i - 1] <= low[rates_total - i - 2] && low[rates_total - i - 3] && low[rates_total - i - 4]))
        {
         bufferMaster[rates_total - i - 1] = high[rates_total - i - 1];
        }
     }

   return(rates_total);
  }


Thanks in Advanced!


Reggards!

 
count = prev_calculated - 1;

mean : i = count = rates_total - 2

>> rates_total - i - 3 = rates_total - (rates_total - 2) - 3 = -1

i don't understand why you calculator all bar every new bar

for(i = count; i < rates_total - 1; i ++)
 
Quiz Test:

mean : i = count = rates_total - 2

>> rates_total - i - 3 = rates_total - (rates_total - 2) - 3 = -1

i don't understand why you calculator all bar every new bar

Hey! Thanks Anyways, i did it!, just i change the scale !

but why u said "i don't understand why you calculator all bar every new bar", because prev_calc only once is 0.

 
fdesu:

Hello Everyone!,

I can't make this work!, debugger show me "Array out of range", surely must be the main loop but where?


Thanks in Advanced!


Reggards!

You have a loop

for(i = count; i < rates_total - 1; i ++)

At some point then i=rates_total-2.

Then you have in your if statement

high[rates_total - i - 3]

Think about the index to the array.

rates_total-i-3 = rates_total-(rates_total-2)-3 = 2-3 = -1
 

Ohh Thanks all for the help!!

Very thankful

 

it is showing Out of range because, the program is requesting for a candle that is not there, you can 

//--- Print the total number (rates_total) of bars in the chart
//--- and print the value of "i" inside the loop as the program is running

Then the prev_calculated is only equal to zero at the beginning of the program and when a new bar is added, but when new ticks are coming it is equal to zero, hence if you are in the H1 TF, per ticks count would be equal to zero, then when a new bar comes, 

// The value of count would still be zero
count = prev_calculated -1

// prev_calculated would be 1 after the first run and zero per tick

this is a common misconception. Read this page https://www.mql5.com/en/forum/151666/page3#comment_3772589

read the above the comments they have showed what is wrong with the logic in your loop

// Then this would provide a different result than what you are thinking
(high[i] >= high[i - 1] && high[i - 2] && high[i - 3])

// it only compares the first two highs , the 3rd and 4th highs are always true in as much as the high is having a value.
// from your code, I am guessing you want the buffer to hold the candles high only if it higher than that of the previous 4 candles to its side

// then ensure "i" is greater than the amount of candles that you are comparing or the program would give "Array out of range error
My indicator disappear anytime I change to new time-frame chart
My indicator disappear anytime I change to new time-frame chart
  • 2014.06.17
  • www.mql5.com
Hello, I start with an indicator (The FIRST PART). It works fine, when changing time frame...
 
Trapheal:

it is showing Out of range because, the program is requesting for a candle that is not there, you can 

Then the prev_calculated is only equal to zero at the beginning of the program and when a new bar is added, but when new ticks are coming it is equal to zero, hence if you are in the H1 TF, per ticks count would be equal to zero, then when a new bar comes, 

this is a common misconception. Read this page https://www.mql5.com/en/forum/151666/page3#comment_3772589

read the above the comments they have showed what is wrong with the logic in your loop

yep is working!!, thanks all :)

Reason: