Download MetaTrader 5
To add comments, please log in or register
Share your program via Code Base. Gain access to millions of traders!
Ernest Klokow
1069
Ernest Klokow 2014.04.08 08:22 

I have need to declare an array that contains double data types, but I get the following error message:

'USDbefore1' - constant expression required

Not sure how I should go about it.

(Will something like "structs" solve the problem? - Obviously a question from a pure novice!)

Here is the code:

    double USDbefore1 = iCustom(NULL,0,"CurrencySlopeStrengthNew",0,1);
    double USDbefore2 = iCustom(NULL,0,"CurrencySlopeStrengthNew",0,2);
    double USDafter = iCustom(NULL,0,"CurrencySlopeStrengthNew",0,0); 
    double EURbefore1 = iCustom(NULL,0,"CurrencySlopeStrengthNew",1,1);
    double EURbefore2 = iCustom(NULL,0,"CurrencySlopeStrengthNew",1,2);
    double EURafter = iCustom(NULL,0,"CurrencySlopeStrengthNew",1,0);
    double GBPbefore1 = iCustom(NULL,0,"CurrencySlopeStrengthNew",2,1);
    double GBPbefore2 = iCustom(NULL,0,"CurrencySlopeStrengthNew",2,2);
    double GBPafter = iCustom(NULL,0,"CurrencySlopeStrengthNew",2,0);
    double CHFbefore1 = iCustom(NULL,0,"CurrencySlopeStrengthNew",3,1);
    double CHFbefore2 = iCustom(NULL,0,"CurrencySlopeStrengthNew",3,2);
    double CHFafter = iCustom(NULL,0,"CurrencySlopeStrengthNew",3,0);
    double JPYbefore1 = iCustom(NULL,0,"CurrencySlopeStrengthNew",4,1);
    double JPYbefore2 = iCustom(NULL,0,"CurrencySlopeStrengthNew",4,2);
    double JPYafter = iCustom(NULL,0,"CurrencySlopeStrengthNew",4,0);
    double AUDbefore1 = iCustom(NULL,0,"CurrencySlopeStrengthNew",5,1);
    double AUDbefore2 = iCustom(NULL,0,"CurrencySlopeStrengthNew",5,2);
    double AUDafter = iCustom(NULL,0,"CurrencySlopeStrengthNew",5,0);
    double CADbefore1 = iCustom(NULL,0,"CurrencySlopeStrengthNew",6,1);
    double CADbefore2 = iCustom(NULL,0,"CurrencySlopeStrengthNew",6,2);
    double CADafter = iCustom(NULL,0,"CurrencySlopeStrengthNew",6,0);
    double NZDbefore1 = iCustom(NULL,0,"CurrencySlopeStrengthNew",7,1);
    double NZDbefore2 = iCustom(NULL,0,"CurrencySlopeStrengthNew",7,2);
    double NZDafter = iCustom(NULL,0,"CurrencySlopeStrengthNew",7,0);

 
   double Before1[8] = {USDbefore1,EURbefore1,GBPbefore1,CHFbefore1,JPYbefore1,AUDbefore1,CADbefore1,NZDbefore1};
   double Before2[8] = {USDbefore2,EURbefore2,GBPbefore2,CHFbefore2,JPYbefore2,AUDbefore2,CADbefore2,NZDbefore2};
   double After[8] = {USDafter,EURafter,GBPafter,CHFafter,JPYafter,AUDafter,CADafter,NZDafter};
Simon Gniadkowski
Moderator
17962
Simon Gniadkowski 2014.04.08 08:45  
ernest02:

I have need to declare an array that contains double data types, but I get the following error message:

'USDbefore1' - constant expression required

Not sure how I should go about it.

(Will something like "structs" solve the problem? - Obviously a question from a pure novice!)

Here is the code:

The problem is your declaration . . . declare it, then set the values.
Ernest Klokow
1069
Ernest Klokow 2014.04.08 08:51  
RaptorUK:
The problem is your declaration . . . declare it, then set the values.


Thanks for helping RaptorUK.

Could you elaborate a little on "declare it" please? Are you referring to the arrays or the variables?

Ernest Klokow
1069
Ernest Klokow 2014.04.08 09:20  

When I declare them one by one like this it works, but I do not want to write 24 lines of code where 3 lines could do it.

 Before1[0] = USDbefore1;
 Before1[1] = EURbefore1;
etc.
Ernest Klokow
1069
Ernest Klokow 2014.04.08 13:16  
Nobody willing to help a struggling programmer?
GreenMoney
379
GreenMoney 2014.04.08 13:25  

As RaptorUK said, the problem is your declaration. An array may be initialized at declaration only by constants. See Initialization of Variables.

Just declare your array and then set each element (one by one) to the desired value. You might even be able to create a loop to set each element in the arrays, which would possibly save lines of code.

whroeder1
13593
whroeder1 2014.04.08 14:00  
    double USDbefore1 = iCustom(NULL,0,"CurrencySlopeStrengthNew",0,1);
    double USDbefore2 = iCustom(NULL,0,"CurrencySlopeStrengthNew",0,2);
You can not declare variables globally and initialize them with non-constants. Either declare them globally and initialize them in OnTick() or move them into OnTick()
   double Before1[8] = {USDbefore1,EURbefore1,GBPbefore1,CHFbefore1,JPYbefore1,AUDbefore1,CADbefore1,NZDbefore1};
Same problem, non-contants
Ian Venner
2583
Ian Venner 2014.04.08 16:16  

Declare the arrays and intitialize them with a constant like this before you use them to store variables.

double Before1[8]={0};

I used zero but you could also use the special constant NULL

double Before1[8]={NULL};

That declares the array and initializes it.

After that you then you can put whatever you want in it.

GreenMoney
379
GreenMoney 2014.04.08 17:40  

Thirteen:

. . . You might even be able to create a loop to set each element in the arrays, which would possibly save lines of code.

Code compiles but has not been tested.

// Declare arrays and initialize to zero
double Before1[8] = {0}, Before2[8] = {0}, After[8] = {0};

// Set array elements to desired values
for (int i = 0; i < 8; i++) {
   After[i] = iCustom(NULL,0,"CurrencySlopeStrengthNew",i,0);
   Before1[i] = iCustom(NULL,0,"CurrencySlopeStrengthNew",i,1);
   Before2[i] = iCustom(NULL,0,"CurrencySlopeStrengthNew",i,2);
}
.
Ernest Klokow
1069
Ernest Klokow 2014.04.08 20:16  

Thank you very much guys! You are really very generous in your advice and assistance. I have learned a lot from this.

Roeder and Thirteen - you were exceptionally helpful. Thanks a lot!

/
To add comments, please log in or register