I am trying to do a backtest at a simple strategy using donchian channels for daytrade. I want to do Intraday trades only ranging fom 10:30am to 16;30pm. I am using intraday data with 1 minute bars. If there are any open positions they should be closed @ 16:30pm. I am only trading long positions.
I have tried this code posted at : https://www.amibroker.com/kb/2014/11/28/how-to-restrict-trading-to-certain-hours-of-the-day/ but its not working as expected.
I have also looked at forums and google for it but I can not make it work properly.
The donchian channels code is working fine and the backtest report is generated but when I look at the trades on it there are some that start at a day and only end in the following showing that the time limit is not working.
I need help with restricting the trading time and ending all open positions by the end of the day.
Here is the code I am using:
tn = TimeNum();
startTime = 103000;
endTime = 163000;
timeOK = tn >= startTime AND tn <= endTime;
Buy = H > Ref(HHV(H,30),-1); BuyPrice = Ref(HHV(H,30),-1) AND timeOK;
Sell = L < Ref(LLV(L,10),-1); SellPrice = Ref(LLV(L,10),-1) AND timeOK OR Cross( tn, endTime );
Any help is much appreciated.Thanks in Advance
@marceloutn you did not follow the advice given in the article, you need your time limits in the BUY or SELL orders (not the BuyPrice). Try this
Buy = H > Ref(HHV(H,30),-1) AND timeOK;
Sell = L < (Ref(LLV(L,10),-1) AND timeOK) OR Cross( tn, endTime );
Thx for your help. It has limited the time of the trades and is closing open posicions at the desired time. I though it has solved everithing but today I was checking the exit price on my backtests and noticed that when the trade was finished by endtime when I was in a long position the exitprice was always at the high of the exitbar and when on a short position the exit price by endtime was always at the low of the exitbar.
That is probably giving me better results then they were supposed to be. I wonder if there is anyway for me to set the exit price, when it hapens by my desired endtime, on the close of the endtime bar so I can have real results in my backtests.
Thank you for all the help
Firstly, the LLV and HHV computations should be assigned to variable. That way unnecessary CPU load is avoided on same multiple calls.
L10 = LLV(L,10);
PL10 = Ref( L10, -1):
Now for the SellPrice, can you try a condition like this:
SellPrice = IIf( timeOK, PL10, C );