CUSUM TREND BY TRADINGVIEW

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #260344 quote
    brian gilbert
    Participant
    Junior
    GOOD EVENING, HERE I AM AGAIN ASKING FOR YOUR HELP WITH A VERY INTERESTING INDICATOR AVAILABLE ON TRADINGVIEW. IT'S THE "CUSUM TREND" INDICATOR, AND THE LINK AND SCRIPT FOR IT ARE BELOW. I'D LOVE TO TEST IT ON OUR PLATFORM. PERHAPS, IF IT WORKS EFFECTIVELY, IT MIGHT BE OF INTEREST TO OTHERS TOO.
    THANKS IN ADVANCE AND GOOD LUCK TO EVERYONE!
    

    https://www.tradingview.com/script/eiuxOoNG-CUSUM-Trend-AlgoPoint/


    // —————————————————————————–

    // | © AlgoPoint                                                               |

    // | Indicator: CUSUM Trend [AlgoPoint]                                        |

    // | Developer: Harmony Algo & AlgoPoint Collaboration                         |

    // —————————————————————————–


    //@version=5

    indicator(“CUSUM Trend”, shorttitle=“CUSUM Trend”, overlay=true, max_labels_count=500)


    // ==========================================

    // 1. USER INPUTS (LUXALGO STYLE UI)

    // ==========================================

    grp_main = “Main Settings”

    string sensitivity = input.string(“Balanced (Swing)”, “Sensitivity”, options=[“Fast (Day Trade)”, “Balanced (Swing)”, “Slow (Trend)”], group=grp_main, tooltip=“Day Trade produces more signals but has fakeout risks. Trend waits for massive moves.”)

    bool show_dash = input.bool(true, “Show Dashboard”, group=grp_main)


    // ==========================================

    // 2. QUANT VARIABLES & CUSUM ENGINE

    // ==========================================

    var int base_len = 21

    var float k_mult = 0.5

    var float h_mult = 3.0


    if sensitivity == “Fast (Day Trade)”

        base_len := 14

        k_mult := 0.4

        h_mult := 2.0

    else if sensitivity == “Balanced (Swing)”

        base_len := 21

        k_mult := 0.5

        h_mult := 3.0

    else if sensitivity == “Slow (Trend)”

        base_len := 50

        k_mult := 0.6

        h_mult := 4.0


    float hma_base = ta.hma(close, base_len)

    float residual = close  hma_base

    float res_std = ta.stdev(residual, base_len)


    // Prevent division by zero or NA

    res_std := na(res_std) or res_std == 0 ? 0.001 : res_std


    float k_drift = res_std * k_mult

    float h_thresh = res_std * h_mult


    var float bullPressure = 0.0 

    var float bearPressure = 0.0 


    bullPressure := math.max(0, nz(bullPressure[1]) + residual  k_drift)

    bearPressure := math.max(0, nz(bearPressure[1])  residual  k_drift)


    // No-Repaint logic: Only confirm signals when the bar is closed

    bool isConfirmed = barstate.isconfirmed

    bool trig_bull = bullPressure > h_thresh and isConfirmed

    bool trig_bear = bearPressure > h_thresh and isConfirmed


    if trig_bull or trig_bear

        bullPressure := 0.0

        bearPressure := 0.0


    // ==========================================

    // 3. REGIME & TRAILING STOP LOGIC

    // ==========================================

    var int regime = 0 // 1: Bull, -1: Bear, 0: Ranging


    float up_band = hma_base + h_thresh

    float dn_band = hma_base  h_thresh


    // Breakout validation with No-Repaint

    if trig_bull

        regime := 1

    else if trig_bear

        regime := -1

    else if regime == 1 and close < dn_band and isConfirmed

        regime := 0

    else if regime == -1 and close > up_band and isConfirmed

        regime := 0


    // ==========================================

    // 4. VISUALIZATION & ALGOPOINT PALETTE

    // ==========================================

    // AlgoPoint Color Palette

    color col_bull = #2ee319

    color col_bear = #f31623

    color col_range = #7f49de


    color cloud_col = regime == 1 ? color.new(col_bull, 85) : regime == -1 ? color.new(col_bear, 85) : color.new(col_range, 85)


    p_up = plot(up_band, color=color.new(color.gray, 70), title=“Upper Band”)

    p_dn = plot(dn_band, color=color.new(color.gray, 70), title=“Lower Band”)

    fill(p_up, p_dn, color=cloud_col, title=“CUSUM Cloud”)


    // Gradient Candle Coloring based on Pressure

    float max_pressure = math.max(bullPressure, bearPressure)

    float pressure_pct = math.min((max_pressure / h_thresh) * 100, 100)


    color candle_bull = color.from_gradient(pressure_pct, 0, 100, color.new(col_bull, 60), col_bull)

    color candle_bear = color.from_gradient(pressure_pct, 0, 100, color.new(col_bear, 60), col_bear)

    color candle_range = color.new(col_range, 30)


    color candle_col = regime == 1 ? candle_bull : regime == -1 ? candle_bear : candle_range

    barcolor(candle_col, title=“Trend Candles”)


    // Trailing Stop Line

    float trail_stop = regime == 1 ? dn_band : regime == -1 ? up_band : na

    plot(trail_stop, color=candle_col, style=plot.style_circles, linewidth=2, title=“Trailing Stop”)


    // ATR Based Professional Signals

    bool bull_start = regime == 1 and regime[1] != 1

    bool bear_start = regime == -1 and regime[1] != -1


    float y1 = low  (ta.atr(30) * 1.05)

    float y2 = high + (ta.atr(30) * 1.05)


    if bull_start

        label.new(bar_index, y1, “Buy”, color=#018208, style=label.style_label_up, textcolor=color.white, size=size.normal)

    if bear_start

        label.new(bar_index, y2, “Sell”, color=#d12208, style=label.style_label_down, textcolor=color.white, size=size.normal)


    // ==========================================

    // 5. HIBRID DASHBOARD

    // ==========================================

    string status_txt = regime == 1 ? “Bullish” : regime == -1 ? “Bearish” : “Ranging”

    color status_col = regime == 1 ? col_bull : regime == -1 ? col_bear : col_range


    string press_txt = pressure_pct > 80 ? “HIGH (Warning)” : pressure_pct > 50 ? “Rising” : “Stable”

    color press_col = pressure_pct > 80 ? color.orange : color.white


    var table dash = table.new(position.top_right, 2, 4, bgcolor=color.new(color.black, 15), border_width=1, border_color=color.new(color.gray, 80))


    if barstate.islast and show_dash

        table.cell(dash, 0, 0, “CUSUM Panel”, text_color=color.gray, text_size=size.small)

        table.cell(dash, 1, 0, “”, text_color=color.gray, text_size=size.small)


        table.cell(dash, 0, 1, “Market State”, text_color=color.white, text_size=size.small)

        table.cell(dash, 1, 1, status_txt, text_color=status_col, text_size=size.small)


        table.cell(dash, 0, 2, “Breakout Pressure”, text_color=color.white, text_size=size.small)

        table.cell(dash, 1, 2, press_txt, text_color=press_col, text_size=size.small)


        table.cell(dash, 0, 3, “Trailing Stop”, text_color=color.white, text_size=size.small)

        table.cell(dash, 1, 3, regime == 0 ? “Waiting…” : str.tostring(trail_stop, “#.####”), text_color=status_col, text_size=size.small)


    // ==========================================

    // 6. ALERTS & WEBHOOK AUTOMATION

    // ==========================================

    // Standard UI Alerts

    alertcondition(bull_start, title=“🟢 BUY Signal”, message=“CUSUM trend broke upward. Bull trend starting!”)

    alertcondition(bear_start, title=“🔴 SELL Signal”, message=“CUSUM trend broke downward. Bear trend starting!”)


    // Dynamic JSON Alerts for Bot Automation

    if bull_start

        alert(‘{“action”: “buy”, “ticker”: “‘ + syminfo.ticker + ‘”, “price”: ‘ + str.tostring(close) + ‘, “type”: “bull_breakout”}’, alert.freq_once_per_bar_close)

        

    if bear_start

        alert(‘{“action”: “sell”, “ticker”: “‘ + syminfo.ticker + ‘”, “price”: ‘ + str.tostring(close) + ‘, “type”: “bear_breakout”}’, alert.freq_once_per_bar_close)

    #260360 quote
    Iván González
    Moderator
    Master

    Hi! here you have

    //---------------------------------------------------------//
    // PRC_CUSUM Trend [AlgoPoint]
    // version = 0
    // 20.04.2026
    // Iván González @ www.prorealcode.com
    // Sharing ProRealTime knowledge
    //---------------------------------------------------------//
    
    
    // ==========================================
    // 1. PARAMETROS
    // ==========================================
    // sensitivity: 1 = Fast, 2 = Balanced, 3 = Slow
    sensitivity = 2
    
    
    IF sensitivity = 1 THEN
    baseLen = 14
    kMult = 0.4
    hMult = 2.0
    ELSIF sensitivity = 3 THEN
    baseLen = 50
    kMult = 0.6
    hMult = 4.0
    ELSE
    baseLen = 21
    kMult = 0.5
    hMult = 3.0
    ENDIF
    
    
    // ==========================================
    // 2. HMA Y RESIDUALES
    // ==========================================
    hmaBase = HullAverage[baseLen](close)
    residual = close - hmaBase
    resStd = STD[baseLen](residual)
    
    
    IF resStd <= 0 THEN
    resStd = 0.001
    ENDIF
    
    
    kDrift = resStd * kMult
    hThresh = resStd * hMult
    
    
    upBand = hmaBase + hThresh
    dnBand = hmaBase - hThresh
    
    
    // ==========================================
    // 3. MOTOR CUSUM (con warm-up)
    // ==========================================
    once bullPressure = 0
    once bearPressure = 0
    once regime = 0
    
    
    IF barindex < baseLen + 2 THEN
    // Warm-up: evitar contaminar con undefined mientras HMA no tiene datos
    bullPressure = 0
    bearPressure = 0
    regime = 0
    ELSE
    newBull = bullPressure[1] + residual - kDrift
    newBear = bearPressure[1] - residual - kDrift
    
    
    IF newBull > 0 THEN
    bullPressure = newBull
    ELSE
    bullPressure = 0
    ENDIF
    
    
    IF newBear > 0 THEN
    bearPressure = newBear
    ELSE
    bearPressure = 0
    ENDIF
    
    
    // Disparo y transporte del regimen
    IF bullPressure > hThresh THEN
    regime = 1
    bullPressure = 0
    bearPressure = 0
    ELSIF bearPressure > hThresh THEN
    regime = -1
    bullPressure = 0
    bearPressure = 0
    ELSIF regime[1] = 1 AND close < dnBand THEN
    regime = 0
    ELSIF regime[1] = -1 AND close > upBand THEN
    regime = 0
    ELSE
    regime = regime[1]
    ENDIF
    ENDIF
    
    
    // ==========================================
    // 4. SENALES DE ENTRADA
    // ==========================================
    bullStart = regime = 1 AND regime[1] <> 1
    bearStart = regime = -1 AND regime[1] <> -1
    
    
    atrOffset = AverageTrueRange[30] * 1.05
    
    
    IF bullStart THEN
    DRAWARROWUP(barindex, low - atrOffset) COLOURED(1, 130, 8, 255)
    ENDIF
    IF bearStart THEN
    DRAWARROWDOWN(barindex, high + atrOffset) COLOURED(209, 34, 8, 255)
    ENDIF
    
    
    // ==========================================
    // 5. VISUALIZACION
    // ==========================================
    IF regime = 1 THEN
    rCol = 46
    gCol = 227
    bCol = 25
    ELSIF regime = -1 THEN
    rCol = 243
    gCol = 22
    bCol = 35
    ELSE
    rCol = 127
    gCol = 73
    bCol = 222
    ENDIF
    
    
    ColorBetween(upBand, dnBand, rCol, gCol, bCol, 20)
    
    
    IF regime = 1 THEN
    trailStop = dnBand
    ELSIF regime = -1 THEN
    trailStop = upBand
    ELSE
    trailStop = undefined
    ENDIF
    
    
    RETURN upBand COLOURED(120, 120, 120, 80) AS "Upper Band", dnBand COLOURED(120, 120, 120, 80) AS "Lower Band", trailStop COLOURED(rCol, gCol, bCol, 255) STYLE(point, 3) AS "Trailing Stop", hmaBase COLOURED(200, 200, 200, 180) AS "HMA Base"
    
    brian gilbert thanked this post
    TSLA-Diario-1.png TSLA-Diario-1.png
    #260402 quote
    brian gilbert
    Participant
    Junior
    THANK'S A LOT, IVAN, YOU WERE FAST AND EFFICIENT AS USUAL... IN ADDITION TO THANK YOU, I ALSO WANTED TO TELL YOU THAT I'M EXAMINING AN INDICATOR ("ANDEAN OSCILLATOR") THAT SEEMS VERY PROFITABLE TO ME, AND THAT A FRIEND OF MINE WHO HAS USED OUR PLATFORM FOR MANY YEARS LINKED TO ME. I'D LIKE TO SHARE IT WITH OTHER TRADERS, BUT I'M NOT SURE WHERE TO POST IT... CAN YOU PLEASE TELL ME?
    


Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

TradingView to ProRealTime Translation Center

New Reply
Author
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by brian gilbert
15 hours, 49 minutes ago.

Topic Details
Forum: TradingView to ProRealTime Translation Center Forum
Started: 04/19/2026
Status: Active
Attachments: 1 files
Logo Logo
Loading...