Custom metrics, was: custom backtest report

Colydog2d
Similar question here

I want to generate a report which shows how many of the trades had a loss greater than my position risk
I generated the custom back test for a fixed value. in the example below i used 200

    // check for number greater than stop loss
   
    if (trade.GetProfit() < -200) 
    {
    if( trade.IsLong() )
            maxlossCountLong++;
        else
            maxlossCountShort++;
            }
}

   // add the custom metric
    bo.AddCustomMetric( "Stoploss trades", stoplossCountLong + stoplossCountShort, 
                         stoplossCountLong, stoplossCountShort, 0 );
     bo.AddCustomMetric( "nBar trades", nbarCountLong + nbarCountShort, 
                         nbarCountLong, nbarCountShort, 0 );                    
	bo.AddCustomMetric( "Normal trades", normalCountLong + normalCountShort, 
                         normalCountLong, normalCountShort, 0 ); 
    bo.AddCustomMetric( "Max loss trades", maxlossCountLong + maxlossCountShort, 
                         maxlossCountLong, maxlossCountShort, 0 );                      
                         
}

this works fine.
I want to now do this report based on a variable of position size which is determined based on account value

in the main section i set the position size here:


// risk % of entire equity on single trade
PositionRisk = 1;

// position size calculation
symbol = Name();  
PctSize =  PositionRisk * buylimitprice / range;
StaticVarSet( symbol + "PctSize", PctSize );  

SetPositionSize( Min(pctsize,20), spspercentofequity );
then in the custom backtest i try to call the variable and use it:


        // check for number greater than stop loss
        PctSize = StaticVarGet( symbol + "PctSize"  );  
        if (trade.GetProfit() < Pctsize) 
        {
        if( trade.IsLong() )
                maxlossCountLong++;
            else
                maxlossCountShort++;
                }
    }

   // add the custom metric
    bo.AddCustomMetric( "Stoploss trades", stoplossCountLong + stoplossCountShort, 
                         stoplossCountLong, stoplossCountShort, 0 );
     bo.AddCustomMetric( "nBar trades", nbarCountLong + nbarCountShort, 
                         nbarCountLong, nbarCountShort, 0 );                    
	bo.AddCustomMetric( "Normal trades", normalCountLong + normalCountShort, 
                         normalCountLong, normalCountShort, 0 ); 
    bo.AddCustomMetric( "Max loss trades", maxlossCountLong + maxlossCountShort, 
                         maxlossCountLong, maxlossCountShort, 0 );

i get an error during the back test and i am at the limit of my coding ability!

anyone able to help?

Moderator comment: In order to receive help you need to help others to help you by providing ALL information, including the error message you get

 // check for number greater than stop loss

    PctSize = StaticVarGet( symbol + "PctSize"  );  

    if (trade.GetProfit() < Pctsize)

---------------------------------------^

Error 6.
Condition in IF, WHILE, FOR statements
has to be Numeric or Boolean type.
You can not use array here,
please use [] (array subscript operator)
to access array elements

The error is just what it says, you can’t use an array in an if statement. PctSize is an array, ie it has a value for every bar. How is the if statement meant to know which bar you mean?

Answer: If you’re using it in a bar loop, you can use the incremental value of the loop as a subscript to identify the current bar.

Eg

for (i = 0; i < BarCount; i++)
{
//...
    if (trade.GetProfit() < Pctsize[i])
3 Likes

thanks Helix

here is the full custom back test section:


SetCustomBacktestProc( "" );

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

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

    // initialize counter
    stoplossCountLong = stoplossCountShort = 0;
	nbarCountLong = nbarCountShort = 0;
	normalCountLong = normalCountShort = 0;
	maxlossCountLong = maxlossCountShort = 0;
	
    // iterate through closed trades
    for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
    {
        symbol = trade.Symbol ;  
      // check for stop-loss exit reason
        if( trade.ExitReason == 2)
        {
         // increase long or short counter respectively
            if( trade.IsLong() )
                stoplossCountLong++;
            else
                stoplossCountShort++;
        }
         
          // check for normal exit reason
        if( trade.ExitReason == 1)
        {
         // increase long or short counter respectively
            if( trade.IsLong() )
                normalCountLong++;
            else
                normalCountShort++;
        }

 
        
      // check for nBar exit reason
        if( trade.ExitReason == 5 )
        {
         // increase long or short counter respectively
            if( trade.IsLong() )
                nbarCountLong++;
            else
                nbarCountShort++;
        }
        
        // check for number greater than stop loss
        PctSize = StaticVarGet( symbol + "PctSize"  ); 
        
        for (i = 0; i < BarCount; i++)
{
//...
    if (trade.GetProfit() < Pctsize[i]) 
        
        {
        if( trade.IsLong() )
                maxlossCountLong++;
            else
                maxlossCountShort++;
                }
                }
    }

   // add the custom metric
    bo.AddCustomMetric( "Stoploss trades", stoplossCountLong + stoplossCountShort, 
                         stoplossCountLong, stoplossCountShort, 0 );
     bo.AddCustomMetric( "nBar trades", nbarCountLong + nbarCountShort, 
                         nbarCountLong, nbarCountShort, 0 );                    
	bo.AddCustomMetric( "Normal trades", normalCountLong + normalCountShort, 
                         normalCountLong, normalCountShort, 0 ); 
    bo.AddCustomMetric( "Max loss trades", maxlossCountLong + maxlossCountShort, 
                         maxlossCountLong, maxlossCountShort, 0 );                      
                         
}

not sure if I have implemented it correctly. as I say, I am at the limit of my knowledge!
it now gives me no errors but just gives me a big count which I assume are all the bars over the period of the back test

Your trade loop needs to be inside the bar loop, not the other way round as you have it.

I suggest you start here and read thoroughly. Then you will have a better understanding of how to implement it.
http://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/

1 Like

thank you!

think I have it working now.