Free: indicator teardowns, a repaint checker and open-source scripts. No buy/sell signals, ever.

Taught to TradeSystems over signals.

Explainers / Lorentzian Classification

Lorentzian Classification, explained from the source

Machine Learning: Lorentzian Classification is an open-source TradingView script by jdehorty (about 37,700 boosts). It colours bars, writes a small number above or below each one, and draws Buy and Sell labels when its model changes sides. This page walks through its published code in order, says what each part counts, and ends with where it fails. It does not say what any signal will do next.

The surprise: it is not a nearest-neighbour search, and the label looks backward

Two things in the code are easy to miss. First, the author's own comments call the neighbour step an "approximate" search that keeps "consecutive furthest neighbors", and on invented prices the eight bars it keeps are far from the true eight nearest. Second, the label stored for each past bar is built from the four bars that ended on that bar, not the four bars after it. The sections below show both, with the lines that cause them.

Step 1: five features per bar

Each bar gets five numbers from the Feature Engineering settings. The defaults are RSI (14, 1), WT (10, 11), CCI (20, 1), ADX (20, 2) and RSI (9, 1); each can be switched to RSI, WT, CCI or ADX, and Feature Count (2 to 5) says how many are used. The normalising functions behind them live in an imported library (MLExtensions) that this page did not read, so it does not describe their scaling. The script pushes each bar's five values onto five arrays that grow for as long as the chart is loaded.

Step 2: the label for a past bar

For every bar the script also stores a label: -1 if the source price four bars earlier was lower than the source now, +1 if it was higher, 0 if equal. The source is Close by default. Read literally, a stored label describes the four bars that ended on that bar: a rise over those four bars is stored as -1, a fall as +1. The Show Default Exits tooltip says trades last four bars, so the author evidently has a four-bar horizon in mind; the code comments do not say which direction the label is meant to point, so treat the backward reading as something to check on your own chart by comparing a bar's number with the move four bars later.

Step 3: Lorentzian distance

The distance between the current bar and a stored bar is the sum, over the features in use, of the natural log of one plus the absolute difference. A small gap between two readings adds almost the same as in an ordinary distance; a very large gap adds much less than it would. That is the whole "Lorentzian" part in this script: a log-compressed distance that makes one extreme feature count for less.

Step 4: how the neighbours are picked

A true nearest-neighbour search would compute the distance to every stored bar and keep the eight smallest. This script scans stored bars from oldest to newest and keeps a running list of at most eight. A bar is skipped if its position is a multiple of four. Otherwise it is added when its distance is at least the current threshold, which starts at -1 on every bar, so the first candidate is always added. When the list grows past eight, the oldest entry is dropped and the threshold is reset to the distance stored at position six of the list. The author describes this as keeping a chronologically spread set and calls it an approximate search.

Because a candidate must be at least as far as the threshold, the kept set tends to drift toward larger distances rather than smaller ones. In our Python port of just this loop (seeds 1 to 3, 1,500 invented bars, five stand-in oscillators, so the figures show the loop's behaviour, not the real library's features), the mean distance of the kept eight was 2.57, 2.41 and 2.53, while the true eight nearest bars averaged 0.107, 0.115 and 0.123. Nothing in the code is hidden here; the comments say "approximate". The point is how far the approximation sits from the thing its name suggests.

Step 5: old neighbours carry over

The two lists are declared with var, so they keep their contents from one bar to the next, and they are not cleared at the start of each bar's scan. A new bar therefore begins with the previous bar's eight neighbours still in place, and they are replaced only as new candidates pass the threshold test. In the same port, on average 4.78, 4.12 and 4.62 of the eight kept entries had been added during an earlier bar's scan. Whether that matters on a real chart depends on the real features; it is a reason the vote can change more slowly than the price does.

Step 6: the vote, the filters and the side

The vote ("prediction" in the script) is the sum of the kept labels, so with eight neighbours it runs from -8 to +8. If it is above zero and the filters pass, the script's side becomes long; below zero and passing, short; otherwise it keeps the previous side. The volatility filter and the regime filter are on by default (regime threshold -0.1), the ADX filter is off, and optional EMA and SMA filters (200 bars) are off. The filters come from the imported library. The number printed above or below each bar is this vote, and the bar colour fades with its size.

Step 7: the kernel filter and the Buy and Sell labels

A Buy or Sell label needs the side to have just changed and the kernel estimate to agree. The kernel estimate is a smoothed line of the source from an imported library (Rational Quadratic, lookback 8, relative weighting 8, regression level 25 by default). With Trade with Kernel on, a new long side needs that line to be rising from the previous bar, and a new short side needs it falling. Enhance Kernel Smoothing swaps that rule for a crossing of two kernel lines and lag 2.

Step 8: exits and the trade-stats table

Exits are hidden by default. Show Default Exits draws a cross four bars after an entry (or earlier if the side flips). Use Dynamic Exits ends the trade when the kernel turns instead. The table in the corner (a first ratio row, trade counts, a win-to-loss ratio and a count of early side flips) is produced by a function in the imported library; the script's own tooltip says it should not replace backtesting and is for calibration. This page did not read that function, so it does not say how a trade is entered, priced or closed in it, or whether costs are included.

The alerts

Eight alertcondition entries exist: open and close long, open and close short, open or close a position, and the kernel turning bullish or bearish. Each is a message that fires when its condition is true on a bar. On a bar that has not closed, a condition that is true can become false before the close, so check the alert's "once per bar close" option against what you want.

What the settings change

Neighbors Count sets how many labels are summed, so it sets the largest possible vote. Max Bars Back and Include Full History change which stored bars are scanned. Feature Count and the feature choices change the distance. Color Compression only changes colour. The filters change when the side may flip. None of the settings changes the "keep a candidate if it is at least the threshold" rule.

Where it fails

Also explained: Breakout Probability (Expo) · [Liquidity Swings [LuxAlgo]](/liquidity-swings-luxalgo) · Support and Resistance Levels with Breaks · Trendlines with Breaks · Order Block Finder

Educational only. Not financial advice. Nothing here is a recommendation to buy or sell anything. Machine Learning: Lorentzian Classification is by jdehorty on TradingView, published under MPL 2.0; this page describes its published open-source code in our own words and is not affiliated with the author. Simulation figures come from synthetic random-walk prices and say nothing about how the script trades.

Also explained: Repaint checker, VWAP, Supertrend, RSI, UT Bot Alerts, Nadaraya-Watson Envelope, Squeeze Momentum, What is repainting on TradingView?, Is TradingView backtesting accurate?, Chandelier Exit, WaveTrend Oscillator, QQE MOD, Pivot highs and lows, Hull Suite, Range Filter, Twin Range Filter, HalfTrend, ZigZag++, SSL Hybrid, CM MacD Ultimate MTF, Volume Profile / Fixed Range, ADX and DI, Support Resistance Channels, Market Structure Break & Order Block, Order Block Finder, Pivot Point SuperTrend, Trendlines with Breaks [LuxAlgo], Divergence for Many Indicators, TradingView script selling terms, Support and Resistance Levels with Break, Liquidity Swings [LuxAlgo], Pine Script errors, Breakout Probability (Expo), Did ChatGPT or Claude write your Pine Sc, SuperTrend AI (Clustering), Machine Learning Adaptive SuperTrend, Dynamic Swing Anchored VWAP, Support and Resistance Power Channel by , Support and Resistance (High Volume Boxe, Trendline Breakouts With Targets by Char, Breakout Finder by LonesomeTheBlue

Next steps

  • Get a script fixed. Compiles but behaves differently live, or changes after a reload? In a Pine Script Audit, an AI model reads every line and writes a report on what to fix for $49, or also fixes those problems for $149. It cannot prove a script is safe.
  • Check your own script. Paste its source into the free repaint checker. It runs in your browser and flags known patterns.
Subscribe to the Teardown Letter: $19/moBack to Taught to Trade

No TradingView account yet? This affiliate link gives you a $15 coupon toward a paid plan, and we earn a commission if you buy one. The free plan is enough to follow this page.