This script reimagines traditional moving averages by introducing floating-point period calculations, allowing for fractional lengths rather than being constrained to whole numbers. At its core, it provides SMA, WMA, and HMA variants that can work with any decimal length, which proves especially valuable when creating dynamic indicators or fine-tuning existing strategies. The most significant improvement lies in the Hull Moving Average implementation. By properly handling floating-point mathematics throughout the calculation chain, this version reduces the overshoot tendencies that often plague integer-based HMAs. The result is a more responsive yet controlled indicator that better captures price action without excessive whipsaw. The visual aspect incorporates a trend gradient system that can adapt to different trading styles. Rather than using fixed coloring, it offers several modes ranging from simple solid colors to more nuanced three-tone gradients that help identify trend transitions. These gradients are normalized against ATR to provide context-aware visual feedback about trend strength. From a practical standpoint, the floating-point approach eliminates the subtle discontinuities that occur when integer-based moving averages switch periods. This makes the indicator particularly useful in systems where the MA period itself is calculated from market conditions, as it can smoothly transition between different lengths without artificial jumps.
At the heart of this implementation lies the concept of continuous weights rather than discrete summation. Traditional moving averages treat each period as a distinct unit with integer indexing. However, when we move to floating-point periods, we need to consider how fractional periods should behave. This leads us to some interesting mathematical considerations. Consider the Weighted Moving Average kernel. The weight function is fundamentally a slope: -x + length where x represents the position in the averaging window. The normalization constant is calculated by integrating (in our discrete case, summing) this slope across the window. What makes this implementation special is how it handles the fractional component - when the length isn't a whole number, the final period gets weighted proportionally to its fractional part. For the Hull Moving Average, the mathematics become particularly intriguing. The standard HMA formula HMA = WMA(2*WMA(price, n/2) - WMA(price, n), sqrt(n)) is preserved, but now each WMA calculation operates in continuous space. This creates a smoother cascade of weights that better preserves the original intent of the Hull design - to reduce lag while maintaining smoothness.
The Simple Moving Average's treatment of fractional periods is perhaps the most elegant. For a length like 9.7, it weights the first 9 periods fully and the 10th period at 0.7 of its value. This creates a natural transition between integer periods that traditional implementations miss entirely.
The Gradient Mathematics The trend gradient system employs normalized angular calculations to determine color transitions. By taking the arctangent of price changes normalized by ATR, we create a bounded space between 0 and 1 that represents trend intensity. The formula (arctan(Δprice/ATR) + 90°)/180° maps trend angles to this normalized space, allowing for smooth color transitions that respect market volatility context. This mathematical framework creates a more theoretically sound foundation for moving averages, one that better reflects the continuous nature of price movement in financial markets. The implementation recognizes that time in markets isn't truly discrete - our sampling might be, but the underlying process we're trying to measure is continuous. By allowing for fractional periods, we're creating a better approximation of this continuous reality.
This floating-point moving average implementation offers tangible benefits for traders and analysts who need precise control over their indicators. The ability to fine-tune periods and create smooth transitions makes it particularly valuable for automated systems where moving average lengths are dynamically calculated from market conditions. The Hull Moving Average calculation now accurately reflects its mathematical formula while maintaining responsiveness, making it a practical choice for both systematic and discretionary trading approaches. Whether you're building dynamic indicators, optimizing existing strategies, or simply want more precise control over your moving averages, this implementation provides the mathematical foundation to do so effectively.
In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publications is governed by House rules. يمكنك جعله مفضلاً لاستخدامه على الرسم البياني.
هل تريد استخدام هذا النص البرمجي على الرسم البياني؟
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.