TradingView-API icon indicating copy to clipboard operation
TradingView-API copied to clipboard

Chart indicator study periods getting out of sync/corrupting themselves over time?

Open brandonros opened this issue 3 years ago • 0 comments

  1. This might not be your library's fault. It might be the indicator, it might be the TradingView API itself.

I have a program that connects, binds a chart + study + indicator, and listens for updates. The update contains a field on whether a trade should be opened (long, short, etc.) trying to "guess" which direction the market is going.

I write them all to a database as it runs for hours, and then diff them when I restart the app.

warn: 09/07/2022 09:06:00 AM: old direction long -> new direction flat (redraw detected, 3910 @ 09/07/2022 09:31:00 AM)
warn: 09/07/2022 08:54:00 AM: old direction short -> new direction flat (redraw detected, 3910 @ 09/07/2022 09:31:00 AM)
warn: 09/07/2022 08:39:00 AM: old direction long -> new direction flat (redraw detected, 3910 @ 09/07/2022 09:31:00 AM)
warn: 09/07/2022 09:31:00 AM: old direction long -> new direction flat (redraw detected, 3908.75 @ 09/07/2022 09:31:00 AM)
warn: 09/07/2022 09:31:00 AM: old direction flat -> new direction long (redraw detected, 3910.25 @ 09/07/2022 09:31:00 AM)
{
      "timestamp": "09/07/2022 09:06:00 AM",
      "date": "09/07/2022",
      "direction": "flat",
      "lastPrice": 3910,
      "lastPriceTimestamp": "09/07/2022 09:31:00 AM",
      "redraw": true,
      "oldDirection": "long"
    },
{
      "timestamp": "09/07/2022 08:54:00 AM",
      "date": "09/07/2022",
      "direction": "flat",
      "lastPrice": 3910,
      "lastPriceTimestamp": "09/07/2022 09:31:00 AM",
      "redraw": true,
      "oldDirection": "short"
    },

I've even tried stopping the chart, recreating it, etc.

async start() {
    await this.startQuoteInstanceListener()
    await this.startChartListener()
    await this.startIndicatorListener()
    this.interval = setInterval(async () => {
      await this.mutex.runExclusive(async () => {
        const intervalMs = this.periodicInterval
        const now = new Date().getTime()
        const delayMs = intervalMs - (now % intervalMs)
        if (delayMs > 0) {
          await delay(delayMs)
        }
        await this.periodicCallback(this)
      })
    }, this.periodicInterval)
  }

async stop() {
   clearInterval(this.interval)
   await this.indicatorStudy.remove()
   await this.chart.delete()
   await this.quoteMarket.close()
   await this.quoteSession.delete()
  }

The only way to detect redraws correctly as been to kill the entire node.js process and start over (so that it pulls fresh data from the TradingView WebSocket API server I'm guessing?)

Curious if you have any experience with this/have any tips on how to handle this. Restarting the application is kind of expensive timewise. I know that the indicator repaints, maybe that's just the final answer. I'm just not sure why we aren't being sent/aren't picking up the study period indicator update changes without a full reconnect/restart.

brandonros avatar Sep 07 '22 13:09 brandonros