Stoch Divergence Conversion
Forums › ProRealTime English forum › ProBuilder support › Stoch Divergence Conversion
- This topic has 9 replies, 4 voices, and was last updated 4 years ago by ashehzi.
-
-
04/16/2020 at 11:52 PM #126458
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 : 0fractal_top1 = f_fractalize(Dinput1) > 0 ? Dinput1[2] : na
fractal_bot1 = f_fractalize(Dinput1) < 0 ? Dinput1[2] : nahigh_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_prev1col1 = regular_bearish_div1 ? color.red : hidden_bearish_div1 ? color.maroon : na
col2 = regular_bullish_div1 ? #00FF00EB : hidden_bullish_div1 ? color.green : naplot(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.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576//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] : nafractalTop1 = 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) thenfractalTop1 = Dinput1[2]endif//fractal_bot1 = f_fractalize(Dinput1) < 0 ? Dinput1[2] : nafractalBot1 = 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) thenfractalBot1 = Dinput1[2]endif//high_prev1 = valuewhen(fractal_top1, Dinput1[2], 0)[2]//high_price1 = valuewhen(fractal_top1, high[2], 0)[2]if fractalTop1 thenhighPrev1 = 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 thenlowPrev1 = Dinput1[2]lowPrice1 = low[2]endif//regular_bearish_div1 = fractal_top1 and high[2] > high_price1 and Dinput1[2] < high_prev1regularBearishDiv1 = fractalTop1 AND high[2] > highPrice1 AND Dinput1[2] < highPrev1//hidden_bearish_div1 = fractal_top1 and high[2] < high_price1 and Dinput1[2] > high_prev1hiddenBearishDiv1 = fractalTop1 and high[2] < highPrice1 and Dinput1[2] > highPrev1//regular_bullish_div1 = fractal_bot1 and low[2] < low_price1 and Dinput1[2] > low_prev1regularBullishDiv1 = fractalBot1 and low[2] < lowPrice1 and Dinput1[2] > lowPrev1//hidden_bullish_div1 = fractal_bot1 and low[2] > low_price1 and Dinput1[2] < low_prev1hiddenBullishDiv1 = 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
06/09/2020 at 11:32 PM #135365“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.
06/10/2020 at 8:51 AM #13537906/10/2020 at 5:51 PM #13548306/11/2020 at 10:29 AM #13556106/11/2020 at 3:58 PM #13560706/11/2020 at 4:18 PM #13561106/12/2020 at 11:34 AM #13570106/12/2020 at 4:24 PM #135770This 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:
1MyRSI = Momentum[RsiPeriod](Close)of course you will have to add further parameters to the indicator if you need to use Macd or Stochastic.
07/16/2020 at 9:24 AM #139409I’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.
-
AuthorPosts
Find exclusive trading pro-tools on