How would I calculate an average of closing prices over the last x days that a particular condition is met, (say volume > 0, for example)? ie. In this example we would want to exclude non-trade days from the moving average calculation.

The only way I can think of is to do something like the following:
X1 = ValueWhen(V > 0, C, 1);
X2 = ValueWhen(V > 0, C, 2);
X3 = ValueWhen(V > 0, C, 3);


AveX = (X1 + X2 + X3 + ………etc) / periods;

The problem with this method (besides the length of the code if doing larger moving average lengths, is that you won’t be able to Optimize the MA length in the code. Is there a simpler/better way to do this?