# TradingView 自定义技术指标完全指南

By [dlhfhfh](https://paragraph.com/@dlhfhfh) · 2025-04-01

---

TradingView 平台支持用户创建自定义技术指标，让交易者能够根据个人需求灵活设计分析工具。本文将详细介绍如何通过 JavaScript 编写自定义指标，并将其集成到 TradingView 图表中。无论你是初学者还是资深交易者，掌握这一技能都能显著提升你的交易分析能力。

自定义指标的基本步骤
----------

TradingView 的自定义指标需要编写 JavaScript 代码（例如 `customIndex.js` 文件），并将其放置在图表库的 `static` 文件夹下。以下是实现自定义指标的核心步骤：

### 1\. 编写指标源代码

以下是 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` 中实现指标的逻辑。 2. 保存到自定义指标文件 将上述代码保存到一个独立的 JavaScript 文件中，文件结构如下： javascript \_\_customIndicators = \[ // 在此插入你的指标对象 \]; 这个文件可以包含多个指标对象，方便组合使用。 👉 [【点击查看】TradingView 30天 独享 Premium 高级会员账号（完整质保30天售后）](https://bit.ly/TradingView-Pro) 3. 在图表中引入指标 在初始化 TradingView 图表时，通过 `indicators_file_name` 参数引入自定义指标文件： javascript widget = new TradingView.widget({ // 其他配置项 indicators\_file\_name: 'customIndex.js' // 自定义指标文件 // 其他配置项 }); 完成这一步骤后，自定义指标即可在图表中加载并使用。 实战案例：自定义 MACD 红绿柱指标 以下是一个完整的自定义 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 变体，掌握自定义指标的开发技巧都能让你的交易分析更上一层楼。立即尝试编写属于你的技术指标，提升交易效率吧！

---

*Originally published on [dlhfhfh](https://paragraph.com/@dlhfhfh/tradingview)*
