array declaration double - is mandatory to compile ?

 

This example works only like this:

   int ifori = 6;

   double ArrLots[6] ={0.3,0.21,0.17,0.12,0.1,0.1};

   double ArrTPs[6]={19,23,32,45,75,140}; 

==================================================================

If i try it in this format - the compiler says: "{" - expression is expected - and i get 2 error:

double ArrTPs[], ArrLots[];

 ArrLots[6] ={0.3,0.21,0.17,0.12,0.1,0.1};

 ArrTPs[6]={19,23,32,45,75,140}; 


==================================================================

This example is not working either and the compiler says: "{" - expression is expected - and i get 4 errors.

double ArrTPs[], ArrLots[];

int ifori = 12;

     ArrTPs[12]= { 9,15,21,27,32,39,45,55,65,75,120,190 };  

     ArrLots[12] = { 0.25,0.33,0.7,0.42,0.33,0.19,0.16,0.14,0.11,0.05,0.02,0.1 };

     
// redefining the arrays based on condition
if (AccountMargin()>1500 && (AccountEquity()/AccountMargin())*100.0<160) {

   

    ifori = 6;

    ArrLots[6] = { 0.3,0.21,0.17,0.12,0.1,0.1 };

    ArrTPs[6] = { 19,23,32,45,75,140 }; 

}

Looks like the arrays enumeration of elements expects the declaration of the type in front of the variable for the element assignment... which is blocking me to redefine the array based on the if condition...


am i doing something wrong ? what ? please be specific about it is wrong and you see it - i read multiple examples and the manual.


thank You ! please advise.

 
CB:

You have to use ArrayResize() if you declare 'frank' arrays like double arr[] <= now it has the size of 0.

See the example of this function: https://www.mql5.com/en/docs/array/arrayresize

Documentation on MQL5: Array Functions / ArrayResize
Documentation on MQL5: Array Functions / ArrayResize
  • www.mql5.com
If ArrayResize() is applied to a static array, a timeseries or an indicator buffer, the array size remains the same – these arrays will not be reallocated. In this case, if The function can be applied only to dynamic arrays. It should be noted that you cannot change the size of dynamic arrays assigned as indicator buffers by the...
 
Carl Schreiber:

You have to use ArrayResize() if you declare 'frank' arrays like double arr[] <= now it has the size of 0.

See the example of this function: https://www.mql5.com/en/docs/array/arrayresize

i am doing that... here is the full code... same result: "{" - expression expected - 4 errors !


double ArrTPs[], ArrLots[];
ArrayResize (ArrLots,12);
ArrayResize (ArrTPs,12);
  
        int ifori = 12;
     ArrTPs[12]= { 9,15,21,27,32,39,45,55,65,75,120,190 };  
     ArrLots[12] = { 0.25,0.33,0.7,0.42,0.33,0.19,0.16,0.14,0.11,0.05,0.02,0.1 };
     
        if (AccountMargin()>1500 && (AccountEquity()/AccountMargin())*100.0<160) {
   
    ifori = 6;
    ArrLots[6] = { 0.3,0.21,0.17,0.12,0.1,0.1 };
    ArrTPs[6] = { 19,23,32,45,75,140 }; 
    
   Alert ("Acc Margin Level:::",(AccountEquity()/AccountMargin())*100.0,":::switched to lower lots levels:::");

   
   } 
 
double ArrTPs[12]={ 9,15,21,27,32,39,45,55,65,75,120,190 };
double ArrLots[12]={ 0.25,0.33,0.7,0.42,0.33,0.19,0.16,0.14,0.11,0.05,0.02,0.1 };

int ifori = 12; 

// redefining the arrays based on condition
if (AccountMargin()>1500 && (AccountEquity()/AccountMargin())*100.0<160) {

    ifori=6;
    ReFill(2,ArrLots,"{0.3,0.21,0.17,0.12,0.1,0.1}",",");
    ReFill(0,ArrTPs,"{19,23,32,45,75,140}",",");
}

//elsewhere in the code outside of any functions
void ReFill(int digits,double &target[],string filler,string seperator)
{
//strip { }
int rep=StringReplace(filler,"{","");
    rep=StringReplace(filler,"{","");
string result[];
ushort u_sep=StringGetCharacter(seperator,0);
int k=StringSplit(filler,u_sep,result);
if(k>0)
{
int new_size=k;
ArrayResize(target,new_size,0);
   double conv=0;
   for(int i=0;i<k;i++)
   {
   conv=StringToDouble(result[i]);
   target[i]=NormalizeDouble(conv,digits);
   }
}
ArrayFree(result);
}
 
Lorentzos Roussos:

wow... isn't there another way ? thank you !

 
CB:

wow... isn't other way ? thank you !

I dont know , i thought so too then i got bored of looking for it.

 
CB:

This example works only like this:

==================================================================

If i try it in this format - the compiler says: "{" - expression is expected - and i get 2 error:


==================================================================

This example is not working either and the compiler says: "{" - expression is expected - and i get 4 errors.

Looks like the arrays enumeration of elements expects the declaration of the type in front of the variable for the element assignment... which is blocking me to redefine the array based on the if condition...


am i doing something wrong ? what ? please be specific about it is wrong and you see it - i read multiple examples and the manual.


thank You ! please advise.

An initialization list can only be used while declaring an array (similar to @Lorentzos Roussos posted, but you don't even have to declare the number of items) :

   double ArrTPs[]= { 9,15,21,27,32,39,45,55,65,75,120,190 };    

You can't declare and then later use an initialization list, you need to proceed otherwise to set or reset an already declared array :

double ArrTPs[],ArrLots[];

const double INIT_TPS[]    = { 9,15,21,27,32,39,45,55,65,75,120,190 };
const double FURTHER_TPS[] = { 19,23,32,45,75,140 };

void OnStart()
  {
//---
   ArrayCopy(ArrTPs,INIT_TPS);

// redefining the arrays based on condition
   if(AccountMargin()>1500 && (AccountEquity()/AccountMargin())*100.0<160) 
     {
      //--- The destination array size is not decreased automatically.
      ArrayFree(ArrTPs);
      ArrayCopy(ArrTPs,FURTHER_TPS);
     }
  }
 
Alain Verleyen:

An initialization list can only be used while declaring an array (similar to @Lorentzos Roussos posted, but you don't even have to declare the number of items) :

You can't declare and then later use an initialization list, you need to proceed otherwise to set or reset an already declared array :

thank you boss !