Liquid Charts API Docs
Liquid Charts Pro Scripts Documentation
Liquid Charts Pro Scripts Documentation
  • Liquid Charts Pro User-Defined Indicators
    • Introduction
      • 1.1 Access to the Liquid Charts scripting framework (UDI vs UDIX)
      • 1.2 Example UDI code
      • 1.3 Adding a UDI to a chart
      • 1.4 Lifecycle
      • 1.5 Alerts
      • 1.6 Indicator-on-indicator
      • 1.7 Technical note: web workers
      • 1.8 Technical note: dates
    • Initialisation: UDI.onInit()
      • 2.1 Return value from UDI.onInit()
      • 2.2 The plots[] array
      • 2.3 The settingsFields[] array
      • 2.4 Context data passed to UDI.onInit()
    • Calculating data: UDI.onCalculate()
      • 3.1 Reading settings fields
      • 3.2 Input data for the UDI
      • 3.3 Updates of the current bar only
      • 3.4 Calculating output data
      • 3.5 Requesting out-of-band updates
      • 3.6 Technical analysis: moving averages, ATR etc
    • Changes to the chart or parameters
      • 4.1 UDI.onContextChange()
      • 4.2 UDI.onParameterChange()
    • Creating drawings, event markers, and bar highlights
      • 5.1 Creating drawings
      • 5.1.1 Drawing properties
      • 5.2 Creating event markers
      • 5.2.1 Click notifications from event markers
      • 5.3 Creating bar highlights
    • Notifying the user
      • 6.1 Creating toast
      • 6.2 Changing the panel background colour
    • Adding HTML to the chart
      • 7.1 Adding HTML
      • 7.2 Positioning the HTML
      • 7.3 Sending messages from the HTML to the UDI
      • 7.4 Sending messages from the UDI to the HTML
    • Recommended trade
      • 8.0 Recommended trade
    • Other indicator topics
      • 9.1 Collecting external data
      • 9.2 Converting dates to chart time
      • 9.3 Multi-timeframe (MTF) indicators
  • Liquid Charts Pro Framework
    • Examples
    • Collections, objects, and properties in the framework
    • Functions in the framework
    • Messages and event handlers
    • Working with candle data
    • Widget HTML and CSS reference
    • Liquid.utils and other helper functions
    • Framework error codes
    • Script contexts
    • Sending mail into Liquid Charts Pro from outside the platform
    • language variables
Powered by GitBook
On this page
  1. Liquid Charts Pro User-Defined Indicators
  2. Calculating data: UDI.onCalculate()

3.6 Technical analysis: moving averages, ATR etc

Many indicators are based on other technical analysis calculations such as moving averages, standard deviation, or values such as ATR or RSI. (For example, an "Awesome Oscillator" is simply a 5-period moving average minus a 34-period moving average.)

UDIs have access to a library, Liquid.ta, of technical analysis calculations. Two of the example UDI files use this library, to calculate a moving average and to build up multiple averages into a MACD calculation.

The Liquid.ta library is documented separately, because it is also available as part of the Liquid Charts Pro scripting framework. UDIs also have access to an Liquid.CandleStore helper.

One of the features of the Liquid.ta library is creating a configurable type of moving average from a user-controlled setting in your UDI. This is illustrated by the MACD example file, and works as follows:

In the settings for your UDI, you include a field with type:"maType". In your UDI code, you read the value of the user field (and typically also of a user-defined period for the average), and use Liquid.ta.CreateMovingAverage() to create the moving average object. For example:

// Read user-controlled maType and period fields from settings 
var maType = data.parameters["maType"];
var period = data.parameters["period"];
// Create a moving average calculation if we don't already have one 
if (!UDI.$myMA) UDI.$myMA = Liquid.ta.CreateMovingAverage(maType, {period: period});
// Load the data into the calculation. If data.currentBarUpdateOnly is set then 
// this automatically becomes an efficient fast update of just the current value.
UDI.$myMA.LoadData(data);

You can pass the data parameter from UDI.onCalculate() directly into the LoadData() of a technical analysis calculation – for both "bar-based" and "value-based" calculations. The LoadData() function automatically checks currentBarUpdateOnly and does an efficient update of only the current value if only the current bar is changing. This is demonstrated by the example above, and also the following instance of a bar-based calculation (ATR):

UDI.onCalculate = function(data, output) {
	// Create an ATR calculation if we don't already have one 
	if (!UDI.$myATR) UDI.$myATR = new Liquid.ta.ATR({period: 14});
	// Pass the data into the ATR calculation. 
	// If data.currentBarUpdateOnly is set then the calculation does a fast call to UpdateCurrent()
	// rather than a full reload of all the data 
	UDI.$myATR.LoadData(data);
};

The Liquid.CandleStore can also be helpful here. You can pass the data parameter from UDI.onCalculate() into a candle store, and it will convert it into an array of candle objects. That array can then be sent into a bar-based technical analysis calculation such as ATR. For example:

UDI.onCalculate = function(data, output) {
	// If we haven't already done so in a previous call, create a candle store
	if (!UDI.$cs) UDI.$cs = new Liquid.CandleStore();
	// Load data into the candle store. This will efficiently update just the current 
	// candle if data.currentBarUpdateOnly = true 
	UDI.$cs.LoadCandles(data);
	// Create an ATR calculation if we haven’t already done so
	if (!UDI.$atr) UDI.$atr = new Liquid.ta.ATR({period: 20});
	// Is this a full (re-)calculation?
	if (!data.currentBarUpdateOnly) {
		// Full recalculation
		// Load the entire candle data from the store into the ATR.
		UDI.$atr.LoadData(UDI.$cs.GetCandleArray());
	} else {
		// Only the current bar is changing.
		// Update the ATR just with the current candle from the store.
		UDI.$atr.UpdateCurrent(UDI.$cs.GetCandle(0));
	}
}}
Previous3.5 Requesting out-of-band updatesNextChanges to the chart or parameters

Last updated 3 months ago