All indicators

Least Squares Moving Average with BackColor

May 24, 2017

Static chart image
Dynamic OverlaysSignalsMoving Averages

The Least Squares Moving Average with BackColor indicator calculates a linear regression-based moving average to identify price trends and visualizes momentum shifts through dynamic background coloring. This tool helps traders distinguish between bullish, bearish, and neutral market regimes by analyzing the slope and volatility of the LSMA line.

Usage

The Usage section describes how the script can be used to identify market direction. Traders can use the LSMA line as a smoothed representation of price action that reacts faster to changes than a standard Simple Moving Average (SMA).

The primary feature of this script is the automated background coloring:

  • Bullish Trend: When the LSMA slope is positive and exceeds the calculated volatility threshold, the background turns green.
  • Bearish Trend: When the LSMA slope is negative and exceeds the calculated volatility threshold, the background turns red.
  • Neutral/Sideways: When the change in the LSMA is insignificant relative to its standard deviation, the background turns a neutral gray, signaling a lack of clear momentum.

Traders may use these color shifts to time entries or exits, or to avoid trading during choppy, neutral periods.

Details

The script utilizes the ta.linreg function to calculate the Least Squares Moving Average, which finds the line of best fit for a set of data points over a specified period.

To determine the trend state, the indicator compares the current LSMA value to its previous value. To filter out market noise, it calculates a volatility threshold using the standard deviation (ta.stdev) of the absolute changes in the LSMA. If the price movement is less than this threshold, the trend is categorized as neutral. This logic provides a more robust signal than a simple slope change, as it requires the momentum to be statistically significant.

Settings

  • Length: Sets the lookback period for the linear regression calculation. Higher values result in a smoother line with more lag, while lower values are more responsive.
  • Offset: Shifts the LSMA line forward or backward on the horizontal axis.
  • Source: Determines the price data used for the calculation (e.g., Open, Close, High, Low).

FAQ

How do I interpret the background colors?

The background colors represent the current trend strength and direction. Green indicates a statistically significant upward slope, red indicates a significant downward slope, and gray indicates a neutral or consolidating market where price change is within the expected volatility range.

Can I set alerts for trend changes?

Yes, the script includes built-in alert functionality that triggers when the LSMA trend transitions from neutral or opposite states into a confirmed bullish or bearish trend.

How do I get access to the Least Squares Moving Average with BackColor?

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.