MQL5 Multi variables Array structure
MQL5 Multi variables Array structure
Loading saved threads...
user2767046 · External communityPost link
External question — Stack Overflow Stack Exchange
Author: user2767046
Original post: https://stackoverflow.com/questions/55396573
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
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.sstatic.net/VspJb.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},
};
}
Quote
Report
nicholishen · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: nicholishen
Original post: https://stackoverflow.com/a/55436123
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
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
);
}
Quote
Report
TheLegs · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: TheLegs
Original post: https://stackoverflow.com/a/79951000
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Based on your latest edit, where you are asking about using a variable to initialise the struct, you should consider having a constructor, like you would in a
class
. You can even overload it.
If you define the struct like this:
struct MyData
{
string name;
double price;
int volume;
// Version 1: requires all parameters
MyData(
string newName,
double newPrice,
int newVolume
):
name(newName),
price(newPrice),
volume(newVolume)
{}
// Version 2: you pre-set some values
MyData(
double newPrice,
int newVolume
):
price(newPrice),
volume(newVolume)
{
name = _Symbol;
}
// Default constructor: if you intend to use this
// method for an array like MyData data[], this must
// also be present
MyData() {}
};
Then, inside
OnStart()
, you can call for example:
void OnStart()
{
// ...the code above goes here, or outside this function
// This calls the default constructor, leaves it empty
MyData test1;
// This uses the first method
test1 = MyData("AUDCAD", 1.209, 188);
// This uses the other method
test1 = MyData(1.209, 188);
// Both of these ways will work with your variable now
double hi = 0.01;
test1 = MyData("AUDCAD", hi, 188);
test1 = MyData(hi, 188);
}
But note that with this method, the usual initialisation (like
test1 = {"AUDCAD", 1.209, 188}
) will no longer work as per
documentation
:
Classes and structures can have an explicit constructor and destructor. If your constructor is explicitly defined, the initialization of a structure or class variable using the initializing sequence is impossible.
By the way,
OnStart()
is for scripts and services, if your intention is to use an Expert Advisor or an Indicator, you should use
OnInit()
instead.
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: nicholishen Source score (net votes, not local likes): 2 Original post: https://stackoverflow.com/a/55436123 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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 ); }
Checking account access…