Indicator · Pine Script v5 · On the chart

AR Forecast Scatterplot

by regalouisei· code credited to Steversteves

This indicator plots an AR forecast on the price chart using the selected source.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code130
Settings9inputs
LicenceNone statedfile: MPL-2.0
Last update8 months ago25 Feb 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

130 lines of Pine Script v5 from pinescript/editors_picks/ar-forecast-scatterplot-ss.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

ar-forecast-scatterplot-ss.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//  /$$$$$$   /$$                                                         /$$                                            
// /$$__  $$ | $$                                                        | $$                                            
//| $$  \__//$$$$$$    /$$$$$$  /$$    /$$ /$$$$$$   /$$$$$$   /$$$$$$$ /$$$$$$    /$$$$$$  /$$    /$$ /$$$$$$   /$$$$$$$
//|  $$$$$$|_  $$_/   /$$__  $$|  $$  /$$//$$__  $$ /$$__  $$ /$$_____/|_  $$_/   /$$__  $$|  $$  /$$//$$__  $$ /$$_____/
// \____  $$ | $$    | $$$$$$$$ \  $$/$$/| $$$$$$$$| $$  \__/|  $$$$$$   | $$    | $$$$$$$$ \  $$/$$/| $$$$$$$$|  $$$$$$ 
// /$$  \ $$ | $$ /$$| $$_____/  \  $$$/ | $$_____/| $$       \____  $$  | $$ /$$| $$_____/  \  $$$/ | $$_____/ \____  $$
//|  $$$$$$/ |  $$$$/|  $$$$$$$   \  $/  |  $$$$$$$| $$       /$$$$$$$/  |  $$$$/|  $$$$$$$   \  $/  |  $$$$$$$ /$$$$$$$/
// \______/   \___/   \_______/    \_/    \_______/|__/      |_______/    \___/   \_______/    \_/    \_______/|_______/ 

//       ___________________
//      /                   \
//     /  _____        _____ \
//    /  /     \      /     \  \
// __/__/       \____/       \__\_____
//|           ___________           ____|
// \_________/           \_________/
//            \  /////// /
//             \/////////
// © Steversteves
//@version=5
indicator("AR Forecast Scatterplot [SS]", overlay=true, max_lines_count = 500, max_labels_count = 500, max_bars_back = 1000)

import Steversteves/SPTS_StatsPakLib/1 as spts 

//@peacefulLizard50262
flush(source)=>
    if source.size() > 0
        for i = source.size() - 1 to 0
            source.get(i).delete()
            source.remove(i)

t1 = "This will plot a line from the first Result to the last Result for the forecast length"
t2 = "Showing results and variances will plot both the results (blue) and the standard errors based on the variance in the dataset. Only plotting reuslts will only plot the most likely outcome without the variance."
t3 = "This will omit the need for you to identify the lookback length yourself and will autofind the best lookback period"
// Inputs 
src =       input.source(close, "Forecast Source"),      train1 = input.int(150, "Train Time"),        len = input.int(14, "Forecast Length"),    
typ =       input.string("Scatter Plot", "Scatter Plot Type", ["Line Plot", "Scatter Plot"]),            show_stats = input.bool(true, "Show Model Statistics")
line_bf =   input.bool(true, "Show Trendline", tooltip = t1),       show_others = input.string("Show Results and Variances","Show Results/Variance", ["Show Results and Variances", "Show Results Only"], tooltip = t2)
autolen =   input.bool(true, "Use Auto lookback based on strongest trend", tooltip = t3)
onlytrend = input.bool(false, "Only Show Trendline") 
// Arrays 
results = array.new_float() 
ucl = array.new_float() 
lcl = array.new_float() 

// Find Trend
trend_finder(len) => 
    ta.correlation(close, time, len) 

t50 = trend_finder(50),     t100 = trend_finder(100),       t150 = trend_finder(150),       t200 = trend_finder(200),   t250 = trend_finder(250),       t300 = trend_finder(300),       t350 = trend_finder(350),
t400 = trend_finder(400),   t450 = trend_finder(450),       t500 = trend_finder(500),       t550 = trend_finder(550),   t600 = trend_finder(600),       t650 = trend_finder(650),       t700 = trend_finder(700),
strongest_trend = math.max(t50, t100, t150, t200, t250, t300, t350, t400, t450, t500, t550, t600, t650, t700) 
int find_trend = strongest_trend == t50 ? 50 : strongest_trend == t100 ? 100 : strongest_trend == t150 ? 150 : strongest_trend == t200 ? 200 : strongest_trend == t250 ? 250 : strongest_trend == t300 ? 300 : strongest_trend == t350 ? 350 : strongest_trend == t400 ? 400 : strongest_trend == t450 ? 450 : strongest_trend == t500 ? 500 : strongest_trend == t550 ? 550 : strongest_trend == t600 ? 600 : strongest_trend == t650 ? 650 : strongest_trend == t700 ? 700 : 100

int train = 0 
if autolen 
    train := find_trend 
else 
    train := train1


// Forecast 
[forecast_result, b, c] = spts.f_forecast(src, src[1], train, len, results, ucl, lcl)

// Model Statistics 
cor = ta.correlation(src, src[len], train) 
r2 = math.pow(cor,2) 
max_val = array.max(ucl) 
min_val = array.min(lcl) 

f_primary_labels(id) => 
    label.new(bar_index + id, array.get(results, id), text = "", color = color.blue, style = label.style_circle, size = size.tiny)
f_secondary_labels(id) => 
    label.new(bar_index + id, array.get(ucl, id), text = "", color = color.lime, style = label.style_circle, size = size.tiny)
f_secondary_labels2(id) => 
    label.new(bar_index + id, array.get(lcl, id), text = "", color = color.red, style = label.style_circle, size = size.tiny)    
f_primary_line(id) => 
    line.new(bar_index + id, y1 = array.get(results, id), x2 = bar_index + 1 + id, y2 = array.get(results, id), color = color.blue, width = 3)
f_secondary_line(id) =>     
    line.new(bar_index + id, y1 = array.get(ucl, id), x2 = bar_index + 1 + id, y2 = array.get(ucl, id), color = color.lime, width = 3)
f_secondary_line2(id) =>    
    line.new(bar_index + id, y1 = array.get(lcl, id), x2 = bar_index + 1 + id, y2 = array.get(lcl, id), color = color.red, width = 3)   

var label1_ar = array.new<label>() 
var label2_ar = array.new<label>() 
var label3_ar = array.new<label>() 
var line1_ar = array.new<line>() 
var line2_ar = array.new<line>() 
var line3_ar = array.new<line>() 

flush(label1_ar)
flush(label2_ar)
flush(label3_ar)
flush(line1_ar)
flush(line2_ar)
flush(line3_ar)

var forecast_line = array.new<line>()
flush(forecast_line)

if barstate.islast and line_bf 
    if forecast_result.size() > 0
        value = 0.0
        for i = 1 to forecast_result.size() - 1
            forecast_line.push(line.new(bar_index + i - 1, forecast_result.get(i - 1), bar_index + i , forecast_result.get(i), color = color.purple, width = 3))


// Plots 
var table data = table.new(position.middle_right, 2, 7, bgcolor = color.blue, frame_color = color.rgb(0, 0, 0), frame_width = 4)
if show_stats 
    table.cell(data, 1, 1, text = "Model Data", bgcolor = color.blue, text_color = color.white) 
    table.cell(data, 1, 2, text = "Correlation: " + str.tostring(math.round(cor,2)), bgcolor = color.blue, text_color = color.white) 
    table.cell(data, 1, 3, text = "R2: " + str.tostring(math.round(r2, 3)), bgcolor = color.blue, text_color = color.white) 
    table.cell(data, 1, 4, text = "Max Forecasted Value: " + str.tostring(math.round(max_val, 2)), bgcolor = color.blue, text_color = color.white) 
    table.cell(data, 1, 5, text = "Min Forecasted Value: " + str.tostring(math.round(min_val, 2)), bgcolor = color.blue, text_color = color.white) 
    table.cell(data, 1, 6, text = "Lookback Length: " + str.tostring(train), bgcolor = color.blue, text_color = color.white) 
if barstate.islast and typ == "Scatter Plot" and not onlytrend
    for idx = 0 to len 
        label1_ar.push(f_primary_labels(idx))
        if show_others == "Show Results and Variances"
            label2_ar.push(f_secondary_labels(idx))
            label3_ar.push(f_secondary_labels2(idx))

if barstate.islast and typ == "Line Plot" and not onlytrend
    for idx = 0 to len 
        line1_ar.push(f_primary_line(idx))
        if show_others == "Show Results and Variances"
            line2_ar.push(f_secondary_line(idx))
            line3_ar.push(f_secondary_line2(idx))

Overview

AR Forecast Scatterplot computes autocorrelation between close and time across multiple lookback lengths to identify the strongest trend, then generates forecast results. Results appear as a scatter plot or line plot with an optional trendline linking the first and last points. Model statistics, results, and variances can be shown based on user settings. The train time sets the dataset length for analysis while the forecast length defines the prediction horizon. Auto lookback option selects the optimal period automatically. The plot draws directly on the main price chart.

Details

Published by

regalouisei on GitHub

Code credit

Steversteves, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Trend

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Forecast Sourcesourceclosesrc
Train Timeint150train1
Forecast Lengthint14len
Scatter Plot TypestringScatter Plottyp
Show Model Statisticsbooltrueshow_stats
Show Trendlinebooltrueline_bf
Show Results/VariancestringShow Results and Variancesshow_others
Use Auto lookback based on strongest trendbooltrueautolen
Only Show Trendlineboolfalseonlytrend
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

948 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 10 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
575 stars

This indicator detects equal highs and lows by comparing prices within user-defined left and right bars using a tolerance percentage. It plots horizontal lines on the price chart for equal highs in one color and lows in another. A screener allows input of mult

Indicator v5 Support & resistanceTrend
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows with Screener
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator calculates an ATR trailing stop line derived from the highest high minus an average true range multiple over a specified lookback period. It plots the trailing stop line directly on the price chart and colors price bars based on whether the clos

Indicator v5 TrendVolatility & bands
Updated 2 months ago · MIT GitHub repository of ATR Trailing Stoploss

All Candlestick Patterns

by shunjizhan
292 stars

All Candlestick Patterns detects and highlights a wide range of bullish, bearish, and neutral candlestick patterns directly on the price chart. It features optional trend filtering using either a 50-period simple moving average or a combination of 50-period an

Indicator v4 Candlestick patternsTrend
Updated 3 years ago · None stated GitHub repository of All Candlestick Patterns

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers