Code: Select all
// demo code for calculating a moving average of an array
Input: N(20), M(10);
vars: ArrayMA(0);
array: AP[](0);
once
array_setmaxindex(AP, N);
// shift array elements to the left
// as it can not be shifted to the right without erasing most of the values
array_copy(AP, 2, AP, 1, N-1);
// store a new value
AP[N] = close/2;
// calculate the moving average of the array.
// ** notice M is not the full array size
ArrayMA = AverageArray(AP, M);
The reason is that function AverageArray (and all functions in MC that computes on arrays) assumes either size M being the full array size or the most recent value being stored at the start of the array. The latter assumption however is not directly supported in MC with function array_copy as it requires the most recent value be stored at the end of the array. User code has to introduce a temporary array and perform multiple array copies to realized this kind of goal.
Does this cause some issue for other users?