BEST RSI Divergences Screener (Regular and Hidden)

Daveatt · study · 147 行 · 点赞 3,256 · TradingView 原页

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

Pine Script

// @version=4
// @author=Daveatt - BEST
 
// ABCD Pattern Screener
 
StudyName        = "BEST RSI Divergences Screener (Regular and Hidden)"
ShortStudyName   = "BEST RSI Divergences Screener (R&H)" 
study(StudyName, shorttitle=ShortStudyName, overlay=false, precision=6)
 
rsiLen       = input(defval=14, title="RSI Length", minval=1)
lbR = input(title="Pivot Lookback Right", defval=5)
lbL = input(title="Pivot Lookback Left", defval=5)
rangeUpper = input(title="Max of Lookback Range", defval=60)
rangeLower = input(title="Min of Lookback Range", defval=5)
plotHidden = input(title="Detect Hidden Divergences?", defval=false)
 
string sec00 = input(defval="BYBIT:BTCUSD", type=input.symbol)
string sec01 = input(defval="BYBIT:ETHUSD", type=input.symbol)
string sec02 = input(defval="BYBIT:EOSUSD", type=input.symbol)
string sec03 = input(defval="BYBIT:XRPUSD", type=input.symbol)
 
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////// UTILITIES ///////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
 
//  ||  Functions:
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
 
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
////////////////////////////// ABCD PATTERN ///////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
 
f_rsi(_src, _len) =>
    _rsi = rsi(_src, _len)
 
_inRange(cond) =>
    bars = barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper
 
f_screener(_ticker)=>
 
    _r = timeframe.period
    _g = barmerge.gaps_off
    _l = barmerge.lookahead_on
 
    _high = security(symbol=_ticker, resolution=_r, expression=high, gaps=_g, lookahead=_l)
    _low = security(symbol=_ticker, resolution=_r, expression=low, gaps=_g, lookahead=_l)
    _close = security(symbol=_ticker, resolution=_r, expression=close, gaps=_g, lookahead=_l)
 
    osc = f_rsi(_close, rsiLen)
 
    plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
    phFound = na(pivothigh(osc, lbL, lbR)) ? false : true
 
    // Regular Bullish
 
    oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
    priceLL = _low[lbR] < valuewhen(plFound, _low[lbR], 1)
 
    // Hidden Bullish
    oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
    priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)
 
    _bull = (plotHidden and priceHL and oscLL and plFound) 
     or (priceLL and oscHL and plFound)
 
 
    // Regular Bearish
    oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
    priceHH = _high[lbR] > valuewhen(phFound, _high[lbR], 1)
 
 
    // Hidden Bearish
    oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
    priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)
 
 
    _bear = (plotHidden and priceLH and oscHH and phFound) 
     or (priceHH and oscLH and phFound)
 
    _color = iff(_bull, color.new(color.green, 0),
     iff(_bear, color.new(color.red, 0), color.new(color.aqua, 70)))
 
    [_bull, _bear, _color]
 
lapos_x = timenow + round(change(time)*12)
 
//f_color_label(_stackPos) => _stackPos ? color.new(color.green, 30) : color.new(color.red, 30)
 
f_geom(_pr_a, _id, _color)=>
 
    var label la = na
    label.delete(la)
    la := label.new(
             x=lapos_x, y=ceil(_pr_a),
             text=_id, xloc=xloc.bar_time, yloc=yloc.price, color=_color,
             style=label.style_labeldown, textcolor=color.black, size=size.small
             )
 
 
[s00_bull, s00_bear, s00_color]  = f_screener(sec00)
[s01_bull, s01_bear, s01_color]  = f_screener(sec01)
[s02_bull, s02_bear, s02_color]  = f_screener(sec02)
[s03_bull, s03_bear, s03_color]  = f_screener(sec03)
 
plotshape(s00_bull or s00_bear ? 0 : na , title='Square 1', style=shape.square, location=location.absolute, color=s00_color, size = size.tiny, offset=-lbR)
 
plotchar(not s00_bull and not s00_bear ? 0 : na, 
 char='-',location=location.absolute, color=s00_color, size = size.normal)
 
plotshape(s01_bull or s01_bear ? 1 : na , title='Square 2', style=shape.square, location=location.absolute, color=s01_color, size = size.tiny, offset=-lbR)
 
plotchar(not s01_bull and not s01_bear ? 1 : na, 
 char='-',location=location.absolute, color=s01_color, size = size.normal)
 
plotshape(s02_bull or s02_bear? 2 : na , title='Square 3', style=shape.square, location=location.absolute, color=s02_color, size = size.tiny, offset=-lbR)
 
plotchar(not s02_bull and not s02_bear  ? 2 : na, 
 char='-',location=location.absolute, color=s02_color, size = size.normal)
 
plotshape(s03_bull or s03_bear ? 3 : na , title='Square 4', style=shape.square, location=location.absolute, color=s03_color, size = size.tiny, offset=-lbR)
 
plotchar(not s03_bull and not s03_bear ? 3 : na, 
 char='-',location=location.absolute, color=s03_color, size = size.normal)
 
// right labels
f_geom(0, sec00, s00_color)
f_geom(1, sec01, s01_color)
f_geom(2, sec02, s02_color)
f_geom(3, sec03, s03_color)
 
// Alerts
alertcondition(s00_bull, title='[Asset 1] BULL DIV', message="{{ticker}} {{interval}} BULL DIV")
alertcondition(s01_bull, title='[Asset 2] BULL DIV', message="{{ticker}} {{interval}} BULL DIV")
alertcondition(s02_bull, title='[Asset 3] BULL DIV', message="{{ticker}} {{interval}} BULL DIV")
alertcondition(s03_bull, title='[Asset 4] BULL DIV', message="{{ticker}} {{interval}} BULL DIV")
 
alertcondition(s00_bear, title='[Asset 1] BEAR DIV', message="{{ticker}} {{interval}} BEAR DIV")
alertcondition(s01_bear, title='[Asset 2] BEAR DIV', message="{{ticker}} {{interval}} BEAR DIV")
alertcondition(s02_bear, title='[Asset 3] BEAR DIV', message="{{ticker}} {{interval}} BEAR DIV")
alertcondition(s03_bear, title='[Asset 4] BEAR DIV', message="{{ticker}} {{interval}} BEAR DIV")

← 返回列表