The "if" tier is probably the fastest. I've done "if" tiers (albeit not as densly coded as your snippet) as well as implemented it in switch/case. I'm sure the string text version you implemented is equally valid. Why do you not want to use the original code? If you want postive feedback, as well as negative feedback, then turn the call function TimeframeCheck into a bool instead of a void like such:
bool TimeframeCheck(string Name, int Timeframe) // returns false if Timeframe is invalid, otherwise returns true { //----+ //---- Checking the correctness of Timeframe variable value if (Timeframe != 1) if (Timeframe != 5) if (Timeframe != 15) if (Timeframe != 30) if (Timeframe != 60) if (Timeframe != 240) if (Timeframe != 1440) { Print(StringConcatenate("Parameter ",Name, " cannot ", "be equal to ", Timeframe, "!!!")); return(false); // timeframe is invalid, return false } return(true); // made it out of the if checks and timeframe is valid //----+ }
The "if" tier is probably the fastest. I've done "if" tiers (albeit not as densly coded as your snippet) as well as implemented it in switch/case. I'm sure the string text version you implemented is equally valid. Why do you not want to use the original code? If you want postive feedback, as well as negative feedback, then turn the call function TimeframeCheck into a bool instead of a void like such:
Thank you, Phillip
I don't mind using the original code and appreciate your suggestion to make the function "bool" instead of "void".
I just wondered how many other ways are there to achieve the same objective?
Of course, when you are on a good thing, stick to it, Helmut
I just wondered how many other ways are there to achieve the same objective?
There are lots of other ways to achieve this particular objective. They're not necessarily better in any respect than the ones you have already discussed, but two alternative examples would be:
bool IsTimeframePermittedInMT4(int tf) { int PermittedTimeframes[] = {PERIOD_M1, PERIOD_M5, PERIOD_M15, PERIOD_M30, PERIOD_H1, PERIOD_H4, PERIOD_D1, PERIOD_W1, PERIOD_MN1}; return (PermittedTimeframes[ArrayBsearch(PermittedTimeframes, tf)] == tf); }
and:
bool IsTimeframePermittedInMT4(int tf) { return (iClose(Symbol(), tf, 0) != 0); }
There are lots of other ways to achieve this particular objective. They're not necessarily better in any respect than the ones you have already discussed, but two alternative examples would be:
and:
bool IsTimeframePermittedInMT4(int tf) { return (iClose(Symbol(), tf, 0) != 0); }
Wow, I like the last one. How does it work?
It relies on the fact that the iClose() function will return zero if the timeframe parameter is not valid. Otherwise, the return value should be a non-zero price. (However, it's just possible that iClose() could return zero when you first call it for a timeframe which is different to the chart you're using, and will then download the data and return non-zero on subsequent calls. I haven't tested this extensively.)
Thank you, jjc
init() { ... /*++++ Adjust for the current chart timeframe */{ static int tf[] = { PERIOD_M1, PERIOD_M5, PERIOD_M15, PERIOD_M30, PERIOD_H1, PERIOD_H4, PERIOD_D1, PERIOD_W1, PERIOD_MN1 }; static string TFtext[] = { "M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN1" }; // OpenNew only. for(Period.index=0; tf[Period.index] < Period(); Period.index++) {} Period.text = TFtext[Period.index]; MagicNumberMin = Magic.Number.Base + TP_ZONES_FORW_MAX * Period.index;// 4 MagicNumberMax = MagicNumberMin + TP_ZONES_FORW_MAX - 1; /*---- Adjust for the current chart timeframe */}Period.text used in the orderComment and chart Comment. MagicNumberMin/Max allows the EA to operate on all charts and all timeframes with one base number.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
In one of the articles (https://www.mql5.com/en/articles/1523, I came across this snippet of code...
It took me a while to get my mind around it. If one "if" is false (Timeframe acceptable) the function is exited without any action taken. I thought I should share it with you.
How many other ways are there to check the Timeframe? For example...
Would this work? Which would be less overhead? Thank you, Helmut