Hi there,
Does anyone know how to close a position based on number of hours?
Thank you.
Put this, example with 10 hours for both long and short.
If longonmarket and (barindex-tradeindex)>=10 then
SELL AT MARKET
ENDIF
If shortonmarket and (barindex-tradeindex)>=10 then
BUY AT MARKET
Endif
Mauro‘s code is correct if you are using 1-hour TF, if you are using lower TF’s you’ll have to write:
ONCE MaxHours = 10
IF Not OnMarket THEN
Count = 0
ELSE
IF OpenHour <> OpenHour[1] THEN
Count = Count + 1
IF Count >= MaxHours THEN //exit any trade, be it Long or Short
SELL AT MARKET
EXITSHORT AT MARKET
ENDIF
ENDIF
ENDIF
Yes Roberto you are right I answered for number of bars.
Thank you both Mauro and Roberto. Very much appreciate your assistance.
There is a small issue with your code Roberto as it does not exit exactly ten hours after a trade is opened when used on a faster time frame than hourly. You would need to record the minute and/or second when a trade is opened and then count the hours and then check for the same minute and/or second in the tenth hour to work out when to precisely close the trade.
I think he doesn’t need an exact number of hours, but if he does I can do it.