[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Low level backtester: plot per trade P&L



PureBytes Links

Trading Reference Links

While getting used to AB and practicing with the backtester interface,
tried to plot per trade P&L out of backtester signals.

Since running within backtester (...actionPortfolio) there is no plot
capability and if inserted like an indicator, result is 0 since not
within backtester.
Is there any procedure in AFL to plot the result in a pane, like
clicking on the backtester 'Equity' button?

Thanks,
Fernando Martin

// backtester per trade P&L 

SetOption("UseCustomBacktestProc", True ); 
barPL=0;
entryprice=O;
tradecontracts=O;

if(Status("action") == actionPortfolio){

   bo = GetBacktesterObject(); 
   bo.PreProcess(); 	// Initialize backtester 

   barPL=Null;		// array trade PL, once trade closes PL=0
   scalePL=0;
   Longtrade=False;
   Shorttrade=False;

   // explore bars
   for(bar=0; bar<BarCount; bar++){ 
     barPL[bar]=0;
     barsignal=False;

     bo.ProcessTradeSignals(bar);
	// explore signals in a bar
     for(sig=bo.GetFirstSignal(bar);sig;sig=bo.GetNextSignal(bar)){  

	// signals in bar
	if( sig.IsExit() OR sig.IsEntry() OR sig.IsScale() ){
//sig.Type= 1-buy, 2-sell, 3-short, 4-cover, 5-scale-in, 6-scale-out
	   switch(sig.Type){
		case 1:	// buy
		entryprice=sig.Price;
		tradecontracts=sig.PosSize;
		Longtrade=True;
		barsignal=True;
		break;
				
		case 2:	// sell
		exitprice=sig.Price;
		tradecontracts=sig.PosSize;
		barPL[bar]=(exitprice-entryprice)*tradecontracts +scalePL;
		// close trade
		Longtrade=False;
		scalePL=0;
		entryprice=0;
		barsignal=True;
		break;
						
		case 3:	// short
		entryprice=sig.Price;
		tradecontracts=sig.PosSize;
		Shorttrade=True;
		barsignal=True;
		break;

		case 4:	// cover
		exitprice=sig.Price;
		tradecontracts=sig.PosSize;
		barPL[bar]=(entryprice-exitprice)*tradecontracts +scalePL;					
		// close trade
		Shorttrade=False;
		scalePL=0;
		entryprice=0;
		barsignal=True;
		break;
/*
		case 5:	// scale-in
		intrade=True;
                barsignal=True;

		break;

		case 6:	// scale-out
		intrade=True;
		barsignal=True;

		break;
*/
		default:
		break;
	   } 		// end of switch(sig.Type)
      } 		// end of if( sig.IsExit() OR ...

			// next signal

   } 			// end of for( sig=bo.GetFirstSignal(bar);...
   // in trade but no signals in bar, PL for this bar
   if( (Longtrade OR Shorttrade) AND NOT barsignal ){	
	if(Longtrade)	       
barPL[bar]=(High[bar]-entryprice)*tradecontracts+scalePL;
	else if(Shorttrade)
barPL[bar]=(entryprice-Low[bar])*tradecontracts+scalePL;	
   }
}                      // end of for(bar=0;bar<BarCount;...
bo.UpdateStats(bar,1); //MAE/MFE is updated when timeinbar is 1
bo.UpdateStats(bar,2);   

bo.PostProcess(); // Finalize backtester 
   
}                     // end of if(Status("action")...

Plot(barPL, "Trade P&L", colorRed, styleLine);

// end of bcktst_pertradepl_1d.afl
----------------------------------------------------------------



Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/