amibroker

HomeKnowledge Base

Adding custom metric: Average adverse excursion

Here is a sample that shows how to create custom metric based on per-trade statisitics.
In this example we will calculate the average value of MAE (maximum adverse excursion) from all trades.

SetCustomBacktestProc(""); 

/* Now custom-backtest procedure follows */ 

if( Status("action") == actionPortfolio 

    
bo GetBacktesterObject(); 

    
bo.Backtest(); // run default backtest procedure 

   
SumMAE 0
   
NumTrades 0

   
// iterate through closed trades first 
   
for( trade bo.GetFirstTrade(); tradetrade bo.GetNextTrade() ) 
   { 
      
// here we sum up maximum adverse excursions
       
SumMAE SumMAE trade.GetMAE();
       
NumTrades++; 
   } 

   
// iterate through eventually still open positions 
   
for( trade bo.GetFirstOpenPos(); tradetrade bo.GetNextOpenPos() ) 
   { 
       
SumMAE SumMAE trade.GetMAE();
       
NumTrades++; 
   } 

   
averageMAE SumMAE NumTrades

   
bo.AddCustomMetric"Avg. adverse excursion"averageMAE ); 



// your trading system here 
//...

No comments yet. Be the first.

Leave a reply