Find first candle of any highr timeframe.

 

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.

 
 what do you have
 
Samuel Akinbowale:
 what do you have
for(i=0; i<=Bars_To_Plot*(Lead_Tf/Special_Candle_Tf); i++)
{ 
    Lead_Tf_Candle_i = iBarShift(Symbol(),Lead_Tf,Time[i],false);
    Lead_Tf_Candle_t = iTime(Symbol(),Lead_Tf,Lead_Tf_Candle_i);
    Special_Candle_i = iBarShift(Symbol(),Special_Candle_Tf,Lead_Tf_Candle_t,false)-Special_Candle_Index;
    ObjectDelete();
    if(Period()==Special_Candle_Tf&&Close[Special_Candle_i]>Open[Special_Candle_i])
     {
     CreateObjectR();
     }
    else
    if(Period()==Special_Candle_Tf&&Close[Special_Candle_i]<=Open[Special_Candle_i])
     {
     CreateObjectR();
     }
}
 
    Lead_Tf_Candle_i = iBarShift(Symbol(),Lead_Tf,Time[i],false);
    Lead_Tf_Candle_t = iBarShift(Symbol(),Lead_Tf,Time[Lead_Tf_Candle_i],false);
//    Lead_Tf_Candle_t = iTime(Symbol(),Lead_Tf,Lead_Tf_Candle_i);
    Special_Candle_i = iBarShift(Symbol(),Special_Candle_Tf,Lead_Tf_Candle_t,false)-Special_Candle_Index;
 
Samuel Akinbowale:

Nope, I am looking for Special_Candle_i on the lower Tf.
In order to get the iBarShift(), I need the specific Candle time, not index.
For example, I want to see the first M1 candle of every M15 (this works fine from my code).
When I want to see the first H4 candle of every W1, I need to subtract 1 from the index.
When I want to see the first D1 of MN1 or W1, it stops working properly.
And, there are other issues like avoiding weekend candles, and the fact that different instruments can open at different time.

So, I am looking for an universal solution to get the index of the n candle of any lower timeframe from any other
higher timeframe candle.

 

Specific candle time is

datetime time=iTime(_Symbol,PERIOD_D1,candle);
 
Marco vd Heijden:

Specific candle time is

I am not looking for the obvious.

for(i=0; i<=Bars_To_Plot*(Lead_Tf/Special_Candle_Tf); i++)
{ 
    // This is supposed to get the higher Tf index according to the lower Tf time.
    Lead_Tf_Candle_i = iBarShift(Symbol(),Lead_Tf,Time[i],false);
    // This is supposed to get the higher Tf time according to the above index.
    Lead_Tf_Candle_t = iTime(Symbol(),Lead_Tf,Lead_Tf_Candle_i);
    // This should, then get the index of the specified lower Tf candle of a specified
    // higher Tf candle, and it does, but when applied to D1, W1, MN1 it doesn't work properly,
    // it plots some, but not all.
    Special_Candle_i = iBarShift(Symbol(),Special_Candle_Tf,Lead_Tf_Candle_t,false)-Special_Candle_Index;
    ObjectDelete();
    if(Period()==Special_Candle_Tf&&Close[Special_Candle_i]>Open[Special_Candle_i])
     {
     CreateObjectR();
     }
    else
    if(Period()==Special_Candle_Tf&&Close[Special_Candle_i]<=Open[Special_Candle_i])
     {
     CreateObjectR();
     }
}
 
You can post a job here: https://www.mql5.com/en/job
Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • www.mql5.com
so I need a simple EA, Indicators CCI,RSI,MFI,BB of course, I should be able to setup the indicators a second of my strategy entry of the trade: if CCI, RSI, MFI is over/under the level I setup and the candle close over or touch the BB the trade will be open. possibility to setup take profit now the hard part... I want to use martingale in case...
 
Marco vd Heijden:
You can post a job here: https://www.mql5.com/en/job

I don't want a job, I need a friendly programmers help.

 

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.

datetime _M15=iTime(_Symbol,PERIOD_M15,0);
datetime _H1=iTime(_Symbol,PERIOD_H1,0);
if(_M15!=iTime(_Symbol,PERIOD_M15,0))
 {
  if(_H1!=iTime(_Symbol,PERIOD_H1,0))
   {
    Print("Here is the first M15 bar");
    _H1=iTime(_Symbol,PERIOD_H1,0);
   }
  _M15=iTime(_Symbol,PERIOD_M15,0);
 }
 
When the code is looking for D1 candles of ie. MN1 candles, it sometimes finds the last D1 candle of a previous month instead of the first one of the specified month. Same with W1 candles from monthly candles.
Reason: