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

为何表格仅第一行触发SMA交叉警报?

Pine Script警报仅触发表格第一行标的的问题排查与修复

问题根源

你代码中调用alert()时使用了freq=alert.freq_once_per_bar参数,这个参数的核心逻辑是单根K线内仅允许触发一次警报。当你通过循环遍历多个标的(ETH、BNB等)时,第一次循环(i=0,对应ETH)会触发警报,后续循环里的alert()调用会因为“同一K线已触发过警报”被系统直接忽略,最终只有第一行标的会触发提示。

修复方案

根据你的需求不同,有两种可行的修复方式:

方式1:每个标的单独触发警报

如果希望每个标的的交叉信号都能独立触发警报,只需将alert()的freq参数改为alert.freq_all,允许单根K线内多次触发警报:

if barstate.islast
    table.cell(table_id=stats, column=0, row=0, text= "Sma" , text_size=size.large, bgcolor=color.new(color.gray, 80), text_color=color.gray)
    
if barstate.isrealtime
    for i = 1 to 40 by 1
        table.cell(table_id=stats, column=0, row=i, text='', text_size=size.large, bgcolor=na, text_color=na)
    if array.size(Sma) > 0
        for i = 0 to array.size(Sma) - 1 by 1      
            table.cell(table_id=stats, column=0, row=i + 2, text=str.tostring(array.get(Sma, i)), text_size=size.large, text_color=array.get(a_01, i) ? color.green : color.red)
            // 修改freq参数为alert.freq_all
            alert(str.tostring(array.get(Sma, i)), freq=alert.freq_all)

方式2:合并所有信号为单次警报

如果不想在同一K线内收到多条警报,可先将所有触发信号的标的信息拼接成一个字符串,循环结束后仅调用一次alert():

if barstate.islast
    table.cell(table_id=stats, column=0, row=0, text= "Sma" , text_size=size.large, bgcolor=color.new(color.gray, 80), text_color=color.gray)
    
if barstate.isrealtime
    for i = 1 to 40 by 1
        table.cell(table_id=stats, column=0, row=i, text='', text_size=size.large, bgcolor=na, text_color=na)
    if array.size(Sma) > 0
        // 初始化空字符串用于拼接警报内容
        alert_msg = ""
        for i = 0 to array.size(Sma) - 1 by 1      
            table.cell(table_id=stats, column=0, row=i + 2, text=str.tostring(array.get(Sma, i)), text_size=size.large, text_color=array.get(a_01, i) ? color.green : color.red)
            // 拼接标的信息到警报字符串
            alert_msg := alert_msg + str.tostring(array.get(Sma, i)) + " "
        // 仅在有信号时触发单次警报
        if alert_msg != ""
            alert(alert_msg, freq=alert.freq_once_per_bar)

内容的提问来源于stack exchange,提问作者Exlusivité Web

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 00:25:17