Maximum system exposure%

Hi,

As I understand it, exposure % in the backtest report shows the average exposure % of the system ("Sum of bar exposures divided by number of bars", per the manual). Is there a simple way to calculate the maximum single bar exposure % over x bars? I'm guessing it can be done manually in the custom backtester using GetPositionValue, but hoping there's a simpler way more conducive to my limited coding ability.

Thanks for you assistance
Dan

Something like this

SetCustomBacktestProc("");
if ( Status( "action" ) == actionPortfolio ) {
   /// @link https://forum.amibroker.com/t/exporting-daily-exposure/14109/6
   /// @link https://forum.amibroker.com/t/maximum-system-exposure/15102/2
   bo = GetBacktesterObject();
   bo.PreProcess(); // Initialize backtester
   //
   pv = 0;
   for ( i = 0; i < BarCount; i++ ) {
      bo.ProcessTradeSignals( i );   
      for (op = bo.GetFirstOpenPos(); op; op = bo.GetNextOpenPos())
         pv[i] += op.Shares * op.GetPrice(i, "C");
   }
   //
   exposure = pv / bo.EquityArray * GetOption("AccountMargin");
   //
   max_exp_value = LastValue(Highest(exposure));
   bo.AddCustomMetric("Max. bar-by-bar exposure", max_exp_value);
   //
   bo.PostProcess(); // Finalize backtester
}

/// ###################################################################################
SetOption( "AccountMargin", 100);
SetPositionSize(10, spsPercentOfEquity);

// dummy system
m = MA( Close, 20 ); 
Buy = Cross( Close, m ); 
Sell = Cross( m, Close );
Short = Cover = 0;

Max. bar-by bar is output at the bottom of backtest report statistics page then.

7

9 Likes

Thanks fxshrat, I was expecting a few tips on how to do it myself, not a complete solution!

Works great, thanks for your help.
Dan

1 Like

One quick note if anyone else looks at this, I actually wanted to use this for futures rather than stocks. My fault for not specifying.

One small change was needed.

Instead of

pv[i] += op.Shares * op.GetPrice(i, "C");

I used

pv[i] += op.GetPositionValue();

Thanks again
Dan

@danm,

Just FYI, Shares property is referring to shares and contracts.

See AB manual:
8

So then (in case of Futures)

pv[i] += op.Shares * op.MarginDeposit;