Moving Average is calculating when there are not enough bars

This can't be correct. There are many examples, but looking at ticker SOVO in the attached image. The chart has 3 daily bars. barindex() has a value of 2. Yet a 200 day moving average has a value calculated of 0.20185.

What on earth have I done within Amibroker to create this situation?

Filter = 1;

AddColumn(BarIndex(),"Number of Bars",1.0);
AddColumn(MA(C,200),"MA200");

2021-09-28 15_56_02-AmiBroker

Your period is 200 but you have a sum of three values divided by that period. BarCount is 3 but period is 200.

13*3/200 = around 0.2

If you want to reduce period if BarCount is less than period

Filter = 1;

per = 200;
bi = BarIndex();

AddColumn(bi,"Number of Bars",1.0);
AddColumn(MA(C,IIf(bi<per, bi, per)),"MA200");

Or if you want to consider only symbols which have required BarCount (otherwise output being Null).

Filter = 1;

per = 200;
my_ma = IIf(BarCount >= per, MA(C,per), Null);

AddColumn(BarIndex(),"Number of Bars",1.0);
AddColumn(my_ma,"MA200");
2 Likes

Thank you for taking the time

This is mathematically sound. I have simply been [wrongly] conditioned over decades by platforms which return NULL for an MA value if bi < per.

And indeed, I'd implemented similar IIf rules based on bi v.s. per comparisons.

Once again, thanks for taking the time. I was wrong to not think it over further in the simplest terms.

If I use the same code as OP on a ticker which has less than 200 bars, I get Null. Only after 200 bars do I start getting the MA values. It, a 200 period moving average, also plots Null until it reaches the first 200 bars. This appears to be different behaviour from the OP who is getting MA values from the 1st, 2nd, 3rd bars and I assume would also plot values from the 1st, 2nd, 3rd bars

I am still using version v6.38.0 of AmiBroker. Is this different behaviour with the newer version? I am not using Pad and Align either.

Use a ticker with BARCOUNT less than period (then you should see similar results).
BarCount is not same as BarIndex.

Of course it isn't right. Downgrade to version 6.38.
IF there are not enough bars in the data a single value of MA at the very last bar for incomplete period due to changes done between recent versions. This change was done in 6.39 after users requested such change but it will be reverted.
If you use "Range": "All quotes" then you will see that you always get NULLS for all bars except the last one.

No your interpretation is incorrect. The original poster is NOT getting values for ANYTHING but the LAST BAR. The image may be misleading because he is displaying BarIndex() but at the same time he is using "Range" : "1 recent bar". So it is LAST BAR displayed, not anything else.

The change done in 6.39 will be reverted in 6.40.1