4/4 Confirmation Signal – FINAL (Half Factor Entry)

tientran95 · study · 183 行 · 点赞 184 · TradingView 原页

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

Pine Script

//@version=5
indicator("4/4 Confirmation Signal – FINAL (Half Factor Entry)", overlay=true, max_labels_count=100)
 
// ============================================
// SETTINGS
// ============================================
 
// Volume
volLookback = input.int(20, "Volume Lookback", group="Volume")
volThreshold = input.float(1.5, "Volume Spike Threshold", step=0.1, group="Volume")
separateBuySell = input.bool(true, "Separate Buy/Sell Volume", group="Volume")
 
// CVD
cvdLength = input.int(14, "CVD Length", group="CVD")
divLookback = input.int(50, "Divergence Lookback", group="CVD")
divSensitivity = input.string("Medium", "Divergence Sensitivity", options=["Low","Medium","High"], group="CVD")
 
// Delta
deltaMALength = input.int(10, "Delta MA Length", group="Delta")
strongDeltaThreshold = input.float(60.0, "Strong Delta %", group="Delta")
 
// Structure
swingLength = input.int(10, "Swing Length", group="Structure")
structureConfirmBars = input.int(3, "Structure Confirm Bars", group="Structure")
 
// ============================================
// VOLUME
// ============================================
 
avgVolume = ta.sma(volume, volLookback)
volumeSpike = volume > avgVolume * volThreshold
 
buyVolume  = close > open ? volume : 0
sellVolume = close < open ? volume : 0
 
avgBuyVolume  = ta.sma(buyVolume, volLookback)
avgSellVolume = ta.sma(sellVolume, volLookback)
 
int volumeSignal = 0
if separateBuySell
    if volumeSpike and buyVolume > avgBuyVolume * volThreshold
        volumeSignal := 1
    else if volumeSpike and sellVolume > avgSellVolume * volThreshold
        volumeSignal := -1
else
    volumeSignal := volumeSpike ? (close > close[1] ? 1 : -1) : 0
 
// ============================================
// DELTA & CVD
// ============================================
 
delta = close > open ? volume : close < open ? -volume : 0
cvd = ta.cum(delta)
cvdSmooth = ta.ema(cvd, cvdLength)
 
// ============================================
// CVD DIVERGENCE
// ============================================
 
pivotLen = divSensitivity == "Low" ? 7 : divSensitivity == "Medium" ? 5 : 3
 
priceLow  = ta.pivotlow(low, pivotLen, pivotLen)
priceHigh = ta.pivothigh(high, pivotLen, pivotLen)
 
var float lastPL = na
var float lastCVDL = na
var int   lastPLBar = na
 
var float lastPH = na
var float lastCVDH = na
var int   lastPHBar = na
 
bool bullishDiv = false
bool bearishDiv = false
 
if not na(priceLow)
    bullishDiv := not na(lastPL) and low < lastPL and cvdSmooth > lastCVDL
    lastPL := low
    lastCVDL := cvdSmooth
    lastPLBar := bar_index
 
if not na(priceHigh)
    bearishDiv := not na(lastPH) and high > lastPH and cvdSmooth < lastCVDH
    lastPH := high
    lastCVDH := cvdSmooth
    lastPHBar := bar_index
 
int cvdSignal = bullishDiv ? 1 : bearishDiv ? -1 : 0
 
// ============================================
// DELTA PRESSURE
// ============================================
 
deltaMA = ta.sma(delta, deltaMALength)
totalVolMA = ta.sma(volume, deltaMALength)
 
buyPressure  = totalVolMA > 0 ? ta.sma(math.max(delta,0), deltaMALength)/totalVolMA*100 : 0
sellPressure = totalVolMA > 0 ? ta.sma(math.abs(math.min(delta,0)), deltaMALength)/totalVolMA*100 : 0
 
int deltaSignal = 0
if buyPressure > strongDeltaThreshold
    deltaSignal := 1
else if sellPressure > strongDeltaThreshold
    deltaSignal := -1
else
    deltaSignal := deltaMA > 0 ? 1 : deltaMA < 0 ? -1 : 0
 
// ============================================
// STRUCTURE
// ============================================
 
swingHigh = ta.pivothigh(high, swingLength, swingLength)
swingLow  = ta.pivotlow(low, swingLength, swingLength)
 
var float lastSH = na
var float lastSL = na
 
if not na(swingHigh)
    lastSH := high[swingLength]
 
if not na(swingLow)
    lastSL := low[swingLength]
 
int structureSignal = 0
if not na(lastSL) and not na(swingLow) and low[swingLength] > lastSL
    structureSignal := 1
if not na(lastSH) and not na(swingHigh) and high[swingLength] < lastSH
    structureSignal := -1
 
// ============================================
// SCORE
// ============================================
 
bullScore = 0
bearScore = 0
 
if volumeSignal == 1
    bullScore += 1
else if volumeSignal == -1
    bearScore += 1
 
if cvdSignal == 1
    bullScore += 1
else if cvdSignal == -1
    bearScore += 1
 
if deltaSignal == 1
    bullScore += 1
else if deltaSignal == -1
    bearScore += 1
 
if structureSignal == 1
    bullScore += 1
else if structureSignal == -1
    bearScore += 1
 
// ============================================
// FINAL BUY / SELL (≥ 1/2 FACTOR)
// ============================================
 
halfFactor = 2
 
earlyBuy  = bullScore >= halfFactor and bullScore > bearScore
earlySell = bearScore >= halfFactor and bearScore > bullScore
 
strongBuy  = bullScore >= 3
strongSell = bearScore >= 3
 
// ============================================
// PLOT SIGNALS
// ============================================
 
plotshape(earlyBuy,  style=shape.labelup,   location=location.belowbar,
          color=color.new(color.lime, 30), text="BUY 2/4", textcolor=color.black)
 
plotshape(earlySell, style=shape.labeldown, location=location.abovebar,
          color=color.new(color.red, 30), text="SELL 2/4", textcolor=color.white)
 
plotshape(strongBuy,  style=shape.triangleup,   location=location.belowbar,
          color=color.lime, size=size.small, text="BUY 3-4")
 
plotshape(strongSell, style=shape.triangledown, location=location.abovebar,
          color=color.red, size=size.small, text="SELL 3-4")

← 返回列表