Basic code. Help can you make this string array code work.

MQL5.royc
89
MQL5.royc  
string look1, look2, symarray[5];

symarray[5] = {"EURUSD","EURJPY","GBPUSD","USDCHF","JPYGBP"};

ArraySetAsSeries(symarray,false); // index starting 0 at left
look1=symarray[0];
look2=symarray[1];
Print("symarray 2 elements 0 = "+look1+" and 1= "+look2);

Simple eh! except that both elements print null!!!! should be EURUSD and EURJPY

Simon Gniadkowski
17359
Simon Gniadkowski  
royclark:

<CODE REMOVED>

Simple eh! except that both elements print null!!!! should be EURUSD and EURJPY

Please read some other posts before posting . . .

Please edit your post . . . please use the SRC button to post code: How to use the SRC button.

Simon Gniadkowski
17359
Simon Gniadkowski  
royclark:


Simple eh! except that both elements print null!!!! should be EURUSD and EURJPY

Do this . . .

string look1, look2, symarray[] = {"EURUSD","EURJPY","GBPUSD","USDCHF","JPYGBP"};
William Roeder
24607
William Roeder  
royclark:
string look1, look2, symarray[5];
symarray[5] = {"EURUSD","EURJPY","GBPUSD","USDCHF","JPYGBP"}; // Does this compile?
ArraySetAsSeries(symarray,false); // index starting 0 at left // What type does ASAS support?

ArraySetAsSeries - MQL4 Documentation

array[] - The numeric array to set.
Keith Watford
Moderator
28953
Keith Watford  
royclark:

string look1, look2, symarray[5];

symarray[5] = {"EURUSD","EURJPY","GBPUSD","USDCHF","JPYGBP"};

ArraySetAsSeries(symarray,false); // index starting 0 at left
look1=symarray[0];
look2=symarray[1];

Print("symarray 2 elements 0 = "+look1+" and 1= "+look2);

Simple eh! except that both elements print null!!!! should be EURUSD and EURJPY


I am so glad that I am not the only one that has problems with this.

From what I have read in the 'Book" your code should work but in similar situations as yours, it doesn't and you have to enter seperate lines of code for each part of the array

Change the code to

string look1, look2, symarray[5];

symarray[0] = "EURUSD";  //"EURJPY","GBPUSD","USDCHF","JPYGBP"};
symarray[1] = "EURJPY";
//symarray[2]=
//etc
//etc
//etc
//etc
//etc
//etc
//etc

ArraySetAsSeries(symarray,false); // index starting 0 at left
look1=symarray[0];
look2=symarray[1];

Print("symarray 2 elements 0 = "+look1+" and 1= "+look2);

and it works, but this is a long-winded way of getting there.

Hopefully somebody knows a better way

GreenMoney
369
GreenMoney  
royclark:
string look1, look2, symarray[5];

symarray[5] = {"EURUSD","EURJPY","GBPUSD","USDCHF","JPYGBP"};

ArraySetAsSeries(symarray,false); // index starting 0 at left
look1=symarray[0];
look2=symarray[1];
Print("symarray 2 elements 0 = "+look1+" and 1= "+look2);

Simple eh! except that both elements print null!!!! should be EURUSD and EURJPY

The problem is that you defined symarray as a string array with five elements but tried to access the an undefined 6th element of the array. Remember: arrays are zero-based, which means the first element's index is zero. So, in your code the element indexes for symarray are 0, 1, 2, 3, and 4. Here is a slight modification to your code which works:

string look1, look2, symarray[5] = {"EURUSD","EURJPY","GBPUSD","USDCHF","JPYGBP"};

ArraySetAsSeries(symarray,false); // index starting 0 at left
look1=symarray[0];
look2=symarray[1];
Print("symarray 2 elements 0 = "+look1+" and 1= "+look2);

Array Test #1


WHRoeder:

ArraySetAsSeries - MQL4 Documentation

array[] - The numeric array to set.

It is true that the Documentation says "numeric" but it appears ArraySetAsSeries() works in this situation. For example, compare the above code and log to:

string look1, look2, symarray[5] = {"EURUSD","EURJPY","GBPUSD","USDCHF","JPYGBP"};

ArraySetAsSeries(symarray,true); // index starting 0 at left  (Changed second arg from "false" to "true")
look1=symarray[0];
look2=symarray[1];
Print("symarray 2 elements 0 = "+look1+" and 1= "+look2);

Array Test #2

Simon Gniadkowski
17359
Simon Gniadkowski  
GumRai:


Hopefully somebody knows a better way

Look up to my earlier post . . .
Keith Watford
Moderator
28953
Keith Watford  
RaptorUK:
Look up to my earlier post . . .


Thanks Raptor.

I had the page open a while before I replied, so missed yours.

Just to be clear.

If I want to assign values to a string array by using {"?","?","?","?"}

I have to do it at the same time as declaring the array?

Simon Gniadkowski
17359
Simon Gniadkowski  
GumRai:


Thanks Raptor.

I had the page open a while before I replied, so missed yours.

Just to be clear.

If I want to assign values to a string array by using {"?","?","?","?"}

I have to do it at the same time as declaring the array?

It certainly looks that way and it's what the bit of testing I did earlier suggests.
Keith Watford
Moderator
28953
Keith Watford  

On a related note, I will be grateful if you can tell me what I am missing here.

This will not compile and I get the error '}' - comma or semicolon expected


//--- input parameters
extern double    LotSizePT1=0.1;
extern double    LotSizePT2=0.2;
extern double    LotSizePT3=0.3;
extern double    LotSizePT4=0.4;
extern double    LotSizePT5=0.5;

//
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

 double LotSizearray[6]={0,LotSizePT1,LotSizePT2,LotSizePT3,LotSizePT4,LotSizePT5};
 
/*
 double LotSizearray[6];
 LotSizearray[1]= LotSizePT1; 
 LotSizearray[2]= LotSizePT2; 
 LotSizearray[3]= LotSizePT3;
 LotSizearray[4]= LotSizePT4;
 LotSizearray[5]= LotSizePT5;
 */
//----
   return(0);
  }
//+------------------------------------------------------------------+

This works fine though

//--- input parameters
extern double    LotSizePT1=0.1;
extern double    LotSizePT2=0.2;
extern double    LotSizePT3=0.3;
extern double    LotSizePT4=0.4;
extern double    LotSizePT5=0.5;

//
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

 //double LotSizearray[6]={0,LotSizePT1,LotSizePT2,LotSizePT3,LotSizePT4,LotSizePT5};
 

 double LotSizearray[6];
 LotSizearray[1]= LotSizePT1; 
 LotSizearray[2]= LotSizePT2; 
 LotSizearray[3]= LotSizePT3;
 LotSizearray[4]= LotSizePT4;
 LotSizearray[5]= LotSizePT5;
 
//----
   return(0);
  }
//+------------------------------------------------------------------+

I'm sure that I will be kicking myself when it is explained :(

Thanks.

William Roeder
24607
William Roeder  
GumRai: I'm sure that I will be kicking myself when it is explained :(

type name[] = { CONSTANT1, CONSTANT2 ...};

you're not using constants but variables.