InvestorUnknown

DMI ForLoop [InvestorUnknown]

InvestorUnknown Mis à jour   
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.
Notes de version:
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")
Script open-source

Dans le véritable esprit de TradingView, l'auteur de ce script l'a publié en open-source, afin que les traders puissent le comprendre et le vérifier. Bravo à l'auteur! Vous pouvez l'utiliser gratuitement, mais la réutilisation de ce code dans une publication est régie par le règlement. Vous pouvez le mettre en favori pour l'utiliser sur un graphique.

Clause de non-responsabilité

Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.

Vous voulez utiliser ce script sur un graphique ?