0
votes

How to create an Array on MQL5 that can hold 3 variablesand how to fill it and how to call it? Please see the picture https://i.imgur.com/VyjmwNW.jpg [![array][1]][1]

After nicholishen Answer when i try to Initialize array useing Variables i get the follwing error : 'hi' - constant expression required 22.mq5 16 18

void OnStart()
{
   struct MyData { string name; double price; int volume; };
   // initializing an array of structs


     double hi= 0.01;


   MyData arr[] = {
      {"USDJPY", hi, 200},
      {"USDJPY", 110.100, 300},
      {"USDJPY", 110.200, 400},
   };

}
1
What do you mean when talking about 3 variables? different types of data (ints and doubles)? or something else?Daniel Kniaz
No the same, I'm new and learning and its driving me crazyuser2767046
so what is the problem? you can have a two-dimensional array, second dimension must be fixed, first can be dynamic and change. e.g. int array[][3]; then add more elements by using ArrayResize function - it is well documented so please show what you did and why it does not work so that we will try to help you wiht your code.Daniel Kniaz
Daniel thank you for your replay, Just want to understand the array structure in MQL5user2767046

1 Answers

0
votes

What I believe you are looking for is an array of structs. A struct is an object that can hold different types of data. Here is a example you can run in your terminal.

void OnStart()
{
   struct MyData { string name; double price; int volume; };
   // initializing an array of structs
   MyData arr[] = {
      {"USDJPY", 110.000, 200},
      {"USDJPY", 110.100, 300},
      {"USDJPY", 110.200, 400},
   };
   // changing some values 
   arr[1].name = "EURUSD";
   arr[1].price = 1.12;
   arr[1].volume = 1000;

   for (int i=0; i<ArraySize(arr); i++)
      printf(
         "Symbol=%s, price=%.3f, vol=%d", 
         arr[i].name, 
         arr[i].price, 
         arr[i].volume
      );
}