All indicators

Machine Learning Z-Score Buy and Sell

Sep 1, 2025

Static chart image
SignalsOscillatorsMachine Learning

The Machine Learning Z-Score Buy and Sell indicator utilizes dynamic Z-score calculations and automated trend correlation to identify mean-reversion opportunities and target price levels.

Usage

The Usage section describes how the script can be used to identify trend exhaustion and subsequent reversion to a mean.

  • Mean Reversion Signals: The script identifies "Buy the Dip" or "Sell the Rally" areas when the Z-score reaches historical extremes (highest/lowest points over the lookback period). These areas are marked with labels on the chart.
  • Target Price (TP): When a signal is generated, the indicator calculates a target price based on the mean. This target is displayed within the label and can be plotted as a horizontal line (green for buy targets, red for sell targets).
  • Automated Lookback: By default, the tool iterates through multiple lookback lengths (from 50 to 500) to find the strongest trend correlation. This ensures the Z-score calculation is always tuned to the most relevant market cycle.
  • Manual Adjustments: Traders can override the machine learning component to manually define the trend length or set a specific standard deviation for the target price (e.g., setting a positive SD to buy dips in an uptrend or a negative SD to short rips in a downtrend).

Details

The script is built on the concept of statistical deviation. The Z-score measures how many standard deviations price is away from the Simple Moving Average (SMA).

The "Machine Learning" aspect is implemented via a trend correlation function. The script calculates the absolute correlation between time and price across ten different lengths (50, 100, 150, 200, 250, 300, 350, 400, 450, and 500). It selects the length with the highest correlation coefficient (maxCor) to determine the optimal SMA and Standard Deviation for the current market environment. This automation removes the qualitative guesswork typically required to choose a lookback period.

Settings

General Settings

  • Auto Adjusted Length: Enables the automated selection of the lookback period based on the highest trend correlation.
  • Length: The manual lookback period used if "Auto Adjusted Length" is disabled.
  • Plot Target Price Lines: Toggles the visibility of the horizontal target price lines projected from signals.
  • Manual Pullback Input: When enabled, allows the user to manually define the target price using a fixed standard deviation.
  • Pullback Standard Deviation: Sets the specific standard deviation value for the manual target price (e.g., 1.0 or -1.0).

FAQ

How do I interpret the Target Price (TP)? The TP represents the price level where the asset is expected to return based on the current mean. It serves as a statistical exit point for mean-reversion trades.

Why are some bars colored red or green? The script highlights bars where the Z-score exceeds a value of 2 (overbought) or falls below -2 (oversold), providing a visual representation of statistical extremes.

How can I access this tool? You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.

Free indicator

Get free access to this indicator on the platforms below.

TradingView
NinjaTrader
MetaTrader 4/5

Unlock the entire LuxAlgo Library

Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.