Incremental display of OHLC plot

Hi,

I am displaying Stock ticker plots from my database in (near) real time. However, each time I need to Rerun to display the latest data. How can I have an incremental update of the OHLC plot every minute?

Thanks

1 Like

Hi @bhawmik -

If you check out the streamlit hello demo, we have an example of continuously updating a plot. The code snippet looks as follows:

import time
import numpy as np

progress_bar = st.sidebar.progress(0)
status_text = st.sidebar.empty()
last_rows = np.random.randn(1, 1)
chart = st.line_chart(last_rows)

for i in range(1, 101):
    new_rows = last_rows[-1, :] + np.random.randn(5, 1).cumsum(axis=0)
    status_text.text("%i%% Complete" % i)
    chart.add_rows(new_rows)
    progress_bar.progress(i)
    last_rows = new_rows
    time.sleep(0.05)

progress_bar.empty()

# Streamlit widgets automatically run the script from top to bottom. Since
# this button is not connected to any other logic, it just causes a plain
# rerun.
st.button("Re-run")

The most important part of the code is chart.add_rows(new_rows), which adds data to the chart, forcing it to re-render. At the end of the loop you can see time.sleep(0.05); for your case, where you want an update for a long period of time, you could use a non-terminating while loop and/or create an on/off button to start the continuously updating chart.

Hi Randy,

Sorry for the late reply.

I tried using add_rows but with no success.

I have been using plotly_chart graph_objects for drawing OHLC charts for stock tickers.

Here is a section of my code:

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Candlestick(x=dataf[‘DateTime’], open=dataf[‘Open’], high=dataf[‘High’], low=dataf[‘Low’], close=dataf[‘Close’]) )

st.plotly_chart(fig)

This correctly draws my OHLC graph but I want it to refresh with each new data arriving every minute.

I am not sure how I can use st.add_rows() in this situation.

Can you please help me with this?

Thanks

1 Like