OPEN-SOURCE SCRIPT

Zero Lag Exponential Moving Average ForLoop [InvestorUnknown]

3 027
Overview

The Zero Lag Exponential Moving Average (ZLEMA) ForLoop indicator is designed for traders seeking a responsive and adaptive tool to identify trend changes. By leveraging a range of lengths and different moving average (MA) types, this indicator helps smooth out price data and provides timely signals for market entry and exit.

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.


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


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 ZLEMA values over a range of lengths ZLEMAForLoop(a, b, c, s) => // Initialize an array to hold ZLEMA trend values var Array = array.new_float(b - a + 1, 0.0) // Loop through the range from 'a' to 'b' for x = 0 to (b - a) // Calculate the current length len = a + x // Calculate the lag based on the length lag = math.floor((len - 1) / 2) // Calculate the smoothing factor alpha alpha = 2 / (len + 1) // Initialize the ZLEMA variable zlema = 0.0 // Compute the ZLEMA value zlema := na(zlema[1]) ? (s + s - s[lag]) : alpha * (s + s - s[lag]) + (1 - alpha) * nz(zlema[1]) // Determine the trend based on ZLEMA value trend = zlema > zlema[1] ? 1 : -1 // Store the trend in the array array.set(Array, x, trend) // Calculate the average of the trend values Avg = array.avg(Array) // Apply the selected moving average type to the average trend value 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 unsupported MA type float(na) // Return the array of trends, the average trend, and the moving average [Array, Avg, MA]



Important Considerations

  • Speed vs. Stability: The ZLEMA ForLoop is designed for fast response times, making it ideal for short-term trading strategies. However, its sensitivity also means it may generate more signals, some of which could be false positives.
  • Use with Other Indicators: To improve the reliability of the signals, it is recommended to use the ZLEMA ForLoop in conjunction with other technical indicators.
  • Customization: Tailor the settings to match your trading style and risk tolerance. Adjusting the lengths, MA type, and thresholds can significantly impact the indicator's performance.


Conclusion

The ZLEMA ForLoop indicator offers a flexible tool for traders looking to capture trend changes quickly. By providing multiple modes and customization options, it allows traders to fine-tune their analysis and make informed decisions. For best results, use this indicator alongside other analytical tools to confirm signals and avoid potential false entries.

免責聲明

這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。