Co-Relation by Onur
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © onurenginogutcu

//@version=4
study("Co-Relation by Onur" , overlay = false)


sym = input(title="Symbol", type=input.symbol, defval="BINANCE:BTCUSDT")
per = input(title="Period", type=input.integer, defval=200, minval=50, maxval=1500)

float topxy = 0
float topx = 0
float topy = 0
float topx2 = 0
float topy2 = 0

symClose = security(sym, "", close)
cur = close

for periyot = 0 to (per - 1)
    topxy := topxy + (symClose[periyot] * cur[periyot])
    topx := topx + (cur [periyot])
    topy := topy + (symClose[periyot])
    topx2 := topx2 + (cur [periyot] * cur [periyot])
    topy2 := topy2 + (symClose[periyot] * symClose[periyot])
    
calc = (topxy - ((topx * topy) / per)) / (sqrt ((topx2 - ((topx * topx)/per)) * (topy2 - ((topy * topy)/per)))) * 100

///////////////////////////////////////////


colText = calc<0 ? color.red : color.green

dt = time - time[1]
if barstate.islast
    label.new(time - 15*dt, calc, text = (tostring (sym) + " Corelation - %" + tostring(calc)), textcolor=color.new(colText,0),  style=label.style_none, xloc=xloc.bar_time)



////////////////////////////////////////////

hline(0,  color=color.gray, linestyle=hline.style_dashed)
hline(100,  color=color.green, linestyle=hline.style_dotted)
hline(-100,  color=color.red, linestyle=hline.style_dotted)
plot (calc  , color=color.yellow)


