Add Column in Backtest Result List

Hi everyone!

I'm trying to add a Stop Loss column in the Backtesting Result List. Following instructions from AmiBroker Knowledge Base » Custom Backtester I'm able to add a static 10% SL from entry price. This is the code:

/* First we need to enable custom backtest procedure and
** tell AmiBroker to use current formula
*/

SetCustomBacktestProc("");

MaxLossPercentStop = 10; // 10% max. loss stop

/* Now custom-backtest procedure follows */
if( Status("action") == actionPortfolio )
{
    bo = GetBacktesterObject();

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

   SumProfitPerRisk = 0;
   NumTrades = 0;

   // iterate through closed trades first
   for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
   {
      // risk is calculated as the maximum value we can loose per trade
      // in this example we are using  max. loss stop
      // it means we can not lose more than (MaxLoss%) of invested amount
      // hence ris

      SL =  ( 1-MaxLossPercentStop / 100 ) * trade.EntryPrice(); 
      trade.AddCustomMetric("Stop Loss", SL  );
      NumTrades++;
   }

// iterate through open positions
    for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
    {
      SL =  (1- MaxLossPercentStop / 100 ) * trade.EntryPrice(); 
      trade.AddCustomMetric("Stop Loss", SL  );
      NumTrades++;
    }

    bo.ListTrades();

} 

What I'd like to achieve is to add in the CBT the following SL:

WMA30 = WMA(Close, 30);
StopLossCompra = ValueWhen(Buy,WMA30);
CierreBarraCompra = ValueWhen(Buy,C);
CalculoStopCompra = StopLossCompra/100;
StopLoss1 = (StopLossCompra - CalculoStopCompra);

So my question is: How can I call these variables in the CBT?

I have checked this post but I follow the instructions given, code below, I got the message that Variable StopLoss1 used without having been initialized and the SL column is empty.

SetCustomBacktestProc( "" );

if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
    // run default backtest procedure without generating the trade list
    bo.Backtest( True );

    // iterate through closed trades
    for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
    {
        // read ATR values and display as custom metric
        StaticVarSet( Name() + "Stop Loss", StopLoss1 );
        symbolSL = StaticVarGet( trade.Symbol + "Stop Loss" );
        trade.AddCustomMetric( "Stop Loss", Lookup( symbolSL, trade.EntryDateTime ) );
    }

    // iterate through open positions
    for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
    {
        // read ATR values and display as custom metric
        StaticVarSet( Name() + "Stop Loss", StopLoss1 );
        symbolSL = StaticVarGet( trade.Symbol + "Stop Loss" );
        trade.AddCustomMetric( "Stop Loss", Lookup( symbolSL, trade.EntryDateTime ) );
    }

    // generate trade list
    bo.ListTrades( );
}

Could anyone point me whether I have to use the first or the second method and give me some clue to fix it?

Thanks a lot and have a good weekend!

No, you don't.

You shouldn't just copy & pasting but in addition reading carefully.

It clearly says: use StaticVarSet() in 1st phase of BT.

You are using it in 2nd phase (CBT).

1st phase (non-CBT) is default backtest where you have set your entry/exit rules.

Last but not least you should read entire knowledge base before coming to forum.

1 Like

Thank you very much. I didn't understand what 1st and 2nd phase were. Thanks a lot! Now it's working perfectly!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.