Counting number of bars between times

Created at 01 Feb 2023, 11:42
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
DY

dykesn92

Joined 31.03.2020

Counting number of bars between times
01 Feb 2023, 11:42


Hello I need to count bars between times for a bot I am building.

 

For example I want the 9pm candle on 5 minutes and I want to know how many bars away this is from the 3am candle.

 

I have seen some posts using the index and some forum work and none of it seemed to actually work.

 

Currently I am using timea - time b then dividing it by 5.

 

The issue with this is that over the weekend the last candle is 855 and the next one is 1105.  On top of this every day theres a gap between 10 and 11.

Are there any solutions to this?


@dykesn92
Replies

PanagiotisChar
01 Feb 2023, 12:43

Hi there,

I would just check the current time against the two time boundaries and set a counter to count each bar inside the OnBar() event. I would increase the counter for every bar opened within the set time boundaries.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

dykesn92
01 Feb 2023, 14:16

Thank you I also had a similar thought but tehn my concern will come about my needing to turn the bot on and off every day at the exact time?


@dykesn92

PanagiotisChar
02 Feb 2023, 08:22

Hi there,

No you don't, you just need to check the current bar time and count only the bars that fall within the certain timeframe.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

dykesn92
02 Feb 2023, 11:22

RE:

PanagiotisChar said:

Hi there,

No you don't, you just need to check the current bar time and count only the bars that fall within the certain timeframe.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

Thanks for your ideas.

 

Okay so reflecting on what you said I built a timer to reset to just before the candle I need so that I can keep a counter at all times 

 

I haven't worked out how to make it work if I was to say turn it on after this for the day though since doing current time minus target candle time / 5 to give you candles is what keeps changing for me.

 

What do you suggest here?


@dykesn92

PanagiotisChar
02 Feb 2023, 11:31

Hi there,

You don't need a timer. If the candle's open time is within your time range, increase it, else set it to 0.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

dykesn92
02 Feb 2023, 11:45

RE:

PanagiotisChar said:

Hi there,

You don't need a timer. If the candle's open time is within your time range, increase it, else set it to 0.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

Thanks


@dykesn92