Skip to main content

Trend Navigator (TN): A KNN Trend Classifier for Your Charts

How Sahi's Trend Navigator uses a KNN classifier to read trend direction, and how to trade its colour-changing line against its baseline.

Revati Krishna
Published: 3 Jul 2026, 05:30 PM IST (2 weeks ago)
Last Updated: 3 Jul 2026, 01:32 PM IST (2 weeks ago)
8 min read

Quick Answer

Trend Navigator (TN) uses a K-Nearest Neighbours (KNN) classifier to decide trend direction. Its main line changes colour bar by bar — lime green for up, red for down, orange for flat — while a slower teal Average line confirms the bigger-picture trend. Trade in the direction of the coloured line when it sits on the correct side of the teal baseline, and avoid orange, choppy stretches. It is free on Sahi, with no subscription required.

Overview

Trend Navigator uses a K-Nearest Neighbors (KNN) machine-learning classifier to decide the current market direction. Instead of a fixed formula, it looks at how recent smoothed price values compare to historical ones — finding the closest historical "neighbours" — and votes on whether the trend is up or down. The result is a trend line that adapts to price behaviour rather than lagging in a fixed, predictable way.

On the Sahi chart it draws two lines overlaid on price: a KNN Classifier Line that changes colour bar-by-bar to show the live trend direction (lime green = up, red = down, orange = flat/neutral), and a slower Average KNN Classifier Line in teal that acts as a trend-confirmation baseline. An optional chart background colour can be switched on to make the trend state even more visible at a glance.

How to Read the Indicator

KNN Classifier Line (colour-changing trend line)

This is the main signal line. It is plotted directly over price and repaints its colour every bar based on whether the smoothed KNN value is rising, falling, or flat:

  • Lime green — the KNN value is rising; the trend reading is bullish.
  • Red — the KNN value is falling; the trend reading is bearish.
  • Orange — the KNN value is unchanged from the previous bar; the trend is neutral / consolidating.

Use this line as your primary trend filter. A clean run of green bars confirms an uptrend; a clean run of red bars confirms a downtrend. Choppy alternation between colours — especially orange — means the market lacks direction; avoid taking fresh trend-following entries in that state. Always wait for the bar to close before acting on a colour. The line's colour on an unfinished bar can still change before the candle closes.

Average KNN Classifier Line (teal baseline)

This is a smoothed moving average of the KNN Classifier Line, drawn in teal. It moves more slowly and does not change colour. Its role is to confirm the bigger-picture trend direction:

  • When the Classifier Line is above the teal Average line, the overall trend is up — bullish signals from the Classifier Line carry more weight.
  • When the Classifier Line is below the teal Average line, the overall trend is down — bearish signals carry more weight.
  • The teal line also acts as dynamic support/resistance. Price and the Classifier Line often pull back toward it before continuing.

Background Colour (optional)

When enabled, the entire chart background fills with a faint colour that mirrors the KNN prediction:

  • Faint green background — the KNN classifier is predicting a bullish trend.
  • Faint red background — the KNN classifier is predicting a bearish trend.

This is purely a visual aid. It uses the same underlying prediction as the Classifier Line, so it adds no new information — it just makes the trend state impossible to miss. It is off by default because it can make a busy chart harder to read; turn it on if you find yourself frequently mis-reading the line colours.

Example Scenarios

Scenario 1 — Classifier Line turns green above the teal line, buy a CE (bullish)

Nifty has been consolidating around 24,820. The KNN Classifier Line had been orange for several bars. On the 15-minute chart a candle closes and the Classifier Line turns lime green, and it is sitting clearly above the teal Average KNN line. The teal line is also starting to slope upward. The trend state has just shifted from neutral to confirmed bullish.

  • What you do: buy 1 lot of Nifty 24,900 CE at a premium of about ₹105.
  • Stop loss: if the Classifier Line turns red and closes below the teal line, the trend has reversed — exit. The premium would be around ₹68 there, making the risk roughly ₹37 per unit.
  • Target: the prior swing high near 25,050. If Nifty reaches it, the CE premium would be roughly ₹160.
  • Why it works: you entered at the earliest confirmed bar of a new uptrend, filtered by the teal line to avoid false flips, with a clear structural exit signal.

Scenario 2 — Classifier Line turns red below the teal line, buy a PE (bearish)

Nifty is trading near 25,080 after a short rally. The Classifier Line had been green but is now printing red bars, and the teal Average KNN line is above the Classifier Line — the trend read has flipped bearish. Each successive red bar on the Classifier Line is closing further below the teal line, confirming the move.

  • What you do: buy 1 lot of Nifty 25,000 PE at a premium of about ₹115.
  • Stop loss: if the Classifier Line crosses back above the teal line and closes green, the bearish signal is invalidated — exit. The premium would be around ₹78 there.
  • Target: 24,900, where the pullback structure sits. The PE premium would be roughly ₹165 there.
  • Why it works: the Classifier Line flipping red beneath its own average is a double-confirmation of momentum turning down, giving a high-probability entry with a clearly defined invalidation.

Scenario 3 — What to avoid

  • Do not trade during orange (neutral) stretches. When the Classifier Line is printing orange bars — or alternating green/orange/red repeatedly — the KNN algorithm does not have a clear directional vote. Entries during this state have no trend backing them.
  • Do not act on a single green or red bar in isolation. One bar's colour change can reverse on the very next bar. Wait for at least two consecutive bars of the same colour, ideally with the Classifier Line on the correct side of the teal Average line, before committing.
  • Do not ignore the teal line relationship. A green Classifier Line that is still below the teal Average line means the short-term reading is bullish but the broader trend context is still bearish. Trading against the teal line alignment is a low-probability entry.
  • Be cautious around major events. News-driven spikes create brief colour changes that do not reflect a genuine trend shift. If the colour flip coincides with an RBI announcement or global data release, wait for at least one clean candle close after the spike before acting.

Settings Panel Guide

Value (Price Input)

  • PriceValue (first dropdown): the price series fed into the KNN algorithm as its input. Default is hl2 (average of high and low). Options: hl2, VWAP, sma, wma, ema, hma. For Nifty intraday, hl2 or ema work well; VWAP is useful on sessions where anchored-VWAP context matters.
  • Smoothing length: the look-back period applied to the PriceValue before it enters the KNN. Default is 5. Lower values make the input more reactive; higher values smooth out noise. Keep between 3–10 for intraday; increase toward 14–20 for positional.

Target (Target Input)

  • TargetValue (second dropdown): the reference series the KNN uses as its classification target. Default is Price Action (an RMA of close). Options: Price Action, VWAP, Volatility, sma, wma, ema, hma. Price Action is the best starting point for most setups. Volatility (ATR-based) can help on high-beta days when raw price is too noisy.
  • Target smoothing length: smoothing period for the TargetValue. Default is 5. Same guidance as PriceValue length above — keep them in the same ballpark for a balanced signal.

KNN Algorithm

  • Number of Closest Values: how many historical "neighbours" the KNN considers when voting. Default is 3. A higher number (e.g. 8–15) produces a smoother, more conservative line that is slower to flip; a lower number (2–4) is more responsive but generates more noise. For Nifty 5-min or 15-min charts, 3–5 is the practical range.
  • Smoothing Period: the period of the moving average applied to the final KNN output to produce the Classifier Line. Default is 50. This is the dominant control over how smoothly the line moves, increase to 75–100 for a less reactive, trend-following signal; decrease to 20–30 if you want earlier turns at the cost of more false flips.

KNN Color

Three colour swatches that control the colour of the KNN Classifier Line in each state. Defaults: lime (up), red (down), orange (neutral). Cosmetic only.

MA KNN Color

One colour swatch for the Average KNN Classifier Line (teal baseline). Default is teal. Cosmetic only.

BG Color

  • Trend Prediction Color (toggle): switches the background colour fill on or off. Default is off. Turn on if you want the trend state to be clearly visible without closely watching the line colours — useful on smaller screens.
  • Up colour swatch: the background fill colour when the prediction is bullish. Default is lime. Cosmetic only.
  • Down colour swatch: the background fill colour when the prediction is bearish. Default is red. Cosmetic only.

Frequently Asked Questions (FAQs)

All topics