Help: StringToEnum or StringToTimeframe

 

Good day, I have used the function EnumToString eons of times, however is there such an opposite function. 

yes this brings about the question into what Enum is it being changed to, this takes us to the next part 


StringToX where X is a specified Enum, I tried to write a StringToTimeframe function which I totally messed up. 


here is the script I wrote to see if it will work of not 

#property script_show_inputs
//--- input parameters
input string      Input1 = "m1";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
ChartOpen(Symbol(),StringToTimeframe(Input1));
  }
//+------------------------------------------------------------------+
ENUM_TIMEFRAMES StringToTimeframe(const string m_word)
{
 string m_string_Period = m_word;
 //Change word to uppercase
 StringToUpper(m_string_Period);
 //add PERIOD_ to word
 m_string_Period = StringFormat("PERIOD_%s",m_string_Period);
 printf("initial word: %s new Word: %s",m_word,m_string_Period);
 return (ENUM_TIMEFRAMES)m_string_Period;
}


I  know my return value is kinda off and not well represented 


How best can I make this work 

without using the following code 

if(m_word == "M1" return PERIOD_M1 ;

//Nor 
Switch(m_word)
case ("M1): return PERIOD_M1;break;

thank you. 

 
I do not see why you would need such a function. Internally always use the plain enum and only to communicate to a human by means of interface or logging use enumtostring.
 
Enrique Dangeroux #:
I do not see why you would need such a function. Internally always use the plain enum and only to communicate to a human by means of interface or logging use enumtostring.

I have a bot that get data from an external source and Period conmes in as either M1 M2, H1 H2 MN1 etc, hence the main Idea Is that, 
once data arrives to the Bot via API services, is we want to view that particular chart take a picture then send it to telegram, 

now the problem I am having is I want to have a StringToTimeframe function that doesnt utilise the series if statements nor the switch statement

 
Jefferson Metha:

Good day, I have used the function EnumToString eons of times, however is there such an opposite function. 

yes this brings about the question into what Enum is it being changed to, this takes us to the next part 


StringToX where X is a specified Enum, I tried to write a StringToTimeframe function which I totally messed up. 


here is the script I wrote to see if it will work of not 


I  know my return value is kinda off and not well represented 


How best can I make this work 

without using the following code 

thank you. 

The idea looks like it would work, but ENUM_TIMEFRAME is having int value and not string (also note mt5 TF values are not sequential like mt4, making difficult to write something that would calculate the value base on the string input), Meaning it can't convert unless you can create 2 buffers, 1 with the string the other with the int values, and returning that for the input string.

 
Jefferson Metha: StringToX where X is a specified Enum,
input string      Input1 = "m1";

There is no need for this. Let the user select the enumeration directly.

input ENUM_TIMEFRAMES Input1 = PERIOD_M1; // Timeframe
 
William Roeder #:

There is no need for this. Let the user select the enumeration directly.

Thank you for the reply, however the reason I had made it to be string input is that the M1, m2 m3, etc is received via api. . 

I tried to change m1 to PERIOD_M1 thinking there was a function that reverses the output of EnumToString. 

From what I have been going through looks like I must have to make the functions. 
 
Jefferson Metha #:
Thank you for the reply, however the reason I had made it to be string input is that the M1, m2 m3, etc is received via api. . 

I tried to change m1 to PERIOD_M1 thinking there was a function that reverses the output of EnumToString. 

From what I have been going through looks like I must have to make the functions. 

For what it's worth:

const ENUM_TIMEFRAMES StringToTimeframe(const string timeframe)
  {
   if(timeframe == "PERIOD_CURRENT")
      return PERIOD_CURRENT;

   if(timeframe == "PERIOD_M1")
      return PERIOD_M1     ;

   if(timeframe == "PERIOD_M2")
      return PERIOD_M2     ;

   if(timeframe == "PERIOD_M3")
      return PERIOD_M3     ;

   if(timeframe == "PERIOD_M4")
      return PERIOD_M4     ;

   if(timeframe == "PERIOD_M5")
      return PERIOD_M5     ;

   if(timeframe == "PERIOD_M6")
      return PERIOD_M6     ;

   if(timeframe == "PERIOD_M10")
      return PERIOD_M10    ;

   if(timeframe == "PERIOD_M12")
      return PERIOD_M12    ;

   if(timeframe == "PERIOD_M15")
      return PERIOD_M15    ;

   if(timeframe == "PERIOD_M20")
      return PERIOD_M20    ;

   if(timeframe == "PERIOD_M30")
      return PERIOD_M30    ;

   if(timeframe == "PERIOD_H1")
      return PERIOD_H1     ;

   if(timeframe == "PERIOD_H2")
      return PERIOD_H2     ;

   if(timeframe == "PERIOD_H3")
      return PERIOD_H3     ;

   if(timeframe == "PERIOD_H4")
      return PERIOD_H4     ;

   if(timeframe == "PERIOD_H6")
      return PERIOD_H6     ;

   if(timeframe == "PERIOD_H8")
      return PERIOD_H8     ;

   if(timeframe == "PERIOD_H12")
      return PERIOD_H12    ;

   if(timeframe == "PERIOD_D1")
      return PERIOD_D1     ;

   if(timeframe == "PERIOD_W1")
      return PERIOD_W1     ;

   if(timeframe == "PERIOD_MN1")
      return PERIOD_MN1    ;

   return PERIOD_CURRENT;
  }

*EDIT: ELSE is not needed because all ifs return and therefor inherently breaks the codeflow.

 
Dave Bieleveld #:

For what it's worth (excuse me the terrible layout, blame the default mql5 code styler):

Remove the else statement and you will have better format.

Not even going into what's wrong with the approach in the first place.

But you do not need the "else". Think about it .
 
Dominik Christian Egert #:
Remove the else statement and you will have better format.

Not even going into what's wrong with the approach in the first place.

But you do not need the "else". Think about it .

Yes, thanks for pointing it out. I do not know what you're hinting at what's "wrong" though. If you have a better solution, then please provide, am always willing to learn!

 
Dave Bieleveld #:

Yes, thanks for pointing it out. I do not know what you're hinting at what's "wrong" though. If you have a better solution, then please provide, am always willing to learn!

I would change the input

input string      Input1 = "m1";


To:
input ENUM_TIMEFRAMES      Input1 = PERIOD_M1;

Let's assume you get a string from an API, and let's assume, as shown above, it is max 8 chars.

I would transform the string into an ulong and use a switch() statement to do the conversion.

Might seem odd, but it's more flexible and better to read as a series of if() expressions.


 
Dominik Christian Egert #:
I would change the input



To:

Let's assume you get a string from an API, and let's assume, as shown above, it is max 8 chars.

I would transform the string into an ulong and use a switch() statement to do the conversion.

Might seem odd, but it's more flexible and better to read as a series of if() expressions.


That’s not my code, I don’t use input = ‘m1’

but if you’re referring to OP, your proposal does make the code arbitrarily/arguably more readable, in return for using switch and limiting the amount of chars. Either way, you have to validate incoming data from the api anyway, so I’m not inclined to make unnecessary castings and conversions. And I guess we’d have to cast the timeframe constants in the “case” also to long, possibly making it even more complex.

unless there is a compelling performance or integrity benefit, the added complexity makes the readability less intuitive, and verbose imho. But it’s matter of style i guess.

For instance, I agree i personally prefer the switch too for translations, however it’s a pity mql does not support strings with it.