lightweight-charts-python icon indicating copy to clipboard operation
lightweight-charts-python copied to clipboard

Best way to do live update of line indicator

Open alpaalpa opened this issue 10 months ago • 4 comments

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


alpaalpa avatar Mar 07 '25 02:03 alpaalpa

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

tedigom52 avatar Mar 08 '25 01:03 tedigom52

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...~~

HEUDavid avatar Mar 10 '25 05:03 HEUDavid

    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

tedigom52 avatar Mar 10 '25 06:03 tedigom52

    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!

HEUDavid avatar Mar 10 '25 09:03 HEUDavid