HULL MA
Summary
This script segment introduces a method for calculating and optionally displaying a Hull Moving Average on a chart. The Hull MA is designed to offer the benefits of reduced lag and increased responsiveness compared to traditional moving averages, making it a valuable tool for traders and analysts looking for smoother trend indications. The script allows users to customize the source data, base length, and length scalar for the HMA calculation, providing flexibility in how the indicator is applied.
User Inputs
show_hma
: A boolean input to toggle the display of the HMA on the chart.hma_src
: The source data for the HMA calculation, defaulted to the closing price (close
).hma_base_length
: The base length for the HMA calculation, with a default value of 8 and a minimum value of 1.hma_length_scalar
: A scalar factor applied to adjust the base length, with a default value of 5 and a minimum value of 0.
Hull MA Calculation Function
Defines a function named
hullma
that calculates the Hull Moving Average given a source (src
) and a length parameter (length
):First, it calculates a weighted moving average (WMA) of the source data over half the specified length.
Then, it calculates another WMA of the source data over the full specified length.
These two WMAs are combined according to the formula (2 \times \text{WMA}(\text{source}, \frac{\text{length}}{2}) - \text{WMA}(\text{source}, \text{length})).
Finally, it calculates a third WMA of the result, using the square root of the length rounded to the nearest integer as the period.
Applying the Hull MA Calculation
Applies the
hullma
function to the source data (hma_src
) using a modified base length (hma_base_length + hma_length_scalar * 6
). This modification adjusts the base length based on the user-provided scalar.
Plotting the Hull MA
Conditionally plots the Hull MA (
hullma__1
) based on theshow_hma
input:If
show_hma
is false, the plot is skipped, andna
(not a number) is returned.If
show_hma
is true, the Hull MA is plotted with a black color and a line width of 2.
Last updated