InvestorUnknown

Infinite Impulse Response Filter ForLoop [InvestorUnknown]

Overview


The Infinite Impulse Response Filter ForLoop indicator is designed for seeking quick and accurate trend identification. Leveraging the Infinite Impulse Response (IIR) filter technique, this indicator provides fast and responsive signals to aid in market timing and trend following.

User Inputs

  • Start and End Lengths: Define the range of lengths over which the IIRF values are calculated.
  • Moving Average Type: Choose from EMA, SMA, WMA, VWMA, or TMA for trend smoothing.
  • Moving Average Length: Specify the length for the chosen MA type.
  • Calculation Source: Select the price data used for calculations (default is close price).

Signal Calculation

Signal Mode (sigmode): Determines the type of signal generated by the indicator. Options are "Fast", "Slow", "Thresholds Crossing", and "Fast Threshold".
1. Slow: is a simple crossing of the midline (0).
2. Fast: positive signal depends if the current MA > MA or MA is above 0.99, negative signals comes if MA < MA or MA is below -0.99.
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 MA changes by more than user defined threshold against the current signal

col1 = MA > 0 ? colup : coldn
var color col2 = na
if MA > MA[1] or MA > 0.99
    col2 := colup
if MA < MA[1] or MA < -0.99
    col2 := coldn
var color col3 = na
if ta.crossover(MA,longth)
    col3 := colup
if ta.crossunder(MA,shortth)
    col3 := coldn
var color col4 = na 
if (MA > MA[1] + fastth)
    col4 := colup
if (MA < MA[1] - fastth)
    col4 := coldn

color col = switch sigmode
    "Slow" => col1
    "Fast" => col2
    "Thresholds Crossing" => col3
    "Fast Threshold" => col4

Visualization Settings

  • Bull Color (colup): The color used to indicate bullish signals.
  • Bear Color (coldn): The color used to indicate bearish signals.
  • Color Bars (barcol): Option to color the bars based on the signal.

Custom function

// Function to calculate an array of IIRF values over a range of lengths
IIRFforLoop(a, b, c, s) =>
    // Initialize an array to store IIRF values
    var Array = array.new_float(b - a + 1, 0.0)
    
    // Loop over the range from 'a' to 'b'
    for x = 0 to (b - a)
        // Calculate the length for the current iteration
        len = a + x
        
        // Calculate the IIRF alpha parameter
        iirfAlpha = 2 / (len + 1)
        
        // Calculate the lag for the IIRF calculation
        iirfLag = math.round(1 / iirfAlpha - 1)
        
        // Initialize the IIRF value
        iirf = 0.0
        
        // Update the IIRF value using the IIR filter formula
        iirf := iirfAlpha * (s + ta.change(s, iirfLag)) + (1 - iirfAlpha) * nz(iirf[1])
        
        // Determine the trend based on the current and previous IIRF values
        trend = iirf > iirf[1] ? 1 : -1
        
        // Store the trend value in the array
        array.set(Array, x, trend)
    
    // Calculate the average of the IIRF values in the array
    Avg = array.avg(Array)
    
    // Calculate the moving average of the average IIRF values based on the selected MA type
    float MA = switch maType
        "EMA" =>   ta.ema(Avg,    c)     // Exponential Moving Average
        "SMA" =>   ta.sma(Avg,    c)     // Simple Moving Average
        "WMA" =>   ta.wma(Avg,    c)     // Weighted Moving Average
        "VWMA" =>  ta.vwma(Avg,   c)     // Volume Weighted Moving Average
        "TMA" =>   ta.trima(Avg,  c)     // Triangular Moving Average
        => 
            runtime.error("No matching MA type found.") // Error handling for invalid MA type
            float(na)
    
    // Return the array of IIRF values, their average, and the moving average
    [Array, Avg, MA]

Important Considerations

Rapid Signal Response: The IIRF ForLoop is designed to provide very fast trend signals, making it suitable for short-term trading and quick decision-making.
Complementary Tool: While powerful, the IIRF ForLoop should be used in conjunction with other indicators and market analysis techniques to confirm signals and improve trading accuracy.

Conclusion

The Infinite Impulse Response Filter ForLoop indicator is a highly responsive and flexible tool that can significantly enhance your trading strategy. Its ability to quickly identify trends and generate signals based on various moving average types and customizable thresholds makes it invaluable for active traders. For the best results, use this indicator alongside other technical analysis tools to confirm signals and ensure robust trading decisions.
开源脚本

本着真正的TradingView精神,该脚本的作者将其开源发布,以便交易者可以理解和验证它。为作者喝彩!您可以免费使用它,但在出版物中重复使用此代码受网站规则的约束。 您可以收藏它以在图表上使用。

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。

想在图表上使用此脚本?