Efficiency Weighted Hull [BackQuant]

BackQuant · study · 193 行 · 点赞 445 · 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/
// © BackQuant
 
//@version=6
indicator("Efficiency Weighted Hull [BackQuant]", shorttitle="Efficiency Hull [BackQuant]", overlay=true)
 
// Constants
const string g1 = "Calculation"
const string g2 = "Trend"
const string g3 = "UI Settings"
 
const color GREEN = #00ff00
const color RED = #ff0000
 
// Inputs
float src = input.source(close, "Source", group=g1)
int len = input.int(34, "Hull Length", minval=4, group=g1)
int erLen = input.int(20, "Efficiency Length", minval=2, group=g1)
float erPower = input.float(1.0, "Efficiency Power", minval=0.25, maxval=4.0, step=0.05, group=g1)
float minComp = input.float(0.0, "Minimum Compensation", minval=0.0, maxval=1.0, step=0.05, group=g1)
 
int slopeLen = input.int(2, "Trend Lookback", minval=1, group=g2)
int noiseLen = input.int(20, "Slope Noise Length", minval=2, group=g2)
float deadZone = input.float(0.20, "Slope Dead Zone", minval=0.0, maxval=3.0, step=0.05, group=g2)
 
bool showRibbon = input.bool(true, "Show Adaptive Ribbon", group=g3)
bool showGradient = input.bool(true, "Show Price Gradient", group=g3)
bool showGlow = input.bool(true, "Show Glow", group=g3)
bool showBase = input.bool(false, "Show Base Hull Reference", group=g3)
bool paintCandles = input.bool(true, "Paint Candles", group=g3)
int lineWidth = input.int(3, "Line Width", minval=1, maxval=6, group=g3)
color longCol = input.color(GREEN, "Bullish", group=g3, inline="COL")
color shortCol = input.color(RED, "Bearish", group=g3, inline="COL")
 
// Lengths
int halfLen = math.max(1, int(math.round(len / 2.0)))
int rootLen = math.max(1, int(math.round(math.sqrt(len))))
 
// Kaufman Efficiency Ratio
float displacement = math.abs(src - src[erLen])
float path = ta.sma(math.abs(src - src[1]), erLen) * erLen
float efficiency = path > 0.0 ? math.min(displacement / path, 1.0) : 0.0
 
float adjustedER = math.pow(efficiency, erPower)
float compensation = minComp + (1.0 - minComp) * adjustedER
 
// Efficiency Weighted Hull
float fast = ta.wma(src, halfLen)
float slow = ta.wma(src, len)
 
float hullRaw = fast + compensation * (fast - slow)
float efficiencyHull = ta.wma(hullRaw, rootLen)
 
// Standard Hull Reference
float standardRaw = 2.0 * fast - slow
float standardHull = ta.wma(standardRaw, rootLen)
 
// Slow Structural Reference
float base = ta.wma(slow, rootLen)
 
// Trend State
float slope = efficiencyHull - efficiencyHull[slopeLen]
float slopeNoise = ta.ema(math.abs(efficiencyHull - efficiencyHull[1]), noiseLen)
float threshold = slopeNoise * deadZone
 
var int trendState = 0
 
if trendState == 0
    trendState := slope >= 0.0 ? 1 : -1
else if slope > threshold
    trendState := 1
else if slope < -threshold
    trendState := -1
 
bool bullFlip = trendState == 1 and trendState[1] == -1
bool bearFlip = trendState == -1 and trendState[1] == 1
 
color trendCol = trendState == 1 ? longCol : shortCol
 
// Strength
float slopeStrength = slopeNoise > 0.0 ? math.min(math.abs(slope) / (slopeNoise * 3.0), 1.0) : 0.0
float trendStrength = math.min(efficiency * 0.60 + slopeStrength * 0.40, 1.0)
 
int strengthBoost = int(math.round(trendStrength * 18.0))
 
// Main Trend
pTrend = plot(efficiencyHull, "Efficiency Weighted Hull", trendCol, lineWidth)
pBase = plot(showRibbon or showBase ? base : na, "Adaptive Base", color.new(trendCol, showBase ? 55 : 88), showBase ? 2 : 1)
 
// Adaptive Ribbon
float ribbonTop = math.max(efficiencyHull, base)
float ribbonBottom = math.min(efficiencyHull, base)
 
color ribbonTopCol = efficiencyHull >= base ? color.new(trendCol, 32) : color.new(trendCol, 90)
color ribbonBottomCol = efficiencyHull >= base ? color.new(trendCol, 90) : color.new(trendCol, 32)
 
fill(
     pTrend,
     pBase,
     ribbonTop,
     ribbonBottom,
     showRibbon ? ribbonTopCol : na,
     showRibbon ? ribbonBottomCol : na,
     title="Efficiency Ribbon")
 
// Price Gradient
bool fillReady = showGradient and not na(efficiencyHull)
 
float grad1 = efficiencyHull + (close - efficiencyHull) * 0.18
float grad2 = efficiencyHull + (close - efficiencyHull) * 0.42
float grad3 = efficiencyHull + (close - efficiencyHull) * 0.70
 
pPrice = plot(fillReady ? close : na, "Price Ref", display=display.none, editable=false)
pGrad1 = plot(fillReady ? grad1 : na, "Gradient Ref 1", display=display.none, editable=false)
pGrad2 = plot(fillReady ? grad2 : na, "Gradient Ref 2", display=display.none, editable=false)
pGrad3 = plot(fillReady ? grad3 : na, "Gradient Ref 3", display=display.none, editable=false)
 
bool priceAbove = close >= efficiencyHull
 
fill(
     pTrend,
     pGrad1,
     priceAbove ? grad1 : efficiencyHull,
     priceAbove ? efficiencyHull : grad1,
     fillReady ? color.new(trendCol, 84) : na,
     fillReady ? color.new(trendCol, math.max(28, 56 - strengthBoost)) : na,
     title="Gradient 1")
 
fill(
     pGrad1,
     pGrad2,
     priceAbove ? grad2 : grad1,
     priceAbove ? grad1 : grad2,
     fillReady ? color.new(trendCol, 92) : na,
     fillReady ? color.new(trendCol, 72) : na,
     title="Gradient 2")
 
fill(
     pGrad2,
     pGrad3,
     priceAbove ? grad3 : grad2,
     priceAbove ? grad2 : grad3,
     fillReady ? color.new(trendCol, 96) : na,
     fillReady ? color.new(trendCol, 84) : na,
     title="Gradient 3")
 
fill(
     pGrad3,
     pPrice,
     priceAbove ? close : grad3,
     priceAbove ? grad3 : close,
     fillReady ? color.new(trendCol, 99) : na,
     fillReady ? color.new(trendCol, 94) : na,
     title="Gradient 4")
 
// Glow
float adaptiveDistance = math.abs(efficiencyHull - base)
float averageStep = ta.ema(math.abs(efficiencyHull - efficiencyHull[1]), noiseLen)
 
float glowWidth = math.max(adaptiveDistance * 0.08, averageStep * 0.50)
float outerGlow = glowWidth * 2.2
 
pGlowLower = plot(showGlow ? efficiencyHull - glowWidth : na, "Glow Lower", display=display.none, editable=false)
pGlowUpper = plot(showGlow ? efficiencyHull + glowWidth : na, "Glow Upper", display=display.none, editable=false)
pOuterLower = plot(showGlow ? efficiencyHull - outerGlow : na, "Outer Glow Lower", display=display.none, editable=false)
pOuterUpper = plot(showGlow ? efficiencyHull + outerGlow : na, "Outer Glow Upper", display=display.none, editable=false)
 
fill(pOuterLower, pOuterUpper, showGlow ? color.new(trendCol, 96) : na, title="Outer Glow")
fill(pGlowLower, pGlowUpper, showGlow ? color.new(trendCol, math.max(62, 86 - strengthBoost)) : na, title="Inner Glow")
 
// Candles
plotcandle(
     open, high, low, close,
     "Trend Candles",
     trendCol,
     trendCol,
     true,
     bordercolor=trendCol,
     display=paintCandles ? display.all : display.none)
 
// Data Window
plot(efficiency, "Efficiency Ratio", display=display.data_window)
plot(compensation, "Hull Compensation", display=display.data_window)
plot(fast, "Fast WMA", display=display.data_window)
plot(slow, "Slow WMA", display=display.data_window)
plot(hullRaw, "Adaptive Hull Raw", display=display.data_window)
plot(standardHull, "Standard Hull", display=display.data_window)
plot(slope, "Trend Slope", display=display.data_window)
plot(trendStrength * 100.0, "Trend Strength", display=display.data_window)
 
// Alerts
alertcondition(bullFlip, "Efficiency Hull Bullish", "Efficiency Weighted Hull turned bullish on {{ticker}}")
alertcondition(bearFlip, "Efficiency Hull Bearish", "Efficiency Weighted Hull turned bearish on {{ticker}}")

← 返回列表