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. Other indicator topics

9.2 Converting dates to chart time

The dates used on the chart depend on the user-configurable time zone. For example, the default setting is GMT+2 with daylight savings on the USA schedule (so that fx markets always open at midnight on Monday).

The chart's time zone is provided via the context data which is available via the data parameter for all functions. For example:

// Get base offset in minutes, and daylight savings mode
var offsetMinutes = data.context.timezone.offset;
var dstMode = data.context.timezone.dstMode; 

The dstMode is one of the values from the UDI.DST enumeration: 0 = no daylight savings, 1 = USA schedule, 2 = European schedule, 3 = Australian schedule

You can convert from the user's chart time to UTC using UDI.convertChartDateToUTC(). This returns an adjusted date as a number of milliseconds, which you can then convert to a Date() object if necessary. For example:

// Convert a value from data.barData.date[] to UTC
var currentBarDate = data.barData.date[0];
var currentAsUTC = UDI.convertChartDateToUTC(currentBarDate);
// Convert to Javascript date object, and output
var dateobj = new Date(currentAsUTC);
console.log("As UTC, bar date is " + dateobj.toISOString());

You can convert in the opposite direction, from UTC to chart time, using UDI.convertUTCToChartDate(). For example:

// Get current date from computer clock (as UTC milliseconds)
var currentDateFromComputerClock = (new Date()).valueOf();
// Convert to chart time 
var currentAsChartTime = UDI.convertUTCToChartDate(currentDateFromComputerClock);

You can also convert UTC to and from other time zones. For example, you can convert UTC to US east coast time, or vice versa.

To do this conversion, you provide a timezone structure, like the one which is included in the UDI's context data. This consists of an offset, in minutes, and a dstMode. For example, the definition of New York time is:

// Base of UTC-5, moving to UTC-4 on the USA daylight savings schedule 
{
	offset: -300,
	dstMode: UDI.DST.USA	// = 1
}

You can then convert from UTC to a different zone using UDI.convertUTCToZone(). For example, converting a date from UTC to US east coast. What this will do is to return the UTC number of milliseconds minus either 300 minutes or 240 minutes depending on whether the USA was in daylight savings at the date you supply:

var usec = UDI.convertUTCToZone(someDate, {offset: -300, dstMode: 1});

Similarly, you can convert from another zone to UTC using UDI.convertZoneToUTC(). This adds either 300 minutes or 240 minutes to the number of milliseconds you supply. For example:

// Convert the example above back to UTC
var utc = UDI.convertZoneToUTC(usec, {offset: -300, dstMode: 1});
Previous9.1 Collecting external dataNext9.3 Multi-timeframe (MTF) indicators