AR Forecast Scatterplot [SS]
// 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)

import Steversteves/SPTS_StatsPakLib/1 as spts 

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."

// Inputs 
src = input.source(close, "Forecast Source"),      train = 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)
// Arrays 
results = array.new_float() 
ucl = array.new_float() 
lcl = array.new_float() 

// Forecast 
[a, 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) 


// Plots 
var table data = table.new(position.middle_right, 2, 6, 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) 
if barstate.islast and typ == "Line Plot"
    for i = 0 to len 
        line.new(bar_index + i, y1 = array.get(results, i), x2 = bar_index + 1 + i, y2 = array.get(results, i), color = color.blue, width = 3)
        if show_others == "Show Results and Variances"
            line.new(bar_index + i, y1 = array.get(ucl, i), x2 = bar_index + 1 + i, y2 = array.get(ucl, i), color = color.lime, width = 3)
            line.new(bar_index + i, y1 = array.get(lcl, i), x2 = bar_index + 1 + i, y2 = array.get(lcl, i), color = color.red, width = 3)

if barstate.islast and typ == "Scatter Plot" 
    for i = 0 to len 
        label.new(bar_index + i, array.get(results, i), text = "", color = color.blue, style = label.style_circle, size = size.tiny)
        if show_others == "Show Results and Variances"
            label.new(bar_index + i, array.get(ucl, i), text = "", color = color.lime, style = label.style_circle, size = size.tiny)
            label.new(bar_index + i, array.get(lcl, i), text = "", color = color.red, style = label.style_circle, size = size.tiny)


var line trendline = na 
if barstate.islast and line_bf 
    line.delete(trendline) 
    trendline := line.new(bar_index[1], y1 = array.get(results, 0), x2 = bar_index + array.size(results) - 1, y2 = array.get(results, array.size(results) - 1), color = color.purple, width  = 3)