Skip to main content

Adding data in code

Trading data can be added to Technical Analysis Chart via code or by reading it from a csv-file. The data itself can be of any source as long as it is given in correct formats. The data source is up to the user to define, as LightningChart does not offer any build-in data providers.

This section explains how to add data to the chart in code. See Reading data from csv to learn about using csv-files.


addDataArray

This method adds an array of data points to the end of the existing data, or to an empty chart. The array entries are of DataPoint type, each consisting of open, high, low, close and dateTime fields, as well as optional volume and openInterest fields.

Technical Analysis Chart automatically sorts the given array according to the dateTime values. However, the method expects the oldest dateTime to be newer than the current newest dateTime on the chart.

const dataArray = [
{ open: 10, high: 12, low: 9, close: 11, dateTime: new Date(2023, 10, 27), volume: 150 },
{ open: 11, high: 13, low: 10, close: 10, dateTime: new Date(2023, 10, 28), volume: 200 },
{ open: 9, high: 12, low: 8, close: 10, dateTime: new Date(2023, 10, 29), volume: 250 },
]
tradingChart.addDataArray(dataArray, false)

The chart.addDataArray() method also allows scrolling the data in real-time applications. Set the scroll parameter of the method true to allow this. This moves the data forward by the number of added data points.

addDataPoint

The chart.addDataPoint() method adds a single data point to the end of the existing data, or to an empty chart. The chart expects the given dateTime value to be newer than the current newest dateTime value on the chart.

tradingChart.addDataPoint({ open: 30, high: 40, low: 20, close: 35, dateTime: new Date(), volume: 350 })

To allow scrolling the data in real-time applications, set the scroll parameter of the method true. This moves the data forward by one data point.

For example, a scrolling real-time application showing maximum of 200 data points before scrolling:

tradingChart.addDataPoint({ open: 30, high: 40, low: 20, close: 35, dateTime: new Date(), volume: 350 }, tradingChart.getPointCount() >= 200)

setData

This method sets an array of new data points to the chart, replacing the old data. The array entries are of DataPoint type, each consisting of open, high, low, close and dateTime fields, as well as optional volume and openInterest fields. The chart automatically sorts the data into ascending order based on dateTime values.

const dataArray = [
{ open: 10, high: 12, low: 9, close: 11, dateTime: new Date(2023, 10, 27), volume: 150 },
{ open: 11, high: 13, low: 10, close: 10, dateTime: new Date(2023, 10, 28), volume: 200 },
{ open: 9, high: 12, low: 8, close: 10, dateTime: new Date(2023, 10, 29), volume: 250 },
]
tradingChart.setData(dataArray)

updateLastDataPoint

This method updates the last (newest) data point of the current set displayed in the chart. It does not update the time stamp of the point, only OHLC-values as well as optional Volume and Open Interest values are affected.

Unlike other data adding methods, updateLastDataPoint() does not use DataPoint types. Instead, open, high, low, close and optional volume and openInterest fields are given separately.

// Updating the last data point with new open, high, low, close and volume values.
tradingChart.updateLastDataPoint(10, 12, 9, 11, 150)

clearData

Any previously pushed data can be cleared with the clearData() method. This clears all trading data including Volume and Open Interest values. All indicators and drawing tools are then automatically updated.

tradingChart.clearData()