Candle within a Superior Period

 

Hello,

I want to detect the relative minute candle number within a superior period.

For example:

Using the 1M timeframe, and the superior timeframe of 5 minutes.

The first minute candle will be labelled 0, the second 1, the third 2, the forth 3, and the final candle 4. No matter what period or iteration of the loop is being used the candles are always labelled the same. 

I've tried using the mod function such as this:

int rel=superior -(shift%superior)-1;

where superior is 5 for the 5M period, shift is the candle number within a loop, but I'm getting inconsistent results and looking for a more stable solution.

Thanks in advance.

 
imamushroom:

Hello,

I want to detect the relative minute candle number within a superior period.

For example:

Using the 1M timeframe, and the superior timeframe of 5 minutes.

The first minute candle will be labelled 0, the second 1, the third 2, the forth 3, and the final candle 4. No matter what period or iteration of the loop is being used the candles are always labelled the same. 

I've tried using the mod function such as this:

where superior is 5 for the 5M period, shift is the candle number within a loop, but I'm getting inconsistent results and looking for a more stable solution.

Thanks in advance.

sorry i am not so good at english
but accroding to the formula,seems that you try to figure out the rel to 'there rel candle on the right of the shift and this candle must within superior minute'?

if so,here may what u want

int  younameit(int curtmpriod,  int suptmpriod,  shift){
       int  sumcnt=suptmpriod/curtmpriod;
       return(sumcnt-shift%sumcnt-1);

}

exam:int rel=younameit(PERIOD_M30,PERIOD_H4,3);
 
Feng Guozheng:
sorry i am not so good at english
but accroding to the formula,seems that you try to figure out the rel to 'there rel candle on the right of the shift and this candle must within superior minute'?

if so,here may what u want

int  younameit(int curtmpriod,  int suptmpriod,  shift){
       int  sumcnt=suptmpriod/curtmpriod;
       return(sumcnt-shift%sumcnt-1);

}

exam:int rel=younameit(PERIOD_M30,PERIOD_H4,3);

Thanks for your response.

I've tried running your code and every time I run it I'm receiving different results if I DebugBreak the code at the same time. Sometimes 1, sometimes 3 etc. This is exact what was happening with my code.

It could of course be something else, elsewhere but I don't know where.

Any other ideas?

 
imamushroom:

Thanks for your response.

I've tried running your code and every time I run it I'm receiving different results if I DebugBreak the code at the same time. Sometimes 1, sometimes 3 etc. This is exact what was happening with my code.

It could of course be something else, elsewhere but I don't know where.

Any other ideas?

the code is not been tested i wrote it with notepad.exe..
maybe wait if someone give u answer,if no maybe i can guid you slove of it then.
 

Fixed.

Here's my solution code:

   datetime t = iTime(NULL,timeframe,shift);

   MqlDateTime ts;
   TimeToStruct(t,ts);
      
   int m = (ts.min % superior)+1;
   

Hope this helps someone else. Tested on 1M using 5M data.

 
imamushroom: The first minute candle will be labelled 0, the second 1, the third 2, the forth 3, and the final candle 4. No matter what period or iteration of the loop is being used the candles are always labelled the same.
int Candle_Number(ENUM_TIMEFRAMES higherTF, int iBar=0){
   datetime  beginHTF = Time[iBar] - Time[iBar] % PeriodSeconds(higherTF);
   int      iBegin    = iBarShift(_Symbol, _Period, beginHTF);
   return iBegin - iBar;
}
 
whroeder1:

Thanks very much. Much better than my code!

Any ideas how I adapt to work with other timeframe combinations aswell.

For example, 5M reference timeframe and 15M superior such the output is relative candle number 0, 1, or 2.

I've tried this but it doesn't work as I'd hope/need.

int Candle_Number(int timeframe, int superior, int shift=0)
{
   datetime  beginHTF = iTime(_Symbol,timeframe,shift) - (iTime(_Symbol,timeframe,shift) % PeriodSeconds(superior));
   int       iBegin   = iBarShift(_Symbol, timeframe, beginHTF);
   return iBegin - shift;
}  

Thanks again.

EDIT:

It seems that I was passing in a shift from a different timeframe and therefore getting unstable results. The above code does work.

Thanks again for your help.

Reason: