OPEN-SOURCE SCRIPT
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
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
Visualization Settings
Custom function
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.
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
Pine Script®
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
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
Pine Script®
// 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]
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.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
For quick access on a chart, add this script to your favorites — learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
For quick access on a chart, add this script to your favorites — learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.