Tradingview定制研究

时间:2019-03-11 23:35:59

标签: tradingview-api

尝试在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构造函数中使用了indicator_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”。有人可以看到问题吗?任何帮助表示赞赏

4 个答案:

答案 0 :(得分:1)

  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(如文档建议的那样,对我不起作用) )。

答案 1 :(得分:1)

在哪里存储自定义指标对象:

widget = new TradingView.widget({

  //...

  custom_indicators_getter: function (PineJS) {
    return Promise.resolve([
      {
        // Replace the <study name> with your study name
        // The name will be used internally by the Charting Library
        name: "<study name>",
        metainfo: {
          _metainfoVersion: 40,
          id: "<study name>@tv-basicstudies-1",
          scriptIdPart: "",
          name: "<study name>",
          description: "<study description>",
          //...
        },
      },
      //...
    ]);
  },

  //...
});

然后,将研究添加到图表渲染中:

widget.onChartReady(function() {
    widget.chart().createStudy('<study description>', false, true);
});

在您的情况下是:

widget.onChartReady(function() {
    widget.chart().createStudy('abcd crypto index', false, true);
});

答案 2 :(得分:0)

我知道这是metainfo部分中的“ description”参数,该参数应与createStudy方法中的“ name”参数匹配。当这两个匹配时,一切对我有用

答案 3 :(得分:0)

不知道这是否已经解决,但是@Mats的回答给了我可靠的线索。

我要验证脚本是否正确加载的方法是调用tvWidget.getStudiesList()函数,并查看我的自定义脚本是否位于数组中靠近底部的位置。

该调用的麻烦是在哪里运行它。我欺骗了tvWidget var,并将其存储在全局范围内,然后在控制台中运行了该命令。找到脚本后,只需复制数组中的名称,然后在createStudy方法中使用它即可。

希望这对某人有帮助