TTP Pair Slope/Hedge
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TheTradingParrot

//@version=5
indicator("TTP Pair Slope/Hedge")

xx = input.symbol("BINANCE:BTCUSDT.P", "X", group = "pair")
yy = input.symbol("BINANCE:BTCUSDT.P", "Y", group = "pair")

x = request.security(xx,timeframe.period,close)
y = request.security(yy,timeframe.period,close)

candlefrom = input.int(1000, "from", group = "candle limits")
candleto = input.int(0, "to", group = "candle limits")

length = candlefrom - candleto


// mean( (x-mean(x))^2 )
// ——————————————————
// mean( (x-mean(x)) * (y-mean(y)) )

meanx = ta.sma(x[candleto], length)
meany = ta.sma(y[candleto], length)

diffx = x[candleto] - meanx
diffy = y[candleto] - meany

top = ta.sma(math.pow(diffx, 2), length)
bottom = ta.sma(diffx * diffy, length)
result = top / bottom

plot(result, "slope", color = color.white)
