Jurik Directional Movement Index (DMX)

mihakralj · study · 208 行 · 点赞 6 · TradingView 原页

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

Pine Script

// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Jurik Directional Movement Index (DMX)", "DMX", overlay=false)
 
// JMA implementation copied from lib/trends_IIR/jma/jma.pine.
jma_filter(series float source, simple int period, simple int phase = 0) =>
    simple float _PHASE      = phase < -100 ? 0.5 : phase > 100 ? 2.5 : (phase * 0.01) + 1.5
    simple float _LEN0       = period < 1.0000000002 ? 1e-10 : (period - 1.0) / 2.0
    simple float _LOG_PARAM  = math.max(math.log(math.sqrt(_LEN0)) / math.log(2.0) + 2.0, 0.0)
    simple float _SQRT_PARAM = math.sqrt(_LEN0) * _LOG_PARAM
    simple float _LEN_ADJ    = _LEN0 * 0.9
    simple float _LEN_DIV    = _LEN_ADJ / (_LEN_ADJ + 2.0)
    simple float _SQRT_DIV   = _SQRT_PARAM / (_SQRT_PARAM + 1.0)
    simple float _P_EXP      = math.max(_LOG_PARAM - 2.0, 0.5)
 
    simple int _CORE_LO  = 32
    simple int _CORE_HI  = 96
    simple int _CORE_CNT = 65
    simple int _REFRESH  = 1024
 
    var float paramA      = na
    var float paramB      = na
    var float lastC0      = na
    var float lastC8      = na
    var float lastA8      = na
    var float lastJma     = na
    var int bars          = 0
 
    var float cycleDelta = 0.0
    var int volIndex     = 0
    var int volCount     = 0
    var array<float> volWindow = array.new_float(10, 0.0)
 
    var array<float> chrono    = array.new_float(128, 0.0)
    var array<float> sortedArr = array.new_float(0)
    var int chronoIdx          = 0
    var int distCount          = 0
    var float coreSum          = 0.0
    var int refreshTick        = 0
 
    float current_jma = na
 
    if not na(source)
        bars += 1
 
        if bars == 1
            paramA := source
            paramB := source
            lastC0 := source
            lastC8 := 0.0
            lastA8 := 0.0
            lastJma := source
            current_jma := source
        else
            float diffA = source - paramA
            float diffB = source - paramB
            float absA = math.abs(diffA)
            float absB = math.abs(diffB)
            float absValue = absA > absB ? absA : absB
            float dLocal = absValue + 1e-10
 
            float oldVol = array.get(volWindow, volIndex)
            cycleDelta += dLocal - oldVol
            array.set(volWindow, volIndex, dLocal)
            volIndex += 1
            if volIndex >= 10
                volIndex := 0
            if volCount < 10
                volCount += 1
            float highD = volCount > 0 ? cycleDelta / (volCount < 10 ? volCount : 10) : dLocal
 
            float dRef = highD
 
            if distCount < 128
                int wLo = 0
                int wHi = distCount
                while wLo < wHi
                    int wMid = int((wLo + wHi) / 2)
                    if array.get(sortedArr, wMid) > highD
                        wHi := wMid
                    else
                        wLo := wMid + 1
                array.insert(sortedArr, wLo, highD)
                array.set(chrono, chronoIdx, highD)
                chronoIdx := (chronoIdx + 1) % 128
                distCount += 1
 
                if distCount == 128
                    float sSeed = 0.0
                    for i = _CORE_LO to _CORE_HI
                        sSeed += array.get(sortedArr, i)
                    coreSum := sSeed
                    refreshTick := 0
                    dRef := coreSum / float(_CORE_CNT)
                else if distCount >= 16
                    int slice = int(math.max(5.0, math.round(distCount * 0.5)))
                    int drop = (distCount - slice) / 2
                    int idxLo = math.max(drop, 0)
                    int idxHi = math.min(drop + slice - 1, distCount - 1)
                    float sWarm = 0.0
                    for i = idxLo to idxHi
                        sWarm += array.get(sortedArr, i)
                    dRef := sWarm / float(idxHi - idxLo + 1)
            else
                float oldVal = array.get(chrono, chronoIdx)
 
                int rLo = 0
                int rHi = 128
                while rLo < rHi
                    int rMid = int((rLo + rHi) / 2)
                    if array.get(sortedArr, rMid) < oldVal
                        rLo := rMid + 1
                    else
                        rHi := rMid
 
                float dRem = 0.0
                if rLo <= _CORE_HI
                    float displaced = rLo < _CORE_LO ? array.get(sortedArr, _CORE_LO) : oldVal
                    dRem := array.get(sortedArr, _CORE_HI + 1) - displaced
                array.remove(sortedArr, rLo)
 
                int iLo = 0
                int iHi = 127
                while iLo < iHi
                    int iMid = int((iLo + iHi) / 2)
                    if array.get(sortedArr, iMid) > highD
                        iHi := iMid
                    else
                        iLo := iMid + 1
 
                float dIns = 0.0
                if iLo <= _CORE_HI
                    float entering = iLo < _CORE_LO ? array.get(sortedArr, _CORE_LO - 1) : highD
                    dIns := entering - array.get(sortedArr, _CORE_HI)
                array.insert(sortedArr, iLo, highD)
 
                array.set(chrono, chronoIdx, highD)
                chronoIdx := (chronoIdx + 1) % 128
 
                coreSum += dRem + dIns
                refreshTick += 1
                if refreshTick >= _REFRESH
                    refreshTick := 0
                    float sFix = 0.0
                    for i = _CORE_LO to _CORE_HI
                        sFix += array.get(sortedArr, i)
                    coreSum := sFix
                dRef := coreSum / float(_CORE_CNT)
 
            if dRef <= 0.0
                dRef := dLocal
 
            float ratio = absValue / dRef
            if ratio < 0.0
                ratio := 0.0
            float d = math.pow(ratio, _P_EXP)
            d := math.min(math.max(d, 1.0), _LOG_PARAM)
 
            float adapt = math.pow(_SQRT_DIV, math.sqrt(d))
            if source > paramA
                paramA := source
            else
                paramA := source - (source - paramA) * adapt
            if source < paramB
                paramB := source
            else
                paramB := source - (source - paramB) * adapt
 
            float prevJma = na(lastJma) ? source : lastJma
            float alpha = math.pow(_LEN_DIV, d)
            float alpha2 = alpha * alpha
            float c0 = (1.0 - alpha) * source + alpha * lastC0
            float c8 = (source - c0) * (1.0 - _LEN_DIV) + _LEN_DIV * lastC8
            float a8 = (_PHASE * c8 + c0 - prevJma) * (alpha * -2.0 + alpha2 + 1.0) + alpha2 * lastA8
            float jmaVal = prevJma + a8
 
            lastC0 := c0
            lastC8 := c8
            lastA8 := a8
            lastJma := jmaVal
            current_jma := jmaVal
 
    current_jma
 
//@function Calculates DMX as JMA-smoothed bipolar DMI
//@param period Number of bars used in the calculation
//@returns [DMX, raw bipolar DMI] in the range -1..1
dmx(simple int period = 14) =>
    if period <= 0
        runtime.error("Period must be greater than 0")
    [plus_di, minus_di, _] = ta.dmi(period, period)
    float di_sum = plus_di + minus_di
    float source = di_sum != 0.0 ? (plus_di - minus_di) / di_sum : 0.0
 
    float dmx_value = jma_filter(source, period)
    [dmx_value, source]
 
// Inputs
i_period = input.int(14, "Period", minval=1, tooltip="Number of bars used in the calculation")
 
// Use the same DI and ADX lengths as DMI; ADX itself is not part of the DMX formula.
[dmx_value, raw_bipolar_dmi] = dmx(i_period)
i_showRaw = input.bool(true, "Show raw bipolar DMI")
 
// Scale plots to compare with DMI's 0..100 lines; the calculation remains in -1..1.
plot(dmx_value * 100.0, "DMX x100", color=color.yellow, linewidth=2)
plot(i_showRaw ? raw_bipolar_dmi * 100.0 : na, "Raw bipolar DMI x100", color=color.gray, linewidth=1)

← 返回列表