TradingView 平台支持用户创建自定义技术指标,让交易者能够根据个人需求灵活设计分析工具。本文将详细介绍如何通过 JavaScript 编写自定义指标,并将其集成到 TradingView 图表中。无论你是初学者还是资深交易者,掌握这一技能都能显著提升你的交易分析能力。
TradingView 的自定义指标需要编写 JavaScript 代码(例如 customIndex.js
文件),并将其放置在图表库的 static
文件夹下。以下是实现自定义指标的核心步骤:
以下是 TradingView 官方提供的自定义指标模板,包含基本结构和参数设置:
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": "输出名称", "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]; } } }
在这个模板中,你可以:
修改 name
和 description
定义指标名称和描述。
通过 plots
和 styles
设置指标的图形样式,例如线条颜色和宽度。
在 constructor
中实现指标的逻辑。
将上述代码保存到一个独立的 JavaScript 文件中,文件结构如下:
javascript __customIndicators = [ // 在此插入你的指标对象 ];
这个文件可以包含多个指标对象,方便组合使用。
👉 【点击查看】TradingView 30天 独享 Premium 高级会员账号(完整质保30天售后)
在初始化 TradingView 图表时,通过 indicators_file_name
参数引入自定义指标文件:
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": { // 红色柱状图 "linestyle": 0, "linewidth": 4, "plottype": 1, "trackPrice": false, "transparency": 35, "visible": true, "color": "#da1155" }, "plot_1": { // MACD 线 "linestyle": 0, "linewidth": 1, "plottype": 0, "trackPrice": false, "transparency": 0, "visible": true, "color": "#FFFFFF" }, "plot_2": { // Signal 线 "linestyle": 0, "linewidth": 1, "plottype": 0, "trackPrice": false, "transparency": 0, "visible": true, "color": "#FFFF00" }, "plot_3": { // 绿色柱状图 "linestyle": 0, "linewidth": 4, "plottype": 1, "trackPrice": false, "transparency": 35, "visible": true, "color": "#33FF33" } }, "precision": 2, "inputs": { "in_0": 12, // 快线周期 "in_1": 26, // 慢线周期 "in_3": "close", // 数据源 "in_2": 9 // 信号线周期 } }, "plots": [ {"id": "plot_0", "type": "line"}, {"id": "plot_1", "type": "line"}, {"id": "plot_2", "type": "line"}, {"id": "plot_3", "type": "line"} ], "styles": { "plot_0": {"title": "Histogram", "histogramBase": 0}, "plot_1": {"title": "MACD", "histogramBase": 0}, "plot_2": {"title": "Signal", "histogramBase": 0}, "plot_3": {"title": "Histogram", "histogramBase": 0} }, "inputs": [ {"id": "in_0", "name": "fastLength", "defval": 12, "type": "integer", "min": 1, "max": 2000}, {"id": "in_1", "name": "slowLength", "defval": 26, "type": "integer", "min": 1, "max": 2000}, {"id": "in_3", "name": "Source", "defval": "close", "type": "source", "options": ["open", "high", "low", "close", "hl2", "hlc3", "ohlc4"]}, {"id": "in_2", "name": "signalLength", "defval": 9, "type": "integer", "min": 1, "max": 50} ] }, constructor: function() { this.main = function(context, input) { this._context = context; this._input = input; var close = PineJS.Stdinput(2); var fastEMA = PineJS.Std.ema(context.new_var(close), input(0), context); var slowEMA = PineJS.Std.ema(context.new_var(close), input(1), context); var macd = fastEMA - slowEMA; var signal = PineJS.Std.sma(context.new_var(macd), input(3), context); var hist = macd - signal; return [hist >= 0 ? hist : 0, macd, signal, hist < 0 ? hist : 0]; } } } ];
这个自定义 MACD 指标通过红绿柱区分正负值,并用白线和黄线分别表示 MACD 和信号线,帮助交易者快速识别趋势变化。
通过以上步骤,你可以轻松在 TradingView 上创建和应用自定义技术指标。无论是简单的收盘价曲线,还是复杂的 MACD 变体,掌握自定义指标的开发技巧都能让你的交易分析更上一层楼。立即尝试编写属于你的技术指标,提升交易效率吧!