How to correctly initialize and work with vectors?

 

Using MetaEditor version 5.00 build 5430, Nov 14, 2025


I've read the docs on vectors, and trying a toy example, which compiles with the errors:

'vector_a' - unexpected token, probably type is missing?        playground.mqh  32      1

'.' - semicolon expected playground.mqh 32 9


Here's the toy code:
// everything above here is boilerplate comments
..
31 vector vector_a = {50228.0,50268.0,50143.0
    }; 
32 vector_a.Percentile(25);

According to the docs, `vector` is its own type, and the way it's used here is as type double. 
On line 32, I can type `vector_a`, followed by a period('.'), hit the Tab key and get the code hint that includes all the applicable methods for matrices and vectors.

If I remove `.Percentile(25) from line 32, leaving just `vector_a;` and compile, I get:

variable already defined        playground.mqh  32      1
   see declaration of variable 'vector_a'       playground.mqh  31      8
idenfitier 'vector_a' already used      playground.mqh  31      8
   see declaration of variable 'vector_a'       playground.mqh  31      8

which says `vector_a` is defined.

How do I correctly define and use vectors?

Thanks in advance for the help.

 
Please, post the complete code.
 
Kyle Young Sangster:

Using MetaEditor version 5.00 build 5430, Nov 14, 2025


I've read the docs on vectors, and trying a toy example, which compiles with the errors:


Here's the toy code:

According to the docs, `vector` is its own type, and the way it's used here is as type double. 
On line 32, I can type `vector_a`, followed by a period('.'), hit the Tab key and get the code hint that includes all the applicable methods for matrices and vectors.

If I remove `.Percentile(25) from line 32, leaving just `vector_a;` and compile, I get:

which says `vector_a` is defined.

How do I correctly define and use vectors?

Thanks in advance for the help.

Stanislav Korotky #:
Please, post the complete code.
File is attached. Like I mentioned, it's just a toy example. Most of it is comments.
Files:
 
Kyle Young Sangster #:
File is attached. Like I mentioned, it's just a toy example. Most of it is comments.

Well, the initialization of the vector compiles normally.

The error comes from the next line:

vector_a;

which is incorrect, because it's incomplete global expression - you should use the variable somehow (for example in assigment/further initialization/calculation/as a parameter for a function call/etc):

vector vector_b = vector_a;

You can't write a standalone variable name in the global scope, but you can do it in a function:

void xyz()
{
   vector_a; // WARNING: expression has no effect
}

Yet, you'll see the warning, because the code makes no sense.

 
Stanislav Korotky #:

Well, the initialization of the vector compiles normally.

The error comes from the next line:

which is incorrect, because it's incomplete global expression - you should use the variable somehow (for example in assigment/further initialization/calculation/as a parameter for a function call/etc):

You can't write a standalone variable name in the global scope, but you can do it in a function:

Yet, you'll see the warning, because the code makes no sense.

I understand what you are referring to. As I tried to point out, I tried to use the Percentile() method on vector_a, which also didn't work. Example:

vector_a.Percentile(25)

This is the original line that produced the first errors in the original post.
 
Kyle Young Sangster #:
I understand what you are referring to. As I tried to point out, I tried to use the Percentile() method on vector_a, which also didn't work. Example:

vector_a.Percentile(25)

This is the original line that produced the first errors in the original post.
It seems, you should learn coding a bit, because your question/error is simple and already answered above (write instructions in a function, not in the global scope, until you know how to use the global scope). There are a number of books on MQL programming, explaining the basics needed to eliminate such errors.
 
Stanislav Korotky #:
It seems, you should learn coding a bit, because your question/error is simple and already answered above (write instructions in a function, not in the global scope, until you know how to use the global scope). There are a number of books on MQL programming, explaining the basics needed to eliminate such errors.
Thanks for taking the time to reply to this basic question. Thank you for the answer.
 
Stanislav Korotky #:
It seems, you should learn coding a bit, because your question/error is simple and already answered above (write instructions in a function, not in the global scope, until you know how to use the global scope). There are a number of books on MQL programming, explaining the basics needed to eliminate such errors.
Thank you again. I reviewed some of the suggested resources, and I see where I was going astray. Thank you again for the help.