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
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?
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?
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.
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; }
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.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.