Skip to main content
v1.1.0

Real-time Data

Visualizing real-time trading data with LightningChart Trader is straightforward. Key differences from static display:

  • Data updates must be applied after the chart has been opened
  • Use scroll=True parameter in add_data_point() and add_data_array() to enable automatic scrolling
  • Use set_data_point_limit() to limit visible data points

Basic Real-time Example

from lightningchart_trader import TAChart
import time
from datetime import datetime, timedelta
import random

LICENSE_KEY_PATH = "license_key.txt"

# Load the license key
license_key = open(LICENSE_KEY_PATH).read()
trader = TAChart(license_key=license_key, load_from_storage=False)

# Generate initial data
start_date = datetime(2024, 1, 1)
price = 100.0
initial_data = []

for i in range(100):
change = random.uniform(-3, 3)
open_price = price
close_price = price + change
high_price = max(open_price, close_price) + random.uniform(0, 2)
low_price = min(open_price, close_price) - random.uniform(0, 2)

initial_data.append({
'open': open_price,
'high': high_price,
'low': low_price,
'close': close_price,
'volume': random.randint(1000000, 5000000),
'dateTime': start_date + timedelta(days=i),
})
price = close_price

trader.set_chart_title('Real-Time Trading Chart')
trader.add_simple_moving_average(14).set_line_color("#EEFF00")
trader.set_data(initial_data)

# Limit visible data points for scrolling effect
trader.set_data_point_limit(120)

trader.open()

# Simulate real-time data streaming
current_date = start_date + timedelta(days=100)

for i in range(400):
change = random.uniform(-3, 3)
open_price = price
close_price = price + change
high_price = max(open_price, close_price) + random.uniform(0, 2)
low_price = min(open_price, close_price) - random.uniform(0, 2)

trader.add_data_point(
open=open_price,
high=high_price,
low=low_price,
close=close_price,
volume=random.randint(1000000, 5000000),
date_time=current_date,
scroll=True # Enable auto-scroll to newest data
)

price = close_price
current_date += timedelta(days=1)
time.sleep(1)

Scrolling Charts

For real-time trading applications, use the scroll parameter and set_data_point_limit() to create a rolling window effect.

scroll Parameter

The scroll parameter in add_data_point() and add_data_array() controls automatic scrolling behavior:

trader.add_data_point(
open=open_price,
high=high_price,
low=low_price,
close=close_price,
volume=volume,
date_time=current_date,
scroll=True # Drops oldest data point, scrolls view to newest
)

When scroll=True, the chart automatically:

  • Removes the oldest data point from the visible range
  • Scrolls the view to display the newest data point

set_data_point_limit()

Limits the maximum number of visible data points on the chart:

trader.set_data_point_limit(120)  # Keep only the last 120 data points

This is useful for maintaining consistent chart density and performance in long-running real-time applications.

Rolling Time Window Example

For more precise control over the visible time range, use set_time_range():

Download example dataset

from lightningchart_trader import TAChart
import pandas as pd
import time
from datetime import datetime

LICENSE_KEY_PATH = "license_key.txt"
DATA_PATH = "Tesla.csv"

# Load the license key
license_key = open(LICENSE_KEY_PATH).read()
trader = TAChart(license_key=license_key, load_from_storage=False)

# Load historical data
df = pd.read_csv(DATA_PATH)
data_points = []
for _, row in df.iterrows():
parsed_date = datetime.strptime(row['Date'], '%m/%d/%Y')
data_points.append({
'open': float(row['Open']),
'high': float(row['High']),
'low': float(row['Low']),
'close': float(row['Close']),
'volume': int(row['Volume']),
'dateTime': parsed_date,
})

trader.set_chart_title('TESLA - Rolling 120 Days')
trader.add_simple_moving_average(14).set_line_color("#EEFF00")
trader.set_data(data_points)
trader.open()

VISIBLE_DAYS = 120 # 120 trading days ≈ 6 months

# Simulates real-time applications.
for i in range(VISIBLE_DAYS, len(data_points)):
start_date = data_points[i - VISIBLE_DAYS]['dateTime']
end_date = data_points[i]['dateTime']
trader.set_time_range(start_date, end_date)
time.sleep(1)