FDH Fractal Dimension & Hurst

Drizzle_Algo56 · study · 128 行 · 点赞 13 · 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/
// © Drizzel_Algo56
//@version=6
indicator("FDH | Fractal Dimension & Hurst", overlay = false, behind_chart = false, max_bars_back = 500, precision = 3)
 
int len = input.int(30, "Fractal Lookback", minval = 10, maxval = 500, group = "Calculation")
int smoothLen = input.int(3, "EMA Smoothing", minval = 1, group = "Calculation")
float lowerLevel = input.float(1.40, "Lower Level", step = 0.01, group = "Levels")
float upperLevel = input.float(1.50, "Upper / Brownian Reference", step = 0.01, group = "Levels")
float extremeLevel = input.float(1.60, "Extreme Level", step = 0.01, group = "Levels")
float hurstLow = input.float(1.30, "Hurst Low Zone", step = 0.01, group = "Levels")
float hurstHigh = input.float(1.40, "Hurst High Zone", step = 0.01, group = "Levels")
bool showHurstRefs = input.bool(true, "Show Hurst References", group = "Levels")
bool showFill = input.bool(true, "Smooth Pane Gradients", group = "Style")
int opacity = input.int(65, "Gradient Strength (%)", minval = 0, maxval = 100, group = "Style")
bool showLowerZone = input.bool(true, "Show Box Below 1.40", group = "Box Lower Zone")
color lowerZoneColor = input.color(#2962FF, "Box Below 1.40 Color", group = "Box Lower Zone")
int lowerZoneOpacity = input.int(65, "Box Below 1.40 Gradient Strength (%)", minval = 0, maxval = 100, group = "Box Lower Zone")
color boxColor = input.color(#2962FF, "Path Dimension Color", group = "Line Colors")
color extremeColor = input.color(#123A8C, "Extreme Zone Color", group = "Zone Colors")
color hurstColor = input.color(#E040FB, "Hurst Dimension Color", group = "Line Colors")
bool showHurstHighZone = input.bool(true, "Show Hurst Above 1.40", group = "Hurst Upper Zone")
color hurstHighColor = input.color(#9C27B0, "Hurst Above 1.40 Color", group = "Hurst Upper Zone")
int hurstHighOpacity = input.int(40, "Hurst Above 1.40 Gradient Strength (%)", minval = 0, maxval = 100, group = "Hurst Upper Zone")
bool showHurstFill = input.bool(true, "Hurst Zone Gradients", group = "Hurst Style")
int hOpacity = input.int(40, "Hurst Gradient Strength (%)", minval = 0, maxval = 100, group = "Hurst Style")
 
if barstate.isfirst and not (lowerLevel < upperLevel and upperLevel < extremeLevel)
    runtime.error("Levels must satisfy Lower < Upper < Extreme.")
if barstate.isfirst and not (hurstLow < hurstHigh)
    runtime.error("Hurst Low Zone must be below Hurst High Zone.")
 
f_sevcik(float src, int n) =>
    float hi = ta.highest(src, n)
    float lo = ta.lowest(src, n)
    float rng = hi - lo
    float length = 0.0
    if rng > 0
        for i = 0 to n - 2
            float dy = (src[i + 1] - src[i]) / rng
            float dx = 1.0 / (n - 1.0)
            length += math.sqrt(dy * dy + dx * dx)
    float result = 1.5
    if rng > 0 and length > 0
        result := 1.0 + (math.log(length) + math.log(2.0)) / math.log(2.0 * (n - 1.0))
    na(src[n - 1]) ? na : result
 
f_hurst_dimension(float src, int n) =>
    float meanVal = ta.sma(src, n)
    float cumulative = 0.0
    float maxDev = -1e10
    float minDev = 1e10
    float squared = 0.0
    for i = 0 to n - 1
        float d = src[i] - meanVal
        squared += d * d
        cumulative += d
        maxDev := math.max(maxDev, cumulative)
        minDev := math.min(minDev, cumulative)
    float sd = math.sqrt(squared / n)
    float rs = sd > 0 ? (maxDev - minDev) / sd : 1.0
    float h = rs > 0 ? math.log(rs) / math.log(n) : 0.5
    h := math.max(0.01, math.min(0.99, h))
    na(src[n - 1]) ? na : 2.0 - h
 
float dBox = ta.ema(f_sevcik(close, len), smoothLen)
float dHurst = ta.ema(f_hurst_dimension(close, len), smoothLen)
 
// Each zone uses one continuous opacity ramp, equivalent to the old
// three adjoining linear ramps, with fewer hidden plots and fill calls.
 
bool upperActive = showFill and dBox > upperLevel
float upperBase = upperActive ? upperLevel : na
float upperEdge = upperActive ? math.min(dBox, extremeLevel) : na
upperBasePlot = plot(upperBase, "Upper Fill Base", color = na, display = display.none)
upperEdgePlot = plot(upperEdge, "Upper Fill Edge", color = na, display = display.none)
fill(upperEdgePlot, upperBasePlot,
     upperEdge, upperBase,
     color.new(boxColor, 100 - opacity), color.new(boxColor, 100),
     title = "Upper Gradient", fillgaps = false)
 
bool extremeActive = showFill and dBox > extremeLevel
float extremeBase = extremeActive ? extremeLevel : na
float extremeEdge = extremeActive ? dBox : na
extremeBasePlot = plot(extremeBase, "Extreme Fill Base", color = na, display = display.none)
extremeEdgePlot = plot(extremeEdge, "Extreme Fill Edge", color = na, display = display.none)
fill(extremeEdgePlot, extremeBasePlot,
     extremeEdge, extremeBase,
     color.new(extremeColor, 100 - opacity), color.new(extremeColor, 100),
     title = "Extreme Gradient", fillgaps = false)
 
bool lowerActive = showFill and showLowerZone and dBox < lowerLevel
float lowerBase = lowerActive ? lowerLevel : na
float lowerEdge = lowerActive ? dBox : na
lowerBasePlot = plot(lowerBase, "Lower Fill Base", color = na, display = display.none)
lowerEdgePlot = plot(lowerEdge, "Lower Fill Edge", color = na, display = display.none)
fill(lowerBasePlot, lowerEdgePlot,
     lowerBase, lowerEdge,
     color.new(lowerZoneColor, 100), color.new(lowerZoneColor, 100 - lowerZoneOpacity),
     title = "Lower Gradient", fillgaps = false)
 
bool hurstLowerActive = showHurstFill and dHurst < hurstLow
float hurstLowerBase = hurstLowerActive ? hurstLow : na
float hurstLowerEdge = hurstLowerActive ? dHurst : na
hurstLowerBasePlot = plot(hurstLowerBase, "Hurst Low Fill Base", color = na, display = display.none)
hurstLowerEdgePlot = plot(hurstLowerEdge, "Hurst Low Fill Edge", color = na, display = display.none)
fill(hurstLowerBasePlot, hurstLowerEdgePlot,
     hurstLowerBase, hurstLowerEdge,
     color.new(hurstColor, 100), color.new(hurstColor, 100 - hOpacity),
     title = "Hurst Low Gradient", fillgaps = false)
 
bool hurstUpperActive = showHurstFill and showHurstHighZone and dHurst > hurstHigh
float hurstUpperBase = hurstUpperActive ? hurstHigh : na
float hurstUpperEdge = hurstUpperActive ? dHurst : na
hurstUpperBasePlot = plot(hurstUpperBase, "Hurst High Fill Base", color = na, display = display.none)
hurstUpperEdgePlot = plot(hurstUpperEdge, "Hurst High Fill Edge", color = na, display = display.none)
fill(hurstUpperEdgePlot, hurstUpperBasePlot,
     hurstUpperEdge, hurstUpperBase,
     color.new(hurstHighColor, 100 - hurstHighOpacity), color.new(hurstHighColor, 100),
     title = "Hurst High Gradient", fillgaps = false)
 
plot(dBox, "Path Dimension | Sevcik", color = boxColor, linewidth = 2)
plot(dHurst, "Hurst Dimension Proxy", color = hurstColor, linewidth = 2)
hline(lowerLevel, "Lower Level", color.new(boxColor, 35), hline.style_dashed)
hline(upperLevel, "Upper Level", color.new(boxColor, 35), hline.style_dashed)
hline(extremeLevel, "Extreme Level", color.new(boxColor, 25), hline.style_dashed)
hline(hurstLow, "Hurst Low Threshold", color.new(hurstColor, 35), hline.style_dotted, display = showHurstRefs ? display.all : display.none)
hline(hurstHigh, "Hurst High Threshold", color.new(hurstHighColor, 35), hline.style_dotted, display = showHurstRefs ? display.all : display.none)

← 返回列表