TradingView 支持自定义指标,不过是把你要定义的指标写成一个 JS 源文件(customIndex.js),放在图表库 static 文件夹下。自定义指标 JS 源代码模板如下:
1 __customIndicators = [ 2 { 3 name: 'ShuBenRSI', 4 metainfo: { 5 '_metainfoVersion': 40, 6 'id': 'ShuBenRSI@tv-basicstudies-1', 7 'scriptIdPart': '', 8 'name': 'ShuBenRSI', 9 'description': 'ShuBenRSI', 10 'shortDescription': 'ShuBenRSI', 11 'is_hidden_study': true, 12 'is_price_study': true, 13 'isCustomIndicator': true, 14 'plots': [{'id': 'plot_0', 'type': 'line'}], 15 'defaults': { 16 'styles': { 17 'plot_0': { 18 'linestyle': 0, 19 'visible': true, 20 'linewidth': 1, 21 'plottype': 2, // 绘制类型为线形图: 2 22 'trackPrice': true, 23 'transparency': 40, 24 'color': '#880000' 25 } 26 }, 27 'precision': 1, // 精度 eg:608.4 28 'inputs': {} 29 }, 30 'styles': { 31 'plot_0': { 32 'title': 'ShuBenRSI', 33 'histogrambase': 0, 34 } 35 }, 36 'inputs': [], 37 }, 38 constructor: function () { 39 this.init = function (context, inputCallback) { 40 this._context = context; 41 this._input = inputCallback; 42 //var symbol = 'p1905'; 43 var symbol = PineJS.Std.ticker(this._context); // 获取所选商品代码 44 this._context.new_sym(symbol, PineJS.Std.period(this._context), PineJS.Std.period(this._context)); 45 }; 46 this.main = function (context, inputCallback) { 47 this._context = context; 48 this._input = inputCallback; 49 this._context.select_sym(1); 50 if(this._context['symbol']['time'] != NaN){ 51 var c = PineJS.Std.close(this._context)-50; 52 var o = PineJS.Std.open(this._context)-50; 53 var l = PineJS.Std.low(this._context)-50; 54 var h = PineJS.Std.high(this._context)-50; 55 console.log('execute custom index!'); 56 console.log('symbol: ', this._context['symbol']['time']); 57 return [o, c]; 58 } 59 60 } 61 } 62 } 63 ];