RSI Divergence Regular

StrategyLAB_ · study · 136 行 · 点赞 1,550 · TradingView 原页

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

Pine Script

//@version=5
indicator("RSI Divergence Liquidity", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=0)
 
//-------------------------
// Inputs
//-------------------------
pivotLen = input.int(7, "Pivot Lookback", minval=1)
rsiLen   = input.int(14, "RSI Length", minval=2)
maxRsiX  = input.float(36.0, "Max minRSI at Swing (x)", minval=1.0, maxval=99.0)
 
swColor  = input.color(color.teal, "Swing Low Color", group="Style")
sigColor = input.color(color.lime, "Signal Color", group="Style")
 
//-------------------------
// RSI
//-------------------------
rsi = ta.rsi(close, rsiLen)
 
//-------------------------
// Helpers
//-------------------------
bodyMin(_o, _c) => math.min(_o, _c)
bodyMax(_o, _c) => math.max(_o, _c)
inBody(_lvl, _o, _c) =>
    float lo = bodyMin(_o, _c)
    float hi = bodyMax(_o, _c)
    _lvl >= lo and _lvl <= hi
 
touchWick(_lvl, _hi, _lo) => _lo <= _lvl and _hi >= _lvl
 
noMiddleBodyCross(_lvl, _swingX) =>
    int barsSinceX = bar_index - _swingX
    if barsSinceX <= 2
        true
    else
        int maxScan = 500
        if (barsSinceX - 1) > maxScan
            false
        else
            bool ok = true
            for off = 2 to (barsSinceX - 1)
                if inBody(_lvl, open[off], close[off])
                    ok := false
            ok
 
//-------------------------
// Swing storage: current (x) + previous (x-1)
//-------------------------
var line  curLine = na
var float curBot  = na
var int   curX    = na
var float curMinRsiX = na
var bool  curTested = false
var bool  curSwept  = false
 
var line  prevLine = na
var float prevBot  = na
var int   prevX    = na
var float prevMinRsiX = na
var bool  prevTested = false
 
//-------------------------
// Detect pivot low
//-------------------------
pl = ta.pivotlow(pivotLen, pivotLen)
 
if pl
    int   px  = bar_index - pivotLen
    float bot = low[pivotLen]
 
    // Cut current swing when a new swing appears
    if not na(curLine) and not curSwept
        line.set_x2(curLine, px)
 
    // Shift current -> previous (x -> x-1)
    prevLine := curLine
    prevBot := curBot
    prevX := curX
    prevMinRsiX := curMinRsiX
    prevTested := curTested
 
    // Create new current swing
    curLine := line.new(px, bot, bar_index, bot, xloc=xloc.bar_index, extend=extend.none, color=swColor, style=line.style_solid, width=1)
    curBot := bot
    curX := px
    curTested := false
    curSwept := false
 
    // min RSI at x, x+1, x-1
    float rx  = rsi[pivotLen]
    float rxp = rsi[pivotLen - 1]
    float rxm = rsi[pivotLen + 1]
    curMinRsiX := math.min(rx, math.min(rxp, rxm))
 
//-------------------------
// Extend current swing line & mark swept
//-------------------------
if not na(curLine) and not curSwept and not na(curBot)
    line.set_x2(curLine, bar_index + 3)
    if close < curBot
        line.set_x2(curLine, bar_index)
        line.set_style(curLine, line.style_dashed)
        curSwept := true
 
//-------------------------
// Signal conditions at bar n close
//-------------------------
float bodyN  = math.abs(close - open)
float bodyN1 = math.abs(close[1] - open[1])
 
bool condCandle = (close > open) and (close[1] < open[1]) 
bool condBody30 = (bodyN1 == 0.0) ? (bodyN > 0.0) : (bodyN > (0.1 * bodyN1))
 
float minRSIn = math.min(rsi, rsi[1])
 
buySig = false
 
// --- Try current (x)
if not buySig and not curTested and not na(curBot) and not na(curX)
    bool touchCur = touchWick(curBot, high, low) or touchWick(curBot, high[1], low[1])
    bool okMidCur = noMiddleBodyCross(curBot, curX)
    bool okRsiX   = (not na(curMinRsiX)) and (curMinRsiX < maxRsiX)
    if condCandle and condBody30 and touchCur and okMidCur and okRsiX
        buySig := (minRSIn > curMinRsiX)
        curTested := true
 
// --- If current not signal -> try previous (x-1)
if not buySig and not prevTested and not na(prevBot) and not na(prevX)
    bool touchPrev = touchWick(prevBot, high, low) or touchWick(prevBot, high[1], low[1])
    bool okMidPrev = noMiddleBodyCross(prevBot, prevX)
    bool okRsiPrev = (not na(prevMinRsiX)) and (prevMinRsiX < maxRsiX)
    if condCandle and condBody30 and touchPrev and okMidPrev and okRsiPrev
        buySig := (minRSIn > prevMinRsiX)
        prevTested := true
 
plotshape(buySig, style=shape.triangleup, location=location.belowbar, size=size.small, color=sigColor, text="")

← 返回列表