在开发 TradingView 自定义指标时,如果您想将数据通过图表显示出来,可以参考以下步骤:
为您的数据创建一个新的 ticker,并设置服务器返回此 ticker 有效的 SymbolInfo。
配置服务器,确保能够返回此 ticker 的有效历史数据。
使用下面的指标模板,并替换占位符的值,比如名称、描述和代码,同时根据需要调整绘图的默认样式。
以下是一个简单的示例代码:
javascript
{
// 替换 为您的指标名称
name: "",
metainfo: {
"_metainfoVersion": 40,
"id": "@tv-basicstudies-1",
"scriptIdPart": "",
"name": "",
"description": "",
"shortDescription": "",
"is_hidden_study": true,
"is_price_study": true,
"isCustomIndicator": true,
"plots": [{"id": "plot_0", "type": "line"}],
"defaults": {
"styles": {
"plot_0": {
"linestyle": 0,
"visible": true,
"linewidth": 2,
"plottype": 2,
"trackPrice": false,
"transparency": 40,
"color": "#0000FF"
}
},
"precision": 2,
"inputs": {}
},
"styles": {
"plot_0": {
"title": "-- output name --",
"histogramBase": 0,
}
},
"inputs": [],
},
constructor: function() {
this.init = function(context, inputCallback) {
this._context = context;
this._input = inputCallback;
var symbol = "";
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);
var v = PineJS.Std.close(this._context);
return [v];
}
}
}
👉 【点击查看】TradingView 30天 独享 Premium 高级会员账号(完整质保30天售后)
如果您希望显示用户的收益曲线,可以按照以下方法实现:
为新的代码创建一个名称,例如 <code>#EQUITY</code>,当然名称可以自行定义。
配置服务器返回有效的数据,包括最小 SymbolInfo。
确保能够返回商品的历史记录,例如用户数据库中的股权历史记录数据。
使用之前的指标模板,创建或扩展现有的指标文件。
以下是相关代码示例:
javascript
{
name: "Equity",
metainfo: {
"_metainfoVersion": 40,
"id": "Equity@tv-basicstudies-1",
"scriptIdPart": "",
"name": "Equity",
"description": "Equity",
"shortDescription": "Equity",
"is_hidden_study": true,
"is_price_study": true,
"isCustomIndicator": true,
"plots": [{"id": "plot_0", "type": "line"}],
"defaults": {
"styles": {
"plot_0": {
"linestyle": 0,
"visible": true,
"linewidth": 1,
"plottype": 2,
"trackPrice": true,
"transparency": 40,
"color": "#880000"
}
},
"precision": 1,
"inputs": {}
},
"styles": {
"plot_0": {
"title": "Equity value",
"histogramBase": 0,
}
},
"inputs": [],
},
constructor: function() {
this.init = function(context, inputCallback) {
this._context = context;
this._input = inputCallback;
var symbol = "#EQUITY";
this._context.new_sym(symbol, PineJS.Std.period(this._context));
};
this.main = function(context, inputCallback) {
this._context = context;
this._input = inputCallback;
this._context.select_sym(1);
var v = PineJS.Std.close(this._context);
return [v];
}
}
}
以下是通过自定义调色板实现 K 线变色的示例代码:
javascript
__customIndicators = [
{
name: "Bar Colorer Demo",
metainfo: {
_metainfoVersion: 42,
id: "BarColoring@tv-basicstudies-1",
name: "BarColoring",
description: "Bar Colorer Demo",
shortDescription: "BarColoring",
scriptIdPart: "",
is_price_study: true,
is_hidden_study: false,
isCustomIndicator: true,
isTVScript: false,
isTVScriptStub: false,
defaults: {
precision: 4,
palettes: {
palette_0: {
colors: [
{ color: "#FFFF00" },
{ color: "#0000FF" }
]
}
}
},
inputs: [],
plots: [{
id: "plot_0",
type: "bar_colorer",
palette: "palette_0"
}],
palettes: {
palette_0: {
colors: [
{ name: "Color 0" },
{ name: "Color 1" }
],
valToIndex: {
100: 0,
200: 1
}
}
}
},
constructor: function() {
this.main = function(context, input) {
this._context = context;
this._input = input;
var valueForColor0 = 100;
var valueForColor1 = 200;
var result =
Math.random() * 100 % 2 > 1 ?
valueForColor0 : valueForColor1;
return [result];
}
}
}
];
希望本文提供的代码和实现指南能帮助您更好地开发 TradingView 自定义指标。