You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何无法在库函数中传入freq=_freq_alert参数?求二选一告警方案

解决Pine Script自定义库告警频率参数类型不匹配问题

问题场景

你在自定义库中编写了告警函数,传入动态生成的频率参数时触发类型错误:
库函数代码:

export freq_alert(string _txt, string _freq_alert) =>
    alert(_txt, _freq_alert)

调用代码:

freq_alert = alert == 'LIVE' ? alert.freq_once_per_bar : alert.freq_once_per_bar_close

f.freq_alert('test', freq_alert)

错误提示:

Cannot call 'alert' with argument 'freq'='_freq_alert'. An argument of 'series string' type was used but a 'input string' is expected.

错误原因

Pine Script中alert()函数的freq参数要求是编译期确定的输入型字符串(input string),而你通过三元表达式生成的freq_alert是运行时动态变化的序列字符串(series string),两者类型不兼容,导致调用失败。

解决方案

因为无法直接传递动态字符串给alert()的freq参数,所以需要通过条件分支在库函数内部处理不同频率的告警调用:

修改后的库函数(二选一场景)

export freq_alert(string _txt, bool use_once_per_bar) =>
    // 根据布尔参数选择对应的告警频率
    if use_once_per_bar
        alert(_txt, alert.freq_once_per_bar)
    else
        alert(_txt, alert.freq_once_per_bar_close)

对应调用方式

// 先判断要使用的频率类型,生成布尔值
use_once_per_bar = alert == 'LIVE'
// 传入布尔参数调用库函数
f.freq_alert('test', use_once_per_bar)

如果后续需要支持更多频率选项,可以扩展条件分支:

export freq_alert(string _txt, int freq_type) =>
    if freq_type == 0
        alert(_txt, alert.freq_once_per_bar)
    elif freq_type == 1
        alert(_txt, alert.freq_once_per_bar_close)
    elif freq_type == 2
        alert(_txt, alert.freq_once_per_tick)

调用时:

freq_type = alert == 'LIVE' ? 0 : 1
f.freq_alert('test', freq_type)

内容的提问来源于stack exchange,提问作者QuickSilvain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 06:17:14