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 = input(false, title='Display Hull MA Set:')
hma_src = input(close, title='Hull MA\'s Source:')
hma_base_length = input.int(8, minval=1, title='Hull MA\'s Base Length:')
hma_length_scalar = input.int(5, minval=0, title='Hull MA\'s Length Scalar:')
  • 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

hullma(src, length) =>
    ta.wma(2 * ta.wma(src, length / 2) - ta.wma(src, length), math.round(math.sqrt(length)))
  • 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

hullma__1 = hullma(hma_src, hma_base_length + hma_length_scalar * 6)
  • 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

plot(not show_hma? na : hullma__1, color=color.new(color.black, 0), linewidth=2, title='Hull MA')
  • Conditionally plots the Hull MA (hullma__1) based on the show_hma input:

    • If show_hma is false, the plot is skipped, and na (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