RSI Divergence Scanner by zdmre

zdmre · study · 209 行 · 点赞 1,917 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zdmre
 
//@version=5
indicator("RSI Divergence Scanner by zdmre", overlay=false)
 
len = input.int(14, minval=1, title='Length')
src = input(close, 'Source')
up = ta.rma(math.max(ta.change(src), 0), len)
down = ta.rma(-math.min(ta.change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)
plot(rsi, 'RSI', color=color.new(#7E57C2, 0))
band1 = hline(70, "Upper Band", color=#787B86)
bandm = hline(50, "Middle Band", color=color.new(#787B86, 50))
band0 = hline(30, "Lower Band", color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")
 
 
ob= ta.cross(rsi, 70) == 1 and rsi >= 70
os = ta.cross(rsi, 30) == 1 and rsi <= 30
 
plot(ob ? rsi : na ,title='Overbought', style=plot.style_circles, color=color.new(color.red, 0), linewidth=5)
plot(os ? rsi : na ,title='Oversold ', style=plot.style_circles, color=color.new(color.green, 0), linewidth=5)
 
if ob
    label1 = label.new(bar_index, rsi , text=str.format("{0,date, y\nMMM-d\n}", time)+'|', style=label.style_label_down, color=color.new(color.red,100))
    label.set_size(label1, size.small)
    label.set_textcolor(label1, color.red)
if os
    label2 = label.new(bar_index, rsi , text='|\n'+ str.format("{0,date, y\nMMM-d}", time), style=label.style_label_up, color=color.new(color.green,100))
    label.set_size(label2, size.small)
    label.set_textcolor(label2, color.green)
 
drawLine(X1, Y1, X2, Y2, ExtendType, Color, LineStyle) =>
    var line Line = na
    Line := line.new(X1, Y1, X2, Y2, xloc.bar_index, ExtendType, Color, LineStyle, 1)
    line.delete(Line[1])
 
rsdcr2(PeriodMinusOne, Deviations, Estimate) =>
    var period = PeriodMinusOne + 1
    var devDenominator = Estimate == 'Unbiased' ? PeriodMinusOne : period
    Ex = 0.0
    Ex2 = 0.0
    Exy = 0.0
    Ey = 0.0
    for i = 0 to PeriodMinusOne by 1
        closeI = nz(rsi[i])
        Ex := Ex + i
        Ex2 := Ex2 + i * i
        Exy := Exy + closeI * i
        Ey := Ey + closeI
        Ey
    ExEx = Ex * Ex
    slope = Ex2 == ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
    linearRegression = (Ey - slope * Ex) / period
    intercept = linearRegression + bar_index * slope
    deviation = 0.0
    for i = 0 to PeriodMinusOne by 1
        deviation := deviation + math.pow(nz(rsi[i]) - (intercept - bar_index[i] * slope), 2.0)
        deviation
    deviation := Deviations * math.sqrt(deviation / devDenominator)
    correlate = ta.correlation(rsi, bar_index, period)
    r2 = math.pow(correlate, 2.0)
    [linearRegression, slope, deviation, correlate, r2]
periodTrend = input.int(140, 'Trend Period', minval=4)
deviationsAmnt = input.float(1.1, ' Deviation', minval=0.1, step=0.1)
estimatorType = input.string('Unbiased', ' Estimator', options=['Biased', 'Unbiased'])
var extendType = input.string('Right', 'Extend', options=['Right', 'Segment']) == 'Right' ? extend.right : extend.none
periodMinusOne = periodTrend - 1
[linReg, slope, deviation, correlate, r2] = rsdcr2(periodMinusOne, deviationsAmnt, estimatorType)
endPointBar = bar_index - periodTrend + 1
endPointY = linReg + slope * periodMinusOne
 
////////////////////HigherHighs-HigherLows-LowerLows-LowerHighs/////////////
length = input(21, title='Zigzag Length')
//------------------------------------------------------------------------------
r = rsi[length]
l = rsi[14]
 
ph = ta.pivothigh(rsi,length,length)
pl = ta.pivotlow(rsi,length,length)
valH = ta.valuewhen(ph,r,0)
valL = ta.valuewhen(pl,r,0)
valpH = ta.valuewhen(ph,r,1)
valpL = ta.valuewhen(pl,r,1)
 
 
//------------------------------------------------------------------------------
d = math.abs(r)
n = bar_index
label lbl = na
HIH = valH > valpH ? "HH" : na 
HIL = valH < valpH ? "LH" : na
LOL = valL < valpL ? "LL" : na
LOH = valL > valpL ? "HL" : na
 
 
if ph and valH > valpH
    lbl := label.new(n[length],math.max(r,l),HIH,color=#ff1100  ,
      style=label.style_label_down,textcolor=color.white,size=size.tiny)
    label.delete(lbl[1])
    linehh = line.new(n[length],math.max(r,l), bar_index, math.max(r,l), extend=extend.right, color=color.red)
    line.delete(linehh[1])
else if ph and valH < valpH
    lbl := label.new(n[length],math.max(r,l),HIL,color=#e79700 ,
      style=label.style_label_down,textcolor=color.white,size=size.tiny)
    label.delete(lbl[1])
    linehl = line.new(n[length],math.max(r,l), bar_index, math.max(r,l), extend=extend.right, color=color.orange)
    line.delete(linehl[1])
else if pl and valL < valpL
    lbl := label.new(n[length],math.min(r,l),LOL,color=#009c0c   ,  
      style=label.style_label_up,textcolor=color.white,size=size.tiny)
    label.delete(lbl[1]) 
    linell = line.new(n[length],math.min(r,l), bar_index, math.min(r,l), extend=extend.right, color=color.green)
    line.delete(linell[1])
else if pl and valL > valpL
    lbl := label.new(n[length],math.min(r,l),LOH,color= #0073ee ,
      style=label.style_label_up,textcolor=color.white,size=size.tiny)
    label.delete(lbl[1])
    linelh = line.new(n[length],math.min(r,l), bar_index, math.min(r,l), extend=extend.right, color=color.blue)
    line.delete(linelh[1])
label.delete(lbl[200])  
barcolor(rsi > linReg + deviation and ta.pivothigh(rsi, length, 0) and rsi >= 65 ? color.white : rsi < linReg - deviation and ta.pivotlow(rsi, length, 1) and rsi <= 35 ? color.yellow : na)
bgcolor(rsi > linReg + deviation and ta.pivothigh(rsi, length, 0) and rsi >= 65 ? color.new(color.red,50) : rsi < linReg - deviation and ta.pivotlow(rsi, length, 1) and rsi <= 35 ? color.new(color.green, 50) : na)
 
 
///////WEDGE
wedgegroup = "**********WEDGE**********"
//Inputs
extendOptionUp = input.string("none", title="Upper Line Extension", 
     options=["none", "left", "right", "both"],group=wedgegroup)
extendOptionLow = input.string("none", title="Lower Line Extension", 
     options=["none", "left", "right", "both"])
lengthwg = input.int(21, 'Upper Length',minval=0, inline='length')
length2wg = input.int(21, 'Lower Length',minval=0, inline='length')
astart = input.int(1, 'Draw Upper Line From Pivot',minval=0, inline='pivot') //def 2
aend = input.int(0, 'To Pivot #',minval=0, inline='pivot')
bstart = input.int(1, 'Draw Lower Line From Pivot',minval=0, inline='pivot2') //def 3
bend = input.int(0, 'To Pivot #', minval=0,inline='pivot2')
colorwg = input.string('Red/Green', 'Line Color:', options=['Red/Green', 'White', 'Yellow', 'Blue'], inline='custom2')
lwidth = input.int(2,'Line Width:', minval=0,maxval=4)
 
//Conditions
upwg = ta.pivothigh(rsi, lengthwg, lengthwg)
dnwg = ta.pivotlow(rsi, length2wg, length2wg)
upchart = ta.pivothigh(high, lengthwg, lengthwg)
dnchart = ta.pivotlow(low, length2wg, length2wg)
 
nw = bar_index
a1 = ta.valuewhen(not na(upwg), nw, astart)
b1 = ta.valuewhen(not na(dnwg), nw, bstart)
a2 = ta.valuewhen(not na(upwg), nw, aend)
b2 = ta.valuewhen(not na(dnwg), nw, bend)
 
ach1 = ta.valuewhen(not na(upchart), nw, astart)
bch1 = ta.valuewhen(not na(dnchart), nw, bstart)
ach2 = ta.valuewhen(not na(upchart), nw, aend)
bch2 = ta.valuewhen(not na(dnchart), nw, bend)
 
 
//Colors
color1 = color.red
color2 = color.green
if colorwg == 'White'
    color1 := color.white
    color2 := color.white
    color2
if colorwg == 'Yellow'
    color1 := color.yellow
    color2 := color.yellow
    color2
if colorwg == 'Blue'
    color1 := color.blue
    color2 := color.blue
    color2
if colorwg == 'Red/Green'
    color1 := color.red
    color2 := color.green
    color2
 
//Lines
 
lineExtendUp = extendOptionUp == "left" ? extend.left :
     extendOptionUp == "right" ? extend.right :
     extendOptionUp == "both" ? extend.both :
         extend.none
lineExtendLow = extendOptionLow == "left" ? extend.left :
     extendOptionLow == "right" ? extend.right :
     extendOptionLow == "both" ? extend.both :
         extend.none
line upper = line.new(nw[nw - a1 + lengthwg], upwg[nw - a1], nw[nw - a2 + lengthwg], upwg[nw - a2], extend=lineExtendUp,color=color1, width=lwidth,style=line.style_dotted) 
line.delete(upper[1])
line lower = line.new(nw[nw - b1 + length2wg], dnwg[nw - b1], nw[nw - b2 + length2wg], dnwg[nw - b2], extend=lineExtendLow, color=color2, width=lwidth, style=line.style_dotted)
line.delete(lower[1])
 
div1 = upwg[nw - a2] > upwg[nw - a1] and upchart[nw - ach2] < upchart[nw - ach1] and upchart > high[nw - ach1]  //and ta.pivotlow(rsi,length,0)
div2 = dnwg[nw - b2] > dnwg[nw - b1] and dnchart[nw - bch2] < dnchart[nw - bch1] and dnchart < low[nw - bch1] //and ta.pivothigh(rsi,length,0)
 
if div1
    line.new(nw[nw - a1 + lengthwg], upwg[nw - a1], nw[nw - a2 + lengthwg], upwg[nw - a2], extend=lineExtendUp,color=color1, width=lwidth,style=line.style_dotted)
    label1 = label.new(nw[nw - a2 + lengthwg], 70 , text="Divergence\n|", style=label.style_label_down, color=color.new(color.red,100))
    label.set_size(label1, size.small)
    label.set_textcolor(label1, color.red)
    
if div2
    line.new(nw[nw - b1 + length2wg], dnwg[nw - b1], nw[nw - b2 + length2wg], dnwg[nw - b2], extend=lineExtendLow, color=color2, width=lwidth, style=line.style_dotted)
    label2 = label.new(nw[nw - b2 + length2wg], 30, text="|\nDivergence", style=label.style_label_up, color=color.new(color.green,100))
    label.set_size(label2, size.small)
    label.set_textcolor(label2, color.green)

← 返回列表