Calculate aggregated stats over N bars per trade in custom backtester

Hi
I have some standard custom backtester code to output certain custom metrics.
What I would like to do is to to create some additional custom metrics for each trade that are based on values 5 days before the buy signal of the trade. The idea is I'm trying to output for each trade a set of metrics I can analyze to see the metrics before/at a buy signal.
If I have a buy signal that is taken on say bar index 150 then I want to go back to bar 145,146,147,148,149 and calculate certain things, this might number of up days, it might be a weighted avg volume over the 5 days, it might number of days RSI is above a certain level for each of the 5 days.
The way I imagine doing this would be in a loop, something like this:

// iterate through closed trades
for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
//get bar index for trade day TRADEINDEX and get the value for x - 5 (TRADEINDEX5BARSAGO)
TRADEINDEX = ??? ;
TRADEINDEX5BARSAGO = TRADEINDEX - 5;

		volp = (V*C)V;
		ATrvalue = ATR(5);
		spread = H-L;
		
		for (i=TRADEINDEX5BARSAGO, i< TRADEINDEX, i++)
		{ 
			//now calculate some metrics
			volp = volp[i] ; //...etc
                            spreadavg = spread[i]  ;  //....

I'm not sure my approach is the right one, do I need the loops? And if so, how should I do it?

Thanks