Skip to content

Blog · TradingView · Article · updated 2026-09-25

Lorentzian Classification explained: the TradingView ML indicator, in plain English

How jdehorty’s Machine Learning: Lorentzian Classification works: nearest neighbours, Lorentzian distance, its five features, filters and kernel regression, plus the default settings.

What it is

Machine Learning: Lorentzian Classification is a TradingView indicator by Justin Dehorty (jdehorty). It is one of the most popular scripts on the platform: a TradingView Editors' Pick, winner of the 2023 Community Award for Most Valuable Pine Script, with over a million views. It predicts whether price is more likely to rise or fall over the next few bars, and plots buy and sell signals when the prediction flips.

Under the hood it is a k-nearest-neighbours classifier. For the current bar it looks back through history for the bars whose indicator readings most resemble now, checks what price did after each of them, and lets them vote. What makes it distinctive is how it measures "resemble": with Lorentzian distance instead of the usual Euclidean distance.

Lorentzian distance, intuitively

Euclidean distance squares each difference, so one feature that is far off dominates the total. Lorentzian distance takes the logarithm of one plus each absolute difference and adds them up: ln(1 + |x - y|) per feature. Large differences grow slowly, so a single outlier reading cannot make two otherwise similar bars look unrelated. Markets are full of such outliers around news and liquidations, and the author's argument is that this makes the neighbour search more robust to them.

distance = sum over features of ln(1 + |feature_now - feature_then|)

The five default features

Each feature is normalized so they share a comparable scale before distances are computed. You can use two to five features; the default uses all five. Feature choice is where most of the indicator's personality lives: RSI and CCI measure momentum, WaveTrend is a smoothed oscillator, and ADX measures trend strength.

F1
RSI, length 14
F2
WaveTrend, channel 10, average 11
F3
CCI, length 20
F4
ADX, length 20
F5
RSI, length 9

How the vote works

  1. 1Each historical bar is labeled by what price did a few bars later: up means a long example, down a short one.
  2. 2For the current bar, the script scans back up to Max Bars Back (2,000 by default) and keeps the closest examples by Lorentzian distance.
  3. 3Only neighbours spaced apart in time are considered, which avoids counting near-duplicate consecutive bars.
  4. 4The nearest Neighbors Count (8 by default) vote; the sum of their labels is the prediction.
  5. 5A signal appears when the prediction changes sign and the filters agree.

Filters that decide whether a signal counts

volatility filter
On by default: skips signals when volatility is outside a normal band
regime filter
On by default, threshold -0.1: requires a trending market by the regime slope
ADX filter
Off by default, threshold 20 when enabled
EMA / SMA filter
Off by default, period 200 when enabled: only trade with the long-term trend
kernel filter
On by default: signals must agree with the kernel regression line

Kernel regression: the trend line

The colored line on the chart is a Nadaraya-Watson kernel regression, a smoothing method that weights recent bars more than distant ones. Defaults: lookback 8, relative weighting 8, regression start at bar 25, and a lag of 2 for the crossover version. With the kernel filter on, a buy signal needs the kernel line to be rising, which removes many counter-trend signals. Dynamic exits, off by default, use the kernel line to exit earlier.

Strengths and limits

Strengths: it adapts to the instrument because it learns from that instrument's own history, it is robust to outliers, and its filters make it usable out of the box. Limits: it has many settings, and the defaults were not tuned for any particular market; nearest-neighbour votes can lag at turning points; and like every indicator, its backtests look better than live results once fees and slippage are counted.

That is exactly the gap TT-Autotune was built to test: tune the settings per token, timeframe and regime on walk-forward data, and compare every tuned cell with the defaults. Across our catalog, 91% of regime cells beat the default settings in the optimizer; our forward testnet books have not yet shown an edge after fees. Both facts matter.

Frequently asked questions

Is Lorentzian Classification really machine learning?
Yes, in the classic sense: it is a k-nearest-neighbours classifier that learns from labeled historical bars. It is not a neural network.
Does it repaint?
Signals are computed on bar close by default; use bar-close alerts for automation.
What are the best settings?
There is no universal best. TT-Autotune tunes them per token, timeframe and regime; see the Autotune catalog for tuned vs default results.
Can I automate it?
Yes. Add it to your TradingView favorites and use the TensorTrader extension’s Batch Create, or use TT-Autotune for tuned settings.

Keep reading

Not financial advice. Performance figures are TensorTrader testnet or backtest results with the method stated; past results do not predict future returns.

All guides · Start on paper · Pricing