Skip to main content
v1.2.0

Adding data in code

Trading data can be added to the 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.


set_data

The set_data() method sets an array of new data points to the chart, replacing any existing data. Each data point must include the fields open, high, low, close, and dateTime, with optional fields volume and openInterest.

data_array = [
{'open': 10, 'high': 12, 'low': 9, 'close': 11, 'dateTime': '2023-10-27', 'volume': 150},
{'open': 11, 'high': 13, 'low': 10, 'close': 10, 'dateTime': '2023-10-28', 'volume': 200},
{'open': 9, 'high': 12, 'low': 8, 'close': 10, 'dateTime': '2023-10-29', 'volume': 250},
]

trader.set_data(data_array)

add_data_array

The add_data_array() method appends an array of new data points to the existing dataset. It accepts the same format as set_data(), ensuring the dateTime values are newer than the current latest data point.

data_array = [
{'open': 9, 'high': 10, 'low': 8, 'close': 9.5, 'dateTime': '2023-10-30', 'volume': 300},
{'open': 10, 'high': 11, 'low': 9, 'close': 10.5, 'dateTime': '2023-10-31', 'volume': 350},
]

trader.add_data_array(data_array, scroll=True)

The scroll parameter enables scrolling for real-time applications. When set to True, the oldest data points are dropped as new ones are added.

add_data_point

The add_data_point() method adds a single data point to the chart. The dateTime of the new data point must be newer than the most recent point on the chart.

trader.add_data_point(open=30, high=40, low=20, close=35, dateTime='2023-11-01', volume=400)

For real-time data applications, use the scroll parameter to allow the chart to discard the oldest data points automatically:

trader.add_data_point(open=31, high=41, low=21, close=36, dateTime='2023-11-02', scroll=trader.point_count >= 200)

update_last_data_point

The update_last_data_point() method modifies the latest data point on the chart without changing its dateTime. It updates the open, high, low, close, and optional volume and openInterest values.

# Updating the most recent data point
trader.update_last_data_point(open=35, high=45, low=25, close=40, volume=450)

This method is useful for correcting or refining the last data point without adding new data.

clear_data

The clear_data() method removes all existing data from the chart, including Volume and Open Interest values. Indicators and drawing tools on the chart are automatically updated to reflect the cleared data.

# Clearing all data from the chart
trader.clear_data()