Counting Bars Question
-
I'll try to keep this brief and to the point. I'm coding for MT5 if it's relevant, but I don't think it is.
I have an EA that basically runs a loop and if it identifies a candle with a specific condition, I take the candleID from it, and I also store the time for the candle in another variable.
Here's what I need it to do:
Once per bar, I want it to check if the candle that happened at that time stored is more than a certain amount of bars away, that way I can have it reset something.
Here's a better example if you're having a difficult time visualizing it.
On init, it scans and finds that candle id 10 meets the condition. The candle id is stored and the time is also stored. Once per bar, I want it to check if that candle is more than 24 bars away, for example, that way I don't have to run the ENTIRE loop once per bar to see how far back the event occurred.
The loop was fine, the storing of those variables is fine. I cannot find a node/condition/etc that allows me to look up a candle ID by its time so that I can somehow count how many bars back it was.
I hate At'ing people, but if you've got time and are willing, maybe @roar might have an idea?
Thanks to whomever has a solution!
-
@cdwilder1
instead of candle id you can store the time of the candle
take the current candle time - the stored time and divide it by the time a candle needs and you have a result of how many bars this event is in the past (like 1m = 60 seconds and so on) or just look for the difference in time of candle 1 minus candle 2 so you don't have to put in an number (issue with last candle friday and first could be tricky) -
@cdwilder1 thats a very important question and I'm happy to know the answer, lol
Convert candle ID to time:
v1 = iTime(NULL,0, 123)
Put this code in a custom mql5 block and the open time of 123th candle is stored to variable v1.
https://www.mql5.com/en/docs/series/itimeConvert time to candle ID:
v1 = iBarShift(NULL,0, D'2020.02.22 12:00')
Put this code in a custom mql5 block and the ID of D'2020.02.22 12:00' candle is stored to variable v1.
https://www.mql5.com/en/docs/series/ibarshift -
@bk7 Ah! That's an excellent work around that I hadn't considered! Thanks for that. But, like you said, that weekend gap may cause issues and this is counting hourly candles, so that may be an issue (but it may be one I can work around, so I'll surely keep this in mind!)
-
@roar Thanks, Roar! Hahaha. You're the best.
Surprisingly, I did NOT expect it to be that complicated! hahaha. I'm still a complete nub with custom coding (hence why I'm drawn to FxDreema). I don't fully grasp it still, but I think you've given me more than enough to try and figure it out. Thanks, bud!