Developed from original source here:
https://www.tradingview.com/script/ia5ozyMF-MLExtensions/
by https://www.tradingview.com/u/jdehorty/
This indicator aims to detect trending or ranging markets.
There is no exact explanation of the mechanics, but comments are added in the code below by interpretation.
First, an adaptive smoothing constant is generated based on average price slope and volatility
The constant looks like a solution of a quadratic equation and scaled to be between 0 and 1 for use in EMA KAMA filter or price.
The output KAMA slope is compared to averaged KAMA slope to select regime
Defaults are based on original authors values and code corrected for a minor error and numerical protections added for edge cases.
The indicator is aimed at algorithmic trading, and was specifically used on Lorentzian kNN Classification algorithm as an optional trade filter. ADX indicator could be an alternative.
Options are available to show raw signal, or a threshold toggled indication - see screenshot.
The code has also been optimised and improved using Grok3 - this is worthwhile if new to LUA programming, and generally improves coding standard.