基于颜色变化的 TradingView Pine 警报条件

TradingView Pine alertcondition based on color change

我正在尝试在此 pinescript 上添加 alertcondition,只是为了在 ATR 脚本的颜色从绿色变为红色以及从红色变为绿色时提醒我。

//Crée par J.Dow
    //Double SuperTrend ATR, Le type ATR calcule la volatilité à partir de l'Average True Range (ATR), il est idéal pour le FOREX

    study(title = "Double SuperTrend ATR", shorttitle = "Double SuperTrend ATR", overlay = true)

    //Mode
    Factor=input(title="Super Trend", defval=3, minval=1,maxval = 100)
    ATR=input(title="ATR", defval=12, minval=1,maxval = 100)


    //Super Trend ATR 1
    Up=hl2-(Factor*atr(ATR))
    Dn=hl2+(Factor*atr(ATR))

    TUp=close[1]>TUp[1]? max(Up,TUp[1]) : Up
    TDown=close[1]<TDown[1]? min(Dn,TDown[1]) : Dn

    Trend = close > TDown[1] ? 1: close< TUp[1]? -1: nz(Trend[1],1)

    Tsl1 = Trend==1? TUp: TDown
    Tsl2 = Trend==1? TDown: TUp

    linecolor = Trend == 1 ? green : red



    //Affichage
    P1 = plot(Tsl1, color = linecolor , style = line , linewidth = 1,title = "SuperTrend ATR-1")
    P2 = plot(Tsl2, color = linecolor , style = line , linewidth = 1,title = "SuperTrend ATR-2")
    fill(P1, P2, color = linecolor == red ? red : green)

greenColor = (Trend == 1)
alertcondition(condition=greenColor, title="Buy", message="green buy")
redColor = (Trend != 1)
alertcondition(condition=redColor, title="Sell", message="red sell")

我弄清楚了如何为每种颜色发出警报,但我如何才能将它放在一个通知颜色变化的警报中,例如,带有消息 "color changed"。

Trend与之前的Trend不同时,可以设置提醒 在代码末尾更改此

greenColor = Trend == 1 and Trend[1] != 1
alertcondition(greenColor, title="Buy", message="green buy")
redColor = Trend != 1 and Trend[1] == 1
alertcondition(redColor, title="Sell", message="red sell")

我的理解是您只想检测颜色的变化,这实际上是变量 Trend 从 1 变为 not(1),反之亦然。我推荐:

change_detection_flag = Trend != Trend[1]
alertcondition(change_detection_flag,title="Change",message="color changed")

请注意,这不是按要求查看它之前是绿色现在是红色,还是之前是红色现在是绿色。