InvestorUnknown

DMI ForLoop [InvestorUnknown]

InvestorUnknown 업데이트됨   
Overview
This indicator utilizes the Directional Movement Index (DMI) combined with a for-loop to provide a robust trend analysis (ADX is not a part of this indicator).

Settings

DMI ForLoop Settings:
Start Length (a): The initial length for DMI calculation (inclusive).
End Length (b):The final length for DMI calculation (inclusive).
EMA Length (c): The length for the Exponential Moving Average applied to the DMI values, in order so smoothen the signal.

Signal Settings:
Signal Mode: Determines the mode of signal calculation. Options are "Fast", "Slow", "Thresholds Crossing", and "Fast Threshold". Default is "Fast".
1. Slow: is a simple crossing of the midline (0).
2. Fast: positive signal depends if the current "DMIema" is above "DMIema" or above 0.99, otherwise the signal is negative.
3. Thresholds Crossing: simple ta.crossover and ta.crossunder of the user defined threshold for Long and Short.
4. Fast Threshold: signal changes if the value of "DMIema" changes by more than user defined threshold against the current signal.

// Slow
dmicol1 = DMIema > 0 ? colup : coldn

// Fast
dmicol2 = DMIema > DMIema[1] or DMIema > 0.99 ? colup : coldn

// Thresholds Crossing
var color dmicol3 = na
if ta.crossover(DMIema,longth)
    dmicol3 := colup
if ta.crossunder(DMIema,shortth)
    dmicol3 := coldn

// Fast Threshold
var color dmicol4 = na 
if (DMIema > DMIema[1] + fastth)
    dmicol4 := colup
if (DMIema < DMIema[1] - fastth)
    dmicol4 := coldn

color dmicol = na
if sigmode == "Slow"
    dmicol := dmicol1
if sigmode == "Fast"
    dmicol := dmicol2
if sigmode == "Thresholds Crossing"
    dmicol := dmicol3
if sigmode == "Fast Threshold"
    dmicol := dmicol4
else
    na


Functionality
The DMI ForLoop indicator calculates an array of DMI values over a specified range of lengths, then averages these values and applies an EMA for smoothing. The result is a dynamic trend indicator that adapts to market conditions.

DMI Calculation:
The indicator iterates through lengths from Start Length to End Length, calculating the positive and negative directional movement (DM) for each period and calculates the average of all the signals at the end. A custom function version of the DMI is used here in order to use DMI with "series" inputs.

// Function to calculate an array of DMI values over a range of lengths
DMIArray(a, b, c) =>
    // Initialize an array to store DMI values, with size based on the range (b - a + 1)
    var dmiArray = array.new_float(b - a + 1, 0.0)
    
    // Loop through each length from a to b
    for x = 0 to (b - a)
        // Calculate the smoothing factor alpha for the current length
        alpha = 1.0 / (a + x)
        
        // Initialize variables for positive and negative DM
        float plus = na
        float minus = na
        
        // Calculate the up and down movements
        up = ta.change(high)
        down = -ta.change(low)
        
        // Determine the positive DM (plusDM)
        plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
        
        // Determine the negative DM (minusDM)
        minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
        
        // Calculate the smoothed positive DM using either SMA or EMA
        plus := na(plus[1]) ? ta.sma(plusDM, (a + x)) : alpha * plusDM + (1 - alpha) * nz(plus[1])
        
        // Calculate the smoothed negative DM using either SMA or EMA
        minus := na(minus[1]) ? ta.sma(minusDM, (a + x)) : alpha * minusDM + (1 - alpha) * nz(minus[1])
        
        // Determine the trend direction: 1 for positive trend, -1 for negative trend, 0 for no trend
        trend = plus > minus ? 1 : plus < minus ? -1 : 0
        
        // Store the trend value in the DMI array
        array.set(dmiArray, x, trend)
    
    // Calculate the average of the DMI array
    dmiAvg = array.avg(dmiArray)
    
    // Apply an EMA to the average DMI value
    DMIema = ta.ema(dmiAvg, c)
    
    // Return the DMI array, its average, and the EMA of the average
    [dmiArray, dmiAvg, DMIema]

// Call the DMIArray function with the input parameters and assign the results to variables
[dmiArray, dmiAvg, DMIema] = DMIArray(a, b, c)

This indicator is versatile and can be tailored to fit various trading/investing strategies by adjusting the input parameters and signal modes.
릴리즈 노트:
Update:

- option to choose different type of Moving Average

maType = input.string("EMA", "MA Type", ["EMA", "SMA", "WMA", "VWMA","TMA"], group = "DMI ForLoop Settings", inline = "M")

DMIArray(a, b, c) =>
    var dmiArray = array.new_float(b - a + 1, 0.0) 
    for x = 0 to (b - a) 
        alpha = 1.0 / (a + x)
        float plus = na 
        float minus = na
        up = ta.change(high) 
        down = -ta.change(low) 
        plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
        minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
        plus := na(plus[1]) ? ta.sma(plusDM, (a + x)) : alpha * plusDM + (1 - alpha) * nz(plus[1]) 
        minus := na(minus[1]) ? ta.sma(minusDM, (a + x)) : alpha * minusDM + (1 - alpha) * nz(minus[1]) 
        trend = plus > minus ? 1 : plus < minus ? -1 : 0 
        array.set(dmiArray, x, trend) 
    dmiAvg = array.avg(dmiArray)
    float DMIma = switch maType
        "EMA" =>   ta.ema(dmiAvg,    c)
        "SMA" =>   ta.sma(dmiAvg,    c)
        "WMA" =>   ta.wma(dmiAvg,    c)
        "VWMA" =>  ta.vwma(dmiAvg,   c)
        "TMA" =>   ta.trima(dmiAvg,  c)
        => 
            runtime.error("No matching MA type found.")
            float(na)
    [dmiArray,dmiAvg, DMIma]

- Adjusted signal calculation for the "Fast" signal to work well with different types of the Moving Average

var color dmicol2 = na
if DMIma > DMIma[1] or DMIma > 0.99
    dmicol2 := colup
if DMIma < DMIma[1] or DMIma < -0.99
    dmicol2 := coldn

- Added Alerts

barconfirm = input.bool(false, "Wait for bar close for Alert?", group = "Alert Settings")

var int alertsignal = na
if dmicol == colup
    alertsignal := 1
if dmicol == coldn
    alertsignal := -1
Long  = ta.crossover(alertsignal,  0)
Short = ta.crossunder(alertsignal, 0)
alertcondition(barconfirm ? Long[1]  : Long, "LONG",   "DMI ForLoop went Long")
alertcondition(barconfirm ? Short[1] : Short,"SHORT", "DMI ForLoop went SHORT")
오픈 소스 스크립트

이 스크립트의 오써는 참된 트레이딩뷰의 스피릿으로 이 스크립트를 오픈소스로 퍼블리쉬하여 트레이더들로 하여금 이해 및 검증할 수 있도록 하였습니다. 오써를 응원합니다! 스크립트를 무료로 쓸 수 있지만, 다른 퍼블리케이션에서 이 코드를 재사용하는 것은 하우스룰을 따릅니다. 님은 즐겨찾기로 이 스크립트를 차트에서 쓸 수 있습니다.

면책사항

이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.

차트에 이 스크립트를 사용하시겠습니까?