Unable to perform Intraday SquareOff


I am trying to squareoff my positions by the day end(intraday) using below piece of code.

_FirstTradeTime = 094500;				// Earliest time to take a trade_
_LastTradeTime = 150000;					// Latest time to take new trades_
_ExitAllPositionsTime = 152500;		// Exit all trades_

_Buy = Buy AND (TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime);_
_Sell = Sell OR (TimeNum() >= ExitAllPositionsTime);_

But my positions are still carried forward till the next day during the backtest. Please help. Thanks in advance !!

Rahul Jain

It appears that all of your entries are on 10 minute intervals - how did you set your Periodicity for the back test? Specifically, are you sure you have a bar that ends on or after 3:25?

I would just add this logic to your code:

isEndOfDay = Nz(TimeFrameExpand(1, inDaily, expandPoint));
Sell = Sell OR (TimeNum() >= ExitAllPositionsTime) OR isEndOfDay;

That way, you will always exit at the end of the trading day, even if trading (or your data) stops before 3:25pm.


hello sir please tell me why bar replay show different time on trade and trade exit and also sometimes it skip entry

@mn_sho: this question is completely unrelated to the one that you replied to. You should start a new thread. Before you do that, however, you should read @Tomasz's posts about how to use this site, do you own search on "bar replay", go through the basic troubleshooting steps (also detailed by @Tomasz), and then if you still can't figure out what's going wrong then post a question with a level of detail that allows others to provide specific help rather than trying to guess what you might be doing.


Thanks @mradtke

I was setting the Periodicity from the settings window. Actually I double checked my data, it seems that there were some bars missing in the data due to which square off condition was never getting hit.

Thanks for your analysis !!

Rahul Jain