PPO DIVERGENCE ALERTS

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #237714 quote
    Truld1Truld1
    Participant
    Junior

    Buon giorno,
    Chiedo la conversione del seguente codice in Pine Script:

    //@version=2
    //Credit to https://www.tradingview.com/script/p3oqCa56-Pekipek-s-PPO-Divergence-BETA/ (I just changed the visuals and added alerts)
    study(“PPO Divergence Alerts”, overlay=false)

    source = open
    long_term_div = input(true, title=”Use long term Divergences?”)
    div_lookback_period = input(55, minval=1, title=”Lookback Period”)
    fastLength = input(12, minval=1), slowLength=input(26,minval=1)
    signalLength=input(9,minval=1)
    smoother = input(2,minval=1)
    fastMA = ema(source, fastLength)
    slowMA = ema(source, slowLength)
    macd = fastMA – slowMA
    macd2=(macd/slowMA)*100
    d = sma(macd2, smoother) // smoothing PPO

    bullishPrice = low

    priceMins = bullishPrice > bullishPrice[1] and bullishPrice[1] < bullishPrice[2] or low[1] == low[2] and low[1] < low and low[1] < low[3] or low[1] == low[2] and low[1] == low[3] and low[1] < low and low[1] < low[4] or low[1] == low[2] and low[1] == low[3] and low[1] and low[1] == low[4] and low[1] < low and low[1] d[1] and d[1] < d[2] // this line identifies bottoms in the PPO

    BottomPointsInPPO = oscMins

    bearishPrice = high
    priceMax = bearishPrice bearishPrice[2] or high[1] == high[2] and high[1] > high and high[1] > high[3] or high[1] == high[2] and high[1] == high[3] and high[1] > high and high[1] > high[4] or high[1] == high[2] and high[1] == high[3] and high[1] and high[1] == high[4] and high[1] > high and high[1] > high[5] // this line identifies tops in the price
    oscMax = d d[2] // this line identifies tops in the PPO

    TopPointsInPPO = oscMax

    currenttrough4=valuewhen (oscMins, d[1], 0) // identifies the value of PPO at the most recent BOTTOM in the PPO
    lasttrough4=valuewhen (oscMins, d[1], 1) // NOT USED identifies the value of PPO at the second most recent BOTTOM in the PPO
    currenttrough5=valuewhen (oscMax, d[1], 0) // identifies the value of PPO at the most recent TOP in the PPO
    lasttrough5=valuewhen (oscMax, d[1], 1) // NOT USED identifies the value of PPO at the second most recent TOP in the PPO

    currenttrough6=valuewhen (priceMins, low[1], 0) // this line identifies the low (price) at the most recent bottom in the Price
    lasttrough6=valuewhen (priceMins, low[1], 1) // NOT USED this line identifies the low (price) at the second most recent bottom in the Price
    currenttrough7=valuewhen (priceMax, high[1], 0) // this line identifies the high (price) at the most recent top in the Price
    lasttrough7=valuewhen (priceMax, high[1], 1) // NOT USED this line identifies the high (price) at the second most recent top in the Price

    delayedlow = priceMins and barssince(oscMins) < 3 ? low[1] : na
    delayedhigh = priceMax and barssince(oscMax) < 3 ? high[1] : na

    // only take tops/bottoms in price when tops/bottoms are less than 5 bars away
    filter = barssince(priceMins) < 5 ? lowest(currenttrough6, 4) : na
    filter2 = barssince(priceMax) < 5 ? highest(currenttrough7, 4) : na

    //delayedbottom/top when oscillator bottom/top is earlier than price bottom/top
    y11 = valuewhen(oscMins, delayedlow, 0)
    y12 = valuewhen(oscMax, delayedhigh, 0)

    // only take tops/bottoms in price when tops/bottoms are less than 5 bars away, since 2nd most recent top/bottom in osc
    y2=valuewhen(oscMax, filter2, 1) // identifies the highest high in the tops of price with 5 bar lookback period SINCE the SECOND most recent top in PPO
    y6=valuewhen(oscMins, filter, 1) // identifies the lowest low in the bottoms of price with 5 bar lookback period SINCE the SECOND most recent bottom in PPO

    long_term_bull_filt = valuewhen(priceMins, lowest(div_lookback_period), 1)
    long_term_bear_filt = valuewhen(priceMax, highest(div_lookback_period), 1)

    y3=valuewhen(oscMax, currenttrough5, 0) // identifies the value of PPO in the most recent top of PPO
    y4=valuewhen(oscMax, currenttrough5, 1) // identifies the value of PPO in the second most recent top of PPO

    y7=valuewhen(oscMins, currenttrough4, 0) // identifies the value of PPO in the most recent bottom of PPO
    y8=valuewhen(oscMins, currenttrough4, 1) // identifies the value of PPO in the SECOND most recent bottom of PPO

    y9=valuewhen(oscMins, currenttrough6, 0)
    y10=valuewhen(oscMax, currenttrough7, 0)

    bulldiv= BottomPointsInPPO ? d[1] : na // plots dots at bottoms in the PPO
    beardiv= TopPointsInPPO ? d[1]: na // plots dots at tops in the PPO

    i = currenttrough5 long_term_bear_filt // long term bearish top divergence
    i3 = delayedhigh > long_term_bear_filt // long term bearish delayedhigh divergence

    i4 = currenttrough4 > lowest(d, div_lookback_period) // long term bullish osc divergence
    i5 = y9 < long_term_bull_filt // long term bullish bottom div
    i6 = delayedlow y2 and oscMax and y3 y2 and y3 < y4) ? true : false
    bearishdiv3 = (long_term_div and oscMax and i and i2) ? true : false
    bearishdiv4 = (long_term_div and i and i3) ? true : false

    bullishdiv1 = (y9 y8) ? true : false
    bullishdiv2 = (delayedlow y8) ? true : false
    bullishdiv3 = (long_term_div and oscMins and i4 and i5) ? true : false
    bullishdiv4 = (long_term_div and i4 and i6) ? true : false

    bearish = bearishdiv1 or bearishdiv2 or bearishdiv3 or bearishdiv4
    bullish = bullishdiv1 or bullishdiv2 or bullishdiv3 or bullishdiv4

    greendot = beardiv != 0 ? true : false
    reddot = bulldiv != 0 ? true : false

    alertcondition( bearish, title=”Bearish Signal (Orange Circle)”, message=”Orange & Bearish: Short ” )
    alertcondition( bullish, title=”Bullish Signal (Purple Circle)”, message=”Purple & Bullish: Long ” )
    alertcondition( greendot, title=”PPO High (Green Dot)”, message=”Green High Point: Short ” )
    alertcondition( reddot, title=”PPO Low (Red Dot)”, message=”Red Low Point: Long ” )

    plot(y10>y2 and oscMax and y3 < y4 ? d :na, title = "Bearish Divergence1", color=orange, style= circles, linewidth=6)
    plot(y9 y8 ? d :na, title = “Bullish Divergence1”, color=purple, style=circles, linewidth=6)
    plot(delayedhigh>y2 and y3 < y4 ? d :na, title = "Bearish Divergence2", color=orange, style= circles, linewidth=6)
    plot(delayedlow y8 ? d :na, title = “Bullish Divergence2”, color=purple, style=circles, linewidth=6)

    plot(long_term_div and oscMax and i and i2 ? d :na, title = “Bearish Divergence3”, color=orange, style= circles, linewidth=6)
    plot(long_term_div and oscMins and i4 and i5 ? d : na, title = “Bullish Divergence3”, color=purple, style=circles, linewidth=6)
    plot(long_term_div and i and i3 ? d :na, title = “Bearish Divergence4”, color=orange, style= circles, linewidth=6)
    plot(long_term_div and i4 and i6 ? d : na, title = “Bullish Divergence4”, color=purple, style=circles, linewidth=6)

    #237812 quote
    Iván GonzálezIván González
    Moderator
    Legend

    Aquí tienes

    //------------------------------------//
    //PRC_PPO divergence
    //version = 0
    //19.09.24
    //Iván González @ www.prorealcode.com
    //Sharing ProRealTime knowledge
    //------------------------------------//
    //-----Inputs-------------------------//
    //------------------------------------//
    source = close  // Using the closing price as the data source
    longtermdiv = 1  // Boolean to toggle long-term divergences
    divlookbackperiod = 55  // Lookback period for divergences
    fastlength = 12  // Fast moving average length
    slowlength = 26  // Slow moving average length
    signallength = 9  // Signal line length for MACD
    smoother = 2  // Smoothing factor for PPO
    //------------------------------------//
    // Calculate PPO
    //------------------------------------//
    fastMA = average[fastlength,1](source)  // Calculate fast moving average
    slowMA = average[slowlength,1](source)  // Calculate slow moving average
    mymacd = fastMA - slowMA  // MACD calculation (difference between fast and slow MA)
    macd2 = mymacd / slowMA * 100  // Convert MACD to PPO (percentage form)
    d = average[smoother](macd2)  // Apply smoothing to the PPO value
    //------------------------------------//
    // Identify price bottoms and plateaus
    //------------------------------------//
    bullishPrice = low  // Use the low of the price to identify bottoms
    // Conditions to detect price bottoms using multiple previous lows
    priceMins = (bullishPrice > bullishPrice[1] AND bullishPrice[1] < bullishPrice[2]) OR(low[1] = low[2] AND low[1] < low AND low[1] < low[3]) OR(low[1] = low[2] AND low[1] = low[3] AND low[1] < low AND low[1] < low[4]) OR(low[1] = low[2] AND low[1] = low[3] AND low[1] = low[4] AND low[1] < low AND low[1] < low[5])
    // Identify bottoms in the PPO (oscillator minima)
    oscMins = d > d[1] AND d[1] < d[2]  // Check if PPO forms a local minimum
    BottomPointsInPPO = oscMins  // Store PPO minima condition
    //------------------------------------//
    // Identify price tops and plateaus
    //------------------------------------//
    bearishPrice = high  // Use the high of the price to identify tops
    // Conditions to detect price tops using multiple previous highs
    priceMax = (bearishPrice < bearishPrice[1] AND bearishPrice[1] > bearishPrice[2]) OR(high[1] = high[2] AND high[1] > high AND high[1] > high[3]) OR(high[1] = high[2] AND high[1] = high[3] AND high[1] > high AND high[1] > high[4]) OR(high[1] = high[2] AND high[1] = high[3] AND high[1] = high[4] AND high[1] > high AND high[1] > high[5])
    // Identify tops in the PPO (oscillator maxima)
    oscMax = d < d[1] AND d[1] > d[2]  // Check if PPO forms a local maximum
    TopPointsInPPO = oscMax  // Store PPO maxima condition
    //------------------------------------//
    // Store previous troughs and peaks
    //------------------------------------//
    if oscMins then
    lasttrough4 = currenttrough4  // Store previous PPO minimum
    currenttrough4 = d[1]  // Update current PPO minimum
    endif
    if oscMax then
    lasttrough5 = currenttrough5  // Store previous PPO maximum
    currenttrough5 = d[1]  // Update current PPO maximum
    endif
    if PriceMins then
    prevlowestLow = lowestLow  // Store previous lowest low in price
    lasttrough6 = currenttrough6  // Store previous price minimum
    currenttrough6 = low[1]  // Update current price minimum
    lowestLow = lowest[divlookbackperiod](low)  // Calculate the lowest low over the lookback period
    endif
    if PriceMax then
    PrevHighestHigh = HighestHigh  // Store previous highest high in price
    lasttrough7 = currenttrough7  // Store previous price maximum
    currenttrough7 = high[1]  // Update current price maximum
    HighestHigh = highest[divlookbackperiod](high)  // Calculate the highest high over the lookback period
    endif
    //------------------------------------//
    // Handle delayed lows and highs for PPO filtering
    //------------------------------------//
    if priceMins AND barssince(oscMins) < 3 then
    delayedlow = low[1]  // Store delayed low if PPO minimum happened within the last 3 bars
    else
    delayedlow = undefined  // Set undefined if condition is not met
    endif
    if priceMax AND barssince(oscMax) < 3 then
    delayedHigh = high[1]  // Store delayed high if PPO maximum happened within the last 3 bars
    else
    delayedHigh = undefined  // Set undefined if condition is not met
    endif
    
    // Filter price minima and maxima within a lookback period
    if barssince(priceMins) < 5 then
    filter = lowest[4](currenttrough6)  // Filter lowest troughs in price
    else
    filter = undefined  // Set undefined if condition is not met
    endif
    if barssince(priceMax) < 5 then
    filter2 = highest[4](currenttrough7)  // Filter highest peaks in price
    else
    filter2 = undefined  // Set undefined if condition is not met
    endif
    //------------------------------------//
    // Update additional values for PPO and price
    //------------------------------------//
    if oscMins then
    y11 = delayedlow  // Assign delayed low when a PPO minimum is identified
    y7 = currenttrough4  // Update current PPO minimum
    y8 = lasttrough4  // Store previous PPO minimum
    y9 = currenttrough6  // Update current price minimum
    endif
    if oscMax then
    y12 = delayedhigh  // Assign delayed high when a PPO maximum is identified
    y3 = currenttrough5  // Update current PPO maximum
    y4 = lasttrough5  // Store previous PPO maximum
    y10 = currenttrough7  // Update current price maximum
    endif
    if oscMax then
    y2 = y2now  // Store the filtered maximum
    y2now = filter2  // Update the filtered maximum
    endif
    if oscMins then
    y6 = y6now  // Store the filtered minimum
    y6now = filter  // Update the filtered minimum
    endif
    
    // Long-term divergence filters
    LongTermBullFilt = prevlowestLow  // Store long-term bullish filter (lowest low)
    LongtermBearFilt = PrevHighestHigh  // Store long-term bearish filter (highest high)
    
    // Draw bullish and bearish divergence points
    if BottomPointsInPPO then
    bulldiv = d[1]  // PPO minimum at current bar
    drawpoint(barindex[1], bulldiv, 2) coloured("maroon")  // Draw maroon point for PPO bottom
    else
    bulldiv = undefined
    endif
    
    if TopPointsInPPO then
    beardiv = d[1]  // PPO maximum at current bar
    drawpoint(barindex[1], beardiv, 2) coloured("green")  // Draw green point for PPO top
    else
    beardiv = undefined
    endif
    
    // Divergence logic conditions
    i = currenttrough5 < highest[divlookbackperiod](d)  // Check if PPO trough is below the highest over the lookback period
    i2 = y10 > LongtermBearFilt  // Check if current price peak is above the long-term bearish filter
    i3 = delayedhigh > LongtermBearFilt  // Check if delayed high is above the long-term bearish filter
    i4 = currenttrough4 > lowest[divlookbackperiod](d)  // Check if PPO peak is above the lowest over the lookback period
    i5 = y9 < LongTermBullFilt  // Check if current price trough is below the long-term bullish filter
    i6 = delayedlow < LongTermBullFilt  // Check if delayed low is below the long-term bullish filter
    //------------------------------------//
    // Divergence drawing conditions
    //------------------------------------//
    if y10 > y2 AND oscMax AND y3 < y4 then
    drawpoint(barindex, d, 5) coloured("orange")  // Draw orange point for bearish divergence
    elsif y9 < y6 AND oscMins AND y7 > y8 then
    drawpoint(barindex, d, 5) coloured("purple")  // Draw purple point for bullish divergence
    elsif delayedhigh > y2 AND y3 < y4 then
    drawpoint(barindex, d, 5) coloured("orange")  // Draw orange point for delayed bearish divergence
    elsif delayedlow < y6 AND y7 > y8 then
    drawpoint(barindex, d, 5) coloured("purple")  // Draw purple point for delayed bullish divergence
    elsif longtermdiv AND oscMax AND i AND i2 then
    drawpoint(barindex, d, 5) coloured("orange")  // Draw orange point for long-term bearish divergence
    elsif longtermdiv AND oscMins AND i4 AND i5 then
    drawpoint(barindex, d, 5) coloured("purple")  // Draw purple point for long-term bullish divergence
    elsif longtermdiv AND i AND i3 then
    drawpoint(barindex, d, 5) coloured("orange")  // Draw orange point for extended bearish divergence
    elsif longtermdiv AND i4 AND i6 then
    drawpoint(barindex, d, 5) coloured("purple")  // Draw purple point for extended bullish divergence
    endif
    
    // Return PPO and zero line for plotting
    return d as "PPO" coloured("black"), 0 as "zero" coloured("grey")  // Return PPO value in black and zero line in grey
    Truld1 thanked this post
    #237829 quote
    Truld1Truld1
    Participant
    Junior

    Gracias por tu trabajo, Te lo agradezco muchissimo

    Grazie per il tuo lavoro, lo apprezzo molto.

    #237830 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    @Truld1

    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. In questo caso Italiano.

    Grazie 🙂

    #237847 quote
    Truld1Truld1
    Participant
    Junior

    Mi spiace pensavo di fare cosa gradita scrivendo nella lingua dell’autore.

    In questo caso rinnovo i miei personali ringraziamenti. Un ottimo lavoro

    robertogozzi thanked this post
Viewing 5 posts - 1 through 5 (of 5 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

PPO DIVERGENCE ALERTS


ProBuilder: Indicatori & Strumenti Personalizzati

New Reply
Author
author-avatar
Truld1 @truld1 Participant
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by Truld1Truld1
1 year, 12 months ago.

Topic Details
Forum: ProBuilder: Indicatori & Strumenti Personalizzati
Language: Italian
Started: 09/17/2024
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...