# How to calculate annual growth rate for each year in custom backtester

Hello,

I would need to calculate the annual growth rate for each year of a bactktest, in order to have those values tabulated in the optimization report. I have found a code by @fxshrat with a similar purpose.

``````procedure vgWinLossPerYear(YrWin, YrLoss, Yrcnt, trade, mode ) {
/// by AmiBroker.com and fxshrat@gmail.com
n = floor(DateTimeConvert(8, dt));
if( trade.GetProfit() >= 0 ) // profit only
VarSet(YrWin+n, Nz(VarGet(YrWin+n))+1);
else  // loss only
VarSet(YrLoss+n, Nz(VarGet(YrLoss+n))+1);
// cnt all per year
VarSet(Yrcnt+n, Nz(VarGet(Yrcnt+n))+1);
}

/// Per year Win/Loss ratio and number of trades, code source at:
SetCustomBacktestProc("");
if ( Status( "action" ) == actionPortfolio ) {
bo = GetBacktesterObject();
bo.Backtest();

yr = Year();
fbr = Status("FirstBarInRange");
lbr = Status("LastBarInRange");
first_yr = LastValue(ValueWhen(fbr, yr));
last_yr = LastValue(ValueWhen(lbr, yr));

vgWinLossPerYear("YrWin", "YrLoss", "Yrcnt", trade, 0 );

vgWinLossPerYear("YrWin", "YrLoss", "Yrcnt", trade, 0 );

// Output per year stats to report file
for ( i = first_yr; i <= last_yr; i++ ) {
YrCnt = VarGet("Yrcnt"+i);
i, YrCnt, VarGet("YrWin"+i) / YrCnt * 100, VarGet("YrLoss"+i) / YrCnt * 100) );
}
}

m = MA( Close, 20 );
Buy = Cross( Close, m );
Sell = Cross( m, Close );
Short = Cover = 0;
``````

Since I am not very familiar to custom backtester yet, I have been unable to modify it to get the metric that I need. I should appreciate it if some user would help me with it.

Thanks in beforehand.

Regards.

Hello,

I was working on an easier approach to have the annual growth for each year displayed in the optimization result list.

``````Buy  = RSI(2) < 5;
Sell = RSI(2) > Optimize("PRD", 80, 60, 95, 5);

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

bo = GetBacktesterObject();
bo.backtest();

Eq = bo.EquityArray();
YearNumber = BeginValue(Year());
NewYear = Year() < Ref(Year(), 1);

for (i = 1; i < BarCount; i++)
{
if(NewYear[i] == 1)
{
VarSet("Equity" + Yearnumber, Eq);
YearGr = ((VarGet("Equity" + Yearnumber) / VarGet("Equity" + (Yearnumber - 1)))-1)*100;
YearNumber++;
}}}
``````

I am stuck with calling the Equity into the loop, I would need to calculate the rate between the equity for the last bar of year n and the equivalent of year n-1. Do you have any suggestion?

Regards.

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