Stoch Divergence Conversion

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #126458 quote
    ashehzi
    Participant
    Average

    Hi,

    I am converting below code from tradingview (https://uk.tradingview.com/scripts/search/stochastic%20divergence/) to PRT 10.3 (IG).

    //@version=4
    //@author=lucemanb
    study(title=”Plain Stochastic Divergence ✂️ “, overlay=false)

    periodK = input(14, title=”K”, minval=1)
    periodD = input(3, title=”D”, minval=1)
    smoothK = input(3, title=”Smooth”, minval=1)

    Dinput1 = sma(stoch(close, high, low, periodK), smoothK)
    SD = sma(Dinput1, periodD)

    plot(title = ‘K’, series = Dinput1 , color = #FF7F00CA,linewidth = 2 )
    plot(title = “D”, series = SD , color = #1E90FF, linewidth = 1)

    f_top_fractal(_src)=>_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
    f_bot_fractal(_src)=>_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
    f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0

    fractal_top1 = f_fractalize(Dinput1) > 0 ? Dinput1[2] : na
    fractal_bot1 = f_fractalize(Dinput1) < 0 ? Dinput1[2] : na

    high_prev1 = valuewhen(fractal_top1, Dinput1[2], 0)[2]
    high_price1 = valuewhen(fractal_top1, high[2], 0)[2]
    low_prev1 = valuewhen(fractal_bot1, Dinput1[2], 0)[2]
    low_price1 = valuewhen(fractal_bot1, low[2], 0)[2]

    regular_bearish_div1 = fractal_top1 and high[2] > high_price1 and Dinput1[2] < high_prev1
    hidden_bearish_div1 = fractal_top1 and high[2] < high_price1 and Dinput1[2] > high_prev1
    regular_bullish_div1 = fractal_bot1 and low[2] < low_price1 and Dinput1[2] > low_prev1
    hidden_bullish_div1 = fractal_bot1 and low[2] > low_price1 and Dinput1[2] < low_prev1

    col1 = regular_bearish_div1 ? color.red : hidden_bearish_div1 ? color.maroon : na
    col2 = regular_bullish_div1 ? #00FF00EB : hidden_bullish_div1 ? color.green : na

    plot(title=’Bearish Divergence’, series = fractal_top1 ? Dinput1[2] : na, color=col1, linewidth=3, offset=-2)
    plot(title=’Bullish Divergence’, series = fractal_bot1 ? Dinput1[2] : na, color=col2, linewidth=3, offset=-2)

    plotshape(fractal_top1 and (regular_bearish_div1 or hidden_bearish_div1), “spot top”, shape.diamond, location.top, color.blue)
    plotshape(fractal_bot1 and (regular_bullish_div1 or hidden_bullish_div1), “spot bottom”, shape.diamond, location.bottom, color.blue)

    I am trying but unsure about “valuewhen(condition, source, occurance)[2] ” containing extra “[2]” at the end.

    Below is my try but need to fix it and make it work.

     

    //DEFPARAM CALCULATEONLASTBARS = 420
    
    //Dinput1 = sma(stoch(close, high, low, periodK), smoothK)
    //Dinput1 = average[smoothK](Stochastic[periodK,periodD](close))
    Dinput1 = Stochastic[periodK,periodD](close)
    
    //SD      = sma(Dinput1, periodD)
    SD = average[smoothK](Dinput1)
    
    //plot(title = 'K', series = Dinput1 , color = #FF7F00CA,linewidth = 2 )
    //plot(title = "D", series = SD , color = #1E90FF,  linewidth = 1)
    
    //f_top_fractal(_src)=>_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
    // function f-top-fractal[src] defined in PRT and called as CALL "f-top-fractal"[src]
    
    //f_bot_fractal(_src)=>_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
    // function f-top-fractal[src] defined in PRT and called as CALL "f-bot-fractal"[src]
    
    //f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0
    // function f-fractalize[src] defined
    
    
    //fractal_top1 = f_fractalize(Dinput1) > 0 ? Dinput1[2] : na
    fractalTop1 = Undefined
    //myffractalize = CALL "f-fractalize"[Dinput1]
    myffractalize = Dinput1[4] < Dinput1[2] and Dinput1[3] < Dinput1[2] and Dinput1[2] > Dinput1[1] and Dinput1[2] > Dinput1[0]
    if (myffractalize > 0) then
    fractalTop1 = Dinput1[2]
    endif
    
    //fractal_bot1 = f_fractalize(Dinput1) < 0 ? Dinput1[2] : na
    fractalBot1 = Undefined
    //myffractalize = CALL "f-fractalize"[Dinput1]
    myffractalize = Dinput1[4] > Dinput1[2] and Dinput1[3] > Dinput1[2] and Dinput1[2] < Dinput1[1] and Dinput1[2] < Dinput1[0]
    if (myffractalize < 0) then
    fractalBot1 = Dinput1[2]
    endif
    
    //high_prev1  = valuewhen(fractal_top1, Dinput1[2], 0)[2]
    //high_price1 = valuewhen(fractal_top1, high[2], 0)[2]
    if fractalTop1 then
    highPrev1 = Dinput1[2]
    highPrice1 = high[2]
    endif
    
    //low_prev1   = valuewhen(fractal_bot1, Dinput1[2], 0)[2]
    //low_price1  = valuewhen(fractal_bot1, low[2], 0)[2]
    if fractalBot1 then
    lowPrev1 = Dinput1[2]
    lowPrice1 = low[2]
    endif
    
    
    //regular_bearish_div1 = fractal_top1  and high[2] > high_price1 and Dinput1[2] < high_prev1
    regularBearishDiv1 = fractalTop1 AND high[2] > highPrice1 AND Dinput1[2] < highPrev1
    
    //hidden_bearish_div1  = fractal_top1  and high[2] < high_price1 and Dinput1[2] > high_prev1
    hiddenBearishDiv1  = fractalTop1  and high[2] < highPrice1 and Dinput1[2] > highPrev1
    
    //regular_bullish_div1 = fractal_bot1  and low[2]  < low_price1  and Dinput1[2] > low_prev1
    regularBullishDiv1 = fractalBot1  and low[2]  < lowPrice1  and Dinput1[2] > lowPrev1
    
    //hidden_bullish_div1  = fractal_bot1  and low[2]  > low_price1  and Dinput1[2] < low_prev1
    hiddenBullishDiv1  = fractalBot1  and low[2]  > lowPrice1  and Dinput1[2] < lowPrev1
    
    //col1 = regular_bearish_div1 ? color.red : hidden_bearish_div1 ? color.maroon : na
    //col2 = regular_bullish_div1 ? #00FF00EB : hidden_bullish_div1 ? color.green  : na
    
    //plot(title='Bearish Divergence', series = fractal_top1 ? Dinput1[2] : na, color=col1, linewidth=3, offset=-2)
    
    //plot(title='Bullish Divergence', series = fractal_bot1 ? Dinput1[2] : na, color=col2, linewidth=3, offset=-2)
    
    //plotshape(fractal_top1 and (regular_bearish_div1 or hidden_bearish_div1), "spot top", shape.diamond, location.top, color.blue)
    //plotshape(fractal_bot1 and (regular_bullish_div1 or hidden_bullish_div1), "spot bottom", shape.diamond, location.bottom, color.blue)
    
    return Dinput1 as "K", SD as "D", regularBearishDiv1 as "RBear", hiddenBearishDiv1 as "HBear", regularBullishDiv1 as "RBull", hiddenBullishDiv1 as "HBull"
    

    Please help.

    Thanks,

    Ash

    #135365 quote
    robertogozzi
    Moderator
    Master

    valuewhen(condition, source, occurance)[2] ” containing extra “[2]” at the end, means that a reference to the second previous value of the result is to be taken into account, so you have to calculate “valuewhen(condition, source, occurance)”, then use the value retained the 2nd previous bar.

    But I don’t know how to convert it into PRT code.

    #135379 quote
    Nicolas
    Keymaster
    Master

    There is no instruction that replace the ValueWhen, you have to make a loop to find the value you want for that particular barindex.

    #135483 quote
    ashehzi
    Participant
    Average

    Thanks.

    Actually the objective is to find hidden bearish and bullish divergences. I’ve code for normal divergence but not for hidden.

    Let me know as when i tried to find hidden divergences on prt, i failed.

    #135561 quote
    Nicolas
    Keymaster
    Master

    If you have coded something to find normal divergences, you would be able to find the hidden ones?

    #135607 quote
    sixeight
    Participant
    Junior

    I’d be very interested in this if you manage to get it coded Ashehzi

    #135611 quote
    robertogozzi
    Moderator
    Master
    #135701 quote
    sixeight
    Participant
    Junior

    That indicator shows on the price chart but sadly it doesn’t show in a separate pane – from the comments it seems others were finding the same.

    #135770 quote
    robertogozzi
    Moderator
    Master

    This should do, it will return 2=dd, 1=hd (long), -2=dd,-1,hd (short) and 0 if none.

    Line 15 can be modified (withiut even changing the name of variables) to accomodate for other indicators:

    MyRSI = Momentum[RsiPeriod](Close)

    of course you will have to add further parameters to the indicator if you need to use Macd or Stochastic.

    x-11.jpg x-11.jpg RSI-Divergences2.itf
    #139409 quote
    ashehzi
    Participant
    Average

    I’ve used https://www.prorealcode.com/prorealtime-indicators/rsi-classical-hidden-divergences-indicator/ before but it doesn’t serve the purpose. I will try to convert the hidden part of this code (rsi-divergences-2.itf) to stoch hidden and will let you know if it serves the purpose.

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

Stoch Divergence Conversion


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
ashehzi @ashehzi Participant
Summary

This topic contains 9 replies,
has 4 voices, and was last updated by ashehzi
5 years, 7 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 04/16/2020
Status: Active
Attachments: 2 files
Logo Logo
Loading...