简体   繁体   English

如何设置tradingview图表库图表自动更新?

[英]How to setup tradingview charting library chart to update automatically?

I am setting up a website displaying chart using tradingview chart library, and managed to setup chart to display from data feed.我正在使用 tradeview 图表库设置一个显示图表的网站,并设法设置图表以从数据馈送中显示。 However once chart is loaded, the chart is not auto updating or refreshing with newer data without reloading webpage.然而,一旦图表被加载,图表不会自动更新或刷新新数据而不重新加载网页。 How do I setup the chart to update automatically (eg. interval 1m, 5m etc)?如何设置图表自动更新(例如间隔 1m、5m 等)? This is the code I used:这是我使用的代码:

    function initOnReady() {
        var widget = window.tvWidget = new TradingView.widget({
            // debug: true, // uncomment this line to see Library errors and warnings in the 

            fullscreen: true,
            symbol: 'AAPL',
            interval: '1D',
            container_id: "tv_chart_container",

            //  BEWARE: no trailing slash is expected in feed URL
            datafeed: new Datafeeds.UDFCompatibleDatafeed("<data feed url>"),
            library_path: "charting_library/",
            locale: getParameterByName('lang') || "en",

            disabled_features: ["use_localstorage_for_settings"],

            enabled_features: ["study_templates"],
            charts_storage_url: 'https://saveload.tradingview.com',
            charts_storage_url: 'http://{$smarty.server.HTTP_HOST}',
            charts_storage_api_version: "1.1",
            client_id: 'tradingview.com',
            user_id: 'public_user_id',
        });

    };

Thanks in advance and appreciate for helps.在此先感谢并感谢您的帮助。

create file named datafeed like so:创建名为 datafeed 的文件,如下所示:

export default {
  onReady: (callback) => {
    console.log("[onReady]: Method call");
    callback({});
  },
  searchSymbols: (userInput, exchange, symbolType, onResultReadyCallback) => {
    console.log("[searchSymbols]: Method call");
  },
  resolveSymbol: (
    symbolName,
    onSymbolResolvedCallback,
    onResolveErrorCallback
  ) => {
    console.log("[resolveSymbol]: Method call", symbolName);
  },
  getBars: async (
    symbolInfo,
    resolution,
    from,
    to,
    onHistoryCallback,
    onErrorCallback,
    firstDataRequest
  ) => {
   
  },
  subscribeBars: (
    symbolInfo,
    resolution,
    onRealtimeCallback,
    subscribeUID,
    onResetCacheNeededCallback
  ) => {
    console.log(
      "[subscribeBars]: Method call with subscribeUID:",
      subscribeUID
    );
  },
  unsubscribeBars: (subscriberUID) => {
    console.log(
      "[unsubscribeBars]: Method call with subscriberUID:",
      subscriberUID
    );
  },
};

and replace it with datafeeds:并将其替换为数据馈送:

import DATAFEED from './datafeed';

function initOnReady() {
        var widget = window.tvWidget = new TradingView.widget({
            // debug: true, // uncomment this line to see Library errors and warnings in the 

            fullscreen: true,
            symbol: 'AAPL',
            interval: '1D',
            container_id: "tv_chart_container",

            //  BEWARE: no trailing slash is expected in feed URL

            datafeed: DATAFEED, // ---> replace here
            library_path: "charting_library/",
            locale: getParameterByName('lang') || "en",

            disabled_features: ["use_localstorage_for_settings"],

            enabled_features: ["study_templates"],
            charts_storage_url: 'https://saveload.tradingview.com',
            charts_storage_url: 'http://{$smarty.server.HTTP_HOST}',
            charts_storage_api_version: "1.1",
            client_id: 'tradingview.com',
            user_id: 'public_user_id',
        });

    };

Notice : Trading view itself manage most actions base on what it needs.注意:交易视图本身根据需要管理大多数操作。 for example if you want to drag the candle chart, trading view calculate the view port and find out how many candle it need's to show then call getBars method in datafeeds.js .例如,如果您想拖动蜡烛图,交易视图计算视口并找出需要显示的蜡烛数量,然后调用datafeeds.js getBars方法。

for see examples: https://github.com/tradingview/charting-library-examples查看示例: https : //github.com/tradingview/charting-library-examples

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM