HomeKnowledge Base

How to close open positions at the end of the day (for daytraders)

If we are backtesting the intraday strategy and we do not want to keep open positions overnight – then it’s necessary to force closing all the open positions before the session end. In order to code it in AFL – the easiest is to use TimeNum() function for that purpose.

The below example shows how to close all open positions after 15:50, just before the end of the trading session (when testing on intraday quotes).

Buy = ...your regular conditions....
Sell = ....your regular conditions.... OR TimeNum() > 155000
For example – in case of MACD crossover – the code will look like:

Buy CrossMACD(), Signal() );
Sell CrossSignal(), MACD() ) OR TimeNum() > 155000

Comments are closed.