Indicators/probuilder · probacktest · proorder · proscreener

TimeSeriesAverage

TimeSeriesAverage in ProBuilder returns a regression based moving average of price over N bars, reacting faster than a simple average. Syntax and examples.

Syntax

probuilder
TimeSeriesAverage[N](price)

Parameters

NameTypeDefaultDescription
NintegernoneNumber of bars in the lookback window. Small values follow price closely, large values produce a smoother, slower line.
priceprice sourcenoneThe series to average. Typically close, but open, high, low, typicalprice or any custom variable is accepted.

Formula

code
TSMA = value at the current bar of the least squares
       regression line fitted to the last N values of price

A straight line is fitted through the last N values of price using linear regression, and the average takes the value of that line at the most recent bar. Because the fit projects the current direction of the data rather than weighting all bars equally, the result hugs a trending series more tightly than a simple average of the same period.

How it works

On every bar, the function refits the regression line over the rolling window of the last N values and outputs the endpoint of that line. The output is a smoothed price series in the same units as the input, so it can be plotted directly on the price chart, compared against other moving averages, or used as a dynamic support and reference level.

Compared with Average (simple moving average), the time series method has less lag in sustained trends because the regression fit extrapolates the slope of the window. The trade-off is behaviour at turning points: when price reverses sharply, the fitted line can briefly point the wrong way or overshoot before the window catches up.

The choice of N controls the balance between responsiveness and stability. A short window such as 10 reacts within a few bars but produces more false direction changes. A longer window such as 50 or 100 filters noise and is better suited to identifying the underlying trend.

Examples

Example 1, Basic 20-bar smoothing (Indicator)

probuilder
// Smooth the closing prices over the last 20 bars
averagePrice = TimeSeriesAverage[20](close)
RETURN averagePrice

Computes the 20-period time series moving average of the close and plots it. The line stays close to price in trends while filtering bar-to-bar noise.

Example 2, Fast and slow average crossover (ProOrder)

probuilder
// Regression based fast line against a simple slow line
fastLine = TimeSeriesAverage[20](close)
slowLine = Average[50](close)

IF fastLine CROSSES OVER slowLine THEN
  BUY 1 CONTRACT AT MARKET
ELSIF fastLine CROSSES UNDER slowLine THEN
  SELL AT MARKET
ENDIF

A classic dual moving average strategy. Using TimeSeriesAverage for the fast line triggers crossovers slightly earlier than a simple average of the same period would.

Example 3, Instruments trading above their average (ProScreener)

probuilder
tsa = TimeSeriesAverage[20](close)
SCREENER[close > tsa] ((close / tsa - 1) * 100 AS "Pct above TSA")

Returns instruments whose last price is above their 20-period time series average, sorted by the percentage distance between price and the line.

Interpretation

Readings follow the usual moving average conventions. Price above a rising line indicates an uptrend, price below a falling line indicates a downtrend. Crossings between price and the line, or between a fast and a slow average, are commonly used as trend-change signals. Because the regression endpoint reacts quickly, signals appear earlier than with a simple average but include more whipsaws in sideways markets. Confirming with a slope condition, for example the line being higher than five bars ago, filters part of that noise.

Common errors and gotchas

  • Wrong bracket type. TimeSeriesAverage(20, close) does not compile. The period goes in square brackets and the price source in parentheses: TimeSeriesAverage[20](close).
  • Overshoot at reversals. The regression fit extrapolates the current slope, so after a sharp reversal the line can keep moving in the old direction for a few bars. Signals taken at exact turning points are less reliable than with slower averages.
  • Insufficient history. The first N bars of a chart do not contain a full window, so early values are unreliable. Backtests should exclude the warm-up period or preload enough bars.
  • Period too short. Values such as TimeSeriesAverage[3] follow price almost tick for tick and generate constant direction changes. For trend identification, periods of 20 or more are typical.