指标触发器
通过 trigger() API 将指标信号连接到 TradingGoose 工作流
trigger() 函数是 TradingGoose 对 PineTS 的扩展,可让你的指标在条件满足时触发工作流执行。它弥合了技术分析与工作流自动化之间的差距——指标可以检测到信号,并立即触发操作,例如发送通知、下单交易或运行复杂的多步骤工作流。
语法
trigger(event, options)参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
event | string | 是 | 事件名称标识符 |
options | object | 是 | 触发器配置(见下文) |
选项对象
| 属性 | 类型 | 必填 | 说明 |
|---|---|---|---|
condition | any | 是 | 触发器触发所需满足的真值条件 |
input | string | 是 | 传递给工作流的文本输入(例如,对发生情况的描述) |
signal | 'long' | 'short' | 'flat' | 是 | 交易信号方向 |
position | 'aboveBar' | 'belowBar' | 'inBar' | 否 | 图表上标记的位置(默认:'aboveBar') |
color | string | 否 | 标记颜色(十六进制字符串,例如 '#FF0000') |
事件名称规则
- 必须匹配模式:
/^[a-z][a-z0-9_]{0,63}$/ - 以小写字母开头
- 仅允许小写字母、数字和下划线
- 最多 64 个字符
基础示例
indicator('RSI Trigger Example');
const rsi = ta.rsi(close, 14);
plot(rsi, 'RSI');
hline(70, 'Overbought', '#FF0000', hline.style_dashed);
hline(30, 'Oversold', '#00FF00', hline.style_dashed);
// Fire when RSI crosses above 70 (overbought)
if (ta.crossover(rsi, 70)) {
trigger('rsi_overbought', {
condition: ta.crossover(rsi, 70),
input: 'RSI crossed above 70 — overbought signal',
signal: 'short',
position: 'aboveBar',
color: '#FF0000'
});
}
// Fire when RSI crosses below 30 (oversold)
if (ta.crossunder(rsi, 30)) {
trigger('rsi_oversold', {
condition: ta.crossunder(rsi, 30),
input: 'RSI crossed below 30 — oversold signal',
signal: 'long',
position: 'belowBar',
color: '#00FF00'
});
}多个触发器
一个指标可以定义多个触发事件。每个事件可以连接到不同的工作流:
indicator('Multi-Signal', { overlay: true });
const fast = ta.ema(close, 9);
const slow = ta.ema(close, 21);
const [macdLine, signalLine, hist] = ta.macd(close, 12, 26, 9);
// EMA crossover trigger
if (ta.crossover(fast, slow)) {
trigger('ema_bullish_cross', {
condition: ta.crossover(fast, slow),
input: 'Fast EMA crossed above slow EMA',
signal: 'long',
position: 'belowBar',
color: '#26A69A'
});
}
// MACD histogram reversal trigger
if (hist > 0 && hist[1] <= 0) {
trigger('macd_positive_flip', {
condition: hist > 0 && hist[1] <= 0,
input: 'MACD histogram flipped positive',
signal: 'long',
position: 'belowBar',
color: '#2962FF'
});
}工作原理
- 评估 — 在每根 K 线上,PineTS 会执行你的指标代码,包括其中对
trigger()的调用 - 条件检查 — 针对当前 K 线解析
condition属性。若为假值,该触发器将被静默跳过 - 校验 — 校验事件名称、options 结构、input 与 signal。无效调用会产生警告(而非错误)
- 捕获 — 有效触发器会连同其元数据一并捕获(event、input、signal、position、color、bar time、bar index)
- 工作流分发 — 实时运行时,捕获到的触发器事件会通过 Indicator Trigger 分发到已连接的工作流
连接到工作流
要在工作流中使用指标触发器:
- 创建一个包含
trigger()调用的指标 - 在工作流编辑器中,添加 Indicator Trigger 块作为工作流的起点
- 将其配置为监听该指标的事件
- 工作流会接收触发器的
input文本和signal,作为工作流输入数据
校验警告
无效的触发器调用会产生警告(而非错误),因此指标可继续运行:
| 警告代码 | 原因 |
|---|---|
indicator_trigger_invalid_event | 事件名称不符合要求的格式 |
indicator_trigger_invalid_options | options 参数不是对象 |
indicator_trigger_condition_unresolved | 无法解析条件值 |
indicator_trigger_invalid_input | input 缺失或不是字符串 |
indicator_trigger_invalid_signal | signal 不是 'long'、'short' 或 'flat' |
indicator_trigger_invalid_time | 无法获取 K 线开盘时间 |
trigger() API 是 TradingGoose 专属扩展,标准 Pine Script 或 PineTS 中并不存在。原生 PineTS 执行这些调用时会抛出 trigger is not defined 错误。在 TradingGoose 之外运行导出的指标前,请移除或适配这些调用。