Afl Coding : Restricting Trading time - Need help

I'm trying to trade simple strategy [Its just for sample coding purpose, don't trade - it will fail ], I want to do Intraday trades only that too with time restiction from 9:30am to 15;00pm. All positions should be closed @ 3:15pm and the same should be reflected in backtest report also. I have tried  the code here : https://www.amibroker.com/kb/2014/11/28/how-to-restrict-trading-to-certain-hours-of-the-day/ but its not working as expected.

_SECTION_BEGIN("Tradetime");
tn = TimeNum();
startTime = 93000; // start in HHMMSS format
endTime = 151500;  // end in HHMMSS format
timeOK = tn >= startTime AND tn <= endTime;

/* To restrict trade time and End the trade @ particular time */
Buy = ( Cross( MACD(), Signal() ) AND timeOK ) OR Cross( tn, endTime );
Sell = (Cross( Signal(), MACD() ) AND timeOK) OR Cross( tn, endTime );

Short=Sell OR Cross(tn,endTime); 
Cover=Buy OR Cross (tn,endTime);  

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

_SECTION_END();

Can someone help with a sample code ? when you run backtest report will be generated but if you check on some Long trades trades ,it will be triggered @ 15:19:00 and exits next day 9:19:00AM which shows that Time restriction isn't effective. Any help is much appreciated.Thanks in Advance :)

SECTION_BEGIN("Tradetime");

/* To restrict trade time and End the trade @ particular time */
Buy = ( Cross( MACD(), Signal() ) AND timenum()>093000 and timenum()<150000;
Short = (Cross( Signal(), MACD() ) AND Timenum()>093000 and Timenum()<150000;

Buy=exrem(Buy,Short);
Short=Exrem(Short,buy);

Short=Sell OR timenum()>151500; 
Cover=Buy OR timenum()>151500;  

_SECTION_END();
1 Like

Thanks Karan…I checked on simple ma …It works…:slight_smile:
If someone else copy pasting there will be few small errors - rectify n use guys !

I tried this code but in back test trade list I found that each trade covered after exact 15 minutes of shorting it and vice versa.

Could anyone help please to define the time limit for the day.