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

Resolve Js output object Object

Open gnuhel opened this issue 3 years ago • 6 comments

Question

When using update_from_tick I ran into this js error with [object Object]. I tried to update the last bar when a the new tick is within the bar timeframe.

js: Uncaught Error: Cannot update oldest data, last time=[object Object], new time=[object Object]

Is it possible to resolve the object data so I can see what I did wrong?

Code example

from datetime import datetime as dt, timedelta

def _on_tick(self, tickEvent):
    backtester = Functions.backtester()
    if not backtester:
        return

    lastCandle = self.chart.candle_data.iloc[-1] if not self.chart.candle_data.empty else None
    # lastCandle = self.chart._last_bar

    tickData = backtester.getTickData(tickEvent)

    if lastCandle is None:
        self.chart.set(tickData)
    else:
        for i, series in tickData.iterrows():
            lastTime = dt.utcfromtimestamp(lastCandle['time']).replace(second=0, microsecond=0)
            seriesTime = dt.strptime(str(series.name), '%Y-%m-%d %H:%M:%S')

            nextCandleTime = lastTime + timedelta(seconds=self.chartTimeFrame.interval)

            if seriesTime < nextCandleTime:
                # update last bar with tick data
                
                series['time'] = lastCandle['time']
                series['price'] = series['close']

                self.chart.update_from_tick(series, cumulative_volume=True)
            else:
                # create a new bar
                self.chart.update(series)

gnuhel avatar Nov 04 '23 21:11 gnuhel

The error your experiencing typically occurs when trying to set/update with data that occurs chronologically before the data you have already set.

Louis

louisnw01 avatar Nov 04 '23 23:11 louisnw01

I already tried to set the time to the last candle time before using update_from_tick

series['time'] = lastCandle['time']
series['price'] = series['close']

self.chart.update_from_tick(series, cumulative_volume=True)

I am not sure what went wrong.

gnuhel avatar Nov 05 '23 06:11 gnuhel

I already tried to set the time to the last candle time before using update_from_tick


series['time'] = lastCandle['time']

series['price'] = series['close']



self.chart.update_from_tick(series, cumulative_volume=True)

I am not sure what went wrong.

Have you tried times that occur after the last bar?

louisnw01 avatar Nov 05 '23 11:11 louisnw01

After doing some tests its likely your times are in the wrong format; in the libraries current version they should either be datetime objects or nanosecond timestamps; this will change in the next update to millisecond timestamps, and i'll add a check to see if the tick time is less than the last bar time.

Louis

louisnw01 avatar Nov 05 '23 14:11 louisnw01

Nice, thanks for your effort. Hope we can get the update soon ;)

gnuhel avatar Nov 05 '23 16:11 gnuhel

Fixed in the latest version.

Louis

louisnw01 avatar Nov 22 '23 17:11 louisnw01