Sadukey_trend_v8

duffnerrola · study · 204 行 · 点赞 8 · 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/
// © ron 68
 
//@version=6
indicator("Sadukey_trend_v8", overlay=true, shorttitle="Sadukey Ichi")
 
// +----------------------------------------------+
// |  Indicator Input Parameters                  |
// +----------------------------------------------+
var groupMain = "General Settings"
shiftOffset = input.int(0, title="Shift (Offset)", group=groupMain)
minChange   = input.float(0.0, title="Minimum change for Strong/Weak", minval=0.0, step=0.0001, group=groupMain)
 
var groupCons = "Consolidation (Ichimoku Cloud)"
useConsZone     = input.bool(true, title="Enable consolidation zone", group=groupCons)
usePriceInCloud = input.bool(true, title="Rule 1: Price inside the cloud", group=groupCons, tooltip="Gray when the closing price lies between the two Sadukey lines.")
useThinCloud    = input.bool(true, title="Rule 2: Cloud is extremely thin (squeeze)", group=groupCons, tooltip="Gray when the distance between the two lines is minimal.")
consThreshold   = input.float(0.1, title="Max. cloud thickness (ATR factor)", step=0.05, minval=0.0, group=groupCons, tooltip="0.1 means: the distance is smaller than 10% of the normal candle size (ATR). Adapts automatically to any asset.")
 
var groupEarly = "Early Warning (earlier consolidation signal)"
useEarlyDetection  = input.bool(true, title="Rule 3: Enable convergence early warning", group=groupEarly, tooltip="Detects an emerging consolidation while the cloud is still narrowing, instead of waiting until it is already thin.")
convergenceBars    = input.int(3, title="Bars of uninterrupted narrowing", minval=2, maxval=10, group=groupEarly)
earlyThresholdMult = input.float(2.0, title="Early warning threshold multiplier", minval=1.0, step=0.1, group=groupEarly, tooltip="The cloud counts as an emerging consolidation when it is smaller than the squeeze threshold x this factor AND has been narrowing continuously for several bars.")
 
var groupStyle = "Colors & Display"
lineThickness = input.int(2, title="Line thickness", minval=1, maxval=10, group=groupStyle)
fillTransp    = input.int(60, title="Fill transparency (0-100)", minval=0, maxval=100, group=groupStyle)
 
colorStrongUp   = input.color(color.blue, title="Strong uptrend", group=groupStyle)
colorWeakUp     = input.color(color.rgb(136, 179, 253), title="Weak uptrend", group=groupStyle)
colorStrongDown = input.color(color.red, title="Strong downtrend", group=groupStyle)
colorWeakDown   = input.color(color.rgb(255, 172, 172), title="Weak downtrend", group=groupStyle)
colorCons       = input.color(color.gray, title="Consolidation phase (cloud)", group=groupStyle)
 
showSignals  = input.bool(true, title="Show Buy/Sell/Cons signals", group=groupStyle)
showWarnings = input.bool(true, title="Show weakness warnings", group=groupStyle)
 
var groupDiv = "Divergence Detection"
showDivergence = input.bool(true, title="Show divergences", group=groupDiv)
pivotLeft   = input.int(5, title="Pivot bars left", minval=1, group=groupDiv)
pivotRight  = input.int(5, title="Pivot bars right", minval=1, group=groupDiv)
maxDivRange = input.int(60, title="Max. distance (bars)", minval=1, group=groupDiv)
minDivRange = input.int(5, title="Min. distance (bars)", minval=1, group=groupDiv)
colorBullDiv = input.color(color.new(color.lime, 0), title="Bullish divergence", group=groupDiv)
colorBearDiv = input.color(color.new(color.orange, 0), title="Bearish divergence", group=groupDiv)
 
// +----------------------------------------------+
// |  Coefficients Array                          |
// +----------------------------------------------+
var array<float> coeffs = array.from(0.11859648, 0.11781324, 0.11548308, 0.11166411, 0.10645106, 0.09997253, 0.09238688, 0.08387751, 0.07464713, 0.06491178, 0.05489443, 0.04481833, 0.03490071, 0.02534672, 0.01634375, 0.00805678, 0.00062421, -0.00584512, -0.01127391, -0.01561738, -0.01886307, -0.02102974, -0.02216516, -0.02234315, -0.02165992, -0.02022973, -0.01818026, -0.01564777, -0.01277219, -0.00969230, -0.00654127, -0.00344276, -0.00050728, 0.00217042, 0.00451354, 0.00646441, 0.00798513, 0.00905725, 0.00968091, 0.00987326, 0.00966639, 0.00910488, 0.00824306, 0.00714199, 0.00586655, 0.00448255, 0.00305396, 0.00164061, 0.00029596, -0.00093445, -0.00201426, -0.00291701, -0.00362661, -0.00413703, -0.00445206, -0.00458437, -0.00455457, -0.00439006, -0.00412379, -0.00379323, -0.00343966, -0.00310850, -0.00285188, -0.00273508, -0.00274361, 0.01018757)
 
// +----------------------------------------------+
// |  Main indicator calculation loop             |
// +----------------------------------------------+
ohlc4_val = (open + close + high + low) / 4.0
p1_val = (ohlc4_val + close) / 2.0
p2_val = (ohlc4_val + open) / 2.0
 
max_bars_back(p1_val, 66)
max_bars_back(p2_val, 66)
 
float vel1 = na
float vel2 = na
 
if bar_index >= 65
    vel1 := 0.0
    vel2 := 0.0
    for i = 0 to 65
        coeff = array.get(coeffs, i)
        vel1 += p1_val[i] * coeff
        vel2 += p2_val[i] * coeff
 
// +----------------------------------------------+
// |  Trend & Ichimoku consolidation              |
// +----------------------------------------------+
float diff = vel1 - vel2
 
// Cloud boundaries
float cloudTop = math.max(vel1, vel2)
float cloudBot = math.min(vel1, vel2)
 
// Rule 1: Is the closing price inside the cloud?
bool priceInCloud = (close <= cloudTop) and (close >= cloudBot)
 
// Rule 2: Is the cloud extremely thin? (measured against the dynamic ATR)
float atr = ta.atr(14)
float cloudThickness = math.abs(vel1 - vel2)
bool cloudIsThin = cloudThickness < (atr * consThreshold)
 
// Rule 3: Early warning - the cloud has been narrowing for several bars in a row
// and is already well below its "normal" level, even if it has not yet reached
// the tight threshold of Rule 2.
bool isNarrowing = true
for i = 0 to convergenceBars - 2
    isNarrowing := isNarrowing and (cloudThickness[i] < cloudThickness[i + 1])
 
bool isApproachingThin = cloudThickness < (atr * consThreshold * earlyThresholdMult)
bool earlyConsZone = useEarlyDetection and isNarrowing and isApproachingThin
 
// Combined consolidation state
bool inConsZone = useConsZone and ((usePriceInCloud and priceInCloud) or (useThinCloud and cloudIsThin) or earlyConsZone)
 
// Trend strength (only applies when NOT in consolidation)
bool isStrongUp   = diff > 0 and diff > nz(diff[1]) + minChange
bool isWeakUp     = diff > 0 and diff <= nz(diff[1]) + minChange
bool isStrongDown = diff < 0 and diff < nz(diff[1]) - minChange
bool isWeakDown   = diff < 0 and diff >= nz(diff[1]) - minChange
 
color plotColor = color.gray
if inConsZone
    plotColor := colorCons
else if isStrongUp
    plotColor := colorStrongUp
else if isWeakUp
    plotColor := colorWeakUp
else if isStrongDown
    plotColor := colorStrongDown
else if isWeakDown
    plotColor := colorWeakDown
 
p1_plot = plot(vel1, color=plotColor, linewidth=lineThickness, offset=shiftOffset, editable=false)
p2_plot = plot(vel2, color=plotColor, linewidth=lineThickness, offset=shiftOffset, editable=false)
fill(p1_plot, p2_plot, color=color.new(plotColor, fillTransp), title="Fill")
 
// +----------------------------------------------+
// |  Signals: Buy, Sell, Cloud breakout          |
// +----------------------------------------------+
// A signal is triggered by a normal cross OR by a breakout from the gray cloud
bool crossUp = ta.crossover(diff, 0)
bool crossDn = ta.crossunder(diff, 0)
 
bool breakoutUp = diff > 0 and inConsZone[1] and not inConsZone
bool breakoutDn = diff < 0 and inConsZone[1] and not inConsZone
 
bool buySignalRaw  = crossUp or breakoutUp
bool sellSignalRaw = crossDn or breakoutDn
bool justEnteredCons = inConsZone and not inConsZone[1]
 
buySignal  = buySignalRaw and barstate.isconfirmed
sellSignal = sellSignalRaw and barstate.isconfirmed
consSignal = justEnteredCons and barstate.isconfirmed
 
warnWeakUpRaw   = isWeakUp and not isWeakUp[1]
warnWeakDownRaw = isWeakDown and not isWeakDown[1]
warnWeakUp   = warnWeakUpRaw and barstate.isconfirmed
warnWeakDown = warnWeakDownRaw and barstate.isconfirmed
 
plotshape(buySignal and showSignals, title="Buy Signal", style=shape.labelup, location=location.belowbar, color=color.new(color.blue, 0), text="BUY", textcolor=color.white, size=size.small, offset=shiftOffset)
plotshape(sellSignal and showSignals, title="Sell Signal", style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), text="SELL", textcolor=color.white, size=size.small, offset=shiftOffset)
// Consolidation signal is drawn above the candle, like Buy/Sell
plotshape(consSignal and showSignals, title="Consolidation", style=shape.diamond, location=location.abovebar, color=color.new(color.gray, 0), text="CONS", textcolor=color.white, size=size.small, offset=shiftOffset)
 
plotshape(warnWeakUp and showWarnings, title="Warning: uptrend weakening", style=shape.triangleup, location=location.belowbar, color=color.new(#38f08b, 0), size=size.tiny, offset=shiftOffset)
plotshape(warnWeakDown and showWarnings, title="Warning: downtrend weakening", style=shape.triangledown, location=location.abovebar, color=color.new(#daad31, 0), size=size.tiny, offset=shiftOffset)
 
// +----------------------------------------------+
// |  Divergence detection                        |
// +----------------------------------------------+
float pivLow  = ta.pivotlow(diff, pivotLeft, pivotRight)
float pivHigh = ta.pivothigh(diff, pivotLeft, pivotRight)
 
bool plFound = not na(pivLow)
bool phFound = not na(pivHigh)
 
float prevPivLowDiff   = ta.valuewhen(plFound, diff[pivotRight], 1)
float prevPivLowPrice  = ta.valuewhen(plFound, low[pivotRight], 1)
float prevPivHighDiff  = ta.valuewhen(phFound, diff[pivotRight], 1)
float prevPivHighPrice = ta.valuewhen(phFound, high[pivotRight], 1)
 
int barsSincePL = ta.barssince(plFound[1])
int barsSincePH = ta.barssince(phFound[1])
 
bool inRangePL = maxDivRange >= barsSincePL and barsSincePL >= minDivRange
bool inRangePH = maxDivRange >= barsSincePH and barsSincePH >= minDivRange
 
bool diffHigherLow = plFound and diff[pivotRight] > prevPivLowDiff and inRangePL
bool priceLowerLow = plFound and low[pivotRight] < prevPivLowPrice
bool bullishDivergence = showDivergence and diffHigherLow and priceLowerLow
 
bool diffLowerHigh = phFound and diff[pivotRight] < prevPivHighDiff and inRangePH
bool priceHigherHigh = phFound and high[pivotRight] > prevPivHighPrice
bool bearishDivergence = showDivergence and diffLowerHigh and priceHigherHigh
 
plotshape(bullishDivergence, title="Bullish Divergence", style=shape.labelup, location=location.belowbar, color=colorBullDiv, text="Bull Div", textcolor=color.white, size=size.small, offset=-pivotRight + shiftOffset)
plotshape(bearishDivergence, title="Bearish Divergence", style=shape.labeldown, location=location.abovebar, color=colorBearDiv, text="Bear Div", textcolor=color.white, size=size.small, offset=-pivotRight + shiftOffset)
 
// +----------------------------------------------+
// |  Alerts                                      |
// +----------------------------------------------+
if buySignal and showSignals
    alert("Sadukey BUY: " + syminfo.ticker + " @ " + str.tostring(close) + " (" + timeframe.period + ")", alert.freq_once_per_bar_close)
 
if sellSignal and showSignals
    alert("Sadukey SELL: " + syminfo.ticker + " @ " + str.tostring(close) + " (" + timeframe.period + ")", alert.freq_once_per_bar_close)
 
if consSignal and showSignals
    alert("Sadukey CONSOLIDATION: market is entering a sideways phase @ " + str.tostring(close) + " (" + timeframe.period + ")", alert.freq_once_per_bar_close)
 
if (warnWeakUp or warnWeakDown) and showWarnings
    alert("Sadukey WARNING: trend is weakening - " + syminfo.ticker + " @ " + str.tostring(close) + " (" + timeframe.period + ")", alert.freq_once_per_bar_close)
 
alertcondition(buySignal, title="Buy Alert", message="Sadukey: BUY signal!")
alertcondition(sellSignal, title="Sell Alert", message="Sadukey: SELL signal!")
alertcondition(consSignal, title="Consolidation Alert", message="Sadukey: market is entering consolidation!")

← 返回列表