tradingview / charting-library-examples

Examples of Charting Library integrations with other libraries, frameworks and data transports
MIT License
1.32k stars 744 forks source link

Streaming Issue in Intraday #359

Open hiran-iouring opened 8 months ago

hiran-iouring commented 8 months ago

I Have implemented streaming using web socket data based on the [https://www.tradingview.com/charting-library-docs/latest/tutorials/implement_datafeed_tutorial/Streaming-Implementation/] but it suits only historical data, not working for Intraday.

Actual Scenario: Assume have refresh the charts at 12:48pm, with 30m interval, got data upto 12:45pm from getBars and streaming subscribed then upto 12:59:59pm data updates on 12:45 candle.

when time reaches to 01:00 pm new candle plots here instead of 01:15pm. Observed it happens only at when Heikin Ashi selects.

Expected How to implement streaming for Intraday(Minute data) and Historical data?

Note: We are not found any relevant example from official documentation. So, we are taken reference as [https://medium.com/@jonchurch/tv-part-2-b8c2d4211f90]

The below code is core logic for candle update

`function updateBar(streamResponse, sub) { var lastBar = sub.lastBar; let resolution = sub.resolution; if (resolution.includes('D')) { // 1 day in minutes === 1440 resolution = 1440 } else if (resolution.includes('W')) { // 1 week in minutes === 10080 resolution = 10080 } var coeff = resolution 60 1000; // console.log({coeff}) var rounded = Math.floor(streamResponse.tradeTime / coeff) * coeff; var lastBarSec = lastBar.time; var _lastBar;

if (rounded > lastBarSec) {
    // create a new candle, use last close as open **PERSONAL CHOICE**
    _lastBar = {
        time: rounded,
        open: lastBar.close,
        high: lastBar.close,
        low: lastBar.close,
        close: streamResponse.tradePricel
    }

} else {
    // update lastBar candle!
    if (streamResponse.tradePrice < lastBar.low) {
        lastBar.low = streamResponse.tradePrice
    } else if (streamResponse.tradePrice > lastBar.high) {
        lastBar.high = streamResponse.tradePrice
    }

    lastBar.close = streamResponse.tradePrice
    _lastBar = lastBar
}
return _lastBar;

}`

buzzsubash commented 8 months ago

following