Custom EMA calculation that works in Backtest

Needed one help with Custom MA calculation. Could not find any topics for this on forum

On 5 min candle

I needed to calculate Moving average of previous 5 days range (H-L)
However I need it to ignore any candles in specific time slots

I can loop back from lastvalue of current bar and ignore certain timenums to calculate moving average but this would not work when I am back testing.

Does someone have any recommendation on what could be possibly done here such that it works in back testing as well as live exploration?

Thanks in advance and apologies if I missed a post or if I ought to send this post elsewhere in forum. I am still in the AFL learning phase