Using an Array in a Calculated Indicator

The calculation in this example has two input numeric inputs. They are:
This function calculates the weighted average of the three values in the array if the value of the AverageType input is “c_WeightedAverage”. Otherwise, it calculates the average in the normal way.
For example:

VB.NET

If AvgType = c_WeightedAverage Then
Return (ArrayImp(0)*3 + ArrayImp(1)*2 + ArrayImp(2))/6;
Else
Return (ArrayImp(0) + ArrayImp(1) + ArrayImp(2))/3;
End If

C#

If (avgType == c_WeightedAverage)
{
return (ArrayImp[0]*3 + ArrayImp[1]*2 + ArrayImp[2])/6;
}
else
{
return (ArrayImp[0] + ArrayImp[1] + ArrayImp[2])/3;
}

Number of Values in an Array

If you do not know how many values will be in the array, use the Lbound and Ubound functions in the array. For example, if the array contains readings taken over the last weeks, you might not know in advance how many readings are in the array.
The following example shows how to use these functions. In this example, there is one calculation input called InputArray, which is an array of indicator readings.
For example:

VB.NET

If ReadingsList.Length = 0 Then
Return 0
End If
 
Dim total As Integer = 0
Dim divisor As Double = 0
For i As Integer =0 To ReadingsList.Length - 1
total = total + (i + 1) * ReadingsList(i)
divisor = divisor + (i + 1)
Next
Return total/divisor

C#

if (ReadingsList.Length == 0)
{
return 0;
}
 
double total = 0;
double divisor = 0;
for (int i = 0; i <= ReadingsList.Length -1;i++)
{
total = total + (i +1) * (double)ReadingsList[i];
divisor = divisor + (i + 1);
}
return total/divisor;
 
Note: To enter test values for an array function, on the Calculation Input dialog, Test Value tab, enter the values separated by semicolons (e.g. 1;2;3).