How to skip count a bar with same OHLC AND Volume = 0

Hi, I want to find the Moving Average. But some candle on my chart is same value for OHLC AND Volume is 0 (mean the stock is suspended).
here I wrote a formula, it's fine if the suspended candle 1, but it's not work if have some suspended candle for several days

Suspend = H == L AND V == 0;  // suspended bar
ValidBar[ 0 ] = Close[ 0 ];

for( i = 0; i < BarCount; i++ )
{
	if(Suspend[i])
		ValidBar[i] = 0;
	else
		ValidBar [i] = Close [i];
}

function MVA ( x ){
	n = Sum(ValidBar<1,x);
	Periode = x + n;
	
	SumVB = Sum(ValidBar,SelectedValue(Periode));

	return SUMVB / x;
}

MA20 = MVA(20);

here is example chart

thank you

You can use SparseCompress and SparseExpand to remove those bars with volume == 0, perform calculations on non-zero-volume bars, and expand again

function MVA( array, periods )
{
   valid_bars = Volume != 0;
   data = SparseCompress( valid_bars, Close;
   return SparseExpand( valid_bars, MA( data, periods ) );
}
1 Like

Thank you sir. it solved my problem.