本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// || This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ \\ ||
// ||------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ ||
// || Published by © Eric Thies on Tradingview.com --> Released on 2/6/2020 ||
// ||------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ ||
// || RSI Divergence - by KingThies \\ ||
// ||------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ ||
//<Start Source> <pine>
//@version=4
study("RSI Divergence", shorttitle="RSI Div", precision=1, overlay=false)
//1. Calculate RSI
src = input(close, 'RSI Source')
len = input(14, minval=1, title="RSI Length")
ob = input(70, "Overbought", input.integer, minval=0, maxval=100)
os = input(30, "Oversold", input.integer, minval=0, maxval=100)
RSI = rsi(src, len)
//2. Divergence Framework
xbars = input(90, "Div lookback period (bars)?", input.integer, minval=1)
hb = abs(highestbars(RSI, xbars)) // Finds bar with highest value in last X bars
lb = abs(lowestbars(RSI, xbars)) // Finds bar with lowest value in last X bars
// (2a. Defining variable values, mandatory in Pine 4)
max = float(na), min = float(na), pivoth = bool(na), divbear = bool(na)
max_rsi = float(na), min_rsi = float(na), pivotl = bool(na), divbull = bool(na)
//2b. Identify Divergences --> "If bar with lowest / highest is current bar, use it's value"
max := hb == 0 ? close : na(max[1]) ? close : max[1]
max_rsi := hb == 0 ? RSI : na(max_rsi[1]) ? RSI : max_rsi[1]
min := lb == 0 ? close : na(min[1]) ? close : min[1]
min_rsi := lb == 0 ? RSI : na(min_rsi[1]) ? RSI : min_rsi[1]
//3. Compare high of current bar being examined with previous bar's high --> "If curr bar high is higher than the max bar high in the lookback window range"
if close > max // "if we have a new high"
max := close // "change variable "max" to use current bar's high value"
max
if RSI > max_rsi // we have a new high
max_rsi := RSI // change variable "max_rsi" to use current bar's RSI value
max_rsi
if close < min // we have a new low
min := close // change variable "min" to use current bar's low value
min
if RSI < min_rsi // we have a new low
min_rsi := RSI // change variable "min_rsi" to use current bar's RSI value
min_rsi
//4. Finds pivot point with at least 2 right candles with lower value
pivoth := max_rsi == max_rsi[2] and max_rsi[2] != max_rsi[3] ? true : na, pivotl := min_rsi == min_rsi[2] and min_rsi[2] != min_rsi[3] ? true : na
if max[1] > max[2] and RSI[1] < max_rsi and RSI <= RSI[1]
divbear := true
divbear
if min[1] < min[2] and RSI[1] > min_rsi and RSI >= RSI[1]
divbull := true
divbull
//5. Plot
plot(RSI, "RSI Bear", color=RSI < 70 ? na : color.red, linewidth=3)
plot(RSI, "RSI Bull", color=RSI > 30 ? na : color.green, linewidth=3)
plot(RSI, "RSI", color=color.gray, linewidth=1)
hline(30)
hline(70)
//Add: Alerts
alertcondition(divbear==true, title='Bearish RSI Divergence', message='Bearish RSI Divergence Spotted on {{exchange}}:{{ticker}}')
alertcondition(divbull==true, title='Bullish RSI Divergence', message='Bearish RSI Divergence Spotted on {{exchange}}:{{ticker}}')
// </End Source> </pine>
// ||----------------------------------------------------------------------------------------------------------------------------------------------------------------------------||
// || RSI Divergence Oscillator - by KingThies ||
// ||--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ||
// || Published by © Eric Thies on Tradingview.com --> Released on 2/6/2020 ||
// ||--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ||