【问题标题】:How to setup tradingview charting library chart to update automatically?如何设置tradingview图表库图表自动更新?
【发布时间】:2020-08-30 08:24:25
【问题描述】:

我正在使用 tradingview 图表库建立一个显示图表的网站,并设法设置图表以从数据馈送中显示。但是,一旦加载图表,图表就不会自动更新或使用较新的数据刷新,而无需重新加载网页。如何设置图表自动更新(例如间隔 1m、5m 等)?这是我使用的代码:

    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',
        });

    };

在此先感谢您的帮助。

【问题讨论】:

  • 看看setInterval函数,它可以在给定的时间间隔内重复执行某个函数
  • 想知道有一个设置或配置可以实时更新而不是手动或间隔更新吗?顺便说一句,通过使用 setInterval 应该调用哪个函数来执行更新? (例如 widget.chart()...?)

标签: javascript tradingview-api


【解决方案1】:

像这样创建名为 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
    );
  },
};

并将其替换为数据馈送:

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',
        });

    };

注意:交易视图本身根据需要管理大多数操作。 例如 如果要拖动蜡烛图,交易视图计算视口并找出需要显示多少蜡烛然后调用datafeeds.js 中的getBars 方法。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2017-01-14
    相关资源
    最近更新 更多