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

[amibroker] Historical chart for user-created portfolio backtest metrics



PureBytes Links

Trading Reference Links

I'd like to plot a very simple indicator with the number of open and closed positions for every bar for a portfolio backtest.

It seems like this must be done through the Custom Backtester Interface therefore I tried to put together the articles:
-Historical Portfolio Backtest Metrics
http://www.amibroker.com/kb/2008/05/19/historical-portfolio-backtest-metrics/
-Advanced Portfolio Backtest Metrics
http://www.amibroker.com/guide/a_custombacktest.html
-Adding Custom Bactester Metrics
http://www.amibroker.com/guide/a_custommetrics.html

but it seems like charting user-created metrics is not that easy since GetValue method for Stats object doesn't allow to get user-created metrics values.

I came out with the below code which works fine but I'm quite sure there must be an easier and quicker way of doing it. Any hints?

Regards,

Paolo



///////////////////////////////////////////////////////////////////

SetOption("UseCustomBacktestProc", True ); 

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

bo = GetBacktesterObject();
 
  bo.PreProcess(); // Initialize backtester
 
  // initialize with null 
  ClosedPos = Null; 
  OpenPos = Null;
  TotalPos = Null;

  for(bar=0; bar < BarCount; bar++)
  {
   bo.ProcessTradeSignals( bar );
  
   // recalculate built-in stats on EACH BAR
   //is it necessary???
   stats = bo.GetPerformanceStats( 0 );

   ClosedPositions = 0; 
   OpenPositions = 0; 
   TotalPositions = 0; 

   // iterate through closed trades first 
   for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade()) 
   { 
       ClosedPositions++;
   } 

   // iterate through eventually still open positions 
   for( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos()) 
   { 
       OpenPositions++; 
   } 

   //TotalPositions = ClosedPositions + OpenPositions;

 
   // the line below reads the metric and stores it as array element
   // you can add many lines for each metric of your choice
   ClosedPos[bar] = ClosedPositions; 
   OpenPos[bar] = OpenPositions;
   //TotalPos[bar] = TotalPositions;

  }

  bo.PostProcess(); // Finalize backtester

  AddToComposite( ClosedPos - Ref(ClosedPos, -1), "~~~ClosedPos", "X", atcFlagEnableInPortfolio | atcFlagDefaults );

  // you can add your own as shown below
  AddToComposite( OpenPos, "~~~OpenPos", "X", atcFlagEnableInPortfolio | atcFlagDefaults ); 
  //AddToComposite( TotalPos, "~~~TotalPos", "X", atcFlagEnableInPortfolio | atcFlagDefaults ); 
}

// Your system codes starts here
Sell = Cross(MACD(), Signal());
Buy = Cross(Signal(), MACD());
SetPositionSize(10 ,spsPercentOfEquity);

////////////////////////////////////////////////////////////////////

and the relative indicator:

//Positions Counter.afl
PlotForeign("~~~ClosedPos", "Closed Positions Historical", colorLightBlue, styleHistogram );
PlotForeign("~~~OpenPos", "Open Positions Historical", colorRed, styleHistogram);
TotalPos = Foreign("~~~ClosedPos", "C") + Foreign("~~~OpenPos", "C");
Plot(TotalPos, "Total Positions Historical", colorWhite, styleHistogram);



------------------------------------

**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to 
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

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

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/