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
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.
input string Input1 = "m1";
There is no need for this. Let the user select the enumeration directly.
input ENUM_TIMEFRAMES Input1 = PERIOD_M1; // Timeframe
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. .
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.
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!
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!
input string Input1 = "m1";
input ENUM_TIMEFRAMES Input1 = PERIOD_M1;
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.