Filter trades by age. Number of bars elapsed rather than minutes.
-
I am trying to close trades after x bars and the only option I can find in the close block is to "Filter trades by age" in minutes, not bars.
When using bars that have no fixed time I have to count the bars.
Is there a way to filter by age in terms of the number of bars that have passed since open ?
If not I will try to build a separate running bar counter for each trade unless you feel it worthy of being added to the wish list.
Thank you.
-
I think once in the past someone asked the same question, I don't remember what solution I gave him. But I think that with some calculations you can get the number of minutes from the bars. I don't know what calculation and I am actually very very bad in mathematics, but here is some example that shows that you can do such things.
MathFloor((TimeCurrent() + 86400) / 86400) * 86400Here TimeCurrent() gives you the current time, the value that this function gives is going up with 1 each second, so the value is the number of seconds since 1 Jan 1970. More here: https://www.epochconverter.com/
And MathFloor() is a MQL function that is described like this "The function returns integer numeric value closest from below."
So, the whole calculation is giving you the time at 00:00 of the current day, no matter if TimeCurrent() is giving you the time at 02:10 or 18:45 of the day.
So there should be similar formula that turns the age of the trade into number of candles.
-
Let's say the age is 55 minutes. The age written in MQL4 is like this:
OrderCloseTime() - OrderOpenTime()If the period is 15 minutes, then you can just divide: 55 / 15. The result is 3.666. Now, you can use either MathFloor() or MathCeil(), the first function will give you 3 from 3.666 and the second will give you 4 from 3.666. Try this: http://prntscr.com/ikmrdw
-
The candles I am using are achievement based and therefore each one has a different amount of time.
I have resorted to:-
a) Running a continuous bar count on each bar open = (Current Bar Number)
b) Recording the current bar number when a new trade is placed = (Trade Start Bar Number)
c) Calculating the number of bars between now & when the trade started = (Current Bar Number) - (Trade Start Bar Number)A little crude and simple but appears to be working.
Many thanks for the input.