本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=3
//Log RSI by @fskrypt
//Divergences by @RicardoSantos (@JustUncleL's edit)
//Edited by @NeoButane 6 Jun. 2018
study(title="Stochastic RSI with Divergences", shorttitle="Stoch RSI Divs", precision=0)
smoothK = input(3, minval=1)
smoothD = input(3, minval=1)
lengthRSI = input(14, minval=1)
lengthStoch = input(14, minval=1)
uselog = input(true, title="Log")
srcIn = input(close, type=source, title="Source")
showdivs = input(true, title="Show Divergences")
showchan = input(false, title="Show Divergences Channel")
highlightdivs = input(false, title="Highlight Divergences")
src = uselog ? log(srcIn) : srcIn
rsi1 = rsi(src, lengthRSI)
kk = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(kk, smoothD)
k = avg(kk, d)
plot(kk, color=blue, linewidth=1, transp=0, title="K")
plot(d, color=orange, linewidth=1, transp=0, title="D")
lower = hline(20, title="Lower Band")
upper = hline(80, title="Upper Band")
fill(upper, lower, color=purple, transp=95, title="Background")
// plotshape(cross(kk, d) and kk > d ? 80 : na, title="Crossover", color=green, style=shape.circle, location=location.absolute)
// plotshape(cross(kk, d) and d > kk ? 20 : na, title="Crossunder", color=red, style=shape.circle, location=location.absolute)
//------------------------------
//@RicardoSantos' Divergence Script
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_top = f_fractalize(k) > 0 ? k[2] : na
fractal_bot = f_fractalize(k) < 0 ? k[2] : na
high_prev = valuewhen(fractal_top, k[2], 0)[2]
high_price = valuewhen(fractal_top, high[2], 0)[2]
low_prev = valuewhen(fractal_bot, k[2], 0)[2]
low_price = valuewhen(fractal_bot, low[2], 0)[2]
regular_bearish_div = fractal_top and high[2] > high_price and k[2] < high_prev
hidden_bearish_div = fractal_top and high[2] < high_price and k[2] > high_prev
regular_bullish_div = fractal_bot and low[2] < low_price and k[2] > low_prev
hidden_bullish_div = fractal_bot and low[2] > low_price and k[2] < low_prev
//-------------------------
plot(showchan?fractal_top:na, title="Top Div Channel", offset=-2, color=silver)
plot(showchan?fractal_bot:na, title="Bottom Div Channel", offset=-2, color=silver)
plot(title='H F', series=showdivs and fractal_top ? k[2] : na, color=regular_bearish_div or hidden_bearish_div ? red : na, linewidth=2, offset=-2)
plot(title='L F', series=showdivs and fractal_bot ? k[2] : na, color=regular_bullish_div or hidden_bullish_div ? green : na, linewidth=2, offset=-2)
plot(title='H D', series=showdivs and fractal_top ? k[2] : na, style=circles, color=regular_bearish_div or hidden_bearish_div ? red : showchan ? silver : na, linewidth=3, offset=-2)
plot(title='L D', series=showdivs and fractal_bot ? k[2] : na, style=circles, color=regular_bullish_div or hidden_bullish_div ? green : showchan ? silver : na, linewidth=3, offset=-2)
plotshape(title='+RBD', series=showdivs and regular_bearish_div ? k[2] : na, text='R', style=shape.labeldown, location=location.absolute, color=red, textcolor=white, offset=-2)
plotshape(title='+HBD', series=showdivs and hidden_bearish_div ? k[2] : na, text='H', style=shape.labeldown, location=location.absolute, color=red, textcolor=white, offset=-2)
plotshape(title='-RBD', series=showdivs and regular_bullish_div ? k[2] : na, text='R', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)
plotshape(title='-HBD', series=showdivs and hidden_bullish_div ? k[2] : na, text='H', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)
//-------------------------
//BG Fills for divs
//Notes:
bg1 = highlightdivs and fractal_top ? k[2] : na
bg2 = highlightdivs and fractal_bot ? k[2] : na
bgcol1 = regular_bearish_div or hidden_bearish_div ? red : na
bgcol2 = regular_bullish_div or hidden_bullish_div ? green : na
bgcolor(bg1 ? bgcol1 : na, transp=80, offset=-2, title="BG Fill H F")
bgcolor(bg2 ? bgcol2 : na, transp=80, offset=-2, title="BG Fill L F")