How to put variables into arrays?

 

Hi. I am trying to build an array in my EA, and I want to build it using variables. For example:

int var1=1,var2=2,var3=3;

int array[2] = {var1,var2,var3};

I believe that MQL4 starts its arrays with array[0], so it needs to be array[2] to define an array that has 3 values. The above example doesn't work because the compiler doesn't expect to see var1, var2, or var3 in the inputs for the array. How to I work around this?

 
double array[2],var1,var2,var3;

var1=iOpen("USDCHF",PERIOD_H1,i);

var2=iClose("USDCHF",PERIOD_H1,i);

var3=iLow("USDCHF",PERIOD_H1,i);

array[0]=var1;

array[1]=var2;

array[2]=var3;

?

 

What I'm trying is:

extern int FirstVar,SecondVar,ThirdVar;

int array[2] = {FirstVar,SecondVar,ThirdVar};

And this doesn't work. Even when I do:

int array[0]=FirstVar;

array[1]=SecondVar;

array[2]=ThirdVar;

It doesn't work. It works if you put a function after the = sign, but not if you put a variable that corresponds to an integer.

 

I dont get a compile error when i do

int testme[2];

int var1=1;

int var2=2;

int var3=3;

testme[0]=var1;

testme[1]=var2;

testme[2]=var3;

but i have not tried running it

 

i would imagine int prolly cant directly figure out the text will be used as a int cuz its not a number?

 

Hmm.... So I tried that it worked for me, but I noticed that I have to int the array[2] first, then add the array[0]=var1 etc after that. How can I use the array[#]={a,b,c}; notation like it shows in the manual though? Everything I have tried results in a compile error.

 

if u use letters u may have to do

string testme[4]={"var1","var2","var3","var4","var5"};

and then do DoubletoStr somewhere

but im a n00b so i dunno

 
int array[6] = {1,4,9,16,25,36};[/code]

That is stated in the manual, and it works.

[code]

int var1=1;

int array[6] = {var1,4,9,16,25,36};

That doesn't work, but that is what I want to work.

 
triwebb1:
Hi. I am trying to build an array in my EA, and I want to build it using variables. For example:

int var1=1,var2=2,var3=3;

int array[2] = {var1,var2,var3};

I believe that MQL4 starts its arrays with array[0], so it needs to be array[2] to define an array that has 3 values. The above example doesn't work because the compiler doesn't expect to see var1, var2, or var3 in the inputs for the array. How to I work around this?

The '2' above refers to the number of array elements, not its index, so for 3 variables, you must use 'array[3]' - MT will allow the above to compile but the array is still of length 2 so 'var3' will have nowhere to go...

Also, as you discovered MQL does not allow arrays to be defined using variables as elements. Not the end of the world though as there are easy work arounds.

A much more important issue would be having a profitable strategy to code in the first place

 

||████████████

\/

Reason: