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?