Traduzione codice TW Hull Butterfly

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #243946 quote
    Msport71Msport71
    Participant
    Senior

    Buongiorno,

    chiedo cortese traduzione del seguente codice che mi sembra interessante.

     

    Grazie per l’aiuto.

    https://it.tradingview.com/script/xtuVIaa8-Hull-Butterfly-Oscillator-LuxAlgo/

     

    // This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
    // © LuxAlgo

    //@version=5
    indicator(“Hull Butterfly Oscillator [LuxAlgo]”, “Hull Butterfly Oscillator [LuxAlgo]”)
    //—————————————————————————–}
    //Settings
    //—————————————————-a————————-{
    length = input(14)

    mult = input(2., ‘Levels Multiplier’)

    src = input(close)

    //Style
    bull_css_0 = input.color(color.new(#0cb51a, 50), ‘Bullish Gradient’
    , inline = ‘inline0’
    , group = ‘Style’)

    bull_css_1 = input.color(#0cb51a, ”
    , inline = ‘inline0’
    , group = ‘Style’)

    bear_css_0 = input.color(color.new(#ff1100, 50), ‘Bearish Gradient’
    , inline = ‘inline1’
    , group = ‘Style’)

    bear_css_1 = input.color(#ff1100, ”
    , inline = ‘inline1’
    , group = ‘Style’)

    //—————————————————————————–}
    //Normalization variables
    //—————————————————————————–{
    var short_len = int(length / 2)
    var hull_len = int(math.sqrt(length))

    var den1 = short_len * (short_len + 1) / 2
    var den2 = length * (length + 1) / 2
    var den3 = hull_len * (hull_len + 1) / 2

    //—————————————————————————–}
    //Hull coefficients
    //—————————————————————————–{
    var lcwa_coeffs = array.new_float(hull_len, 0)
    var hull_coeffs = array.new_float(0)

    if barstate.isfirst
    //Linearly combined WMA coeffs
    for i = 0 to length-1
    sum1 = math.max(short_len – i, 0)
    sum2 = length – i

    array.unshift(lcwa_coeffs, 2 * (sum1 / den1) – (sum2 / den2))

    //Zero padding of linearly combined WMA coeffs
    for i = 0 to hull_len-2
    array.unshift(lcwa_coeffs, 0)

    //WMA convolution of linearly combined WMA coeffs
    for i = hull_len to array.size(lcwa_coeffs)-1
    sum3 = 0.
    for j = i-hull_len to i-1
    sum3 += array.get(lcwa_coeffs, j) * (i – j)

    array.unshift(hull_coeffs, sum3 / den3)

    //—————————————————————————–}
    //Hull squeeze oscillator
    //—————————————————————————–{
    var os = 0
    var len = array.size(hull_coeffs)-1
    hma = 0.
    inv_hma = 0.

    for i = 0 to len
    hma += src[i] * array.get(hull_coeffs, i)
    inv_hma += src[len-i] * array.get(hull_coeffs, i)

    hso = hma – inv_hma

    cmean = ta.cum(math.abs(hso)) / bar_index * mult

    os := ta.cross(hso, cmean) or ta.cross(hso, -cmean) ? 0
    : hso < hso[1] and hso > cmean ? -1
    : hso > hso[1] and hso < -cmean ? 1
    : os

    //—————————————————————————–}
    //Plot
    //—————————————————————————–{
    //Colors
    css0 = color.from_gradient(hso, 0, cmean, bull_css_0, bull_css_1)
    css1 = color.from_gradient(hso, -cmean, 0, bear_css_1, bear_css_0)
    css = hso > 0 ? css0 : css1

    //Oscillator line/histogram
    plot(hso, ‘Hull Butterfly’, css
    , style = plot.style_histogram)

    plot(hso, ‘Hull Butterfly’, chart.fg_color)

    //Dots
    plot(os > os[1] and os == 1 ? hso : na, ‘Bullish Dot’
    , bull_css_1
    , 2
    , plot.style_circles)

    plot(os < os[1] and os == -1 ? hso : na, ‘Bearish Dot’
    , bear_css_1
    , 2
    , plot.style_circles)

    //Levels
    plot(cmean, color = color.gray, editable = false)

    plot(cmean / 2, color = color.gray, editable = false)

    plot(-cmean / 2, color = color.gray, editable = false)

    plot(-cmean, color = color.gray, editable = false)

    //—————————————————————————–}

    #243949 quote
    Iván GonzálezIván González
    Moderator
    Legend
    //--------------------------------------------//
    // PRC_Hull Butterfly Oscillator
    // Version = 0
    // 17.02.25
    // Translated by Iván González @ www.prorealcode.com
    // Sharing ProRealTime knowledge
    //--------------------------------------------//
    //--------------------------------------------//
    // Input Parameters
    //--------------------------------------------//
    oscPeriod = 14  // Oscillator length
    mult = 2.0  // Level multiplier
    src = close  // Data source (closing price)
    
    //--------------------------------------------//
    // Normalized Variable Definitions
    //--------------------------------------------//
    shortLen = round(oscPeriod / 2)
    hullLen = round(sqrt(oscPeriod))
    
    den1 = shortLen * (shortLen + 1) / 2
    den2 = oscPeriod * (oscPeriod + 1) / 2
    den3 = hullLen * (hullLen + 1) / 2
    
    //--------------------------------------------//
    // Hull Coefficients Calculation
    //--------------------------------------------//
    
    // Calculation of linearly combined WMA coefficients
    FOR i = 0 TO oscPeriod-1 DO
    sum1 = max(shortLen - i, 0)
    sum2 = oscPeriod - i
    $lcwaCoeffs[i] = 2 * (sum1 / den1) - (sum2 / den2)
    NEXT
    
    // Zero padding for alignment
    FOR i = 0 TO hullLen-2 DO
    $lcwaCoeffs[i] = 0
    NEXT
    
    // Convolution of the linearly combined WMA
    FOR i = hullLen TO oscPeriod-1 DO
    sum3 = 0
    FOR j = i-hullLen TO i-1 DO
    sum3 = sum3 + $lcwaCoeffs[j] * (i - j)
    NEXT
    $hullCoeffs[i] = sum3 / den3
    NEXT
    
    //--------------------------------------------//
    // Hull Squeeze Oscillator Calculation
    //--------------------------------------------//
    hma = 0
    invHma = 0
    len = oscPeriod - 1
    
    FOR i = 0 TO len DO
    hma = hma + src[i] * $hullCoeffs[i]
    invHma = invHma + src[len-i] * $hullCoeffs[i]
    NEXT
    
    hso = hma - invHma
    
    // Cumulative mean calculation for level normalization
    cmean = CUMSUM(ABS(hso)) / barindex * mult
    
    // Oscillator signal calculation based on crosses over levels
    IF (hso CROSSES OVER cmean OR hso CROSSES UNDER cmean) OR (hso CROSSES OVER -cmean OR hso CROSSES UNDER -cmean) THEN
    os = 0
    ELSIF hso < hso[1] AND hso > cmean THEN
    os = -1
    ELSIF hso > hso[1] AND hso < -cmean THEN
    os = 1
    ENDIF
    
    //--------------------------------------------//
    // Define Colors
    //--------------------------------------------//
    IF hso > 0 THEN
    r = 0
    g = 181
    b = 26  // Green
    ELSE
    r = 255
    g = 17
    b = 0  // Red
    ENDIF
    
    //--------------------------------------------//
    // Draw Reversal Points
    //--------------------------------------------//
    IF os > os[1] AND os = 1 THEN
    DRAWTEXT("●", barindex, hso, Dialog, Bold, 14) COLOURED(0, 181, 26, 255)  // Green bullish dot
    ENDIF
    
    IF os < os[1] AND os = -1 THEN
    DRAWTEXT("●", barindex, hso, Dialog, Bold, 14) COLOURED(255, 17, 0, 255)  // Red bearish dot
    ENDIF
    
    //--------------------------------------------//
    // Plot
    //--------------------------------------------//
    RETURN hso AS "Hull Butterfly" STYLE(HISTOGRAM) COLOURED(r, g, b),cmean AS "Nivel 100%" STYLE(LINE) COLOURED(128, 128, 128),cmean / 2 AS "Nivel 50%" STYLE(LINE) COLOURED(128, 128, 128),-cmean / 2 AS "Nivel -50%" STYLE(LINE) COLOURED(128, 128, 128),-cmean AS "Nivel -100%" STYLE(LINE) COLOURED(128, 128, 128)
    Msport71 thanked this post
    #243963 quote
    LucasBestLucasBest
    Participant
    Senior

    Pubblico anche la traduzione che ho fatto di questo indicatore qualche mese fa, così come il file itf

    // ProRealTime Code for Hull Butterfly Oscillator [LuxAlgo]
    
    Src = customclose // Direct assignment in ProBuilder
    
    // Normalization variables
    once ShortLen = floor(Length / 2)
    once HullLen = floor(sqrt(Length))
    once Den1 = ShortLen * (ShortLen + 1) / 2
    once Den2 = Length * (Length + 1) / 2
    once Den3 = HullLen * (HullLen + 1) / 2
    
    once init = 0
    
    If init = 0 then
    
    For i = 0 to Length - 1 do
    sum1 = max(ShortLen - i, 0)
    sum2 = Length - i
    $lcwaCoeffs[LastSet($lcwaCoeffs)+1] = 2 * (sum1 / Den1) - (sum2 / Den2)
    next
    
    For i = 0 to HullLen-2 do
    $lcwaCoeffs[LastSet($lcwaCoeffs)+1] = 0
    next
    
    For i = HullLen to LastSet($lcwaCoeffs) do
    sum3 = 0
    For j = i-HullLen to i-1 do
    sum3 = sum3 + $lcwaCoeffs[LastSet($lcwaCoeffs)-j]*(i - j)
    next
    $HullCoeffs[lastset($HullCoeffs)+1] = sum3 / den3
    next
    
    init = 1
    Lag = barindex-1
    hso = 0
    hsocum = 0
    NBar = 0
    
    EndIf
    
    If init = 1 then
    
    once os = 0
    once len = lastset($HullCoeffs)
    hma = 0
    invhma = 0
    
    For i = 0 to len do
    hma = hma + src[i] * $HullCoeffs[lastset($hullcoeffs)-i]
    invhma = invhma + src[len-i] * $HullCoeffs[lastset($hullcoeffs)-i]
    next
    
    hso = hma - invhma
    
    //hsocum = abs(hso)
    
    if abs(hso) > 0 then
    hsocum = hsocum + abs(hso)
    NBar = Nbar + 1
    endif
    
    cmean = Mult * hsocum / NBar
    
    os = 0
    If hso crosses over cmean or hso crosses under -cmean then
    os = 0
    Elsif hso < hso[1] and hso > cmean then
    os = -1
    Elsif hso > hso[1] and hso < -cmean then
    os = 1
    EndIf
    
    if hso > 0 then
    colR = 0
    colG = 128
    colB = 0
    else
    colR = 178
    colG = 34
    colB = 34
    endif
    
    If os > os[1] and os = 1 then
    drawpoint(barindex,hso,5) coloured("green",255)
    endif
    
    If os < os[1] and os = -1 then
    drawpoint(barindex,hso,5) coloured("red",255)
    endif
    
    Endif
    
    Return cmean style(line,1) coloured("gray"), -cmean style(line,1) coloured("gray"), cmean/2 style(line,1) coloured("gray"), -cmean/2 style(line,1) coloured("gray"), hso style(histogram,1) coloured(colR,colG,colB,255), hso style(line,3) coloured("black",255)
    
    Msport71 thanked this post
    Hull-Butterfly-Oscillator.itf
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Traduzione codice TW Hull Butterfly


ProBuilder: Indicatori & Strumenti Personalizzati

New Reply
Author
author-avatar
Msport71 @carlo-pasca Participant
Summary

This topic contains 2 replies,
has 3 voices, and was last updated by LucasBestLucasBest
1 year, 7 months ago.

Topic Details
Forum: ProBuilder: Indicatori & Strumenti Personalizzati
Language: Italian
Started: 02/17/2025
Status: Active
Attachments: 1 files
ProRealCode ProRealCode
Loading...