Divergence Long/Short Signals

Deutz1 · study · 156 行 · 点赞 173 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Deutz1
//@version=6
indicator("Divergence Long/Short Signals", overlay=true, max_labels_count=500)
 
//--------------------------------------------------
// INPUTS
//--------------------------------------------------
groupOsc   = "Oscillators"
groupPivot = "Pivot Settings"
groupVol   = "Volume Filter"
groupDraw  = "Display"
 
rsiLen   = input.int(14, "RSI Length", minval=2, group=groupOsc)
cciLen   = input.int(20, "CCI Length", minval=2, group=groupOsc)
 
leftBars  = input.int(5, "Pivot Left Bars", minval=1, group=groupPivot)
rightBars = input.int(5, "Pivot Right Bars", minval=1, group=groupPivot)
 
volLen  = input.int(20, "Volume SMA Length", minval=1, group=groupVol)
volMult = input.float(1.2, "Volume Multiplier", minval=0.1, step=0.1, group=groupVol)
 
showWeakSignals   = input.bool(true, "Show Weak Signals", group=groupDraw)
showMediumSignals = input.bool(true, "Show Medium Signals", group=groupDraw)
showStrongSignals = input.bool(true, "Show Strong Signals", group=groupDraw)
showPivotMarks    = input.bool(false, "Show Pivot Marks", group=groupDraw)
 
//--------------------------------------------------
// BASE SERIES
//--------------------------------------------------
rsiValue = ta.rsi(close, rsiLen)
cciValue = ta.cci(hlc3, cciLen)
volSma   = ta.sma(volume, volLen)
 
//--------------------------------------------------
// PIVOTS
//--------------------------------------------------
pivotLow  = ta.pivotlow(low, leftBars, rightBars)
pivotHigh = ta.pivothigh(high, leftBars, rightBars)
 
isBullPivot = not na(pivotLow)
isBearPivot = not na(pivotHigh)
 
// Values on confirmed pivot bar
bullPrice = low[rightBars]
bullRsi   = rsiValue[rightBars]
bullCci   = cciValue[rightBars]
bullVol   = volume[rightBars]
bullVolMa = volSma[rightBars]
 
bearPrice = high[rightBars]
bearRsi   = rsiValue[rightBars]
bearCci   = cciValue[rightBars]
bearVol   = volume[rightBars]
bearVolMa = volSma[rightBars]
 
//--------------------------------------------------
// PREVIOUS PIVOT STORAGE
//--------------------------------------------------
var float prevBullPrice = na
var float prevBullRsi   = na
var float prevBullCci   = na
var float prevBullVol   = na
 
var float prevBearPrice = na
var float prevBearRsi   = na
var float prevBearCci   = na
var float prevBearVol   = na
 
//--------------------------------------------------
// SIGNALS
//--------------------------------------------------
bool weakLong   = false
bool mediumLong = false
bool strongLong = false
 
bool weakShort   = false
bool mediumShort = false
bool strongShort = false
 
//--------------------------------------------------
// BULLISH DIVERGENCES
// Price makes lower low, oscillators make higher low
//--------------------------------------------------
if isBullPivot
    currentBullIndex = bar_index - rightBars
 
    if not na(prevBullPrice)
        bullDivCci = bullPrice < prevBullPrice and bullCci > prevBullCci
        bullDivRsi = bullPrice < prevBullPrice and bullRsi > prevBullRsi
        bullVolOk  = bullVol > bullVolMa * volMult and bullVol > prevBullVol
 
        strongLong := bullDivCci and bullDivRsi and bullVolOk
        mediumLong := bullDivCci and bullDivRsi and not strongLong
        weakLong   := bullDivCci and not bullDivRsi
 
        if weakLong and showWeakSignals
            label.new(currentBullIndex, bullPrice, "Weak Long", style=label.style_label_up, color=color.new(color.lime, 0), textcolor=color.black, size=size.small)
 
        if mediumLong and showMediumSignals
            label.new(currentBullIndex, bullPrice, "Medium Long", style=label.style_label_up, color=color.new(color.teal, 0), textcolor=color.white, size=size.small)
 
        if strongLong and showStrongSignals
            label.new(currentBullIndex, bullPrice, "Strong Long", style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.small)
 
    prevBullPrice := bullPrice
    prevBullRsi   := bullRsi
    prevBullCci   := bullCci
    prevBullVol   := bullVol
 
//--------------------------------------------------
// BEARISH DIVERGENCES
// Price makes higher high, oscillators make lower high
//--------------------------------------------------
if isBearPivot
    currentBearIndex = bar_index - rightBars
 
    if not na(prevBearPrice)
        bearDivCci = bearPrice > prevBearPrice and bearCci < prevBearCci
        bearDivRsi = bearPrice > prevBearPrice and bearRsi < prevBearRsi
        bearVolOk  = bearVol > bearVolMa * volMult and bearVol > prevBearVol
 
        strongShort := bearDivCci and bearDivRsi and bearVolOk
        mediumShort := bearDivCci and bearDivRsi and not strongShort
        weakShort   := bearDivCci and not bearDivRsi
 
        if weakShort and showWeakSignals
            label.new(currentBearIndex, bearPrice, "Weak Short", style=label.style_label_down, color=color.new(color.orange, 0), textcolor=color.black, size=size.small)
 
        if mediumShort and showMediumSignals
            label.new(currentBearIndex, bearPrice, "Medium Short", style=label.style_label_down, color=color.new(color.red, 10), textcolor=color.white, size=size.small)
 
        if strongShort and showStrongSignals
            label.new(currentBearIndex, bearPrice, "Strong Short", style=label.style_label_down, color=color.new(color.maroon, 0), textcolor=color.white, size=size.small)
 
    prevBearPrice := bearPrice
    prevBearRsi   := bearRsi
    prevBearCci   := bearCci
    prevBearVol   := bearVol
 
//--------------------------------------------------
// OPTIONAL PIVOT MARKS
//--------------------------------------------------
plotshape(showPivotMarks and isBullPivot, title="Bull Pivot", style=shape.circle, location=location.belowbar, color=color.new(color.green, 70), size=size.tiny, offset=-rightBars)
plotshape(showPivotMarks and isBearPivot, title="Bear Pivot", style=shape.circle, location=location.abovebar, color=color.new(color.red, 70), size=size.tiny, offset=-rightBars)
 
//--------------------------------------------------
// ALERTS
//--------------------------------------------------
alertcondition(weakLong, title="Weak Long", message="Weak Long signal")
alertcondition(mediumLong, title="Medium Long", message="Medium Long signal")
alertcondition(strongLong, title="Strong Long", message="Strong Long signal")
 
alertcondition(weakShort, title="Weak Short", message="Weak Short signal")
alertcondition(mediumShort, title="Medium Short", message="Medium Short signal")
alertcondition(strongShort, title="Strong Short", message="Strong Short signal")

← 返回列表