1. Trend Duration Forecast Strategy Description
Determines trend direction from the slope of a Hull Moving Average (HMA), measures each trend's actual duration, stores a rolling history of durations per direction, then uses that historical average to "forecast" how much longer the current trend might run.
1.1 Indicator Concept
This indicator is built on a simple idea: trends tend to repeat in duration. It uses the slope of the HMA (Hull Moving Average) to determine whether the market is in an uptrend or downtrend (N consecutive rising bars = up, N consecutive falling bars = down), precisely measures HOW MANY BARS each trend lasted before reversing, and STORES a rolling set of the most recent durations - SEPARATELY per direction (up and down never mixed into one pool).
Every time a NEW trend begins, the indicator immediately displays a "forecast" = the average of past trend durations in the SAME direction (e.g. a new uptrend starting → the forecast is the average of PAST uptrends only, excluding the downtrend that just ended). This forecast is drawn visually as an arrow extending forward from the trend's start toward "where this trend would end if it lasted exactly as long as the historical average".
1.2 Indicator Features
- Trend-Colored HMA Line: the Hull moving average line itself, switching colors between up/down based on the current trend - the main reference line everything else is derived from.
- Live Trend Label (Real Length): a floating label for EACH direction (up/down), always showing how many bars the most recent same-direction trend (whether finished or still running) has actually lasted.
- Arrow + "Probable Length" Label: when a new trend begins, draws an arrow from the start point that auto-extends every bar toward the forecast position (start + the same-direction historical average), plus a number label showing that average, placed at the arrow's midpoint.
- Duration History Table: a bottom-right table listing every stored duration sample (bullish and bearish, up to "Trend Sample Size" samples each) plus an average row at the end - with hover tooltips explaining each cell.
- No Alerts: the indicator has zero alertcondition() calls.
1.3 How to Use the Indicator
- Compare the current "Real Length" against the forecast "Probable Length" to gauge whether the trend is "young" or "old" relative to that pair/timeframe's own historical pattern - if Real Length has already exceeded Probable Length by a lot, the trend is running unusually long compared to history, possibly nearing its end.
- Use the history table to gauge market consistency: if the duration samples swing wildly (e.g. 5, 40, 8, 35...), the average forecast is less reliable than in a market with consistently similar trend durations.
- Treat "Probable Length" as a SIMPLE HISTORICAL AVERAGE, NOT a scientifically grounded statistical model - it's a linear extrapolation from the past, ignoring current volatility/volume/macro context.
- Don't expect a forecast the very first time the indicator runs: the FIRST EVER trend in each direction will show a NaN/blank "Probable Length" since there's no historical sample yet to average - you need at least 1 completed cycle per direction.
- Increase "Trend Sample Size" to make the forecast reflect the market's longer-term character (less sensitive to a few recent outliers); decrease it to keep the forecast tracking the market's most recent "personality".
- Tune "Trend Detection Sensitivity" to balance responsiveness (a low value, switching direction easily on short-term noise) against stability (a high value, only switching on a long streak, fewer false signals).
1.4 How the Indicator Works
Inputs & Roles
- length ("Smoothing Length", Integer, default: 50): the HMA period. Increase → smoother, more lagging HMA; decrease → faster reacting, noisier.
- trendLength ("Trend Detection Sensitivity", Integer, default: 3): number of consecutive bars required to accept the HMA as "consistently rising" (ta.rising) or "consistently falling" (ta.falling) - this is the condition that defines trend, not a comparison of the current HMA against a fixed level.
- samples ("Trend Sample Size", Integer, default: 10, min 2): number of the most recent trend durations kept SEPARATELY for each direction (up and down each have their own independent array).
- coloUP/coloDN (default: green #12E49E / orange #EC5610): colors used across all up/down drawings.
Main Logic Blocks
📈 Flow 1: Determining Trend via HMA Slope (confirmed bars only)
- hma = ta.hma(close, length) computed every bar. Only when barstate.isconfirmed: if ta.rising(hma, trendLength) is true → trend := true; if ta.falling(hma, trendLength) is true → trend := false.
- Important: these are 2 SEPARATE if statements (not if/else) - if the HMA is moving sideways (not enough consecutive rising bars, nor enough consecutive falling bars), BOTH conditions are false, and trend RETAINS its last assigned value - meaning trend isn't "the current bar's direction", it's "the direction of the most recently confirmed N-bar consecutive streak".
🔢 Flow 2: Counting the Current Trend's Duration (every bar)
- if trend or not trend: TrendCount += 1 - this expression looks like it's always true, but it's actually a way of checking that trend is NO LONGER na (since na or not na = na = false in Pine) - meaning the counter only starts running AFTER trend has been determined for the first time, never incrementing while it's still undetermined.
🔄 Flow 3: Detecting a Reversal & Initializing a New Forecast
- if trend != trend[1]: deletes the old arrow line + forecast label, clears the "new trend just detected" tooltip on both TrendUP/TrendDN labels.
- If it just flipped TO bullish (trend = true): pushes TrendCount (the duration of the BEARISH trend that just ended) into the bearishCount array; creates a NEW TrendUP label with the forecast = bullishCount.avg() - NOTE: at this moment bullishCount has NOT been touched in this branch, so this is precisely the average of PAST bullish trends only (excluding the trend that just ended); freezes the text of TrendDN (the label for the bearish trend that just ended) into its FINAL duration; creates the LengthLine arrow (1-bar, to be extended in Flow 4) and an empty LabelProbLen label (to be filled in Flow 4).
- The else branch (flipping to bearish) is a full mirror.
- TrendCount := 0: resets the counter for the newly started trend.
- Note: if this is the FIRST EVER trend in a given direction (that direction's array is still EMPTY), array.avg() on an empty array returns na → the "Probable Length" label shows an undefined value until at least 1 historical sample of that direction exists.
🖊️ Flow 4: Updating the Live Label & Extending the Forecast (every bar)
- For the CURRENTLY RUNNING direction: updates the corresponding label (TrendUP/TrendDN) text to "Real Length: [current TrendCount]" - this number climbs every bar while the trend continues.
- Extends LengthLine to origin_x1 + same_direction_historical_average + 1 - this is exactly "where this trend would end if it lasted as long as the historical average".
- Moves LabelProbLen to the arrow's exact midpoint, updating its text to show the forecast average.
- Example: an uptrend starts at bar 500, bullishCount.avg()=22 (averaging 22 bars from past uptrends). At bar 510 (10 bars after it started): TrendDN is unchanged (currently in an uptrend), TrendUP shows "Real Length: 10". LengthLine extends to bar 500+22+1=523. LabelProbLen sits at the midpoint (500+523)/2≈511, showing "Probable Length: 22".
📦 Flow 5: FIFO Cap on the Sample Arrays
- If bullishCount.size() > samples: removes the FRONT element (oldest, since push() always adds to the END) - symmetric for bearishCount. Keeps exactly "Trend Sample Size" most recent samples per direction.
🎨 Flow 6: Drawing the HMA Line & History Table
- The HMA line is drawn with width 3, colored by the current trend.
- The table only draws on barstate.islast: lists every stored sample (index column + bullish duration column + bearish duration column), each cell with its own hover tooltip, plus an average row at the end for each direction (see the Note above on the potential index-out-of-bounds risk).
Outputs & Usage Roles
- Colored HMA line: the main trend reference, the foundation for every other measurement.
- Real Length label: a live running counter of how long the current/most recently ended trend has lasted.
- Arrow + Probable Length: a visual forecast based on the same-direction historical average - compare against Real Length to judge whether the trend is "young" or "overdue" relative to statistics.
- History table: the full raw data behind the averages, useful for judging how reliable the forecast is (consistent samples vs. wildly varying ones).


