Find first candle of any highr timeframe. - page 2

 
Marco vd Heijden:

You look at for example M15 bars.

Then you want the first one ?

That will be the one that trips over along with the first H1 bar.

So you filter for that.

Your solution is very basic.
I don't want to check for every possible case seperately.
I want a universal solution for all cases possible.

But, I guess I will need to do some checkinig for some cases.

 
Because you have to check them both to see if they both tripped over.
 
Marco vd Heijden:
Because you have to check them both to see if they both tripped over.

Good hint, I did some checking, thanks.

 
If it can help anybody:

for(i=0; i<=Bars_To_Plot*(Lead_Tf/Special_Candle_Tf); i++)
{ 
    // Get the index of the higher Tf candle:
    Lead_Tf_Candle_i = iBarShift(Symbol(),Lead_Tf,Time[i],false);
    // Get the time of the higher Tf candle:
    Lead_Tf_Candle_t = iTime(Symbol(),Lead_Tf,Lead_Tf_Candle_i);
    // Get the index of the lower Tf candle:
    Special_Candle_i = iBarShift(Symbol(),Special_Candle_Tf,Lead_Tf_Candle_t,false)-Special_Candle_Index;
    // Get the time of the lower Tf candle:
    Special_Candle_t = iTime(Symbol(),Special_Candle_Tf,Special_Candle_i);
    // Check if the time of the lower Tf candle is right with the higher Tf candle:
    if(Special_Candle_t<Lead_Tf_Candle_t)
     {
    // Adjust the lower Tf index:
    Special_Candle_i = iBarShift(Symbol(),Special_Candle_Tf,Lead_Tf_Candle_t,false)-Special_Candle_Index-1;
     }
    if(Period()==Special_Candle_Tf&&Close[Special_Candle_i]>Open[Special_Candle_i])
     {
     DoSomething();
     }
    else
    if(Period()==Special_Candle_Tf&&Close[Special_Candle_i]<=Open[Special_Candle_i])
     {
     DoSomething();
     }
}
 
Dadas:

Hello everybody!

I need help with the code snippet to find the first candle (or any next candle) of a lower timeframe for any other higher timeframe candle. Problems arrise from H4 and higher. I want to write myself an indicator to extract the OHLC data from that specific lower Tf candle and plot something according to that data.

All help appreciated.

Please use the CODE button (Alt-S) when inserting code — https://www.mql5.com/en/articles/24#insert-code or attach the file. This time I fixed it for you.

//+------------------------------------------------------------------+
//| Find the index of the first small timeframe bar inside a big bar |
//| Inputs:                                                          |
//|   symbol    = symbol                                             |
//|   t_big     = open time of the big bar                            |
//|   tf_big    = timeframe of the big bar                             |
//|   tf_small  = timeframe of the small bar                           |
//| Output:                                                          |
//|   index of the first small bar inside the big bar in history      |
//+------------------------------------------------------------------+
int FirstSmallBarIndex(string symbol, datetime t_big, ENUM_TIMEFRAMES tf_big, ENUM_TIMEFRAMES tf_small)
{
   datetime times_small[];
   CopyTime(symbol, tf_small, t_big, 3, times_small);  // copy first 3 small bars inside the big bar

   if(ArraySize(times_small) == 0) 
      return(-1);  // return -1 if no small bar found

   return iBarShift(symbol, tf_small, times_small[0], true); // exact index of the first small bar
}
 
Dadas:

Hello everybody!

I need help with the code snippet to find the first candle (or any next candle) of a lower timeframe for any other higher timeframe candle. Problems arrise from H4 and higher. I want to write myself an indicator to extract the OHLC data from that specific lower Tf candle and plot something according to that data.

All help appreciated.

Hi @Dadas

Try this article https://www.mql5.com/en/articles/599#access_bar_time

MQL5 Programming Basics: Time
MQL5 Programming Basics: Time
  • 2013.04.26
  • www.mql5.com
The article focuses on standard MQL5 functions for working with time, as well as programming techniques and practically useful functions for working with time that are required when creating Expert Advisors and indicators. Particular attention is paid to the general theory of time measurement. This article should be of interest primarily to novice MQL5 programmers.