Logistic Regression (LR): A Machine-Learning Trend Classifier
How Sahi's Logistic Regression indicator classifies the market as bullish or bearish, and how to trade its arrows and exits.
Quick Answer
Logistic Regression (LR) is a machine-learning classifier adapted for price charts. Each bar it trains on price and asks whether the current pattern looks more bullish or bearish, flipping only when the evidence crosses a threshold. On the chart, an aqua arrow-up below a bar is a bullish (CE) cue and a pink arrow-down above a bar is a bearish (PE) cue; small crosses mark the time-based exit after the holding period. Trade the first bar of a flip and respect the holding-period exit. It is free on Sahi, with no subscription required.
Overview
Logistic Regression is a machine-learning model originally built for binary classification — predicting whether something belongs to one of two groups. Here it is adapted for price charts: it trains on price data bar by bar, computes a probability-like prediction, and classifies the market as either bullish or bearish. The core math is a sigmoid function that squashes any input into a 0–1 range, updated through gradient descent over several hundred iterations each bar. In plain terms, the indicator keeps asking "does the current price pattern look more like the start of a rally or the start of a drop?" and flips its answer only when the evidence crosses a threshold.
On the Sahi chart it draws aqua (blue-toned) arrow-up labels below bars when it turns bullish, and pink arrow-down labels above bars when it turns bearish. When a trade has run for its allotted holding period, it marks the exit with a cross: aqua crosses above the bar for long exits, pink crosses below the bar for short exits. Optionally it can also overlay two curves — a blue loss curve and a lime prediction curve — and display a small stats label in the top-right corner showing cumulative return, trade count, signal accuracy, and signal hit rate.
How to Read the Indicator
Aqua Arrow-Up Label (bullish signal)
A solid aqua label pointing upward appears below the bar when the indicator flips from neutral or bearish to bullish. This is the entry cue for a long / CE trade. The label appears at bar close, so always wait for the candle to fully close before acting — an intra-bar label can disappear if the bar continues lower.
- Aqua label below the bar = model has turned bullish; look for a long entry.
- The label only appears on the first bar of a new bullish phase, not on every continuation bar.
Pink Arrow-Down Label (bearish signal)
A solid pink label pointing downward appears above the bar when the indicator flips from neutral or bullish to bearish. This is the entry cue for a short / PE trade.
- Pink label above the bar = model has turned bearish; look for a short entry.
- Same rule: wait for bar close before acting.
Aqua Cross (long exit / holding-period end)
A small aqua cross mark appears above the bar after a bullish signal has persisted for exactly the number of bars set in the Holding Period input. This is a time-based exit signal for the long trade, telling you the indicator's holding window for that signal is over.
- Aqua cross = the current bullish signal's holding clock has expired; consider exiting or tightening your stop.
- An aqua cross can also appear if the signal flips bearish before the holding period is up — in that case the flip itself acts as the exit trigger.
Pink Cross (short exit / holding-period end)
A small pink cross mark appears below the bar after a bearish signal has run for the full holding period, or when the signal flips bullish.
- Pink cross = the bearish holding window is closed; consider exiting the PE trade.
Loss Curve (blue line) and Prediction Curve (lime line)
These two overlaid lines are hidden by default and only appear when Show Loss & Prediction Curves is turned on. Both are scaled to the price axis using a minimax normalisation so they are visible alongside price.
- Blue line = the model's training loss scaled to price — lower is better; a falling blue line means the model is converging well on recent data.
- Lime line = the model's probability prediction scaled to price — when the lime line is above the blue line, the model leans bullish; below it, bearish.
- When using Price Data for Signal Generation (the default mode), signals are driven by price vs. the loss line rather than by a crossover of these two curves. The curves are most useful for understanding model state, not for direct entries.
Stats Label (info box)
When Show Info is enabled, a small dark text label appears at the most recent bar showing live backtesting stats:
- CR — cumulative return in price points scaled by lot size.
- Trades — total completed trades since chart load.
- Signal Accuracy — the ratio of profitable trades to losing trades.
- Signal Hit Rate — percentage of trades that closed profitably.
- Bar Time — how far into the current bar the chart is (useful on live feeds to see if a bar is nearly closed).
These stats reset when you reload the chart. Treat them as a quick directional sanity check, not a full backtest.
Example Scenarios
Scenario 1 — Aqua label fires at support, buy a CE (bullish)
Nifty is trading around 24,820 after a two-day pullback. On the 15-minute chart a pink label had appeared two sessions ago marking a bearish phase. At 10:30 AM today a new aqua arrow-up label appears below a candle that closes at 24,835 — the indicator has flipped bullish. Price is also sitting near a prior intraday low at 24,800, adding confluence.
- What you do: buy 1 lot of Nifty 24,900 CE at a premium of about ₹95.
- Stop loss: if a pink label fires (model flips back bearish) before the holding period ends, exit immediately — that candle's close is likely around 24,790, where the CE premium would be near ₹60.
- Target: the aqua cross (holding-period exit) fires around bar 5; if Nifty is near 24,950 by then the CE premium would be roughly ₹145.
- Why it works: you entered on the first bar of a confirmed bullish flip, with a clear signal-based stop just one label away.
Scenario 2 — Pink label fires at intraday high, buy a PE (bearish)
Nifty has rallied to 25,080 and is testing a prior day's high. On the 15-minute chart an aqua label had been in effect for four bars. At 11:15 AM a new pink arrow-down label appears above the bar closing at 25,065 — the indicator has flipped bearish. The flip happens right at resistance, giving a clean short setup.
- What you do: buy 1 lot of Nifty 25,000 PE at a premium of about ₹105.
- Stop loss: if an aqua label fires (model flips bullish again), exit — price would be near 25,110 at that point and the PE would be around ₹68.
- Target: the pink cross fires after the holding period; if Nifty is near 24,970 by then, the PE premium would be roughly ₹155.
- Why it works: you shorted on the first bar of a confirmed bearish flip at a well-tested intraday high.
Scenario 3 — What to avoid
- Do not enter mid-signal. The aqua or pink label only appears on the first bar of a phase change. If you missed bar 1 and price has already run 40–50 points, the risk/reward for entering on bar 3 or 4 is poor — the holding-period clock is already ticking toward the exit cross.
- Do not ignore the holding-period clock. The exit cross appears after a fixed number of bars regardless of whether the trade is profitable. If you are deep in-the-money as the cross approaches, take partial profits rather than waiting for a signal flip that may not come.
- Be cautious when Loss and Prediction Curves are nearly flat or tangled. When the two curves (if shown) are close together and zig-zagging, the model has low confidence. A label that fires during this period is a lower-quality signal — wait for a session with clear curve separation.
- Avoid the first 2–3 bars of the session. The normalisation lookback needs enough bars to stabilise. Signals that fire in the opening minutes of a session (before the lookback fills) may be spurious.
- Do not chain a higher-timeframe Resolution with a very short Lookback. Setting Resolution to a 1-hour timeframe and Lookback to 2 means the model trains on only 2 hourly bars — far too little data. Keep the product of Resolution × Lookback equivalent to at least 15–20 base candles.
Settings Panel Guide
Model Inputs
- Price Type: which price series the model trains on. Default is Close. Options: Open, High, Low, Close, HL2, OC2, OHL3, HLC3, OHLC4. For Nifty intraday, Close is standard; HLC3 is a good alternative if you want to reduce wick noise.
- Resolution: the timeframe from which price data is fetched. Default is blank (current chart timeframe). Set to a higher timeframe (e.g. "15" for 15 minutes from a 5-minute chart) to get smoother, slower signals. Leaving it blank is recommended unless you want deliberate higher-timeframe confirmation.
- Lookback Window Size: how many bars the model trains over each iteration. Default is 3, minimum 2. A larger window (6–10) produces fewer but smoother signals; a smaller window reacts faster but generates more noise. For Nifty 15-minute charts, 3–5 works well.
- Normalization Lookback: the number of bars used to scale the loss and prediction curves back onto the price axis. Default is 2, maximum 240. Increase to 20–30 if the overlay curves look erratic or clipped at extremes.
- Learning Rate: controls how large each gradient step is during training. Default is 0.0009. Range 0.0001–0.01. Lower values = slower convergence but more stable. Higher values = faster adaptation but can overshoot. Leave at default unless you are experimenting.
- Training Iterations: how many gradient-descent steps run per bar. Default is 1000. Range 50–20,000. More iterations = better-fitted model but heavier computation. For intraday live charts, 500–1000 is the practical sweet spot.
- Optional Calculation: toggles between two internal dataset modes. Default is on (true). When on, the model uses bar timestamps as the primary feature and price as the secondary. Turn off to run price against a log-transformed synthetic dataset instead. Leave on for standard use.
- Use Price Data for Signal Generation: when on (default), signals are triggered by price crossing above/below the scaled loss line. When off, signals are triggered by the scaled loss and prediction curves crossing each other. The default (on) is more responsive for intraday trading.
Signal Filtering
- Filter Signals by: adds an extra condition before a new label can fire. Default is None. Options:
- None — every model flip generates a label immediately.
- Volatility — only fires when short-term ATR(1) is above long-term ATR(10); suppresses signals during low-volatility consolidation.
- Volume — only fires when a smoothed RSI of volume exceeds 49; suppresses signals when participation is thin.
- Both — both conditions must be true simultaneously; the strictest filter, fewest signals.
Trade Management
- Holding Period: the number of bars after a signal before the exit cross appears. Default is 5. Minimum 1. Set this to match your typical trade duration — on a 15-minute chart, 5 bars = 75 minutes. Increase to 8–10 for broader targets; decrease to 2–3 for scalping.
Display
- Show Loss & Prediction Curves: toggles the blue (loss) and lime (prediction) overlay lines. Default is off. Turn on to study model behaviour or for educational purposes; turn off for a cleaner chart during live trading.
- Show Info: toggles the stats label at the latest bar. Default is on. Shows cumulative return, trade count, signal accuracy, signal hit rate, and bar-time progress. Turn off if the label overlaps with other indicators.
Backtesting
- Lot Size: scales the cumulative-return figure in the stats label. Default is 0.01. Options: 0.01, 0.1, 0.2, 0.3, 0.5, 1, 2, 3, 5, 10, 20, 30, 50, 100, 1000. Set to match your actual lot multiplier to get meaningful ₹ return figures in the info label.