快速开始

编写并部署你的第一个自定义指标

创建你的第一个指标

打开指标编辑器 — 将指标编辑器组件添加到工作区,或前往指标列表并点击“新建指标”

编写代码 — 从 indicator() 声明开始,定义输入、计算数值并绘制结果

在图表上预览 — 编辑时,数据图表组件会实时渲染指标输出

保存并部署 — 为指标命名并保存到工作区,即可在各图表和工作流中使用

最小示例

每个指标都以 indicator() 调用开始,并至少包含一个 plot():

indicator('Simple Moving Average', { overlay: true });

const length = input.int(20, 'Length');
const sma = ta.sma(close, length);

plot(sma, 'SMA', { color: '#2962FF' });

这将创建一个指标,包含:

  • 一个可在 UI 中配置的 Length 参数(默认 20)
  • 基于收盘价计算的简单移动平均线
  • 绘制为价格图表叠加层的蓝色线条

指标选项

indicator() 函数接受一个标题和一个选项对象:

indicator('My Indicator', {
  overlay: true,        // Plot on price chart (false = separate pane)
  precision: 2,         // Decimal places for values
  format: format.price, // Display format
});
选项类型默认值说明
overlaybooleanfalse绘制在价格图表上,而不是单独的面板
precisionnumberauto小数位数
formatstringformat.inherit显示格式(format.price、format.volume、format.percent)
scalestringauto缩放类型
max_bars_backnumberauto可引用的最大历史 K 线数量
max_lines_countnumberauto最大线条绘制对象数
max_labels_countnumberauto最大标签绘制对象数
max_boxes_countnumberauto最大方框绘制对象数

更完整的示例

以下是一个布林带指标,演示了输入、元组解构、多个绘图和填充:

indicator('Bollinger Bands', { overlay: true });

const length = input.int(20, 'Length');
const mult = input.float(2, 'StdDev');

// ta.bb returns [middle, upper, lower] as a tuple
const [middle, upper, lower] = ta.bb(close, length, mult);

const upperPlot = plot(upper, 'Upper', { color: '#F23645' });
plot(middle, 'Middle', { color: '#2962FF' });
const lowerPlot = plot(lower, 'Lower', { color: '#089981' });

// Fill the area between upper and lower bands
fill(upperPlot, lowerPlot);

添加工作流触发器

trigger() 函数是 TradingGoose 的扩展,可将指标信号接入工作流自动化:

indicator('MACD Crossover Signal');

const [macdLine, signalLine, hist] = ta.macd(close, 12, 26, 9);

plot(hist, 'Histogram', { style: plot.style_histogram });
plot(macdLine, 'MACD');
plot(signalLine, 'Signal');

// Fire a workflow when MACD crosses above signal line
if (ta.crossover(macdLine, signalLine)) {
  trigger('macd_bullish_cross', {
    condition: ta.crossover(macdLine, signalLine),
    input: 'MACD bullish crossover detected',
    signal: 'long',
    position: 'belowBar',
    color: '#00FF00'
  });
}

trigger() API 是 TradingGoose 独有的,在标准 Pine Script 中并不存在。完整文档请参阅触发器指南。

后续步骤