Calculating data: UDI.onCalculate()

Your UDI must have a UDI.onCalculate() function. This is called whenever there is new data and, therefore, your indicator needs (or may need) to update its output values.

A simple example of the UDI.onCalculate() function is as follows:

UDI.onCalculate = function(data, output)
{
	// Get the user-selected value of the "period" parameter
	var period = data.parameters["period"];
	
	// The data to be worked on is in the array data.valueData.
	// We then put our output - the average - in output.values, which 
	// is an array of arrays, with entries depending on the number of plots
	for (var i = 0; i < data.valueCount - period; i++) {
		var sum = 0;
		for (var j = 0; j < period; j++) sum += data.valueData[i + j];
		output.values[0][i] = sum / period;
	}
};

The UDI.onCalculate() function receives two parameters: data and output. The data tells the UDI about the chart's data values, and also about things such as the current value of the UDI's settings. The UDI then does its calculations and puts the results into output.

There are two types of UDI:

· If your UDI has a Source field, then it receives a single series of input values, such as close prices or high prices, in data.valueData

· If your UDI does not have a Source field, then it receives the full bar data for the chart, in data.barData

Last updated