enum symbo string

 
could someone provide guidance on how to do enum symbol selction    am working on an multicurrency EA   that i need to  enum symbols selection     example  i want to have       enum symbol( eurusd,0)   0 is to disable 
 

Here: https://www.mql5.com/en/docs/function_indices

you have all functions listed if you search for enum you'll find EnumToString() and there is an example.

Another example you can easily find when you place the editor cursor on enum and then press F1..

Documentation on MQL5: List of MQL5 Functions
Documentation on MQL5: List of MQL5 Functions
  • www.mql5.com
List of MQL5 Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

For hardcoded symbols in your system you can do this : 

#property version   "1.00"
input string Prefix="";//Broker Prefix (before symbol)
input string Suffix="";//Broker Suffix (after symbol)
//if your system operates strictly in one of those , or indicators operate strictly in one of those then
//you hardcode them , but .. >> 
enum preset_symbols_available{
psa_off=0,//off
psa_eurusd=1,//eurusd
psa_gbpusd=2,//gbpusd
psa_usdjpy=3//usdjpy
};
input preset_symbols_available UserSelectedSymbol1=psa_eurusd;//Select symbol 1
input preset_symbols_available UserSelectedSymbol2=psa_gbpusd;//Select symbol 2
input preset_symbols_available UserSelectedSymbol3=psa_off;//Select symbol 3
//..>> but you need to transform into symbols in the market watch in case they have a prefix or a suffix 
//so create a list of active user symbols 
string ActiveUserSelectedSymbols[];
//a simple and neat add to array function 
  int add_to_string_array(string &which_array[],string add_this){
      int ns=ArraySize(which_array)+1;//get the size of your array , add 1 , but its still a number
      ArrayResize(which_array,ns,0);//so you need to expand the array by this number
      which_array[ns-1]=add_this;//and log the new element in the array
      return(ns-1);//and return the absolute position of the new element just in case
      }
//you will also need a symbol sanitizer 
  //for this we return the "proper" symbol if it exists and we also return true if it does
    bool sanitize_and_check(preset_symbols_available selection,//we send the user selection
                            string enum_prefix,//the enum prefix if any , in this example its the "psa_" part , there will be comments on this , bet 10 doge
                            string broker_prefix,//the broker's prefix
                            string broker_suffix,//the broker's suffix
                            string &sanitized_symbol//we return the "proper" symbol here
                            ){
        bool exists=false;
        //so first check is the symbol on or off ?
          if(selection!=psa_off){//if it is not off we are interested
          //secondly , we need to clean up the selection its kinda impossible for there to be 
          //a symbol called psa_eurusd 
            //first we turn the enumeration to text 
              sanitized_symbol=EnumToString(selection);
            //then we remove the enum prefix if any 
              if(StringLen(enum_prefix)>0){
              StringReplace(sanitized_symbol,enum_prefix,"");//we replace the enum prefix with air so its gone
              //now the symbol looks like "eurusd"
              }
            //we turn it to all caps 
              StringToUpper(sanitized_symbol);
            //we add the broker prefix and suffix 
              sanitized_symbol=broker_prefix+sanitized_symbol+broker_suffix;
            //and we check if it exists .
              //3 scenarios here 
              //a.it exists and is in market watch / b.it exists but not in market watch / c.it does not exist
                bool custom=false;
                exists=SymbolExist(sanitized_symbol,custom);
                //if it exists select it 
                  if(exists){
                  exists=SymbolSelect(sanitized_symbol,true);
                  }
          }  
        return(exists);
        }
int OnInit()
  {
  //Empty the list of selected symbols 
    ArrayFree(ActiveUserSelectedSymbols);
  //we go through the symbol inputs and add to the active symbols as needed and as possible
    string actual_symbol="";
    string enum_prefix="psa_";
    bool exists=sanitize_and_check(UserSelectedSymbol1,enum_prefix,Prefix,Suffix,actual_symbol);
    //if its on and it exists (both checked in the function) we get a clean symbol in actual symbol
    //so we add it to the array 
      if(exists)
        {
        add_to_string_array(ActiveUserSelectedSymbols,actual_symbol);
        }
    //we continue this way for the rest of the inputs 
      exists=sanitize_and_check(UserSelectedSymbol2,enum_prefix,Prefix,Suffix,actual_symbol);
      if(exists){add_to_string_array(ActiveUserSelectedSymbols,actual_symbol);}

      exists=sanitize_and_check(UserSelectedSymbol3,enum_prefix,Prefix,Suffix,actual_symbol);
      if(exists){add_to_string_array(ActiveUserSelectedSymbols,actual_symbol);}          
  
  //Print users symbols 
    string message="You have selected nothing sir";
    if(ArraySize(ActiveUserSelectedSymbols)>0){
    message="You have selected :\n";
    for(int i=0;i<ArraySize(ActiveUserSelectedSymbols);i++){
    message+=ActiveUserSelectedSymbols[i]+"\n";
    }
    }    
    Alert(message);
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   
  }

void OnTick()
  {
//---
   
  }

 
Lorentzos Roussos #:

For hardcoded symbols in your system you can do this : 

thank you guys    how can we do   enum   for each symbol   ie  

enum preset_symbols_available{
psa_off=0,//off
psa_eurusd=1,//eurusd}

and 

enum preset_symbols_available{
psa_off=0,//off
psa_gbpusd=1,//gbpusd}

so i caan disable or  enable specific pair without loading  tall symbols in the enum

 
Hannah Wanjiku Kimari - #:

thank you guys    how can we do   enum   for each symbol   ie  

and 

so i caan disable or  enable specific pair without loading  tall symbols in the enum

So that you don't type all the symbols in the enum ? 

You can create a one time string export which reads your market watch and grabs assets , and then exports mq5 code for you to utilize and have a long enum list .

You'll have to be very careful doing that too , not to let in any prefix - suffix in the enum . 

If you mean the user being able to add any # of symbols (without limitation , i.e. not just 10 inputs) , and without annoying the hair out of their head , you'd have to create  a custom 

symbol selection screen .  

 
Lorentzos Roussos #:

So that you don't type all the symbols in the enum ? 

You can create a one time string export which reads your market watch and grabs assets , and then exports mq5 code for you to utilize and have a long enum list .

You'll have to be very careful doing that too , not to let in any prefix - suffix in the enum . 

If you mean the user being able to add any # of symbols (without limitation , i.e. not just 10 inputs) , and without annoying the hair out of their head , you'd have to create  a custom 

symbol selection screen .  

okay thanks bro and what about the order comment   like if i want order comment for every pair 
example 
EURUSD;  COMMENT1
GBPUSD;  COMMENT2

USDJPY ;  COMMENT3  ETC    such that every pair that opens has its own comment

 
Hannah Wanjiku Kimari - #:

okay thanks bro and what about the order comment   like if i want order comment for every pair 
example 
EURUSD;  COMMENT1
GBPUSD;  COMMENT2

USDJPY ;  COMMENT3  ETC    such that every pair that opens has its own comment

That is easy if we assume you have this (as a selection for the user) :

enum preset_symbols_available{
psa_off=0,//off
psa_eurusd=1,//eurusd
psa_gbpusd=2,//gbpusd
psa_usdjpy=3//usdjpy
};

then you can create a counterpart list in the same order with your comment and read it based on the values the user selected 

lets assume a one input ea 

input preset_symbols_available symbolo=psa_eurusd;//symbol

your user has selected psa_eurusd which is also the value of 1 in the enumeration

so if there were a list like so : 

string comment_list[]={"Off","Eurusd comment","Gbpusd comment","Usdjpy comment"};

you could fetch the comment directly from index 1 which is the eurusd comment like so :

string comment_to_use=comment_list[(int)symbolo];
 
Lorentzos Roussos #:

That is easy if we assume you have this (as a selection for the user) :

then you can create a counterpart list in the same order with your comment and read it based on the values the user selected 

lets assume a one input ea 

your user has selected psa_eurusd which is also the value of 1 in the enumeration

so if there were a list like so : 

you could fetch the comment directly from index 1 which is the eurusd comment like so :

thank you

 
Lorentzos Roussos #:

That is easy if we assume you have this (as a selection for the user) :

then you can create a counterpart list in the same order with your comment and read it based on the values the user selected 

lets assume a one input ea 

your user has selected psa_eurusd which is also the value of 1 in the enumeration

so if there were a list like so : 

you could fetch the comment directly from index 1 which is the eurusd comment like so :


hey bro    i tried your suggestion  from the code you sent  and am not very sure  am doing the correct thing 
check this code below you could just add one or 2 enum symbols and their comment  it will be a good point of reference to me thank you very much 
 
Hannah Wanjiku Kimari - #:


hey bro    i tried your suggestion  from the code you sent  and am not very sure  am doing the correct thing 
check this code below you could just add one or 2 enum symbols and their comment  it will be a good point of reference to me thank you very much 

Well the way this is coded it can't be applied . 

try this with strings 

 
Lorentzos Roussos #:

Well the way this is coded it can't be applied . 

try this with strings 

thanks bro you are the best

Reason: