Time Series Model Indicator
// 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("Time Series Model", overlay=true)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                           Groups                                                                                    ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Groups 
inp = "Inputs"
tab = "Tables" 
cors = "Correlation Assessments"
fil = "Fills" 
anom = "Outliers"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                            User Inputs                                                                              ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
len = input.int(50, title="Length of Assessment", group = inp)
allowinput = input.bool(false, title="Allow manual calculcations", group=inp) 
userinput = input.float(14, title="User input hours in advance", group = inp) 
showtable = input.bool(false, title="Show Data Table", group=tab) 
timeframe = input.timeframe("", "Timeframe", group=inp)
trendtable = input.bool(false, title="Show Trend Table", group=tab) 
displayfills = input.bool(true, title="Display outer band fills?", group=fil) 
showoutlier = input.bool(false, title="Show Outlier", group=anom) 
showcor = input.bool(false, title="Show Variable Correlations", group=cors)
showinfill = input.bool(true, title="Show Time Series Fill", group=fil)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                            Slope Calculations                                                                       ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Get the price data
price = close

// Define the time variable
timea = time

// Compute the slope and intercept of the regression line
n = len
sum_x = ta.sma(timea, n)
sum_y = ta.sma(price, n)
sum_xy = ta.sma(timea * price, n)
sum_x2 = ta.sma(timea * timea, n)
slope = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x * sum_x)
intercept = (sum_y - slope * sum_x) / n

cor = ta.correlation(time, close, len) 
// Compute the regression line
reg_line = slope * timea + intercept

// Compute the residuals
residuals = price - reg_line

// Compute the standard error of the regression
sum_sq_residuals = ta.sma(residuals * residuals, n)
std_error = math.sqrt(sum_sq_residuals / (n - 2))

// Compute the upper and lower bounds of the standard error region
std_error_upper = reg_line + std_error
std_error_lower = reg_line - std_error

divse = std_error / 4 

// Extend by SD 

sd = ta.stdev(reg_line, 500) 

sd1 = reg_line + 1.5 * sd 
sd2 = sd1 - (divse * 2) 
sd3 = sd2 - (divse * 4)  
sd4 = sd3 - (divse * 6) 
sd5 = sd4 - (divse * 8)  

sd6 = reg_line - 1.5 * sd 
sd7 = sd6 + (divse * 2)
sd8 = sd7 + (divse * 4) 
sd9 = sd8 + (divse * 6)
sd10 = sd9 + (divse * 8) 



er1 = 1.5 * sd 

dif = sd1 - sd2 
difmax = ta.highest(dif, len) 
difmin = ta.lowest(dif, len) 


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                            Colours                                                                                  ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
color green = color.new(color.green, 50) 
color red = color.new(color.red, 65) 
color orange = color.new(color.orange, 85) 
color gray = color.new(color.gray, 50) 

aquaNavy1 = color.new(color.rgb(0, 128, 128), 75) 
aquaNavy2 = color.new(color.rgb(0, 139, 139), 75) 
aquaNavy3 = color.new(color.rgb(0, 128, 129), 75) 
aquaNavy4 = color.new(color.rgb(0, 130, 140), 75) 
aquaNavy5 = color.new(color.rgb(0, 120, 130), 75) 
aquaNavy6 = color.new(color.rgb(0, 124, 134), 75) 
aquaNavy7 = color.new(color.rgb(0, 125, 136), 75) 
aquaNavy8 = color.new(color.rgb(0, 122, 132), 75) 
aquaNavy9 = color.new(color.rgb(0, 118, 128), 75) 
aquaNavy10 = color.new(color.rgb(0, 116, 126), 75) 

aquaNavytable1 = color.new(color.rgb(0, 120, 130), 55)
aquaNavytable2 = color.new(color.rgb(0, 124, 134), 55)
aquaNavytable3 = color.new(color.rgb(0, 125, 136), 55) 

shade1 = color.new(color.rgb(200, 220, 255), 85)  // Light shade
shade2 = color.new(color.rgb(150, 180, 240), 85)  // Medium shade
shade3 = color.new(color.rgb(100, 140, 220), 85)   // Dark shade
shade4 = color.rgb(50, 100, 200)   // Dark shade
shade5 = color.rgb(20, 60, 160)    // Darkest shade

shade6 = color.new(color.rgb(82, 142, 204), 85) 
shade7 = color.new(color.rgb(61, 107, 153), 85) 
shade8 = color.new(color.rgb(41, 71, 102), 85) 
shade9 = color.new(color.rgb(20, 35, 51), 85)
shade10 = color.new(color.rgb(10, 17, 25), 85) 
shade11 = color.new(color.rgb(5, 9, 12), 85) 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                            Outliers                                                                                 ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Z for outlier detection 
sma = ta.sma(close, len) 
zsd = ta.stdev(close, len) 

z = (close - sma) / zsd

bool extreme_positive_outlier = z >= 3.0 
bool extreme_negative_outlier = z <= -3.0 

bgcolor(extreme_positive_outlier ? red : na, display = showoutlier ? display.all : display.none) 
bgcolor(extreme_negative_outlier ? red : na, display = showoutlier ? display.all : display.none) 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                            Assessments                                                                              ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Assessments 
bool uptrend = cor >= 0.5 and cor[5] >= 0.5 
bool downtrend = cor <= -0.5 and cor[5] <= -0.5 
falling = ta.falling(reg_line, 3)
rising = ta.rising(reg_line, 3) 


color sent = uptrend ? green : downtrend ? red : gray 
color reg_line_col = rising ? color.green : falling ? color.red : color.gray 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                            Outerband Plots                                                                           ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

plot(reg_line, color=reg_line_col, title="Regression Line", linewidth=4)
aa = plot(std_error_upper, color=sent)
bb = plot(std_error_lower, color=sent) 
cc = plot(sd1, color=aquaNavy1) 
dd = plot(sd2, color=aquaNavy2)
ee = plot(sd3, color=aquaNavy3) 
ff = plot(sd4, color=aquaNavy4) 
gg = plot(sd5 - divse, color=aquaNavy5) 
hh = plot(sd7, color=aquaNavy6) 
ii = plot(sd8, color=aquaNavy7) 
jj = plot(sd9, color=aquaNavy8) 
kk = plot(sd10, color=aquaNavy9)
ll = plot(sd6, color=aquaNavy10) 

fill(ll, hh, color = displayfills ? aquaNavy6 : na)
fill(hh, ii, color= displayfills ? aquaNavy7 : na)
fill(ii, jj, color= displayfills ? aquaNavy8 : na)
fill(jj, kk, color= displayfills ? aquaNavy9 : na) 
fill(hh, bb, color= displayfills ? aquaNavy10 : na) 

fill(cc, dd, color= displayfills ? aquaNavy1 : na)
fill(dd, ee, color= displayfills ? aquaNavy2 : na)
fill(ee, ff, color= displayfills ? aquaNavy3 : na) 
fill(ff, gg, color= displayfills ? aquaNavy4 : na)
fill(gg, aa, color= displayfills ? aquaNavy5 : na)

fill(aa, bb, color=sent) 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                           Trend Assessment                                                                          ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


bool strongup = cor >= 0.9 
bool modup = cor >= 0.6 and cor < 0.9 
bool weakup = cor >= 0.5 and cor < 0.6 
bool strongdown = cor <= -0.9 
bool moddown = cor <= -0.6 and cor > -0.9 
bool weakdown = cor <= -0.5 
bool notrend = cor < 0.5 and cor > -0.5 

fallingcor = ta.falling(cor, 2) 
risingcor = ta.rising(cor, 2) 

corchange = ta.change(cor, 3) 

bool weakeningup = cor >= 0.5 and fallingcor 
bool weakeningdown = cor <= -0.5 and risingcor 
bool strenghteningup = cor >= 0.5 and risingcor 
bool strengtheningdown = cor <= -0.5 and fallingcor 
bool notrendup = cor < 0.5 and cor > -0.5 and risingcor 
bool notrenddown = cor < 0.5 and cor > -0.5 and fallingcor

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                          Linear Regression Time Data                                                                ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


cl_reg_array = array.new_float() 
cl_sq_array = array.new_float() 
time_array = array.new_float() 
time_sq_array = array.new_float() 
cl_cl_array = array.new_float() 

hi_reg_array = array.new_float() 
hi_sq_array = array.new_float() 
hi_time_array = array.new_float() 

lo_reg_array = array.new_float() 
lo_sq_array = array.new_float() 
lo_time_array = array.new_float() 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                            Security Requests                                                                         ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

cl = request.security(syminfo.ticker, timeframe, close) 
cl_lag = request.security(syminfo.ticker, timeframe, close[1]) 
hi = request.security(syminfo.ticker, timeframe, high) 
hi_lag = request.security(syminfo.ticker, timeframe, high[1]) 
lo = request.security(syminfo.ticker, timeframe, low) 
op = request.security(syminfo.ticker, timeframe, open) 

for i = 0 to len 
    array.push(cl_reg_array, cl[i])
    array.push(cl_sq_array, cl[i] * cl[i]) 
    array.push(time_array, time[i])
    array.push(time_sq_array, time[i] * time[i])
    array.push(cl_cl_array, cl[i] * time[i])  
    array.push(hi_reg_array, hi[i]) 
    array.push(hi_sq_array, hi[i] * hi[i]) 
    array.push(hi_time_array, hi[i] * time[i]) 
    array.push(lo_reg_array, lo[i]) 
    array.push(lo_sq_array, lo[i] * lo[i]) 
    array.push(lo_time_array, lo[i] * time[i]) 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                           Define LinReg Variables                                                                   ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var float y = 0.0 // intercept 
var float y2 = 0.0 
x = array.sum(time_array) 
x2 = array.sum(time_sq_array) 
var float xy = 0.0 
var float b1 = 0.0 
var float bbb2 = 0.0 
var float abc = 0.0 
var float abc1 = 0.0 
var float r1 = 0.0 
var float r2 = 0.0 

// Linreg for Close 
y := array.sum(cl_reg_array)
y2 := array.sum(cl_sq_array) 

xy := array.sum(cl_cl_array) 

b1 := xy - (x * y) / len 
bbb2 := x2 - (math.pow(x, 2) / len) 

cl_slope = (b1 / bbb2)

abc := y - (cl_slope * x) 
abc1 := abc / len 

pcl = (time * cl_slope) + abc1 

pcl_sm = ta.sma(pcl, 14) 
pcl_sd = ta.stdev(pcl_sm, 14)

se_residuals = array.new_float() 

for i = 0 to len 
    array.push(se_residuals, (pcl[i] - cl[i]) * (pcl[i] - cl[i])) 

se_add = array.sum(se_residuals) 

r1 := se_add / (len - 2) 
r2 := math.sqrt(r1) 

pcl_ucl = pcl + r2 
pcl_lcl = pcl - r2 

// Close Correlation 

cl_time_co = array.covariance(cl_reg_array, time_array) 
cl_sd = array.stdev(cl_reg_array)
time_sd = array.stdev(time_array) 

cl_cor = cl_time_co / (cl_sd * time_sd) 

// LinReg for High 

y := array.sum(hi_reg_array)
y2 := array.sum(hi_sq_array) 

xy := array.sum(hi_time_array) 

b1 := xy - (x * y) / len 
bbb2 := x2 - (math.pow(x, 2) / len) 

hi_slope = (b1 / bbb2)

abc := y - (hi_slope * x) 
abc1 := abc / len 

phi = (time * hi_slope) + abc1 

se_residuals_hi = array.new_float() 

for i = 0 to len 
    array.push(se_residuals_hi, (phi[i] - hi[i]) * (phi[i] - hi[i])) 

se_add_hi = array.sum(se_residuals_hi) 

r1 := se_add_hi / (len - 2) 
r2 := math.sqrt(r1) 

phi_ucl = phi + r2 
phi_lcl = phi - r2 

hif1 = plot(phi, color = shade6) 
hif2 = plot(phi - r2, color = shade7) 
hif3 = plot(phi + r2, color = shade8)

fill(hif1, hif2, color= showinfill ? shade6 : na) 
fill(hif1, hif3, color= showinfill ? shade8 : na)

// High Correlation

hi_cov = array.covariance(hi_reg_array, time_array)
hi_sd = array.stdev(hi_reg_array) 

hi_cor = hi_cov / (hi_sd * time_sd) 

// Linreg for Low

y := array.sum(lo_reg_array)
y2 := array.sum(lo_sq_array) 

xy := array.sum(lo_time_array) 

b1 := xy - (x * y) / len 
bbb2 := x2 - (math.pow(x, 2) / len) 

lo_slope = (b1 / bbb2)

abc := y - (lo_slope * x) 
abc1 := abc / len 

plo = (time * lo_slope) + abc1 

se_residuals_lo = array.new_float() 

for i = 0 to len 
    array.push(se_residuals_lo, (phi[i] - hi[i]) * (phi[i] - hi[i])) 

se_add_lo = array.sum(se_residuals_lo) 

r1 := se_add_lo / (len - 2) 
r2 := math.sqrt(r1) 

fill1 = plot(plo, color = showinfill ? shade9 : na) 
fill2 = plot(plo - r2, color= showinfill ? shade10 : na) 
fill3 = plot(plo + r2, color= showinfill ? shade11 : na)

fill(fill1, fill2, color= showinfill ? shade9 : na) 
fill(fill1, fill3, color= showinfill ? shade11 : na) 

plo_ucl = plo + r2 
plo_lcl = plo - r2 

bo = plot(pcl, color=shade2) 
ba = plot(pcl + r2, color=shade1)  
be = plot(pcl-r2, color=shade3) 

fill(bo, ba, color= showinfill ? shade1 : na) 
fill(ba, be, color= showinfill ? shade3 : na) 

// Low Correlation 

lo_cov = array.covariance(lo_reg_array, time_array)
lo_sd = array.stdev(lo_reg_array) 

lo_cor = lo_cov / (lo_sd * time_sd) 

userinputprice = ((time + (userinput * 3600)) * slope) + intercept
userinputprice2 = userinputprice + er1 
userinputprice3 = userinputprice - er1

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                           Model Fit                                                                                 ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

maxoutside = math.max(plo_ucl, pcl_ucl, phi_ucl, pcl, phi, plo) 
minoutside = math.min(plo_lcl, pcl_lcl, phi_lcl, plo, pcl, phi) 

bool outside = close > maxoutside or close < minoutside 
bool inside = close <= maxoutside and close >= minoutside 
bool outsidereg = close > sd1 or close < sd6 
bool insidereg = close <= std_error_upper and close >= std_error_lower

var float outside_count = 0  
var float inside_count = 0 
var float outside_count_reg = 0 
var float inside_count_reg = 0 

for i = 0 to len 
    if outside[i] 
        outside_count := outside_count + 1 
    if inside[i] 
        inside_count := inside_count + 1
    if outsidereg[i] 
        outside_count_reg := outside_count_reg + 1 
    if insidereg[i] 
        inside_count_reg := inside_count_reg + 1 

cumulative_count = outside_count + inside_count 
outside_perc = (outside_count / cumulative_count) * 100 
inside_perc = (inside_count / cumulative_count) * 100 

cumulative_reg_count = outside_count_reg + inside_count_reg 
outside_reg_perc = (outside_count_reg / cumulative_reg_count) * 100 
inside_reg_perc = (inside_count_reg / cumulative_reg_count) * 100 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                            Manual Calculation Parameters                                                             ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if allowinput
    pcl := ((time + (userinput * 3600)) * cl_slope) + abc1
    pcl_ucl := pcl + r2 
    pcl_lcl := pcl - r2 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                    Correlation Change Assessment for Data Tables                                                    ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

cl_cor_change = ta.change(cl_cor, len) 
hi_cor_change = ta.change(hi_cor, len) 
lo_cor_change = ta.change(lo_cor, len) 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///                                                            Data Tables                                                                               ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


trendtablePosInput = input.string(title="Position", defval="Bottom Right", options=["Bottom Left", "Bottom Right", "Top Left", "Top Right", "Bottom Centre"], tooltip="Select where you want the table to draw.")
var trendtablePos  = trendtablePosInput == "Bottom Centre" ? position.bottom_center : trendtablePosInput == "Bottom Left" ? position.bottom_left : trendtablePosInput == "Bottom Right" ? position.bottom_right : trendtablePosInput == "Top Left" ? position.top_left : trendtablePosInput == "Top Right" ? position.top_right : na
var trendtabledata = table.new(trendtablePos, 4, 6, bgcolor = color.black, frame_color = color.white, frame_width = 2, border_color = color.black)

tablePosInput = input.string(title="Position", defval="Top Right", options=["Bottom Left", "Bottom Right", "Top Left", "Top Right"], tooltip="Select where you want the table to draw.")

var tablePos  = tablePosInput == "Bottom Left" ? position.bottom_left : tablePosInput == "Bottom Right" ? position.bottom_right : tablePosInput == "Top Left" ? position.top_left : tablePosInput == "Top Right" ? position.top_right : na

var regtable = table.new(tablePos, columns = 4, rows = 20, border_color = aquaNavytable2, border_width = 0)

if showtable
    table.cell(regtable, 1, 1, text = "Regression Slope", bgcolor = aquaNavy1, text_color = color.white) 
    table.cell(regtable, 1, 2, text = "Y = " + str.tostring(slope) + "x +" + str.tostring(intercept), bgcolor = aquaNavytable2, text_color = color.white)
    table.cell(regtable, 1, 3, text = "Standard Error:" + str.tostring(math.round(std_error,2)), bgcolor = aquaNavytable3, text_color = color.white)
    table.cell(regtable, 1, 4, text = "# of Closes Outside Range: " + str.tostring(outside_count_reg) + " or " + str.tostring(math.round(outside_reg_perc, 2)) + "% of closes.",  bgcolor = aquaNavytable2, text_color = color.white)
    table.cell(regtable, 1, 5, text = "# of Closes Inside Range: " + str.tostring(inside_count_reg) + " or " + str.tostring(math.round(inside_reg_perc, 2)) + "% of closes.", bgcolor = aquaNavytable2, text_color = color.white)
    if allowinput    
        table.cell(regtable, 1, 6, text = "Predicted Price at:" + str.tostring(userinput) + " hours in the future should fall between " + str.tostring(math.round(userinputprice2, 2)) + " and" + str.tostring(math.round(userinputprice3,2)), bgcolor = aquaNavytable3, text_color = color.white)
    table.cell(regtable, 1, 7, text = "Linear Regression Over Time", bgcolor = aquaNavy5, text_color = color.white) 
    table.cell(regtable, 1, 8, text = "Y = " + str.tostring(cl_slope) + "x + " + str.tostring(abc1), bgcolor = aquaNavy6, text_color = color.white) 
    table.cell(regtable, 1, 9, text = "Standard Error: " + str.tostring(math.round(r2, 2)), bgcolor = aquaNavy7, text_color = color.white) 
    table.cell(regtable, 1, 10, text = "# of Closes Outside Range: " + str.tostring(outside_count) + " or " + str.tostring(math.round(outside_perc, 2)) + "% of closes", bgcolor = aquaNavy8, text_color = color.white) 
    table.cell(regtable, 1, 11, text = "# of Closes Inside Range: " + str.tostring(inside_count) + " or " + str.tostring(math.round(inside_perc,2)) + "% of closes", bgcolor = aquaNavy8, text_color = color.white) 
    if allowinput
        table.cell(regtable, 1, 12, text = "Predicted Price at: " + str.tostring(userinput) + " hours in the future should fall between " + str.tostring(math.round(pcl_ucl, 2)) + " and" + str.tostring(math.round(pcl_lcl,2)) , bgcolor = aquaNavy9, text_color = color.white)


if trendtable 
    table.cell(trendtabledata, 1, 1, text = "Correlation: " + str.tostring(math.round(cor, 2)), bgcolor = color.black, text_color = color.white) 
    table.cell(trendtabledata, 2, 1, text = strongup ? "Strong Uptrend" : modup ? "Moderate Uptrend" : weakup ? "Weak Uptrend" : strongdown ? "Strong Downtrend" : moddown ? "Moderate Downtrend" : weakdown ? "Weak Downtrend" : notrend ? "No Trend" : na, bgcolor = color.black, text_color = color.white) 
    table.cell(trendtabledata, 1, 2, text = weakeningup ? "Uptrend is Weakening" : weakeningdown ? "Downtrend is Weakening" : strenghteningup ? "Uptrend is Strengthening" : strengtheningdown ? "Downtrend is Stregthening" : notrendup ? "No Trend, uptrend forming" : notrenddown ? "No Trend, downtrend forming" : na, bgcolor = color.black, text_color = color.white) 
    table.cell(trendtabledata, 2, 2, text = str.tostring(math.round(corchange, 2)), bgcolor = color.black, text_color = color.white) 
    if showcor 
        table.cell(trendtabledata, 1, 3, text = "Close Correlation to Time: " + str.tostring(math.round(cl_cor, 2)), bgcolor = color.black, text_color = color.white)
        table.cell(trendtabledata, 1, 4, text = "High Correlation to Time: " + str.tostring(math.round(hi_cor, 2)), bgcolor = color.black, text_color = color.white)
        table.cell(trendtabledata, 1, 5, text = "Low Correlation to Time: " + str.tostring(math.round(lo_cor, 2)), bgcolor = color.black, text_color = color.white)
        table.cell(trendtabledata, 2, 3, text = str.tostring(math.round(cl_cor_change, 2)), bgcolor = color.black, text_color = color.white)
        table.cell(trendtabledata, 2, 4, text = str.tostring(math.round(hi_cor_change, 2)), bgcolor = color.black, text_color = color.white)
        table.cell(trendtabledata, 2, 5, text = str.tostring(math.round(lo_cor_change, 2)), bgcolor = color.black, text_color = color.white)



    



