Bentrix Docs
HomePricingDashboard

Hilbert Transform Instantaneous Trendline HT Trendline

Moving averagesoverlayone linein every package

Price averaged over exactly one measured dominant cycle, so the cycle cancels and what remains is the trend.

It measures a de-cycled centre whose length is not a setting but a measurement. Read as a trend line with no period to choose; it has a long warm-up because the Hilbert recursion must start well inside the series.

01The study, live

Live figureScroll to zoom, drag the bars to pan.
bar —O — H — L — C —HT Trendline —first value at —
Show where:

Computed by Bentrix’s own code over the same synthetic bars on every page — an illustration of how the study behaves, not a market.

What this figure shows

The figure is computed over 300 sample bars. HT Trendline has no value on the first 63 — the bars it needs before it can answer — and its first value stands on bar 64. Of the 237 bars that have a value, the close is above the line on 110 and below it on 127. Counting the conditions the study declares on these bars: price crosses above the line 4 times; price crosses below the line 4 times. Press a condition under the figure to see each place marked. As states rather than moments: price is above the line on 110 bars; price is below the line on 127 bars.

02Its parts, named

Warm-upno value on the first 63 bars: thestudy needs them before it cananswerThe linethe study's value, one for each barThe bars it readseach bar's close, unless you chooseanother price
Computed by Bentrix’s own code over the sample bars. It is drawn on once, as you reach the figure.

03What you see

One of the studies that treat price as a signal with a dominant cycle: a smoother whose window is that cycle's own measured length.

HT Trendline is a single smooth line on the price pane: an average of price taken over exactly one measured cycle, so that the cycle itself cancels out and what is left is the trend.

04Where it comes from

John Ehlers adapted the Hilbert transform from signal processing in Rocket Science for Traders (2001); TA-Lib implements his formulas.

05The formula

Step 1 — the price is smoothed over four bars:

st=4Pt+3Pt−1+2Pt−2+Pt−310

Step 2 — the Hilbert transform is a fixed filter over seven bars, scaled by the period measured on the bar before:

HT(x)t=ct(0.0962xt+0.5769xt−2−0.5769xt−4−0.0962xt−6),ct=0.075Tt−1+0.54

Step 3 — the two components. The detrended price, delayed three bars, is the in-phase component; its transform is the quadrature:

Dt=HT(s)t,It=Dt−3,Qt=HT(D)t

Step 4 — the period. The components are advanced a quarter turn against each other, each result smoothed (a fifth of the new value, four fifths of the old), and the angle between one bar and the next gives the period:

It′=It−HT(Q)t,Qt′=Qt+HT(I)t
Ret=0.2(It′It−1′+Qt′Qt−1′)+0.8Ret−1,Imt=0.2(It′Qt−1′−Qt′It−1′)+0.8Imt−1
Tt=360arctan(Imt/Ret),Periodt=0.33Tt+0.67Periodt−1
Ptthe price of bar t
ctthe filter's scale, from the period measured on the bar before
stthe smoothed price
It, Qtthe in-phase and the quadrature component
Ttthe period measured on this bar, the angle taken in degrees; it is kept between 0.67 and 1.5 times the bar before and between 6 and 50 bars, then smoothed (a fifth new, four fifths old)
Periodtthe dominant cycle period, in bars

Step 5 — the trendline. The price itself is averaged over one cycle, and the last four averages are weighted:

mt=1n∑i=0n−1Pt−i,Trendlinet=4mt+3mt−1+2mt−2+mt−310
nthe dominant cycle period, rounded to whole bars
mtthe average price over one cycle

The line this study draws is the trendline.

In words: The Hilbert transform produces, from the detrended price, a quadrature component 90° out of phase with it. From the in-phase and quadrature components come the phase, its rate of change (the period), and the rest. The calculations are long; TA-Lib's implementation is the one computed here.

The Trendline itself is the last step: the price averaged over one measured cycle — as many bars as the dominant cycle period says on that bar — and then smoothed over four bars with weights 4, 3, 2 and 1.

06A worked example

The length of the dominant cycle comes from the Hilbert transform and cannot be carried by hand; what is done with it can.

Suppose the measured cycle on this bar is 20 bars. The study averages the last 20 prices — one whole wave, so its rise and its fall cancel — and then smooths the result with a short weighted average of the last four such values, with weights 4, 3, 2, 1 out of 10.

If the last four cycle-long averages were 100.10, 100.14, 100.20 and 100.28 (oldest first), the trendline reads (1 × 100.10 + 2 × 100.14 + 3 × 100.20 + 4 × 100.28) / 10 = 100.21.

When the measured cycle shortens to 12 bars, the average is taken over 12 — the window breathes with the market.

Computed by Bentrix on a known wave: on Bentrix's hundred-bar sample wave — closes of 100 + 0.05 i + 2 sin(2π i / 20) for bars i = 0 to 99, a 20-bar wave two points high on a slow rise, each bar opening at the close before, its high and low half a point beyond its body — the trendline reads 104.425 on the last bar. The wave itself is gone from it: what is left is the slow rise, about half a point behind where that rise stands on the last bar (104.95).

07Built in Assay

The same study written in Assay, Bentrix’s script language, and drawn by Bentrix itself as the script’s graph: each block is one step of the formula. On the ladder the steps stand in the script’s order, one a row; on the board each wire carries a value from the block that gives it to the block that reads it, left to right. The script computes exactly what the study computes — Bentrix checks the two against each other, candle by candle.

THE SCRIPT’S GRAPH
//@version=6
indicator("Hilbert Transform Instantaneous Trendline", overlay = true)
// TA-Lib's HT_TRENDLINE: the price averaged over the dominant cycle that the Hilbert transform measures,
// which TA-Lib's own recursion starts 63 bars in.
trendline = talib.HT_TRENDLINE(close)
plot(trendline, "Trendline")

Paste it into the Assay IDE to put it on a chart, or to change it. Looking at the graph is free in every package; editing it as blocks is Plus.

08Readings — what traders look for

Price against the trendline. In Ehlers' reading, price oscillating around the line is a market in a cycle, and price staying on one side is a market in a trend. It fails when no cycle is present: the measured period then wanders, and the line with it.

The slope. Read as the direction of the trend with the cycle taken out. It fails on short histories and after gaps, where the period measurement has not settled.

09In different markets and timeframes

The Hilbert trendline is a smoother whose window is the measured cycle, so it is at its best where the market has a cycle to measure. In a strong trend with no cycle the measured period wanders and the line with it.

10With other studies

Read against price as Ehlers did — price oscillating around it is a cycling market, price holding one side a trending one — and beside the trend mode study, which makes that judgement automatically.

11What it does not tell you

!

A measured cycle is a description of the recent window; markets are not obliged to keep a rhythm, and the measures need a long warm-up.

12Inputs and defaults

HT Trendline (Close, Chart)settings card
1Applied priceClose
2TimeframeChart
3Wait for the candle to closeOn
  1. 1
    Applied price — CloseAny of the seven prices, and several at once, each its own line with its own look.
  2. 2
    Timeframe — ChartThe chart's own by default; any higher one, read on the last closed candle. A finer one is never offered.
  3. 3
    Wait for the candle to close — OnThe value of a higher timeframe is taken only from a candle that has finished.

The settings in use

  • In useIt has no inputs: the window is the measured cycle, between 6 and 50 bars.

13Limits and common misreadings

No inputs. The window is whatever the cycle measurement says, between its built-in limits.

63 bars before the first value, the warm-up of the Hilbert transform in TA-Lib.

14Conditions an alert or a strategy can watch

price crosses above the line eventprice crosses below the line eventprice is above the line stateprice is below the line state

Event: the bar it happens on an event happens on one barState: every bar while it holds a state holds on every bar while it lasts

These are the conditions the study itself declares — the same rules the alert editor and the strategy engine read, and the ones the figure's chips show.

15Practise it in the laboratory

Put the HT Trendline and an SMA 20 on one chart; replay a cycling stretch and note how the trendline passes through the middle of the swings while the SMA rides up and down with them.

Open the laboratory

17On pages of their own

18In Bentrix

  • From the menu: a chart's Indicators button › Moving averages › HT Trendline, or type HT Trendline in its search.
  • Its settings: the settings card opens when it is added; its legend's name opens it again.

19From an agent or a program

Two tools reach it. study_values computes it over a workbench’s candles up to the clock — no chart needed — and study_attach puts it on a chart, where it is drawn and saved with the chart ({"chart": "<chart uri>", "kind": "ht_trendline"}).

The study, as a chart files it

This is a new HT Trendline’s entry — its kind, its calculation, the price it reads and its settings. Change a setting here to read it with other inputs.

{
  "applied": "close",
  "convention": "talib",
  "kind": "ht_trendline",
  "settings": {
    "shape": "none"
  }
}

An agent

An agent calls the MCP tool with it:

{
  "name": "study_values",
  "arguments": {
    "bench": "bentrix://lab/\u2026/bench/\u2026",
    "symbol": "EURUSD",
    "timeframe": "H1",
    "limit": 5,
    "study": {
      "applied": "close",
      "convention": "talib",
      "kind": "ht_trendline",
      "settings": {
        "shape": "none"
      }
    }
  }
}

A program

A program does the same in its language — bench and market as in the built-in indicators guide:

Python

values = client.study.values(bench, market, "H1", {"applied": "close", "convention": "talib", "kind": "ht_trendline", "settings": {"shape": "none"}}, limit=5)
print("times:", values["times"])
for line in values["lines"]:
    print("line:", line["line"])
    print("  newest values:", line["values"])

JSJavaScript

const values = await client.study.values({ bench, symbol: market, timeframe: "H1", study: { "applied": "close", "convention": "talib", "kind": "ht_trendline", "settings": { "shape": "none" } }, limit: 5 });
console.log("times:", values.times);
for (const line of values.lines) {
  console.log("line:", line.line);
  console.log("  newest values:", line.values);
}

C#C#

var values = await client.Study.Values(bench, market, "H1", new Dictionary<string, object?> { ["applied"] = "close", ["convention"] = "talib", ["kind"] = "ht_trendline", ["settings"] = new Dictionary<string, object?> { ["shape"] = "none" } }, limit: 5);
Console.WriteLine($"times: {values!["times"]!}");
foreach (var line in values!["lines"]!.AsArray())
{
    Console.WriteLine($"line: {line!["line"]!}");
    Console.WriteLine($"  newest values: {line!["values"]!}");
}

GOGo

values := must(client.Study.Values(ctx, mcp.StudyValuesArgs{Bench: bench, Symbol: market, Timeframe: "H1", Study: map[string]any{"applied": "close", "convention": "talib", "kind": "ht_trendline", "settings": map[string]any{"shape": "none"}}, Limit: mcp.Ptr[int64](5)}))
fmt.Println("times:", at(values, "times"))
for _, line := range at(values, "lines").([]any) {
	fmt.Println("line:", at(line, "line"))
	fmt.Println("  newest values:", at(line, "values"))
}

Java

Object values = client.study.values(new Bentrix.StudyApi.ValuesArgs(bench, market, "H1", obj("applied", "close", "convention", "talib", "kind", "ht_trendline", "settings", obj("shape", "none"))).limit(5L));
System.out.println("times:" + " " + at(values, "times"));
for (Object line : (List<Object>) at(values, "lines")) {
    System.out.println("line:" + " " + at(line, "line"));
    System.out.println("  newest values:" + " " + at(line, "values"));
}

C++C++

auto values = client.study.values_(StudyApi::ValuesArgs(bench, market, "H1", Json::Object{{"applied", "close"}, {"convention", "talib"}, {"kind", "ht_trendline"}, {"settings", Json::Object{{"shape", "none"}}}}).limit(5));
std::cout << "times:" << " " << values["times"].dump() << std::endl;
for (const Json &line : values["lines"].arr()) {
    std::cout << "line:" << " " << line["line"].dump() << std::endl;
    std::cout << "  newest values:" << " " << line["values"].dump() << std::endl;
}

Julia

values = client.study.values(bench, market, "H1", Dict{String,Any}("applied" => "close", "convention" => "talib", "kind" => "ht_trendline", "settings" => Dict{String,Any}("shape" => "none")); limit = 5)
println("times:", " ", values["times"])
for line in values["lines"]
    println("line:", " ", line["line"])
    println("  newest values:", " ", line["values"])
end

RR

values <- client$study$values(bench, market, "H1", list(applied = "close", convention = "talib", kind = "ht_trendline", settings = list(shape = "none")), limit = 5)
cat("times:", format(values$times), "\n")
for (line in values$lines) {
  cat("line:", format(line$line), "\n")
  cat("  newest values:", format(line$values), "\n")
}

RRust

let values = client.study().values(mcp::StudyValuesArgs::new(bench.clone(), market.clone(), "H1", serde_json::json!({"applied": "close", "convention": "talib", "kind": "ht_trendline", "settings": {"shape": "none"}})).limit(5))?;
println!("times: {}", values["times"]);
for line in values["lines"].as_array().into_iter().flatten() {
    println!("line: {}", line["line"]);
    println!("  newest values: {}", line["values"]);
}

Reading the answer

  • times — the candles’ open times, in milliseconds since 1970 (UTC), oldest first.
  • lines — one entry per line, in this order, each with its line name and its values; values[i] belongs to the candle at times[i].
  • The last value is the candle at the clock’s, which may still be forming: it changes until that candle closes, as on the chart. Nothing after the clock is ever computed.
  • null is a candle with no value yet — the warm-up, about 1 × the period at the start of the history.
lineWhat it is
Mainline

In a strategy

Filed under a key of your choice in a strategy document’s studies — here ht_trendline: {"ht_trendline": {"applied": "close", "convention": "talib", "kind": "ht_trendline", "settings": {"shape": "none"}}} — its rules can name:

WriteWhat it is
ht_trendlineits main line, as a value in a compare
{"cond": "ht_trendline:price.cross_up"}a condition it declares itself
{"cond": "ht_trendline:price.cross_down"}a condition it declares itself
{"cond": "ht_trendline:price.above"}a condition it declares itself
{"cond": "ht_trendline:price.below"}a condition it declares itself

20Sources

  1. Ehlers, John F. Rocket Science for Traders. Wiley, 2001.

Bentrix 0.1.382Every number, name and default on these pages is read from the application itself.What is comingWhat changed