本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=4
study("Divergence RSI-VWAP", overlay=false)
// ================================================================================================================================================================================
// RSI VWAP INDICATOR
// ================================================================================================================================================================================
// Initial inputs
Act_RSI_VWAP = input(true, "RSI VOLUME WEIGHTED AVERAGE PRICE")
RSI_VWAP_length = input(17, "RSI-VWAP LENGTH")
RSI_VWAP_overSold = input(20, "RSI-VWAP OVERSOLD", type=input.float)
RSI_VWAP_overBought = input(80, "RSI-VWAP OVERBOUGHT", type=input.float)
// RSI with VWAP as source
RSI_VWAP = rsi(vwap(close), RSI_VWAP_length)
// Plotting, overlay=false
r=plot(RSI_VWAP, color = RSI_VWAP > RSI_VWAP_overBought ? color.red : RSI_VWAP < RSI_VWAP_overSold ? color.lime : color.blue, title="rsi", linewidth=2, style=plot.style_line)
h1=plot(RSI_VWAP_overBought, color = color.gray, style=plot.style_stepline)
h2=plot(RSI_VWAP_overSold, color = color.gray, style=plot.style_stepline)
fill(r,h1, color = RSI_VWAP > RSI_VWAP_overBought ? color.red : na, transp = 60)
fill(r,h2, color = RSI_VWAP < RSI_VWAP_overSold ? color.lime : na, transp = 60)
rsiRSI = RSI_VWAP
// DIVS code
pivRSI = input(false,"Hide pivots?")
shrt = input(false,"Shorter labels?")
hidel = input(false, "Hide labels and color background")
xbarsRSI = input(defval=90, title="Div lookback period (bars)?", type=input.integer, minval=1)
hbRSI = abs(highestbars(rsiRSI, xbarsRSI)) // Finds bar with highest value in last X bars
lbRSI = abs(lowestbars(rsiRSI, xbarsRSI)) // Finds bar with lowest value in last X bars
// Defining variable values, mandatory in Pine 3
maxRSI = float(na)
max_rsiRSI = float(na)
minRSI = float(na)
min_rsiRSI = float(na)
pivothRSI = bool(na)
pivotlRSI = bool(na)
divbearRSI = bool(na)
divbullRSI = bool(na)
// If bar with lowest / highest is current bar, use it's value
maxRSI := hbRSI == 0 ? close : na(maxRSI[1]) ? close : maxRSI[1]
max_rsiRSI := hbRSI == 0 ? rsiRSI : na(max_rsiRSI[1]) ? rsiRSI : max_rsiRSI[1]
minRSI := lbRSI == 0 ? close : na(minRSI[1]) ? close : minRSI[1]
min_rsiRSI := lbRSI == 0 ? rsiRSI : na(min_rsiRSI[1]) ? rsiRSI : min_rsiRSI[1]
// 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 > maxRSI // we have a new high
maxRSI := close // change variable "max" to use current bar's high value
if rsiRSI > max_rsiRSI // we have a new high
max_rsiRSI := rsiRSI // change variable "max_rsi" to use current bar's RSI value
if close < minRSI // we have a new low
minRSI := close // change variable "min" to use current bar's low value
if rsiRSI < min_rsiRSI // we have a new low
min_rsiRSI := rsiRSI // change variable "min_rsi" to use current bar's RSI value
// Finds pivot point with at least 2 right candles with lower value
pivothRSI := (max_rsiRSI == max_rsiRSI[2]) and (max_rsiRSI[2] != max_rsiRSI[3]) ? true : na
pivotlRSI := (min_rsiRSI == min_rsiRSI[2]) and (min_rsiRSI[2] != min_rsiRSI[3]) ? true : na
// Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
if (maxRSI[1] > maxRSI[2]) and (rsiRSI[1] < max_rsiRSI) and (rsiRSI <= rsiRSI[1])
divbearRSI := true
if (minRSI[1] < minRSI[2]) and (rsiRSI[1] > min_rsiRSI) and (rsiRSI >= rsiRSI[1])
divbullRSI := true
// Alerts
alertcondition(divbearRSI, title='Bear div', message='Bear div')
alertcondition(divbullRSI, title='Bull div', message='Bull div')
// Plots divergences and pivots with offest
l = divbearRSI ?
label.new (bar_index-1, rsiRSI[1]+1, "BEAR", color=color.red, textcolor=color.white, style=label.style_labeldown, yloc=yloc.price, size=size.small) :
divbullRSI ?
label.new (bar_index-1, rsiRSI[1]-1, "BULL", color=color.green, textcolor=color.white, style=label.style_labelup, yloc=yloc.price, size=size.small) :
pivothRSI ?
label.new (bar_index-2, max_rsiRSI+1, "PIVOT", color=color.blue, textcolor=color.white, style=label.style_labeldown, yloc=yloc.price, size=size.small) :
pivotlRSI ?
label.new (bar_index-2, min_rsiRSI-1, "PIVOT", color=color.blue, textcolor=color.white, style=label.style_labelup, yloc=yloc.price, size=size.small) :
na
// Shorter labels
if shrt
label.set_text (l, na)
// Hides pivots or labels
if (pivRSI and (pivothRSI or pivotlRSI)) or hidel
label.delete (l)
// Colors indicator background
bgcolor (hidel ? (divbearRSI ? color.red : divbullRSI ? color.green : na) : na, offset=-1, transp=50)
bgcolor (hidel ? (pivRSI ? na : (pivothRSI or pivotlRSI ? color.blue : na)) : na, offset=-2, transp=50)