Trend Reversal Probability

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #243846 quote
    Stenozar
    Participant
    Master
    Ciao, ho visto questo indicatore che mi pare interessante e chiedo se può essere tradotto.
    Grazie
    //@version=6
    indicator(“Trend Reversal Probability [Algoalpha]”, “AlgoAlpha – Trend Reversal”, false, explicit_plot_zorder = true)
    // Inputs
    group1 = “Settings”
    oscPeriod = input.int(20, “Oscillator Length”, minval = 1, group=group1, tooltip=”Length for the oscillator calculation”)
    lvl = input.bool(true, “Enable Probability Levels”, group=group1, tooltip=”Display probability level lines on the chart”)
    group2 = “Colors”
    upColor = input.color(#00ffbb, “Up Color”, group=group2, tooltip=”Color for upward trend”)
    downColor = input.color(#ff1100, “Down Color”, group=group2, tooltip=”Color for downward trend”)
    // Amazing Oscillator Calculation
    midpointPrice = hl2
    shortSMA = ta.sma(midpointPrice, 5)
    longSMA = ta.sma(midpointPrice, 34)
    amazingOsc = shortSMA – longSMA
    // RSI-like Calculation
    rise = ta.rma(math.max(ta.change(amazingOsc), 0), oscPeriod)
    fall = ta.rma(-math.min(ta.change(amazingOsc), 0), oscPeriod)
    customRSI = (fall == 0 ? 100 : rise == 0 ? 0 : 100 – (100 / (1 + rise / fall))) – 50
    opacityLevel = customRSI > 0 and customRSI > customRSI[1] or customRSI < 0 and customRSI < customRSI[1] ? 30 : 80
    barColor = customRSI > 0 ? color.new(upColor, opacityLevel) : color.new(downColor, opacityLevel)
    // Track the bar counts between crosses of customRSI
    var durations = array.new_int()
    cut = ta.barssince(ta.cross(customRSI, 0))
    if cut == 0 and cut != cut[1]
        durations.unshift(cut[1])
    basis = durations.avg()
    col = customRSI > 0 ? upColor : downColor
    avg = plot(basis, title=”Average Duration”, display=lvl ? display.all : display.none, color=color.gray)
    upper = plot(basis + durations.stdev(), title=”Upper 1 SD”, display=lvl ? display.all : display.none, color=color.gray)
    upper1 = plot(basis + durations.stdev() * 2, title=”Upper 2 SD”, display=lvl ? display.all : display.none, color=color.gray)
    upper2 = plot(basis + durations.stdev() * 3, title=”Upper 3 SD”, display=lvl ? display.all : display.none, color=color.gray)
    lower = plot(basis – durations.stdev(), title=”Lower 1 SD”, display=lvl ? display.all : display.none, color=color.gray)
    fill(lower, upper2, basis + durations.stdev() * 3, basis – durations.stdev(), #ffe6002c, color.new(color.blue, 70))
    transpcon1 = 10
    chg1 = cut * 0.80
    chg2 = cut * 0.60
    chg3 = cut * 0.40
    chg4 = cut * 0.20
    plot(cut, title=”Signal Duration”, color=color.new(col, transpcon1 + 60), style=plot.style_columns, linewidth=4)
    plot(chg1, title=”Signal Duration 80%”, color=color.new(col, transpcon1 + 55), style=plot.style_columns, linewidth=4)
    plot(chg2, title=”Signal Duration 60%”, color=color.new(col, transpcon1 + 45), style=plot.style_columns, linewidth=4)
    plot(chg3, title=”Signal Duration 40%”, color=color.new(col, transpcon1 + 30), style=plot.style_columns, linewidth=4)
    plot(chg4, title=”Signal Duration 20%”, color=color.new(col, transpcon1 + 25), style=plot.style_columns, linewidth=4)
    var lab = array.new_label()
    while lab.size() > 0
        lab.shift().delete()
    if lvl
        lab.push(label.new(bar_index, basis – durations.stdev(), “14%”, color=chart.fg_color, textcolor=chart.bg_color, style=label.style_label_left))
        lab.push(label.new(bar_index, basis, “50%”, color=chart.fg_color, textcolor=chart.bg_color, style=label.style_label_left))
        lab.push(label.new(bar_index, basis + durations.stdev(), “84%”, color=chart.fg_color, textcolor=chart.bg_color, style=label.style_label_left))
        lab.push(label.new(bar_index, basis + durations.stdev() * 2, “98%”, color=chart.fg_color, textcolor=chart.bg_color, style=label.style_label_left))
        lab.push(label.new(bar_index, basis + durations.stdev() * 3, “99%”, color=chart.fg_color, textcolor=chart.bg_color, style=label.style_label_left))
    volatility = ta.stdev(cut, 100) / 2
    plotchar(cut > basis + durations.stdev() * 3 ? cut + volatility : na, title=”Extreme Reversal Probability”, char=”▼”, location=location.absolute, color=color.orange, size=size.tiny)
    // Function to approximate cumulative probability using error function
    f_cdf(z) =>
        a1 = 0.254829592
        a2 = -0.284496736
        a3 = 1.421413741
        a4 = -1.453152027
        a5 = 1.061405429
        p = 0.3275911
        sign = z < 0 ? -1 : 1
        x = math.abs(z) / math.sqrt(2)
        t = 1 / (1 + p * x)
        erf_approx = 1 – (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * math.exp(-x * x)
        0.5 * (1 + sign * erf_approx)
    z = (cut – durations.avg()) / durations.stdev()
    probability = f_cdf(z)
    var data_table = table.new(position=position.middle_right, columns=2, rows=8, bgcolor=color.new(chart.bg_color, 10), border_width=1, border_color=chart.fg_color, frame_color=chart.fg_color, frame_width=1)
    table.cell(data_table, text_halign=text.align_center, column=0, row=0, text=”Reversal Probability (P)”, text_color=chart.fg_color, text_size=size.tiny)
    table.merge_cells(data_table, 0, 0, 1, 0)
    table.cell(data_table, text_halign=text.align_center, column=0, row=1, text=str.tostring(probability * 100, “#.#”) + “%”, text_color=chart.fg_color, text_size=size.large, bgcolor=color.from_gradient(probability, 0, 1, color.new(color.blue, 50), #ffe6002c))
    table.merge_cells(data_table, 0, 1, 1, 7)
    // Alerts
    alertcondition(probability > 0.84, title=”High Reversal Probability”, message=”Reversal probability crossed 84%”)
    alertcondition(probability > 0.98, title=”Extreme Reversal Probability”, message=”Reversal probability crossed 98%”)
    alertcondition(probability < 0.14, title=”Low Reversal Probability”, message=”Reversal probability below 14%”)
    alertcondition(cut == 0, title=”Probability Reset”, message=”Reversal probability reset”)
    #243879 quote
    roccafragius
    Participant
    Junior

    Hello, I’m really interested also to the translation of this indicator, very useful!
    Ciao, sono molto interessato anche alla traduzione di questo indicatore, molto utile!

    #243886 quote
    robertogozzi
    Moderator
    Master

    @roccafragius

    Pubblica solo nella lingua del forum in cui stai postando. Ad esempio solo l’inglese nei forum di lingua inglese e il francese solo nei forum di lingua francese.

    Grazie 🙂

    #243944 quote
    Iván González
    Moderator
    Master

    Ciao. Eccolo qui:

    //--------------------------------------------//
    //PRC Trend Reversal Probability
    //version = 0
    //17.02.2025
    //Iván González @ www.prorealcode.com
    //Sharing ProRealTime knowledge
    //--------------------------------------------//
    // inputs
    //--------------------------------------------//
    oscPeriod=20
    lvl=1 // boolean - Enable probability levels
    //--------------------------------------------//
    // Amazing Oscillator
    //--------------------------------------------//
    midpointprice=(high+low)/2
    shortSMA=average[5,0](midpointprice)
    longSMA=average[34,0](midpointprice)
    amazingOSC=shortSMA-longSMA
    //--------------------------------------------//
    // RSI-like
    //--------------------------------------------//
    change=amazingOSC-amazingOSC[1]
    //Rise
    src1 = max(change,0)
    length = oscPeriod
    alpha = 1/length
    if barindex <= max(60,2*length) then
    Rise = average[length](src1)
    else
    Rise = alpha*src1 + (1-alpha)*Rise[1]
    endif
    //Fall
    src2 = -min(change,0)
    length = oscPeriod
    alpha = 1/length
    if barindex <= max(60,2*length) then
    Fall = average[length](src2)
    else
    Fall = alpha*src2 + (1-alpha)*Fall[1]
    endif
    
    // RSI calculation
    if Fall=0 then
    customRSI=100
    elsif Rise=0 then
    customRSI=0
    else
    customRSI=100-(100/(1+rise/fall))-50
    endif
    // barcolor
    if customRSI>0 then
    r=0
    g=255
    else
    r=255
    g=0
    endif
    //--------------------------------------------//
    // Track the bar counts between crosses of customRSI
    //--------------------------------------------//
    cut=barssince(customRSI crosses over 0 or customRSI crosses under 0)
    if cut=0 and cut<>cut[1] then
    $durations[z+1]=cut[1]
    z=z+1
    endif
    // Average Calculation
    sumDurations=0
    countValid=0
    
    for i=1 to z do
    sumDurations=sumDurations+$durations[i]
    countValid=countValid+1
    next
    
    if countValid>0 then
    basis=sumDurations/countValid
    else
    basis=0
    endif
    // Standard Deviation Calculation
    sumSquaredDiffs=0
    for i=1 to z do
    sumSquaredDiffs = sumSquaredDiffs + ($durations[i] - basis) * ($durations[i] - basis)
    next
    IF countValid > 1 THEN
    stdDev = sqrt(sumSquaredDiffs / countValid)
    ELSE
    stdDev = 0
    ENDIF
    // Plot probability lines
    if lvl then
    avg=basis
    upper=basis+stdDev
    upper1=basis+stdDev*2
    upper2=basis+stdDev*3
    lower=basis-stdDev
    endif
    transpcon1=10
    chg1=cut*0.8
    chg2=cut*0.6
    chg3=cut*0.4
    chg4=cut*0.2
    //--------------------------------------------//
    // Reversal Probability
    //--------------------------------------------//
    if islastbarupdate then
    k=(cut-basis)/stdDev
    // Error Function Approximation (erf)
    a1 = 0.254829592
    a2 = -0.284496736
    a3 = 1.421413741
    a4 = -1.453152027
    a5 = 1.061405429
    p = 0.3275911
    
    once sign = 1
    IF k < 0 THEN
    sign = -1
    else
    sign = 1
    ENDIF
    
    x = abs(k) / sqrt(2)
    t = 1 / (1 + p * x)
    
    erfApprox = 1 - (((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * exp(-x * x))
    
    probability = 0.5 * (1 + sign * erfApprox)
    
    probPct=round(probability*100,1)
    drawtext("#probPct# %",-100,-100)anchor(topright,xshift,yshift)
    drawtext("Reversal Probability",-100,-80)anchor(topright,xshift,yshift)
    endif
    //--------------------------------------------//
    return avg as "50%" coloured("grey")style(line,2),upper as "84%" coloured("grey")style(line,2),upper1 as "98%" coloured("grey")style(line,2),upper2 as "99%" coloured("grey")style(line,2),lower as "14%" coloured("grey")style(line,2),cut as ""style(histogram)coloured(r,g,0,transpcon1+60),chg1 as ""style(histogram)coloured(r,g,0,transpcon1+55),chg2 as ""style(histogram)coloured(r,g,0,transpcon1+45),chg3 as ""style(histogram)coloured(r,g,0,transpcon1+30),chg4 as ""style(histogram)coloured(r,g,0,transpcon1+25)
    #243954 quote
    Stenozar
    Participant
    Master

    Ciao Ivan, ho copiato il codice ma mi da il seguente errore: Linea 1: Uno dei seguenti caratteri sarebbe più appropriato di “null”: una stringa di testo

    Come posso risolvere?

    Grazie

    #243958 quote
    robertogozzi
    Moderator
    Master

    Hai fatto il copia e Incolla in modo errato.

    Riprova, oppure importa il file ITF allegato.

    #243961 quote
    Stenozar
    Participant
    Master

    Grazie Roberto, ma anche importando il file mi da lo stesso identico errore; io utilizzo la versione 12, non so se può influire.

    #243962 quote
    roccafragius
    Participant
    Junior

    Thank you so much Ivan!!!!!!!!

    #243968 quote
    Iván González
    Moderator
    Master

    Beh, non so perché ricevi un errore. L'ho copiato e incollato di nuovo e non ho avuto problemi…

    #243972 quote
    Gibban
    Participant
    New

    Ciao Ivan,
    Indicatore molto bello, piccole cose sono nella lista dei desideri.
    1. Ci sarà una ristampa di “Reversal Propability” quindi sarà difficile vedere il valore, vedere l’immagine allegata.
    2. C’è la possibilità di ottenere etichette alla fine delle righe presenti nell’edizione originale?

    #243975 quote
    cjr30
    Participant
    Master
    Tralascio il suggerimento di Ivan, poiché l'ho adattato.
    #243978 quote
    Ciccarelli Franco
    Participant
    Junior

    Si può avere uno scrinner  da questo indicatore?

    Grazie

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

Trend Reversal Probability


ProBuilder: Indicatori & Strumenti Personalizzati

New Reply
Author
author-avatar
Stenozar @stenozar Participant
Summary

This topic contains 11 replies,
has 7 voices, and was last updated by Ciccarelli Franco
11 months, 3 weeks ago.

Topic Details
Forum: ProBuilder: Indicatori & Strumenti Personalizzati
Language: Italian
Started: 02/14/2025
Status: Active
Attachments: 5 files
Logo Logo
Loading...