Check the name of the base currency

 

Good evening everyone,

I would to code something corresponding to this sentence


"if the currency pair on the current chart is one of those : USDJPY or EURJPY or CADJPY".


Is someone can help?

Luciole

 
Luciole:

Good evening everyone,

I would to code something corresponding to this sentence


"if the currency pair on the current chart is one of those : USDJPY or EURJPY or CADJPY".


Is someone can help?

Luciole

//check the extensions like .m or m or .e or whatever....
if( (Symbol() == "USDJPY") || (Symbol() == "USDJPYm") ) 
{ //do something specific for this currency } 
else if( (Symbol() == "EURJPY") || (Symbol() == "EURJPYm") ) 
{ //do something different for this currency } 
else if( (Symbol() == "CADJPY") || (Symbol() == "CADJPYm") ) 
{ //do something else for this currency } 
else { //set some default variables }
 
Lars Rompe:

Thanks a lot !


I do not find any information on "USDJPYm" what is that?


Could I write something like that?

if( (Symbol() == "USDJPY")  || (Symbol() == "EURJPY") || (Symbol() == "CADJPY") 
 
Luciole:
if( (Symbol() == "USDJPY")  || (Symbol() == "EURJPY") || (Symbol() == "CADJPY") )

Sure you can, but count the brackets...


The suffix "m" or "e" applies to several brokers with different account models to differentiate ECN from standard accounts or even cent accounts.

You may also convert the symbol using string operations. Define a string variable called zsymbol, where zsymbol = StringSubstr(Symbol(),0,5)

 
Lars Rompe:

Sorry for the brackets!


How do I know if I have to concert the symbol using operation?

Will it depends on the brocker ? or on the operation I will do? 

 
Luciole:

Sorry for the brackets!


How do I know if I have to concert the symbol using operation?

Will it depends on the brocker ? or on the operation I will do? 

Hi, no worries.

As long as you just see the "regular" symbols without suffix, it will work without the string operations - just try, compile etc.

For usage with other brokers, you may need to adjust the code.

 
I'd like to propose a different solution. This is a common pattern in programming, and typically you wouldn't create unique expressions each time you'd want to do something like this. In python for example you simply write your expression as 
if _Symbol in ["USDJPY", "EURJPY", "CADJPY"]:
   ...

In mql it's slightly more verbose but the concept is all the same. Additionally you don't want hard-coded comparisons because as soon as you switch brokers with different symbol suffixes, everything breaks. Instead you should use StringFind to eliminate the need to reprogram suffixes. 

Here is an example of how you'd do it in MQL.... 

void OnStart()
{
   const string allowed_symbols[] = {"USDJPY", "EURJPY", "CADJPY"};
   bool allowed = is_allowed_symbol(_Symbol, allowed_symbols);
   printf("%s %s an allowed symbol.", _Symbol, allowed ? "is" : "is NOT");
}
bool is_allowed_symbol(const string symbol, const string &symbol_array[])
{
   for(int i=ArraySize(symbol_array)-1; i>=0; --i)
      if(StringFind(symbol, symbol_array[i]) >= 0)
         return true;
   return false;
}
 

65
Luciole 2018.10.03 20:12    FR

So this code is a bit diffcult for me. What i have in mind is to have different if conditions like that. I really cant figure out how to use your code, which seem very neat, but difficult a complexe one. The thing is am setting thoses conditions in order to define the number of lots that the program should take according to quote currency.

if( (Symbol() == "USDJPY") || (Symbol() == "EURJPY") || (Symbol() == "CADJPY") ) ... if( (Symbol() == "AUDNZD") || (Symbol() == "EURNZD") || (Symbol() == "GBPNZD") ) ... if( (Symbol() == "AUDUSD") || (Symbol() == "EURUSD") || (Symbol() == "GBPUSD") )

...

 ... Does it makes sense?

 
Luciole:

Good evening everyone,

I would to code something corresponding to this sentence


"if the currency pair on the current chart is one of those : USDJPY or EURJPY or CADJPY".


Is someone can help?

Luciole

if(StringSubstr(Symbol(),0,6)=="USDJPY")   
 
Luciole:

65
Luciole 2018.10.03 20:12    FR

So this code is a bit diffcult for me. What i have in mind is to have different if conditions like that. I really cant figure out how to use your code, which seem very neat, but difficult a complexe one. The thing is am setting thoses conditions in order to define the number of lots that the program should take according to quote currency.

 ... Does it makes sense?

The logic makes sense but the code doesn't. You need to create a function for membership testing. See my example in this post.  

 
nicholi shen:

The logic makes sense but the code doesn't. You need to create a function for membership testing. See my example in this post.  

Ok, so I am going to dive into it, but it raises some questions.

1/ you write Void OnStart while on my MetaEditor  I only have Void OnTick. Should I wrote Voide OnStart or is it ok to keep Void OnStart?


2/ this section is a function inside the code that do what?

bool is_allowed_symbol(const string symbol, const string &symbol_array[])
{
   for(int i=ArraySize(symbol_array)-1; i>=0; --i)
      if(StringFind(symbol, symbol_array[i]) >= 0)
         return true;
   return false;
}


3/ Do you reckon I should write something like that ?

   if const string allowed_symbols[] = {"USDJPY", "EURJPY", "CADJPY"};
   {
        ...
   }

   if const string allowed_symbols[] = {"AUDNZD", "EURNZD", "GBPNZD"};
   {
        ...
   }

   if const string allowed_symbols[] = {"AUDUSD", "EURUSD", "GBPUSD"};
   {
        ...
   }
Reason: