UniEMA Cascade

igorad · study · 273 行 · 点赞 38 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © igorad
//
// UniEMA Cascade v1.4 - every Order stage of UniEMA plotted
// Author: Igor Durkin (igorad), (C) 2011-2026
// Algorithm: "The UniEMA: A Universal Exponential Moving Average",
//            Technical Analysis of Stocks & Commodities, November 2026
//@version=6
 
indicator("UniEMA Cascade", overlay = true)
 
enum prType
    iclose      = "Close"
    iopen       = "Open"
    ihigh       = "High"
    ilow        = "Low"
    imedian     = "Median"
    itypical    = "Typical"
    iwclose     = "Weighted Close"
    imbody      = "Median Body"
    iavg        = "Average"
    itrendb     = "Trend Biased"
    itrendbe    = "Trend Biased(Ext)"
    idemark     = "Demark Weighted Close"
    iwmbody     = "Weighted Median Body"
    haclose     = "HA Close"
    haopen      = "HA Open"
    hahigh      = "HA High"
    halow       = "HA Low"
    hamedian    = "HA Median"
    hatypical   = "HA Typical"
    hawclose    = "HA Weighted Close"
    hambody     = "HA Median Body"
    haavg       = "HA Average"
    hatrendb    = "HA Trend Biased"
    hatrendbe   = "HA Trend Biased(Ext)"
    hademark    = "HA Demark Weighted Close"
    hawmbody    = "HA Weighted Median Body"
 
const int MAX_ORDER = 30
 
//---- Calculation parameters
const string GC = "Calculation"
string  tf            = input.timeframe("", "TimeFrame", group = GC,
     tooltip = "Empty = chart timeframe. A higher timeframe uses confirmed bars only (no repainting). A timeframe lower than the chart's falls back to the chart timeframe.")
prType  iprice        = input.enum(prType.iclose, "Price", group = GC)
int     length        = input.int(14, "Length", minval = 1, group = GC)
int     poles         = input.int(1, "Poles", minval = 1, group = GC)
int     order         = input.int(20, "Order", minval = 1, maxval = MAX_ORDER, group = GC,
     tooltip = "Number of cascade stages; each stage is plotted as a line.")
float   weightFactor  = input.float(2.0, "Weight Factor", minval = 0.01, step = 0.1, group = GC,
     tooltip = "Wilder = 1, EMA = 2. Lag of stage k = k * (Length - 1) / (Weight Factor * Order).")
float   dampingFactor = input.float(0.0, "Damping Factor", step = 0.1, group = GC,
     tooltip = "Lag compensation between poles: 0 = plain cascade, 1 = full (DEMA at Poles = 2). No effect at Poles = 1.")
 
//---- Display parameters
const string GD = "Display"
bool    showCascade   = input.bool(true, "Show Cascade", group = GD)
string  coloring      = input.string("Slope Strength", "Coloring", options = ["Slope Strength", "Speed", "Slope", "Gradient"], group = GD,
     tooltip = "Slope Strength: speed and direction - Neutral Color at flat slope, moving to UpTrend / DnTrend Color as the slope grows.\nSpeed: speed only - Slow Color at flat slope, moving to Fast Color as the slope grows in either direction.\nSlope: UpTrend / DnTrend Color by direction only.\nGradient: color by stage number, from Fast Color (stage 1) to Slow Color (last stage); shows the cascade structure.")
float   slopeScale    = input.float(0.5, "Slope Scale", minval = 0.01, step = 0.05, group = GD,
     tooltip = "Slope Strength / Speed: |line change| / average |price change| at which a line reaches full color. Roughly an efficiency ratio: near 1 = clean trend, near 0 = chop.")
int     slopePeriodIn = input.int(0, "Slope Period", minval = 0, group = GD,
     tooltip = "Period of the average |price change| (Wilder smoothing). 0 = Length.")
color   neutralColor  = input.color(color.new(color.gray, 70), "Neutral Color", group = GD)
color   upColor       = input.color(color.blue, "UpTrend Color", group = GD)
color   dnColor       = input.color(color.red, "DnTrend Color", group = GD)
color   fastColor     = input.color(color.yellow, "Fast Color", group = GD)
color   slowColor     = input.color(color.blue, "Slow Color", group = GD)
int     cascadeWidth  = input.int(1, "Cascade Line Width", minval = 1, group = GD)
int     lineWidth     = input.int(3, "UniEMA Line Width", minval = 1, group = GD)
string  lineStyle     = input.string("Line", "Line Style", options = ["Line", "Step"], group = GD,
     tooltip = "Step suits a higher TimeFrame.")
 
int slopePeriod = slopePeriodIn > 0 ? slopePeriodIn : length
 
 
getPrice(prType itype) =>
    var float haC = na
    var float haO = na
    float pC = haC
    float pO = haO
    if barstate.isfirst
        haO := open
        haC := close
    else
        haC := (open + high + low + close) / 4
        haO := (pO + pC) / 2
    float haH = math.max(high, haO, haC)
    float haL = math.min(low,  haO, haC)
 
    switch itype
        prType.iclose    => close
        prType.iopen     => open
        prType.ihigh     => high
        prType.ilow      => low
        prType.imedian   => hl2
        prType.itypical  => hlc3
        prType.iwclose   => hlcc4
        prType.imbody    => (open + close) / 2
        prType.iavg      => (high + low + open + close) / 4
        prType.itrendb   => close > open ? (high + close) / 2 : (low + close) / 2
        prType.itrendbe  => close > open ? high : low
        prType.idemark   => close > open ? (2 * high + low + close) / 4 : close < open ? (high + 2 * low + close) / 4 : (high + low + 2 * close) / 4
        prType.iwmbody   => (high + low + (open + close) / 2) / 3
        prType.haclose   => haC
        prType.haopen    => haO
        prType.hahigh    => haH
        prType.halow     => haL
        prType.hamedian  => (haH + haL) / 2
        prType.hatypical => (haH + haL + haC) / 3
        prType.hawclose  => (haH + haL + haC + haC) / 4
        prType.hambody   => (haO + haC) / 2
        prType.haavg     => (haH + haL + haO + haC) / 4
        prType.hatrendb  => haC > haO ? (haH + haC) / 2 : (haL + haC) / 2
        prType.hatrendbe => haC > haO ? haH : haL
        prType.hademark  => haC > haO ? (2 * haH + haL + haC) / 4 : haC < haO ? (haH + 2 * haL + haC) / 4 : (haH + haL + 2 * haC) / 4
        prType.hawmbody  => (haH + haL + (haO + haC) / 2) / 3
 
 
// UniXMA_arr: same core as UniEMA (constant memory, intrabar recompute from
// previous state); returns a fresh array with the output of every Order stage.
unixmaArr(float src, int len, int poles, int order, float wf, float df) =>
    int n = poles * order
    var tmp  = array.new_float(n, src)
    var prev = array.new_float(n, src)
    var int prevBar = -1
    float alpha = wf * order / (len + wf * order - 1)
    float bet   = 1.0 - alpha
 
    if bar_index != prevBar
        for i = 0 to n - 1
            prev.set(i, tmp.get(i))
        prevBar := bar_index
 
    float[] out = array.new_float(order, src)
    if not barstate.isfirst
        float uniema = src
        for k = 0 to order - 1
            for j = 0 to poles - 1
                int   i = poles * k + j
                float v = bet * prev.get(i) + alpha * uniema
                tmp.set(i, v)
                uniema := j == 0 ? v : uniema + df * (tmp.get(poles * k) - v)
            out.set(k, uniema)
    out
 
 
// Both in the requested timeframe's context, so colors stay consistent on a higher TimeFrame:
//   out - stage values
//   nrm - stage slope / average |price change| (signed)
calc(int off) =>
    float   p   = getPrice(iprice)
    float[] out = unixmaArr(p, length, poles, order, weightFactor, dampingFactor)
    float[] prv = out[1]
    float   mad = ta.rma(nz(math.abs(p - p[1])), slopePeriod)
 
    float[] nrm = array.new_float(order, 0.0)
    if not na(prv) and mad > 0
        for k = 0 to order - 1
            nrm.set(k, (out.get(k) - prv.get(k)) / mad)
    [out[off], nrm[off]]
 
 
bool htf = timeframe.in_seconds(tf) > timeframe.in_seconds()
 
// confirmed higher-timeframe bars only: previous HTF bar with lookahead_on (no repainting)
[hout, hnrm] = request.security(syminfo.tickerid, tf, calc(1), lookahead = barmerge.lookahead_on)
[cout, cnrm] = calc(0)
float[] out = htf ? hout : cout
float[] nrm = htf ? hnrm : cnrm
 
// value of stage k; the last stage is drawn separately as the main line
stageVal(float[] a, int k) =>
    k < order - 1 and not na(a) ? a.get(k) : na
 
slopeColor(float v) =>
    var int t = 0
    t := v > v[1] ? 1 : v < v[1] ? -1 : t
    t > 0 ? upColor : t < 0 ? dnColor : na
 
// normalized slope of stage k
nrmVal(int k) =>
    k < order and not na(nrm) ? nrm.get(k) : na
 
// Slope Strength: Neutral .. UpTrend / DnTrend
strengthColor(int k) =>
    float x = nrmVal(k)
    na(x) or x == 0 ? neutralColor : color.from_gradient(math.min(math.abs(x) / slopeScale, 1.0), 0.0, 1.0, neutralColor, x > 0 ? upColor : dnColor)
 
// Speed: Slow .. Fast, direction ignored
speedColor(int k) =>
    float x = nrmVal(k)
    na(x) ? slowColor : color.from_gradient(math.min(math.abs(x) / slopeScale, 1.0), 0.0, 1.0, slowColor, fastColor)
 
stageColor(float v, int k) =>
    color sc = slopeColor(v)
    switch coloring
        "Slope Strength" => strengthColor(k)
        "Speed"          => speedColor(k)
        "Slope"          => sc
        => color.from_gradient(k, 0, math.max(order - 1, 1), fastColor, slowColor)
 
plotStyle = lineStyle == "Step" ? plot.style_stepline : plot.style_line
 
float s1  = stageVal(out, 0)
float s2  = stageVal(out, 1)
float s3  = stageVal(out, 2)
float s4  = stageVal(out, 3)
float s5  = stageVal(out, 4)
float s6  = stageVal(out, 5)
float s7  = stageVal(out, 6)
float s8  = stageVal(out, 7)
float s9  = stageVal(out, 8)
float s10 = stageVal(out, 9)
float s11 = stageVal(out, 10)
float s12 = stageVal(out, 11)
float s13 = stageVal(out, 12)
float s14 = stageVal(out, 13)
float s15 = stageVal(out, 14)
float s16 = stageVal(out, 15)
float s17 = stageVal(out, 16)
float s18 = stageVal(out, 17)
float s19 = stageVal(out, 18)
float s20 = stageVal(out, 19)
float s21 = stageVal(out, 20)
float s22 = stageVal(out, 21)
float s23 = stageVal(out, 22)
float s24 = stageVal(out, 23)
float s25 = stageVal(out, 24)
float s26 = stageVal(out, 25)
float s27 = stageVal(out, 26)
float s28 = stageVal(out, 27)
float s29 = stageVal(out, 28)
 
plot(showCascade ? s1  : na, "Stage 1",  stageColor(s1,  0),  cascadeWidth, plotStyle)
plot(showCascade ? s2  : na, "Stage 2",  stageColor(s2,  1),  cascadeWidth, plotStyle)
plot(showCascade ? s3  : na, "Stage 3",  stageColor(s3,  2),  cascadeWidth, plotStyle)
plot(showCascade ? s4  : na, "Stage 4",  stageColor(s4,  3),  cascadeWidth, plotStyle)
plot(showCascade ? s5  : na, "Stage 5",  stageColor(s5,  4),  cascadeWidth, plotStyle)
plot(showCascade ? s6  : na, "Stage 6",  stageColor(s6,  5),  cascadeWidth, plotStyle)
plot(showCascade ? s7  : na, "Stage 7",  stageColor(s7,  6),  cascadeWidth, plotStyle)
plot(showCascade ? s8  : na, "Stage 8",  stageColor(s8,  7),  cascadeWidth, plotStyle)
plot(showCascade ? s9  : na, "Stage 9",  stageColor(s9,  8),  cascadeWidth, plotStyle)
plot(showCascade ? s10 : na, "Stage 10", stageColor(s10, 9),  cascadeWidth, plotStyle)
plot(showCascade ? s11 : na, "Stage 11", stageColor(s11, 10), cascadeWidth, plotStyle)
plot(showCascade ? s12 : na, "Stage 12", stageColor(s12, 11), cascadeWidth, plotStyle)
plot(showCascade ? s13 : na, "Stage 13", stageColor(s13, 12), cascadeWidth, plotStyle)
plot(showCascade ? s14 : na, "Stage 14", stageColor(s14, 13), cascadeWidth, plotStyle)
plot(showCascade ? s15 : na, "Stage 15", stageColor(s15, 14), cascadeWidth, plotStyle)
plot(showCascade ? s16 : na, "Stage 16", stageColor(s16, 15), cascadeWidth, plotStyle)
plot(showCascade ? s17 : na, "Stage 17", stageColor(s17, 16), cascadeWidth, plotStyle)
plot(showCascade ? s18 : na, "Stage 18", stageColor(s18, 17), cascadeWidth, plotStyle)
plot(showCascade ? s19 : na, "Stage 19", stageColor(s19, 18), cascadeWidth, plotStyle)
plot(showCascade ? s20 : na, "Stage 20", stageColor(s20, 19), cascadeWidth, plotStyle)
plot(showCascade ? s21 : na, "Stage 21", stageColor(s21, 20), cascadeWidth, plotStyle)
plot(showCascade ? s22 : na, "Stage 22", stageColor(s22, 21), cascadeWidth, plotStyle)
plot(showCascade ? s23 : na, "Stage 23", stageColor(s23, 22), cascadeWidth, plotStyle)
plot(showCascade ? s24 : na, "Stage 24", stageColor(s24, 23), cascadeWidth, plotStyle)
plot(showCascade ? s25 : na, "Stage 25", stageColor(s25, 24), cascadeWidth, plotStyle)
plot(showCascade ? s26 : na, "Stage 26", stageColor(s26, 25), cascadeWidth, plotStyle)
plot(showCascade ? s27 : na, "Stage 27", stageColor(s27, 26), cascadeWidth, plotStyle)
plot(showCascade ? s28 : na, "Stage 28", stageColor(s28, 27), cascadeWidth, plotStyle)
plot(showCascade ? s29 : na, "Stage 29", stageColor(s29, 28), cascadeWidth, plotStyle)
 
// main line = last stage = UniEMA(Length, Poles, Order, WF, DF)
float uniema   = na(out) ? na : out.get(order - 1)
color mainSign = slopeColor(uniema)
color mainCol  = switch coloring
    "Slope Strength" => strengthColor(order - 1)
    "Speed"          => speedColor(order - 1)
    => mainSign
plot(uniema, "UniEMA", mainCol, lineWidth, plotStyle)

← 返回列表