Better way to calculate number of months in backtest?


I have added a custom metric that gives me the correlation of monthly rolling annual backest returns with the S&P500 across the entire backtest period. To use the Correlation function I need to retrieve the total number of months. After searching around the only way I found was this one:

        MonthCounter = 1;
	Months = Month(); 
	for( bar = 1; bar < BarCount; bar++ )
		if ( Months[bar] != Months[bar-1] ) MonthCounter[bar] = MonthCounter[bar-1] + 1;
		else MonthCounter[bar] = MonthCounter[bar-1];

I decided to do this because BarCount is not adjusted when I change timeframe. So, this does not give me the right result (still daily bar count):


Just curious if I missed something. If not, and anybody else looks for this, here is the answer.



There is not looping required.

mth = Month();
new_mth = mth != Ref(mth,-1);
bir = Status("barinrange");
MonthCounter = Cum(IIf(bir, new_mth, 0));

Sorry for the late thanks ... only saw this now.

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