Backtest report not able to fetch and display trades - ORB

Hi
I am using Amibroker 6.00.2. I am new to AFL.
I am trying to write code for "Opening Range Breakout" strategy.
I could see the "Sell and Cover trades" within "Scan" of analysis, but when I run backtest, Backtest report (which is blank) not able to fetch and diplay trades.
Please could you help to solve this problem? Or will also be helpful if anyone could share backtestable sample AFL for "Opening Range Breakout" strategy.
Thank You in advance!

Please find code of my AFL below:

//Not yet added stop loss related code
market_start_time=091500;
brake_out_time=093000;
market_end_time=151500; 
//target_point=Optimize("target_point",20,5,500,1);  
target_point=50;
EndDay = (Day()!= Ref(Day(), 1)); 
buy_occurred = False;
short_occurred = False;

Highest_since_start = HighestSince(market_start_time,H,1); 
Lowest_since_start =LowestSince(market_start_time,L,1); 
ORBH = ValueWhen(brake_out_time,Highest_since_start,1); 
ORBL = ValueWhen(brake_out_time,Lowest_since_start,1); 

if(buy_occurred == False){
Buy=IIf(Cross(C,ORBH) AND (TimeNum()>brake_out_time) ,True,Null); 
buy_occurred = True;
}

if(short_occurred == False){
Short =IIf((Cross(ORBL,C) AND (TimeNum()>brake_out_time)) ,True,Null); 
short_occurred = True;
}

BuyPrice=ValueWhen(Buy,C,1);
ShortPrice=ValueWhen(Short,C,1);

SellPrice1=BuyPrice+target_point; 
Sell_Cond=Cross(H,SellPrice1);
Sell=IIf(((buy_occurred==True) AND  ((Sell_Cond==True) OR (EndDay==True))),True ,Null);
//Sell=IIf(((buy_occurred==True) AND  ((Sell_Cond==True) OR (market_end_time <= TimeNum()))),True ,Null);
SellPrice=ValueWhen(Sell,H,1);
 
CoverPrice1=ShortPrice-target_point;
Cover_cond= IIf(L<=CoverPrice1,True,False); 
Cover=IIf((short_occurred==True) AND (Cover_Cond==True OR EndDay==True),True,Null) ;
//Cover=IIf((short_occurred==True) AND (Cover_Cond==True OR market_end_time <= TimeNum()),True,Null) ;
CoverPrice=ValueWhen(Cover,C,1);

1 Like

Please post your full code along with stoploss or more preferably trailing stopploss (you do not have to have target and stoploss code separately. Once in trade your trailing stoploss should automatically take care of both loss and profit) ad also define your requirements for backtest results clearly. I might be able to help you then.

Thank you for your reply Santy.
Please find the backtest requirement:
Would like to:
1> Run this code on last 10 years Nifty data.
2> Would like to see Annual returns and other analysis details for the ORB strategy, as mentioned in code.
3> Logic of expected ORB strategy is given below:

  • Take "Nifty High and Low" for first 15 mins from market open.
  • Market start time: 091500
  • Endtime to consider "Nifty High and Low" is : 093000
  • Market end time: 151500
  • Take Buy or Short position after 093000. Need only one position during entire day, and want to square-off the position before end of day.
  • Take Buy position (after 093000) immediately after this condition satisfies=> current Nifty Close > High occurred between period 091500 to 093000
  • Take Short position (after 093000) immediately after this condition satisfies=> current Nifty Close < Low occurred between period 091500 to 093000
  • Square-off above Buy or Short position, when Stoploss or Target triggers. OR Square-off above Buy or Short position just before End Of Day.
    Hope above logic makes sense.

Please find the modified code with stoploss below, your help to backtest this strategy will highly be appreciated - Thank You.

_SECTION_BEGIN("ORB");
market_start_time=091500;
brake_out_time=093000;
market_end_time=151500; 
//target_point=Optimize("target_point",20,5,500,1);  
stoploss_point=30;
target_point=50;
EndDay = (Day()!= Ref(Day(), 1)); 
buy_occurred = False;
short_occurred = False;

Highest_since_start = HighestSince(market_start_time,H,1); 
Lowest_since_start =LowestSince(market_start_time,L,1); 
ORBH = ValueWhen(brake_out_time,Highest_since_start,1); 
ORBL = ValueWhen(brake_out_time,Lowest_since_start,1); 

if(buy_occurred == False){
Buy=IIf(Cross(C,ORBH) AND (TimeNum()>brake_out_time) ,True,Null); 
buy_occurred = True;
}

if(short_occurred == False){
Short =IIf((Cross(ORBL,C) AND (TimeNum()>brake_out_time)) ,True,Null); 
short_occurred = True;

}

/////////////////////////////////////////////////////////////////////////////////////////////
BuyPrice=ValueWhen(Buy,C,1);
ShortPrice=ValueWhen(Short,C,1);

SellPrice_profit=BuyPrice+target_point; 
SellPrice_stoploss=BuyPrice-stoploss_point;
Sell_Cond=Cross(H,SellPrice_profit);
Sell_Cond_SL= IIf(L <= SellPrice_stoploss,True,False);

Sell=IIf(((buy_occurred==True) AND  ((Sell_Cond==True) OR (Sell_Cond_SL==True) OR (EndDay==True))),True ,Null);
//Sell=IIf(((buy_occurred==True) AND  ((Sell_Cond==True) OR (market_end_time <= TimeNum()))),True ,Null);
SellPrice=ValueWhen(Sell,H,1);


  
CoverPrice_profit=ShortPrice-target_point;
CoverPrice_stoploss=ShortPrice+stoploss_point;
Cover_cond= IIf(L<=CoverPrice_profit,True,False);
Cover_cond_SL= IIf(H>=CoverPrice_stoploss,True,False); 
Cover=IIf((short_occurred==True) AND (Cover_Cond==True OR Cover_cond_SL==True OR EndDay==True),True,Null) ;

//Cover=IIf((short_occurred==True) AND (Cover_Cond==True OR market_end_time <= TimeNum()),True,Null) ;
CoverPrice=ValueWhen(Cover,C,1);

_SECTION_END();

Before jumping into the backtesting part, have you put it on chart and confirmed to yourself that it is working as per your idea?

I afraid that there is no exrem and plotshape codes to flash signals on chart.

what way you getting signals from the above code?

Are you sure that you are getting correct signals as per your idea that you have tried to code in the above coding?

I think the answer to the above questions will lead you to solve your query to a great extent, otherwise just work out the above pointers and give me a shout here to proceed.

1 Like

Hi Santy - Sure I will try to plot the charts and will try to answer your questions within my solution soon ..
But I am new to Amibroker and AFL, I started exploring Amibroker three days back, might take little time to understand it well, and give it a good shape.

YEP, no problem.

We all are learning in our ways always.

I have already given you some clues like EXREM & PLOTSHAPES.

DO come back to the forum whenever you are stuck with all details, but always try yourself 100% first.

Good Luck!!!

Sure .. Thank you ..