# 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.&#x20;

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sway-technologies.gitbook.io/sway-charts-pro-scripts-documentation/sway-charts-pro-user-defined-indicators/calculating-data-udi.oncalculate.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
