Download MetaTrader 5

Convert a string to a variable

To add comments, please log in or register
Michalis Phylactou
12673
Michalis Phylactou  

Hi guys

I am writing an indicator where the user defines which currencies to be used

extern string               Currency1="EURUSD";
extern string               Currency2="GBPUSD";
extern string               Currency3="USDCHF";

.

.

.

extern string               Currency13="EURNZD";
extern string               Currency14="GBPCAD";

then these currency names are stored into an array     

      currencyString[0]=Currency1;

.

.

.

      currencyString[11]=Currency12;

      currencyString[12]=Currency13;

my question is rather than assigning values to the array one by one is there a way to apply a loop to do that, given the variable names are common

for example

for (int i=0; i<13; i++){

currencyString[i]=Currency +(i+1);

}

the above of course will return an error as Currency is not defined.

If there is a way to concatenate text ("Currency" +i)  and then look if there is a variable that has that name problem is solved.

Thank you

Marco vd Heijden
Moderator
5679
Marco vd Heijden  
Michalis Phylactou:

Hi guys

I am writing an indicator where the user defines which currencies to be used

extern string               Currency1="EURUSD";
extern string               Currency2="GBPUSD";
extern string               Currency3="USDCHF";

.

.

.

extern string               Currency13="EURNZD";
extern string               Currency14="GBPCAD";

then these currency names are stored into an array     

      currencyString[0]=Currency1;

.

.

.

      currencyString[11]=Currency12;

      currencyString[12]=Currency13;

my question is rather than assigning values to the array one by one is there a way to apply a loop to do that, given the variable names are common

for example

for (int i=0; i<13; i++){

currencyString[i]=Currency +(i+1);

}

the above of course will return an error as Currency is not defined.

If there is a way to concatenate text ("Currency" +i)  and then look if there is a variable that has that name problem is solved.

Thank you

for (int i=0; i<SymbolsTotal(1); i++){

currencyString[i]=SymbolName(i,1);

}
for (int i=0; i<SymbolsTotal(1); i++){

currencyString[i]=SymbolName(i,1)+IntegerToString(i+1));

}
Michalis Phylactou
12673
Michalis Phylactou  
Marco vd Heijden:

Thank you

It may be able to work, but only because there is a function in MQL5 that looks the order of symbols in the market watch.

However I wish to know if there is a way to write a string and see if there is a variable with that string and return its value.

Marco vd Heijden
Moderator
5679
Marco vd Heijden  
Michalis Phylactou:

Thank you

It may be able to work, but only because there is a function in MQL5 that looks the order of symbols in the market watch.

However I wish to know if there is a way to write a string and see if there is a variable with that string and return its value.

StringFind()
Michalis Phylactou
12673
Michalis Phylactou  
Marco vd Heijden:

StringFind() searches to see if a substring exists within a string.

What I want is to specify a string, see if a variable exists with that string and return its value

Marco vd Heijden
Moderator
5679
Marco vd Heijden  

I dont get it.

The string is the variable ? what more can it return but it's name/position ?

Then you simply loop over

WHOLE_ARRAY

And if it's a match you know it's location.

Benjamin Dixon
24175
Benjamin Dixon  

Answer to OP question is NO but here is one other idea:

extern string               DCDCurrencies="EURUSD,,USDJPY,,GBPUSD"; //then you can loop the string segments in to an array
Personally I'd just go with your original code though.
Benjamin Dixon
24175
Benjamin Dixon  
Marco vd Heijden:

I dont get it.

The string is the variable ? what more can it return but it's name/position ?

Then you simply loop over

And if it's a match you know it's location.

I'm sure he means "...string NAME and return it's value." Like what he's asked in his original post.
Michalis Phylactou
12673
Michalis Phylactou  
Benjamin Dixon:

Answer to OP question is NO but here is one other idea:

Personally I'd just go with your original code though.

Thank you Benjamin

Is not very user friendly however.

I just wanted to know if it can be done , for educational purposes and cleaning up the code, as I get faced with this quite often.

To add comments, please log in or register