TradingView 是一款广受欢迎的图表分析工具,允许用户在平台上添加自定义技术指标。这些指标通过 JavaScript 文件实现,既能满足个性需求,也可以进一步挖掘市场数据。本文将详细介绍如何创建自定义技术指标,助您快速上手。
在 TradingView 中,自定义技术指标的核心是通过一个 JavaScript 文件来定义的。以下是官方提供的基础模板:
javascript
{
name: "", // 替换为您的指标名称
metainfo: {
"_metainfoVersion": 40,
"id": "@tv-basicstudies-1",
"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 = "<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);
var v = PineJS.Std.close(this._context); // 示例:使用收盘价
return [v];
}
}
}
将以上模板保存到 JavaScript 文件中,通常命名为 customIndex.js,并确保文件结构如下:
javascript
__customIndicators = [
/* 在此处填写您的指标对象,由模板生成 */
];
在创建 TradingView 图表组件时,通过 new TradingView.widget() 方法引入自定义指标文件:
javascript
widget = new TradingView.widget({
indicators_file_name: 'customIndex.js' // 自定义指标文件路径
});
至此,您的自定义指标已成功加载至图表中。
以下是一个完整的自定义 MACD 指标的代码示例:
javascript
__customIndicators = [
{
name: "自定义(MACD)",
metainfo: {
"_metainfoVersion": 40,
"id": "macd-custom@tv-basicstudies-1",
"name": "自定义(MACD)",
"description": "自定义(MACD)",
"shortDescription": "MACD_CUSTOM",
"is_price_study": false,
"isCustomIndicator": true,
defaults: {
styles: {
plot_0: {
linewidth: 4, // 红色线宽度
plottype: 1, // 绘制柱状图
color: "#da1155" // 红色
},
plot_3: {
linewidth: 4, // 绿色线宽度
plottype: 1,
color: "#33FF33" // 绿色
}
},
precision: 2, // 保留小数点位数
inputs: {
in_0: 12, // 快速均线周期
in_1: 26, // 慢速均线周期
in_2: 9 // 信号线周期
}
},
plots: [
{"id": "plot_0", "type": "line"},
{"id": "plot_3", "type": "line"}
],
inputs: [
{id: "in_0", name: "快线周期", defval: 12, type: "integer", min: 1, max: 2000},
{id: "in_1", name: "慢线周期", defval: 26, type: "integer", min: 1, max: 2000},
{id: "in_2", name: "信号周期", defval: 9, type: "integer", min: 1, max: 50}
],
},
constructor: function() {
this.main = function(context, inputCallback) {
var fast = PineJS.Std.ema(PineJS.Std.close(context), inputCallback(0), context);
var slow = PineJS.Std.ema(PineJS.Std.close(context), inputCallback(1), context);
var macd = fast - slow;
var signal = PineJS.Std.sma(macd, inputCallback(2), context);
return [macd, signal];
}
}
}
];
上述代码实现了 MACD 指标的自定义,并通过不同颜色区分红绿柱。
👉 【点击查看】TradingView 30天 独享 Premium 高级会员账号(完整质保30天售后)
TradingView 提供了强大的图表分析功能,而自定义技术指标更进一步增强了用户体验与数据可视化能力。只需通过简单的 JavaScript 文件,即可满足多种场景下的个性化需求。您可以利用本文中的模板和示例,轻松创建复杂指标,高效管理市场分析。
如果您仍有疑问,可参考 TradingView 的官方文档进行更深入的学习。