首页 > 解决方案 > Tradingview 定制研究

问题描述

尝试在 Tradingview 中创建自定义研究,但在加载 tradingview 小部件时出现以下错误:

Error: unexpected study id:abcd
    at Function.o.findStudyMetaInfoByDescription (library.4b362457b3a7eceed386.js:29)
    at y.createStudy (library.4b362457b3a7eceed386.js:718)
    at t.<anonymous> (index.jsx:178)
    at charting_library.min.js:1
    at e.fire (library.4b362457b3a7eceed386.js:16)
    at a (library.4b362457b3a7eceed386.js:22)
    at Object.l [as emitOnce] (library.4b362457b3a7eceed386.js:22)
    at ft (library.4b362457b3a7eceed386.js:441)
    at t (library.4b362457b3a7eceed386.js:442)
    at e.fire (library.4b362457b3a7eceed386.js:16)

我一直在遵循https://github.com/tradingview/charting_library/wiki/Creating-Custom-Studies上的说明

我已使用说明中的模板,只是更改为我的研究名称“abcd”

__customIndicators = [
  // *** your indicator object, created from the template ***
  {
    // Replace the <study name> with your study name
    // The name will be used internally by the Charting Library

    name: 'abcd',
    //name: '<study name>',
    metainfo: {
      _metainfoVersion: 40,
      id: 'abcd@tv-basicstudies-1',
      //id: '<study name>@tv-basicstudies-1',
      scriptIdPart: '',
      name: 'abcd',
      //name: '<study name>',

      // This description will be displayed in the Indicators window
      // It is also used as a "name" argument when calling the createStudy method
      description: 'abcd crypto index',
      //description: '<study description>',

      // This description will be displayed on the chart
      shortDescription: 'abcd index',
      //shortDescription: '<short study description>',

      is_hidden_study: true,
      is_price_study: true,
      isCustomIndicator: true,

      plots: [{ id: 'plot_0', type: 'line' }],
      defaults: {
        styles: {
          plot_0: {
            linestyle: 0,
            visible: true,

            // Plot line width.
            linewidth: 2,

            // Plot type:
            //    1 - Histogram
            //    2 - Line
            //    3 - Cross
            //    4 - Area
            //    5 - Columns
            //    6 - Circles
            //    7 - Line With Breaks
            //    8 - Area With Breaks
            plottype: 2,

            // Show price line?
            trackPrice: false,

            // Plot transparency, in percent.
            transparency: 40,

            // Plot color in #RRGGBB format
            color: '#0000FF',
          },
        },

        // Precision of the study's output values
        // (quantity of digits after the decimal separator).
        precision: 2,

        inputs: {},
      },
      styles: {
        plot_0: {
          // Output name will be displayed in the Style window
          title: '-- output name --',
          histogramBase: 0,
        },
      },
      inputs: [],
    },

    constructor: function() {
      this.init = function(context, inputCallback) {
        this._context = context;
        this._input = inputCallback;

        // Define the symbol to be plotted.
        // Symbol should be a string.
        // You can use PineJS.Std.ticker(this._context) to get the selected symbol's ticker.
        // For example,
        //    var symbol = "AAPL";
        //    var symbol = "#EQUITY";
        //    var symbol = PineJS.Std.ticker(this._context) + "#TEST";
        var symbol = '#abcd';
        //var symbol = '<TICKER>';
        this._context.new_sym(symbol, PineJS.Std.period(this._context), PineJS.Std.period(this._context));
      };

      this.main = function(context, inputCallback) {
        this._context = context;
        this._input = inputCallback;

        this._context.select_sym(1);

        // You can use following built-in functions in PineJS.Std object:
        //    open, high, low, close
        //    hl2, hlc3, ohlc4
        var v = PineJS.Std.close(this._context);
        return [v];
      };
    },
  },
];

我在 Tradingview 构造函数中使用了 Indicators_file_name 参数。

然后我尝试像这样创建研究:

tvWidget.onChartReady(() => {
   tvWidget.chart().createStudy('abcd', false, true);
});

我正在使用 UDF 替代方案与服务器端集成。

在为 tradingview 小部件启用调试模式时,我可以在浏览器控制台中看到以下内容:

1 custom indicator loaded.
Datafeed settings received: {"supports_search":true,"supports_group_request":false,"supports_marks":false,"supports_timescale_marks":false,"supports_time":true,"exchanges":[{"value":"NasdaqNM","name":"NasdaqNM","desc":"NasdaqNM"}],"symbols_types":[{"name":"All types","value":""},{"name":"Stock","value":"stock"},{"name":"Index","value":"index"}],"supported_resolutions":["D"]}
library.4b362457b3a7eceed386.js:698 Symbol resolve requested: `HOLD` 
library.4b362457b3a7eceed386.js:698 Symbol resolved: `HOLD`, SymbolInfo in server response {"name":"HOLD","timezone":"America/New_York","minmov":1,"minmov2":0,"pointvalue":1,"session":"24x7","has_intraday":false,"has_no_volume":true,"description":"Portfolio Performance","type":"crypto","supported_resolutions":["D"],"pricescale":1000,"ticker":"HOLD"}
library.4b362457b3a7eceed386.js:698 Symbol info after post-processing: `HOLD`, SymbolInfo {"name":"HOLD","timezone":"America/New_York","minmov":1,"minmov2":0,"pointvalue":1,"session":"24x7","has_intraday":false,"has_no_volume":true,"description":"Portfolio Performance","type":"crypto","supported_resolutions":["1D"],"pricescale":1000,"ticker":"HOLD","base_name":["HOLD"],"legs":["HOLD"],"full_name":"HOLD","pro_name":"HOLD","data_status":"streaming"}
library.4b362457b3a7eceed386.js:339 Event "symbol", arguments: [{"category":"Symbol","label":"HOLD","value":""}]
...
...
...
library.4b362457b3a7eceed386.js:339 Event "onChartReady", arguments: []
library.4b362457b3a7eceed386.js:339 Event "chart_style", arguments: [{"category":"Chart","value":"AREA"}]
index.js:2178 Error: unexpected study id:abcd
    at Function.o.findStudyMetaInfoByDescription (library.4b362457b3a7eceed386.js:29)
    at y.createStudy (library.4b362457b3a7eceed386.js:718)
    at t.<anonymous> (index.jsx:178)
    at charting_library.min.js:1
    at e.fire (library.4b362457b3a7eceed386.js:16)
    at a (library.4b362457b3a7eceed386.js:22)
    at Object.l [as emitOnce] (library.4b362457b3a7eceed386.js:22)
    at ft (library.4b362457b3a7eceed386.js:441)
    at t (library.4b362457b3a7eceed386.js:442)
    at e.fire (library.4b362457b3a7eceed386.js:16)

因此,从这里看来,tradingview 小部件正在加载我的指标文件,但仍然显示“意外的研究 id:abcd”。谁能看到问题?非常感谢任何帮助

标签: tradingview-api

解决方案


  1. 除了“description:”必须与“name:”完全相同以使其正常工作之外,createStudy() 按描述进行搜索,因此请确保正确调用它。

    // referring to your description: 'abcd crypto index' widget.chart().createStudy("abcd crypto index", false, true);

  2. 确保将包含您的文件__customIndicators放入public\charting_library\static\your-file-that-contains-__customIndicators.js

  3. 如果您使用的是 TradeView v1.14,您可能应该使用indicators_file_name参数,而不是custom_indicators_getter(就像文档推荐的那样,这对我不起作用)。


推荐阅读