louisnw01 / lightweight-charts-python

Python framework for TradingView's Lightweight Charts JavaScript library.
MIT License
1.16k stars 215 forks source link

subchart sync not working when zooming in and out or full screen change or trading time change - live data updates #436

Closed esteban2006 closed 1 month ago

esteban2006 commented 3 months ago

Expected Behavior

as the user would zoom in or out in withing the main chart, the subchart should replicate the action mantaining the same position on the time scale, specially.

Current Behaviour

When zooming in or our or going full screen the subchart loose track of the current time scale and position

https://github.com/user-attachments/assets/6bda6e56-d528-4e6c-9a61-c2a165b03924

Reproducible Example

import pandas as pd
from lightweight_charts import Chart

if __name__ == '__main__':
    chart = Chart(inner_width=1, inner_height=0.8)
    chart.time_scale(visible=False)

    chart2 = chart.create_subchart(width=1, height=0.2, sync=True)
    line = chart2.create_line()

    df = pd.read_csv('ohlcv.csv')
    df2 = pd.read_csv('rsi.csv')

    chart.set(df)
    line.set(df2)

    chart.show(block=True)

Environment

- OS: windows
- name='lightweight_charts', version='2.0.1',
- python ='3.1',
digital-stoic commented 3 months ago

+1 Facing same issue here.

nithesh10 commented 2 months ago

How did you fix this ?

digital-stoic commented 2 months ago

Hi all, FYI how I could fix in my case:

The issue was the number of X data points, that must be the same between the candles/lines of all charts and subcharts, regardless of the Xmin and Xmax after a fit().

esteban2006 commented 2 months ago

@digital-stoic could you share your solution ?

digital-stoic commented 2 months ago

@esteban2006 : It's just a data quality thing, not code per se: Ensure that the number of data points on the X axis is the same between your chart and subcharts.

louisnw01 commented 1 month ago

Sync is based on the logical index of a datapoint, not the time. In your case, you need to pad the beggining of your indicator with empty values.