How can I create a real-time candlestick bar chart using the lightweight_charts library by TradingView?

I have created a candlestick bar chart which is being displayed on my streamlit application using the following code which I found on the forum here. This code snippet was posted by @dataprofessor, thank you for sharing the same.

import pandas as pd
from lightweight_charts.widgets import StreamlitChart

chart = StreamlitChart(width=900, height=600)

df = pd.read_csv(‘ohlcv.csv’)
chart.set(df)

chart.load()

I would like to convert this static chart into one that prints live, candle by candle with an interval time which can be set by the user. I have tried multiple different way but none of them are working. I would greatly appreciate if someone can help me with achieving this. There is a method in the lightweight_charts library called “chart.update()” which can be used to do this, but that method does not seem to be working on the streamlit application.

I am also looking for a solution to this. I would like to update timeframe using the switcher method. It works well using the normal chart.show() but not on streamlit chart.load()
This is my code
[chart.topbar.switcher(‘timeframe’, (‘1m’, ‘5m’, ‘15m’, ‘1h’, ‘1d’),
default=INITIAL_TIMEFRAME, func=on_timeframe_change)]

Thank you for your response. Are you using python itself or some other language? I didn’t fully understand the piece of code you’ve shared :sweat_smile:

StreamlitChart has limitation see the documentation.

But updating data and plot can be done using experimental_fragment with run_every parameter.

Thanks for your response. You’re right, I didn’t give attention to the second part of the documentation.

However could you please elaborate on the method you’ve mentioned for updating the data and the chart? I tried out multiple different methods none of which worked, but I never came across the method you have mentioned.

There is a complete code that I posted at Issue with st.experimental_fragment functions - #9 by ferdy

@st.experimental_fragment(run_every=120)

It will rerun every 120 sec or 2 minutes.

Thank you. I will try and see how I can incorporate this in my application.

1 Like