Multi Asset + Correlation Overlay
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version = 5
// Author = TradeAutomation


indicator("Multi Asset + Correlation Overlay", shorttitle="Multi Asset + Correlation Overlay", overlay=true)


//Selects Ticker and Provides Inputs
Asset = input.string(title="Overlay Ticker", defval="SPY", tooltip="Input the ticker of the symbol you want to overlay on your chart here.")
Source = input.source(close, "Source", tooltip="Select the type of data you want to pull in for the asset. I.e. close=closing price.")
Offset = input.int(0, "Ticker Offset", tooltip="This can be a positive or negative number that you can use to move the overlayed asset up/down to better align with your current chart's layout")
Length = input.int(200, "# of Bars for Correlation Calculation", tooltip="The overlayed asset's correlation coeffecient prints on the chart next to the ticker of the added asset")

//Pulls in Ticker Data
Resolution = input.timeframe(title="Resolution", defval="60")
Ticker = request.security(Asset, Resolution, Source)

//Calculates Correlation
Correlation = str.tostring(ta.correlation(Source, Ticker, Length), '#.##')

//Plots and Labels
color1 = input.color(color.purple, "Color of Plot")
plot(Ticker+Offset, color = color1)
var label label1 = na
if time > time[1]
    label.delete(label1)
    label1 := label.new(x=bar_index+1, y=Ticker+Offset, text=Asset+", CC:"+Correlation, xloc=xloc.bar_index, color=color.new(color1,100), style= label.style_label_left, textcolor=color1, size=size.small)
 