Created
August 11, 2026 00:41
-
-
Save pedr0-fr/80bd55878de3fada5a9b7eb36a394520 to your computer and use it in GitHub Desktop.
HMA w projection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //@version=6 | |
| // Note: no timeframe/timeframe_gaps args — Pine forbids them in scripts that create | |
| // drawings (our projection polyline). Use the chart's timeframe. | |
| indicator(title="Hull Moving Average (Flat-Price Projection)", shorttitle="HMA+P", overlay=true) | |
| length = input.int(9, "Length", minval = 2) | |
| src = input(close, "Source") | |
| projBars = input.int(20, "Projection bars", minval = 1, maxval = 500) | |
| hmaCol = input.color(#2196F3, "Color") // #2196F3 = TradingView's default plot blue | |
| // ── Historical HMA: byte-for-byte the same math as the original ────────────── | |
| halfLen = length / 2 // int division, exactly as ta.wma(src, length/2) | |
| sqrtLen = math.floor(math.sqrt(length)) | |
| raw = 2 * ta.wma(src, halfLen) - ta.wma(src, length) | |
| hullma = ta.wma(raw, sqrtLen) | |
| plot(hullma, "HMA", color = hmaCol) // unchanged historical values | |
| // ── Projection: recompute the HMA at future offsets k = 1..projBars, | |
| // assuming every future bar closes at the current price (flat) ──────────── | |
| var polyline projLine = na | |
| if barstate.islast and not na(hullma) | |
| polyline.delete(projLine) | |
| flat = src // assumed constant future price | |
| // Future values of raw = 2*WMA(half) - WMA(full) under the flat assumption. | |
| // futRaw index (k-1) holds raw at future offset k. | |
| futRaw = array.new_float() | |
| for k = 1 to projBars | |
| numH = 0.0 | |
| for i = 0 to halfLen - 1 | |
| numH += (halfLen - i) * (i < k ? flat : src[i - k]) | |
| wmaH = numH / (halfLen * (halfLen + 1) / 2) | |
| numF = 0.0 | |
| for i = 0 to length - 1 | |
| numF += (length - i) * (i < k ? flat : src[i - k]) | |
| wmaF = numF / (length * (length + 1) / 2) | |
| futRaw.push(2 * wmaH - wmaF) | |
| // Final smoothing: WMA(sqrtLen) over the raw series, spanning the | |
| // boundary between historical raw[] and future futRaw values. | |
| pts = array.new<chart.point>() | |
| pts.push(chart.point.from_index(bar_index, hullma)) // anchor at current HMA → continuous join | |
| for k = 1 to projBars | |
| num = 0.0 | |
| for i = 0 to sqrtLen - 1 | |
| m = k - i | |
| v = m >= 1 ? futRaw.get(m - 1) : raw[-m] | |
| num += (sqrtLen - i) * v | |
| pts.push(chart.point.from_index(bar_index + k, num / (sqrtLen * (sqrtLen + 1) / 2))) | |
| projLine := polyline.new(pts, line_color = hmaCol, line_style = line.style_dashed, line_width = 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment