1.2 Example UDI code
It's possible to learn how to write UDIs just from these example files, without reading this document. The number at the start of each file, e.g. 01-udi-basic-sma.js, is the suggested order for working through the examples.
The code in the example files and this guide uses one convention, entirely optional. When the code wants to store a value for later use, across different calls to UDI.onCalculate(), it does so by storing the value in the UDI object – rather than using a global variable, for example – and it prefaces the name with $. For example, from the 04-udi-macd.js file:
UDI.$myFastMA = Sway.ta.CreateMovingAverage(maType, {period: fastPeriod});
The $ has no special meaning in Javascript. The example code does this for two reasons: (a) to make clear what is a built-in function or property of the UDI object versus its own private data which the code is storing, and (b) to avoid any possible collision between names of the private variables and names of built-in UDI properties or functions.
Last updated