I would like to run a covariance test between any current time section of a stock/etf, example SPY 5-3-2023, and any past historical section of the same stock, example 9-25-17. I have run into limitations before using lagged time values in pinscript as it limits you to some fixed amount which will not allow going back more than say 2-3 years which I will want to do often; especially because I often use 1hour and 30m data bars. Here are the ideas I have sketched out so far, but I don't know how to get these working in a proper code. any help would be greatly appreciated.
//@version=5
indicator("covariance of ata with past price")
length = input.int(40, "length to analyze")
price = (close)
//initializiation
//ata = "active trading area" or current price
var ata_array = array.new_float(0)
//disregard this --float avg_ata_array = array.avg(roc_array)
// historic price
var past_array = array.new_float(0)
// disregard this -- float avg_past_array = array.avg(roc_array)
// enter dates
start1 = input.time(timestamp("05 May 2023 00:00:00"), "ata Start date")
start2 = input.time(timestamp("12 Dec 2019 00:00:00"), "historic Start date")
//add data to the ata array
if time >= start1
array.push(ata_array, price)
// avg_ata_array := array.avg(ata_array)
//add data to the historic array
if time >= start2
array.push(past_array, price)
// ave_past_array := array.avg(past_array)
// run covariance
covariance = array.covariance(ata_array, past_array, length)
hline(0.00, color=#ecef30, title="Zero Line")