> For the complete documentation index, see [llms.txt](https://bondingblock.gitbook.io/quant/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bondingblock.gitbook.io/quant/hull-ma.md).

# 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

```javascript
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

```javascript
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

```javascript
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

```javascript
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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://bondingblock.gitbook.io/quant/hull-ma.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
