STD Filtered indicator

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #246772 quote
    Stenozar
    Participant
    Master

    Buon pomeriggio, è possibile tradurre questo indicatore che mi pare interessante:

    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
    // © loxx

    //@version=5
    indicator(“STD-Filtered, N-Pole Gaussian Filter [Loxx]”,
    shorttitle=”STDFNPGF [Loxx]”,
    overlay = true)

    import loxx/loxxexpandedsourcetypes/4

    greencolor = #2DD204
    redcolor = #D2042D

    //factorial calc
    fact(int n)=>
    float a = 1
    for i = 1 to n
    a *= i
    a

    //alpha calc
    _alpha(int period, int poles)=>
    w = 2.0 * math.pi / period
    float b = (1.0 – math.cos(w)) / (math.pow(1.414, 2.0 / poles) – 1.0)
    float a = – b + math.sqrt(b * b + 2.0 * b)
    a

    //n-pole calc
    _makeCoeffs(simple int period, simple int order)=>
    coeffs = matrix.new<float>(order + 1, 3, 0.)
    float a = _alpha(period, order)
    for r = 0 to order
    out = nz(fact(order) / (fact(order – r) * fact(r)), 1)
    matrix.set(coeffs, r, 0, out)
    matrix.set(coeffs, r, 1, math.pow(a, r))
    matrix.set(coeffs, r, 2, math.pow(1.0 – a, r))
    coeffs

    //n-pole calc
    _npolegf(float src, simple int period, simple int order)=>
    var coeffs = _makeCoeffs(period, order)
    float filt = src * matrix.get(coeffs, order, 1)
    int sign = 1
    for r = 1 to order
    filt += sign * matrix.get(coeffs, r, 0) * matrix.get(coeffs, r, 2) * nz(filt[r])
    sign *= -1
    filt

    //std filter
    _filt(float src, int len, float filter)=>
    float price = src
    float filtdev = filter * ta.stdev(src, len)
    price := math.abs(price – nz(price[1])) < filtdev ? nz(price[1]) : price
    price

    smthtype = input.string(“Kaufman”, “Heiken-Ashi Better Smoothing”, options = [“AMA”, “T3”, “Kaufman”], group= “Source Settings”)
    srcoption = input.string(“Close”, “Source”, group= “Source Settings”,
    options =
    [“Close”, “Open”, “High”, “Low”, “Median”, “Typical”, “Weighted”, “Average”, “Average Median Body”, “Trend Biased”, “Trend Biased (Extreme)”,
    “HA Close”, “HA Open”, “HA High”, “HA Low”, “HA Median”, “HA Typical”, “HA Weighted”, “HA Average”, “HA Average Median Body”, “HA Trend Biased”, “HA Trend Biased (Extreme)”,
    “HAB Close”, “HAB Open”, “HAB High”, “HAB Low”, “HAB Median”, “HAB Typical”, “HAB Weighted”, “HAB Average”, “HAB Average Median Body”, “HAB Trend Biased”, “HAB Trend Biased (Extreme)”])

    period = input.int(25,’Period’, group = “Basic Settings”)
    order = input.int(5,’Order’, group = “Basic Settings”, minval = 1)

    filterop = input.string(“Gaussian Filter”, “Filter Options”, options = [“Price”, “Gaussian Filter”, “Both”, “None”], group= “Filter Settings”)
    filter = input.float(1, “Filter Devaitions”, minval = 0, group= “Filter Settings”)
    filterperiod = input.int(10, “Filter Period”, minval = 0, group= “Filter Settings”)

    colorbars = input.bool(true, “Color bars?”, group = “UI Options”)
    showSigs = input.bool(true, “Show signals?”, group= “UI Options”)

    kfl=input.float(0.666, title=”* Kaufman’s Adaptive MA (KAMA) Only – Fast End”, group = “Moving Average Inputs”)
    ksl=input.float(0.0645, title=”* Kaufman’s Adaptive MA (KAMA) Only – Slow End”, group = “Moving Average Inputs”)
    amafl = input.int(2, title=”* Adaptive Moving Average (AMA) Only – Fast”, group = “Moving Average Inputs”)
    amasl = input.int(30, title=”* Adaptive Moving Average (AMA) Only – Slow”, group = “Moving Average Inputs”)

    [haclose, haopen, hahigh, halow, hamedian, hatypical, haweighted, haaverage] = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, [close, open, high, low, hl2, hlc3, hlcc4, ohlc4])

    float src = switch srcoption
    “Close” => loxxexpandedsourcetypes.rclose()
    “Open” => loxxexpandedsourcetypes.ropen()
    “High” => loxxexpandedsourcetypes.rhigh()
    “Low” => loxxexpandedsourcetypes.rlow()
    “Median” => loxxexpandedsourcetypes.rmedian()
    “Typical” => loxxexpandedsourcetypes.rtypical()
    “Weighted” => loxxexpandedsourcetypes.rweighted()
    “Average” => loxxexpandedsourcetypes.raverage()
    “Average Median Body” => loxxexpandedsourcetypes.ravemedbody()
    “Trend Biased” => loxxexpandedsourcetypes.rtrendb()
    “Trend Biased (Extreme)” => loxxexpandedsourcetypes.rtrendbext()
    “HA Close” => loxxexpandedsourcetypes.haclose(haclose)
    “HA Open” => loxxexpandedsourcetypes.haopen(haopen)
    “HA High” => loxxexpandedsourcetypes.hahigh(hahigh)
    “HA Low” => loxxexpandedsourcetypes.halow(halow)
    “HA Median” => loxxexpandedsourcetypes.hamedian(hamedian)
    “HA Typical” => loxxexpandedsourcetypes.hatypical(hatypical)
    “HA Weighted” => loxxexpandedsourcetypes.haweighted(haweighted)
    “HA Average” => loxxexpandedsourcetypes.haaverage(haaverage)
    “HA Average Median Body” => loxxexpandedsourcetypes.haavemedbody(haclose, haopen)
    “HA Trend Biased” => loxxexpandedsourcetypes.hatrendb(haclose, haopen, hahigh, halow)
    “HA Trend Biased (Extreme)” => loxxexpandedsourcetypes.hatrendbext(haclose, haopen, hahigh, halow)
    “HAB Close” => loxxexpandedsourcetypes.habclose(smthtype, amafl, amasl, kfl, ksl)
    “HAB Open” => loxxexpandedsourcetypes.habopen(smthtype, amafl, amasl, kfl, ksl)
    “HAB High” => loxxexpandedsourcetypes.habhigh(smthtype, amafl, amasl, kfl, ksl)
    “HAB Low” => loxxexpandedsourcetypes.hablow(smthtype, amafl, amasl, kfl, ksl)
    “HAB Median” => loxxexpandedsourcetypes.habmedian(smthtype, amafl, amasl, kfl, ksl)
    “HAB Typical” => loxxexpandedsourcetypes.habtypical(smthtype, amafl, amasl, kfl, ksl)
    “HAB Weighted” => loxxexpandedsourcetypes.habweighted(smthtype, amafl, amasl, kfl, ksl)
    “HAB Average” => loxxexpandedsourcetypes.habaverage(smthtype, amafl, amasl, kfl, ksl)
    “HAB Average Median Body” => loxxexpandedsourcetypes.habavemedbody(smthtype, amafl, amasl, kfl, ksl)
    “HAB Trend Biased” => loxxexpandedsourcetypes.habtrendb(smthtype, amafl, amasl, kfl, ksl)
    “HAB Trend Biased (Extreme)” => loxxexpandedsourcetypes.habtrendbext(smthtype, amafl, amasl, kfl, ksl)
    => haclose

    src := filterop == “Both” or filterop == “Price” and filter > 0 ? _filt(src, filterperiod, filter) : src

    out = _npolegf(src, period, order)

    out := filterop == “Both” or filterop == “Gaussian Filter” and filter > 0 ? _filt(out, filterperiod, filter) : out

    sig = nz(out[1])

    state = 0
    if (out > sig)
    state := 1
    if (out < sig)
    state := -1

    pregoLong = out > sig and (nz(out[1]) < nz(sig[1]) or nz(out[1]) == nz(sig[1]))
    pregoShort = out < sig and (nz(out[1]) > nz(sig[1]) or nz(out[1]) == nz(sig[1]))

    contsw = 0
    contsw := nz(contsw[1])
    contsw := pregoLong ? 1 : pregoShort ? -1 : nz(contsw[1])

    goLong = pregoLong and nz(contsw[1]) == -1
    goShort = pregoShort and nz(contsw[1]) == 1

    var color colorout = na
    colorout := state == -1 ? redcolor : state == 1 ? greencolor : nz(colorout[1])

    plot(out, “N-Pole GF”, color = colorout, linewidth = 3)
    barcolor(colorbars ? colorout : na)

    plotshape(showSigs and goLong, title = “Long”, color = color.yellow, textcolor = color.yellow, text = “L”, style = shape.triangleup, location = location.belowbar, size = size.tiny)
    plotshape(showSigs and goShort, title = “Short”, color = color.fuchsia, textcolor = color.fuchsia, text = “S”, style = shape.triangledown, location = location.abovebar, size = size.tiny)

    alertcondition(goLong, title = “Long”, message = “STD-Filtered, N-Pole Gaussian Filter [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}”)
    alertcondition(goShort, title = “Short”, message = “STD-Filtered, N-Pole Gaussian Filter [Loxx]: Short\nSymbol: {{ticker}}\nPrice: {{close}}”)

    #247707 quote
    Iván González
    Moderator
    Master

    Ciao! Ti darò la mia conversione fino a N=4. Devo ancora creare un pezzo di codice che emuli la funzione TW che consente N poli gaussiani.

    // ----------------------------------------//
    //PRC_NPole Gaussian Filter
    // Indicador: Filtro Gaussiano N-Polo + STD
    //version = 0
    //28.05.2025
    //Iván González @ www.prorealcode.com
    //Sharing ProRealTime knowledge
    // ----------------------------------------//
    // ---------- PARÁMETROS DE USUARIO 
    //----------------------------------------//
    NPole = 4//input(4, "Nº de Polos (2 a 4)", 2, 4)
    Period = 25//input(25, "Periodo del filtro gaussiano", 2)
    FilterMode = 3//input(3, "Modo de filtrado: 1=Precio, 2=Filtro, 3=Ambos", 0, 3)
    FilterStrength = 1// input(1, "Multiplicador de desviación", 0)
    FilterPeriod = 10//input(10, "Periodo de desviación", 1)
    ATRPeriod = 14//input(14, "Periodo ATR para señal", 1)
    //----------------------------------------//
    // ---------- VARIABLES BÁSICAS
    //----------------------------------------//
    src = close
    atr = AverageTrueRange[ATRPeriod](close)
    //----------------------------------------//
    // ---------- FILTRO PREVIO SOBRE FUENTE 
    //----------------------------------------//
    IF (FilterMode = 1 OR FilterMode = 3) AND FilterStrength > 0 AND BarIndex > FilterPeriod THEN
    iprice = src
    devSrc = STD[FilterPeriod](src)
    filtdevSrc = FilterStrength * devSrc
    
    IF ABS(iprice - iprice[1]) < filtdevSrc THEN
    iprice = iprice[1]
    ELSE
    iprice = iprice
    ENDIF
    ELSE
    iprice = src
    ENDIF
    //----------------------------------------//
    // ---------- FILTRO GAUSSIANO N-POLO 
    //----------------------------------------//
    Series = iprice
    Period = MAX(Period, 2)
    
    IF BarIndex = 0 THEN
    w = 2 * 3.141592654 / Period
    w = 180 * w / 3.141592654
    
    IF NPole = 2 THEN
    b = (1 - COS(w)) / (1.41421 - 1)
    ELSIF NPole = 3 THEN
    b = (1 - COS(w)) / (1.25992 - 1)
    ELSIF NPole = 4 THEN
    b = (1 - COS(w)) / (1.18920 - 1)
    ENDIF
    
    aa = -b + SQRT(b * b + 2 * b)
    a1 = 1 - aa
    
    a12 = a1 * a1
    a13 = a12 * a1
    a14 = a12 * a12
    a2 = aa * aa
    a3 = a2 * aa
    a4 = a2 * a2
    
    y1 = Series
    y2 = y1
    y3 = y2
    y4 = y3
    ENDIF
    
    IF NPole = 2 THEN
    y = a2 * Series + 2 * a1 * y1 - a12 * y2
    y2 = y1
    y1 = y
    ELSIF NPole = 3 THEN
    y = a3 * Series + 3 * a1 * y1 - 3 * a12 * y2 + a13 * y3
    y3 = y2
    y2 = y1
    y1 = y
    ELSIF NPole = 4 THEN
    y = a4 * Series + 4 * a1 * y1 - 6 * a12 * y2 + 4 * a13 * y3 - a14 * y4
    y4 = y3
    y3 = y2
    y2 = y1
    y1 = y
    ENDIF
    
    AFR = y
    //----------------------------------------//
    // ---------- FILTRO POSTERIOR 
    //----------------------------------------//
    IF (FilterMode = 2 OR FilterMode = 3) AND FilterStrength > 0 AND BarIndex > FilterPeriod THEN
    iiprice = AFR
    dev = STD[FilterPeriod](AFR)
    filtdev = FilterStrength * dev
    
    IF ABS(iiprice - iiprice[1]) < filtdev THEN
    iiprice = iiprice[1]
    ELSE
    iiprice = iiprice
    ENDIF
    ELSE
    iiprice = AFR
    ENDIF
    //----------------------------------------//
    // ---------- LÓGICA DE SEÑALES 
    //----------------------------------------//
    sig = iiprice[1]
    
    state = 0
    IF iiprice > sig THEN
    state = 1
    ELSIF iiprice < sig THEN
    state = -1
    ENDIF
    
    pregoLong = iiprice > sig AND (iiprice[1] < sig[1] OR iiprice[1] = sig[1])
    pregoShort = iiprice < sig AND (iiprice[1] > sig[1] OR iiprice[1] = sig[1])
    
    IF BarIndex = 0 THEN
    contsw = 0
    ENDIF
    IF pregoLong THEN
    contsw = 1
    ELSIF pregoShort THEN
    contsw = -1
    ELSE
    contsw = contsw[1]
    ENDIF
    
    goLong = pregoLong AND contsw[1] = -1
    goShort = pregoShort AND contsw[1] = 1
    //----------------------------------------//
    // ---------- COLORES DINÁMICOS 
    //----------------------------------------//
    
    IF state = 1 THEN
    red = 50
    green = 200
    blue = 50
    ELSIF state = -1 THEN
    red = 255
    green = 75
    blue = 100
    ENDIF
    //----------------------------------------//
    // ---------- REPRESENTACIÓN VISUAL 
    //----------------------------------------//
    IF goLong THEN
    drawarrowup(barindex, low - 0.15 * atr) coloured("blue")
    ENDIF
    
    IF goShort THEN
    drawarrowdown(barindex, high + 0.15 * atr) coloured("fuchsia")
    ENDIF
    //----------------------------------------//
    return iiprice coloured(red, green, blue) style(line, 3)
    #247848 quote
    Stenozar
    Participant
    Master

    Grazie Ivan, riesci a completare la traduzione?

    Grazie

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

STD Filtered indicator


ProBuilder: Indicatori & Strumenti Personalizzati

New Reply
Author
author-avatar
Stenozar @stenozar Participant
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by Stenozar
8 months ago.

Topic Details
Forum: ProBuilder: Indicatori & Strumenti Personalizzati
Language: Italian
Started: 05/04/2025
Status: Active
Attachments: No files
Logo Logo
Loading...