3.4 Calculating output data
You put the results of your UDI's calculations into the output object which is passed to your UDI.onCalculate().
The output has a values member which is an array of arrays. The length, and order, of output.values depends on the plots defined for your indicator. For example:
In this example, output.values will contain 7 sub-arrays. Items [0] and [1] will be for the channel plot; item [2] will be for the line plot; and items [3] to [6] will be for the candle plot.
Each sub-array inside output.values will already have been initialised so that it contains the required number of elements (matching the number of bars on the chart). In other words, the length of each sub-array, such as output.values[0].length, will be the same as data.valueCount. (Therefore, you assign values into the sub-arrays rather than using push() to add data.)
If UDI.onCalculate() is being called for an update of the current bar only, with data.currentBarUpdateOnly set, then the contents of output.values will be the last results you passed back. Typically, you only then need to re-calculate the latest value in each sub-array. For example (not fully efficient!):
Although the framework pre-creates and pre-populates the arrays inside output.values, it's permissible for you to replace them with new arrays instead of writing your values into the existing arrays. This is typically most relevant when working with technical analysis calculations. For example, let's say that you have used the Sway.ta library to calculate a moving average of the input values:
You can then copy the moving average results into a system-provided output array such as [1]:
However, it is both simpler and faster just to take the array from the technical analysis calculation and use that to replace the pre-prepared buffer:
Last updated