Trend Filter (Jamallo)

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #262476 quote
    brian gilbert
    Participant
    Average

    Good morning dears,

    I found another interesting indicator on TW—especially regarding its "wave-like" moving average, but also the signals.
    If possible, I’d like to have a version of it that works on our platform... can you help me?
    

    here i copy the link:

    https://www.tradingview.com/script/SPa7FILs-Trend-filter-v2-Jamallo/


    and here there is the code:

    //@version=5

    // The K Line in this indicator applies a composite exponential-Gaussian kernel

    // to a Vervoort Stop output (not raw price, unlike the ENW indicator).

    // The kernel is NOT a true Nadaraya–Watson regression, NOT a true wavelet transform.

    // It is a dual-kernel weighted MA:

    //  – Exponential recency decay (inspired by NW exponential kernel)

    //  – Center-biased Gaussian spatial envelope (inspired by Gaussian wavelet window)

    // Bands use percentile ranking of price deviation from K line,

    // replacing the fixed deviation scaling used in the ENW indicator.

    indicator(“Trend filter v2 [Jamallo]”, overlay=true)


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

    // INPUTS

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


    // — A Line (Vervoort Stop) ————————————————

    group_a   = “A Line”

    vPeriod   = input.int(100,  “ATR Period”,   minval=1,      group=group_a)

    vMultiplier = input.float(1.0, “ATR Multiplier”, minval=0.1, step=0.1, group=group_a)


    // — K Line (Vervoort Stop + Kernel) —————————————

    group_k      = “K Line”

    vPeriodSignal   = input.int(100,  “ATR Period”,   minval=1,       group=group_k)

    vMultiplierSignal = input.float(2.0, “ATR Multiplier”, minval=0.1, step=0.1, group=group_k)

    kernelLenK    = input.int(100,  “Kernel Length”, minval=5, maxval=200,  group=group_k)

    kernelAlphaK   = input.float(0.3, “Kernel Alpha”,  minval=0.1, maxval=100, step=0.1, group=group_k)


    // — Percentile Bands ——————————————————

    group_pct      = “Percentile Bands”

    percentilePeriod   = input.int(14, “Lookback”,  minval=10,      group=group_pct)

    upperPercentile   = input.int(80, “Inner Upper”, minval=50, maxval=99, group=group_pct)

    lowerPercentile   = input.int(80, “Inner Lower”, minval=50, maxval=99, group=group_pct)

    outerUpperPercentile = input.int(95, “Outer Upper”, minval=50, maxval=99, group=group_pct)

    outerLowerPercentile = input.int(95, “Outer Lower”, minval=50, maxval=99, group=group_pct)


    // — A Line Coloring ——————————————————-

    group_colors   = “A Line Coloring”

    colorCodeVervoort = input.bool(true,     “Color Code”, group=group_colors)

    bull_col     = input.color(color.teal,  “Bull”,    inline=”c”, group=group_colors)

    bear_col     = input.color(color.maroon, “Bear”,    inline=”c”, group=group_colors)

    neutral_col    = input.color(color.blue,  “Neutral”,  inline=”c”, group=group_colors)


    // — Display —————————————————————

    group_display = “Display”

    showALine   = input.bool(true, “A Line”,      group=group_display)

    showKLine   = input.bool(true, “K Line”,      group=group_display)

    showRangeBands = input.bool(true, “Inner Bands”,    group=group_display)

    showOuterBands = input.bool(true, “Outer Bands”,    group=group_display)

    showAKCross  = input.bool(true, “A/K Cross Signals”, group=group_display)

    showTrendFill = input.bool(true, “Background Fill”,  group=group_display)


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

    // HEIKIN ASHI SMOOTHED PRICE

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

    var float haClose = na

    var float haOpen = na


    haClose := (open + high + low + close) / 4.0

    haOpen := na(haOpen[1]) ? (open + close) / 2.0 : (haOpen[1] + haClose[1]) / 2.0

    haHigh = math.max(high, math.max(haOpen, haClose))

    haLow  = math.min(low, math.min(haOpen, haClose))


    haPrice = (haOpen + haHigh + haLow + haClose) / 4.0


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

    // VERVOORT STOP FUNCTION

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

    f_vervoort(int period, float multiplier) =>

      v_atr = ta.atr(period)

      loss = multiplier * v_atr


      var float v_stop = 0.0

      var bool trend_is_up = true


      if bar_index == 0

        v_stop := haClose – loss

        trend_is_up := true

      else

        float prev_v_stop = nz(v_stop[1], haClose – loss)

        trend_is_up := haClose > prev_v_stop


        if trend_is_up

          v_stop := math.max(prev_v_stop, haPrice – loss)

        else

          v_stop := math.min(prev_v_stop, haPrice + loss)


        if trend_is_up and haClose < v_stop

          trend_is_up := false

          v_stop := haPrice + loss

        else if not trend_is_up and haClose > v_stop

          trend_is_up := true

          v_stop := haPrice – loss


      v_stop


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

    // COMPOSITE KERNEL FUNCTION

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

    f_compositeKernel(series float src, int len, float alpha) =>

      float sum = 0.0

      float weightSum = 0.0

      float center = len / 2.0


      for i = 0 to len – 1

        float x = float(i)

        float price = src[len – 1 – i]


        float dist = math.abs(x – center)

        float recencyWeight = math.exp(-alpha * (len – 1 – i) / len)

        float localWeight  = math.exp(-math.pow(dist / (len / 3), 2))

        float weight    = recencyWeight * localWeight


        sum += price * weight

        weightSum += weight


      weightSum > 0 ? sum / weightSum : src


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

    // CALCULATIONS

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

    float a = f_vervoort(vPeriod, vMultiplier)


    float k_raw = f_vervoort(vPeriodSignal, vMultiplierSignal)

    float k   = f_compositeKernel(k_raw, kernelLenK, kernelAlphaK)


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

    // A LINE COLORING LOGIC

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

    var int trendState = 0

    aLineSlope = a – nz(a[1], a)


    if haClose > a and aLineSlope > 0

      trendState := 1

    else if haClose < a and aLineSlope < 0

      trendState := -1


    line_color_complex = colorCodeVervoort ? (trendState == 1 ? bull_col : trendState == -1 ? bear_col : neutral_col) : neutral_col


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

    // PERCENTILE BANDS

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

    deviationUp  = math.max(haPrice – k, 0)

    deviationDown = math.max(k – haPrice, 0)


    upperThreshold = ta.percentile_nearest_rank(deviationUp,  percentilePeriod, upperPercentile)

    lowerThreshold = ta.percentile_nearest_rank(deviationDown, percentilePeriod, lowerPercentile)

    upperThreshold2 = ta.percentile_nearest_rank(deviationUp,  percentilePeriod, outerUpperPercentile)

    lowerThreshold2 = ta.percentile_nearest_rank(deviationDown, percentilePeriod, outerLowerPercentile)


    upper = k + upperThreshold

    lower = k – lowerThreshold

    upper2 = k + upperThreshold2

    lower2 = k – lowerThreshold2


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

    // PLOTTING

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

    kColor     = color.new(color.white, 30)

    outerColor   = color.new(color.blue, 50)

    innerBandColor = color.new(color.gray, 50)


    trendFillSimple = showTrendFill ? (a > k ? color.new(color.green, 95) : color.new(color.red, 95)) : na

    upperBandFill  = color.new(#008080, 90)

    lowerBandFill  = color.new(#800020, 90)


    aPlot   = plot(showALine   ? a   : na, “A Line (Vervoort)”,     color=line_color_complex, linewidth=1)

    kPlot   = plot(showKLine   ? k   : na, “K Line (Vervoort + Kernel)”, color=kColor,      linewidth=1)


    upperPlot = plot(showRangeBands ? upper : na, “Upper Band”,    color=innerBandColor, linewidth=1)

    lowerPlot = plot(showRangeBands ? lower : na, “Lower Band”,    color=innerBandColor, linewidth=1)

    upper2Plot = plot(showOuterBands ? upper2 : na, “Outer Upper Band”, color=outerColor,   linewidth=1)

    lower2Plot = plot(showOuterBands ? lower2 : na, “Outer Lower Band”, color=outerColor,   linewidth=1)


    fill(aPlot,   kPlot,   color=trendFillSimple, title=”Trend Fill”)

    fill(upper2Plot, upperPlot, color=upperBandFill,  title=”Upper Band Fill”)

    fill(lowerPlot, lower2Plot, color=lowerBandFill,  title=”Lower Band Fill”)


    // Signals

    longSignal = ta.crossover(a, k)

    shortSignal = ta.crossunder(a, k)


    plotshape(showAKCross and longSignal, “Long Signal”, shape.triangleup,  location.belowbar, color.new(color.green, 0), size=size.small)

    plotshape(showAKCross and shortSignal, “Short Signal”, shape.triangledown, location.abovebar, color.new(color.red,  0), size=size.small)

    ————————————————————————————————————————————————————————————-Thank you and have a good, fresh Sunday!


    #262503 quote
    Iván González
    Moderator
    Legend

    Here you have:

    //----------------------------------------------
    //PRC_Trend Filter v2 Jamallo
    //version = 0
    //13.07.26
    //Ivan Gonzalez @ www.prorealcode.com
    //Traducido de "Trend filter v2 [Jamallo]" (PineScript v5)
    //Autor original: Jamallo
    //Sharing ProRealTime knowledge
    //----------------------------------------------
    // --- Parametros (ajustables en el panel del indicador) ---
    // A Line (Vervoort Stop)
    vPeriodA = 100
    vMultA = 1.0
    // K Line (Vervoort Stop + Kernel)
    vPeriodK = 100
    vMultK = 2.0
    kernLen = 100
    kernAlpha = 0.3
    // Percentile Bands
    pctPeriod = 14
    pctUpIn = 80
    pctLoIn = 80
    pctUpOut = 95
    pctLoOut = 95
    //----------------------------------------------
    // --- Heikin Ashi suavizado ---
    //----------------------------------------------
    once haOpen = (open + close) / 2
    haClose = (open + high + low + close) / 4
    if barindex > 0 then
       haOpen = (haOpen + haClose[1]) / 2
    endif
    haHigh = max(high, max(haOpen, haClose))
    haLow = min(low, min(haOpen, haClose))
    haPrice = (haOpen + haHigh + haLow + haClose) / 4
    //----------------------------------------------
    // --- A Line: Vervoort Stop (vPeriodA, vMultA) ---
    //----------------------------------------------
    lossA = vMultA * averagetruerange[vPeriodA]
    if barindex <= vPeriodA then
       aUp = 1
       aStop = haClose
    else
       prevA = aStop[1]
       if haClose > prevA then
          aUp = 1
       else
          aUp = 0
       endif
       if aUp = 1 then
          aStop = max(prevA, haPrice - lossA)
       else
          aStop = min(prevA, haPrice + lossA)
       endif
       if aUp = 1 and haClose < aStop then
          aUp = 0
          aStop = haPrice + lossA
       elsif aUp = 0 and haClose > aStop then
          aUp = 1
          aStop = haPrice - lossA
       endif
    endif
    aLine = aStop
    //----------------------------------------------
    // --- K Line: Vervoort Stop (vPeriodK, vMultK) ---
    //----------------------------------------------
    lossK = vMultK * averagetruerange[vPeriodK]
    if barindex <= vPeriodK then
       kUp = 1
       kStop = haClose
    else
       prevK = kStop[1]
       if haClose > prevK then
          kUp = 1
       else
          kUp = 0
       endif
       if kUp = 1 then
          kStop = max(prevK, haPrice - lossK)
       else
          kStop = min(prevK, haPrice + lossK)
       endif
       if kUp = 1 and haClose < kStop then
          kUp = 0
          kStop = haPrice + lossK
       elsif kUp = 0 and haClose > kStop then
          kUp = 1
          kStop = haPrice - lossK
       endif
    endif
    kRaw = kStop
    //----------------------------------------------
    // --- Kernel compuesto (recencia exp * envolvente gaussiana) ---
    //----------------------------------------------
    centerK = kernLen / 2.0
    nK = min(kernLen - 1, barindex)
    sumK = 0.0
    wsumK = 0.0
    for jj = 0 to nK do
       xK = kernLen - 1 - jj
       distK = abs(xK - centerK)
       recK = exp(-kernAlpha * jj / kernLen)
       locK = exp(-square(distK / (kernLen / 3.0)))
       wK = recK * locK
       sumK = sumK + kRaw[jj] * wK
       wsumK = wsumK + wK
    next
    if wsumK > 0 then
       kLine = sumK / wsumK
    else
       kLine = kRaw
    endif
    //----------------------------------------------
    // --- Bandas por percentil (nearest-rank manual O(N2)) ---
    //----------------------------------------------
    devUp = max(haPrice - kLine, 0)
    devDn = max(kLine - haPrice, 0)
    neff = min(pctPeriod, barindex + 1)
    // Lado alcista: percentiles pctUpIn (inner) y pctUpOut (outer) de devUp
    resInUp = 100000000.0
    resOutUp = 100000000.0
    for mm = 0 to neff - 1 do
       cntLE = 0
       for ii = 0 to neff - 1 do
          if devUp[ii] <= devUp[mm] then
             cntLE = cntLE + 1
          endif
       next
       if cntLE * 100 >= pctUpIn * neff then
          if devUp[mm] < resInUp then
             resInUp = devUp[mm]
          endif
       endif
       if cntLE * 100 >= pctUpOut * neff then
          if devUp[mm] < resOutUp then
             resOutUp = devUp[mm]
          endif
       endif
    next
    // Lado bajista: percentiles pctLoIn (inner) y pctLoOut (outer) de devDn
    resInDn = 100000000.0
    resOutDn = 100000000.0
    for mm = 0 to neff - 1 do
       cntLE = 0
       for ii = 0 to neff - 1 do
          if devDn[ii] <= devDn[mm] then
             cntLE = cntLE + 1
          endif
       next
       if cntLE * 100 >= pctLoIn * neff then
          if devDn[mm] < resInDn then
             resInDn = devDn[mm]
          endif
       endif
       if cntLE * 100 >= pctLoOut * neff then
          if devDn[mm] < resOutDn then
             resOutDn = devDn[mm]
          endif
       endif
    next
    upper = kLine + resInUp
    lower = kLine - resInDn
    upper2 = kLine + resOutUp
    lower2 = kLine - resOutDn
    //----------------------------------------------
    // --- Color de la A Line segun tendencia ---
    //----------------------------------------------
    if barindex = 0 then
       trendState = 0
    else
       aSlope = aLine - aLine[1]
       if haClose > aLine and aSlope > 0 then
          trendState = 1
       elsif haClose < aLine and aSlope < 0 then
          trendState = -1
       else
          trendState = trendState[1]
       endif
    endif
    if trendState = 1 then
       rA = 0
       gA = 128
       bA = 128
    elsif trendState = -1 then
       rA = 128
       gA = 0
       bA = 0
    else
       rA = 0
       gA = 0
       bA = 255
    endif
    //----------------------------------------------
    // --- Relleno tendencia (A vs K) y bandas ---
    //----------------------------------------------
    if aLine > kLine then
       rF = 0
       gF = 150
       bF = 0
    else
       rF = 200
       gF = 0
       bF = 0
    endif
    colorbetween(aLine, kLine, rF, gF, bF, 25)
    colorbetween(upper2, upper, 0, 128, 128, 25)
    colorbetween(lower, lower2, 128, 0, 32, 25)
    //----------------------------------------------
    // --- Senales de cruce A/K ---
    //----------------------------------------------
    offA = averagetruerange[14]
    if barindex > vPeriodK and aLine crosses over kLine then
       drawarrowup(barindex, low - 0.3 * offA) coloured(0, 200, 0, 255)
    endif
    if barindex > vPeriodK and aLine crosses under kLine then
       drawarrowdown(barindex, high + 0.3 * offA) coloured(200, 0, 0, 255)
    endif
    //----------------------------------------------
    return aLine coloured(rA, gA, bA, 255) as "A Line (Vervoort)", kLine coloured(220, 220, 220, 255) as "K Line", upper coloured(150, 150, 150, 200) as "Upper", lower coloured(150, 150, 150, 200) as "Lower", upper2 coloured(60, 110, 220, 200) as "Outer Upper", lower2 coloured(60, 110, 220, 200) as "Outer Lower"
    


    brian gilbert thanked this post
    TSLA-Diario-2.png TSLA-Diario-2.png
    #262508 quote
    brian gilbert
    Participant
    Average

    THANK YOU VERY MUCH!!

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
1 week, 6 days ago.

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