array handling somewhat awkward

Questions about MultiCharts and user contributed studies.
cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

array handling somewhat awkward

Postby cnbiz850 » 25 Nov 2012

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);
Clearly, the goal of the above code of getting a moving average is not achieved.

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?

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: array handling somewhat awkward

Postby TJ » 25 Nov 2012

Can't say it is related; because I haven't tested your code...

for MultiCharts PowerLanguage, you'll need to put the ONCE in a block:

Code: Select all

once begin
array_setmaxindex(AP, N);
end;

cnbiz850
Posts: 33
Joined: 15 Oct 2012
Has thanked: 1 time
Been thanked: 1 time

Re: array handling somewhat awkward

Postby cnbiz850 » 25 Nov 2012

Found a messy discrepancy related to this issue.

LinRegArray has essentially the same code as LinearReg, indicating that it relies on the assumption that the most recent values are stored at the start of the array (i.e. value1 is toward 1 as in PriceValueArray[Value1] ). The result is, if the recent values are stored at the end, that both slope and angle are opposite to the correct values.

This is true when the size passed into the function is the full size of the array.

waveslider
Posts: 223
Joined: 16 Oct 2011
Has thanked: 66 times
Been thanked: 20 times

Re: array handling somewhat awkward

Postby waveslider » 30 Apr 2013

This affects me certainly.

If you establish an array of 100 values and try to average the last 2 (eg. averagearray(arrayname,2) ) you will not get a correct value unless the array size is the exact size of the length of the average you are calculating (as mentioned above).

That IS awkward, especially since you can't have the array size as an input.


Return to “MultiCharts”