创建自定义指标:TradingView 中文开发指南
April 1st, 2025

如何将您的数据显示为图表指标

想要在 TradingView 图表上展示自定义数据,例如独特的指标?以下是实现这一目标的简明步骤指南,帮助您快速上手。

操作步骤

  1. 创建专属数据代码为您的数据生成一个新的 ticker(数据代码),并确保服务器返回该 ticker 的有效 SymbolInfo 信息。

  2. 配置历史数据设置服务器以提供该 ticker 的历史数据,确保数据完整且可调用。

  3. 应用指标模板使用以下代码模板,替换占位符内容(如名称、描述和代码),并根据需要调整绘图样式,例如线条颜色或宽度。

以下是一个基础模板示例:

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 = "自定义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]; } } }

通过调整 symbol 和绘图参数,您可以轻松实现数据的可视化。

实用示例

获取其他商品代码的数据

假设您想在图表上展示用户的收益曲线,以下是具体实现方法:

  • 定义新代码创建一个独特的代码名称,例如 #EQUITY,用于标识收益数据。

  • 服务器支持修改服务器逻辑,确保其识别该代码并返回对应的 SymbolInfo 和历史数据。

  • 数据准备服务器需从数据库提取收益历史记录,并以标准格式返回,类似普通商品(如 AAPL)的数据结构。

  • 创建指标使用模板生成指标文件,示例如下:

javascript { name: "Equity", metainfo: { "_metainfoVersion": 40, "id": "Equity@tv-basicstudies-1", "name": "Equity", "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": 1, "plottype": 2, "trackPrice": true, "transparency": 40, "color": "#880000" } }, "precision": 1, "inputs": {} }, "styles": { "plot_0": { "title": "收益值", "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]; } } }

👉 【点击查看】TradingView 30天 独享 Premium 高级会员账号(完整质保30天售后)

实现 K 线动态变色

通过自定义指标,您还可以为 K 线添加动态颜色效果。以下是一个简单示例:

javascript __customIndicators = [{ name: "Bar Colorer Demo", metainfo: { "_metainfoVersion": 42, "id": "BarColoring@tv-basicstudies-1", "name": "BarColoring", "description": "K线变色演示", "shortDescription": "变色K线", "is_price_study": true, "is_hidden_study": false, "isCustomIndicator": true, "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: "颜色 0" }, { name: "颜色 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]; } } }];

此代码通过随机选择颜色值,为 K 线赋予动态变色效果,增强图表的可视化表现力。

Subscribe to xkcfbtzawlw
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from xkcfbtzawlw

Skeleton

Skeleton

Skeleton