Call a function for calculating next higher timeframe ** SOLVED **
-
Hi folks, looks like today I'm a little stupid. One of my indicators requires the input of a timeframe. In order to make it flexible, I'd like to calculate the next higher timerframe. For instance if chart timeframe is H1 it should return H4, . for M15 -> M30 and so on.
I create that project Next Timeframe Project and hope that someone can help me.In details:
I copied a sample code from mql5.com://+---------------------------------------------------------------------+
//| subGetNextTimeFrame function - returns the next timeframe |
//+---------------------------------------------------------------------+
int subGetNextTimeFrame()
{
int TimeFrames[9] = { 1,5,15,30,60,240,1440,10080,43200 };
int CurrentTimeIndex = ArrayBsearch(TimeFrames,_Period,8,0,MODE_ASCEND);
return((ENUM_TIMEFRAMES) TimeFrames[CurrentTimeIndex+1]);
}and put it on "on Init" tab in a custom code block. However, I fail to call that function somehow and receive the error "'subGetNextTimeFrame' - function can be declared only in the global scope".
What am I doing wrong?
-
I found the solution. I do the following in the on Init tab:

The in variables vi_current_timeframe and vi_next_timeframe I defined in variables as integer.
Here the code if someone wants to do the same:
//
int TimeFrames_cust[9] = { 1,5,15,30,60,240,1440,10080,43200 };
int CurrentTimeIndex = ArrayBsearch(TimeFrames_cust,_Period,8,0,MODE_ASCEND);vi_current_timeframe= (ENUM_TIMEFRAMES) TimeFrames_cust[CurrentTimeIndex];
vi_next_timeframe= (ENUM_TIMEFRAMES) TimeFrames_cust[CurrentTimeIndex+1];
// -
@trader-philipps Wow thanks, this is very helpful.
-
Impressive!
This is why coders will always beat all of us simple fxDreema users 
-
@trader-philipps hi Philipps , do you think this would work:

i am using the code you provided, the idea is to save a price level in variable , let say the ea is attached to m1 and once a price level is saved the ea should look on to the next higher timeframe and see if the candle closes above that level or not ,
-
@zackry Should work. I use it the same way in one of my current projects in order to use an indicator on next higher timeframes.
-
will this work in reverse? like I need the next lower timeframe?
-
@bhadzlagayan said in Call a function for calculating next higher timeframe ** SOLVED **:
will this work in reverse? like I need the next lower timeframe?
The code would need to be changed, but yes, it could.
-
Since the initialization of this is done, in the OnInit() function, add a condition to prevent "out of range" that is if am in the lowest TF in my array, they is no need adding 1 (for the next HTF) or subtracting 1 (for the next LTF), it should just output a message letting you know.
-
Thank you for the reply. Turns out you can directly code.
You can call the function Period(). This will give you the current timeframe then use if else or swtich to lower it down.
If ( Period() == PERIOD_H4 ) TF = PERIOD_H1;
Not elegant, i know, but works. Just make sure that the TF is a variable with a ENUM_TIMEFRAMES as the type.