Array out of range

 

Hello everyone! I am new to MQL4. Would love to have some help. I am trying to create an indicator and getting "Array out of range" on the code below for "var[1]". Would anyone know why?


double var[];

for(int i=0; i<rates_total; i++)

        {

                var[i]=Close[i];

        }


Any help would be very much appreciated!

Best regards!

 
Axelrod D:

Hello everyone! I am new to MQL4. Would love to have some help. I am trying to create an indicator and getting "Array out of range" on the code below for "var[1]". Would anyone know why?

You have to size the array

 

you mean add a number here:

double var[SIZE_HERE];

for(int i=0; i<rates_total; i++)

        {

                var[i]=Close[i];

        }

I was wishing to use rates_total. If I understood right, that would include all candles on chart. But still get  "'[' - invalid index value" error with that.


 
Axel D: you mean add a number here:
  1. No, because you don't know what the maximum of rates_total will be, you can't declare a constant size.

  2. Perhaps you should read the manual.
    ArrayResize The function sets a new size for the first dimension
              ArrayResize - Array Functions - MQL4 Reference

  3. Why are you trying to use an array in an indicator. Use a buffer and it will be automatically resized for you.
 

As I understood, arrays would work like this on the chart:

https://pasteboard.co/IdNeOhR.png

So I wanted to be able to work using the second-last value from a line (array) in order to create the last value from another one.

Thank you guys very much for the help!

 
  1. Please use the link button Use the link button See the difference? https://pasteboard.co/IdNeOhR.png
              Messages Editor

  2. Please don't post a link to or attach an image, just insert the image.
              Messages Editor

    That is supposed to mean something?

  3. Axel DSo I wanted to be able to work using the second-last value from a line (array) in order to create the last value from another one.
    That sounds like gibberish to me. Only the "last value" can create the "last value."
 

Thank you for the tip!

I will try to explain it better:

Lets say this black line with dots above is line_a. We could then have line_b being always equal to Close as default. Then whenever line_a[1] < Close then line_b[0] = line_a[1].

Reason: