Best way to do live update of line indicator
Question
For a chart, there is a method to do live updates using the 'update_from_tick' method. Is there an equivalent to provide corresponding live update of line indicators.
Otherwise, what's the best way to update line-indicators on a chart? I see that there's an 'update' method, but that requires the whole Series to be passed as argument.
Code example
update_from_tick method just combine volume and change ohlc of last_bar. If the indicator you want to update is just a LINE, you don't need to change the ohlc or volume, so you just need to simply pass the the single series to update() method
update_from_tick method just combine volume and change ohlc of last_bar. If the indicator you want to update is just a LINE, you don't need to change the ohlc or volume, so you just need to simply pass the the single series to update() method
~~So what if the latest indicator value (at the same bar) is continuously updated? For example, the current bar is not completed, but it has been drawn in the chart. The value of the same bar has changed. How to update it? Calling update will raise an error, so I can only use the set method to update the entire line...~~
line_name = f"SMA {period}"
if line_name in self._lines:
ma_values = await indicators.calculate_sma(data, period, slice=True)
if not ma_values.empty:
ma_values = ma_values.iloc[-1]
ma_values = pd.Series(ma_values.to_dict())
self._lines[line_name].update(ma_values)
print(ma_values)
"""
time 2025-03-10 15:44:00
close 82099.2
SMA 100 82286.992
dtype: object
"""
this is my code and it just work
calculate and update sma values on every candle tick even if candle not closed yet
it would be easier to solve if you show the codes and errors
line_name = f"SMA {period}" if line_name in self._lines: ma_values = await indicators.calculate_sma(data, period, slice=True) if not ma_values.empty: ma_values = ma_values.iloc[-1] ma_values = pd.Series(ma_values.to_dict()) self._lines[line_name].update(ma_values) print(ma_values) """ time 2025-03-10 15:44:00 close 82099.2 SMA 100 82286.992 dtype: object """this is my code and it just work
calculate and update sma values on every candle tick even if candle not closed yet
it would be easier to solve if you show the codes and errors
Thank you for your reply. Your solution is correct with no issues.
It was probably my incorrect use of the update method or something else that caused the problem — I didn’t fully get the real reason at the time. Thank you!