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