Arbitrage Spread
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © I_Leo_I

//@version=5
indicator(title="Arbitrage Spread")
symbol1 = input.symbol(defval="BINANCE:BTCUSDT.P" , title="Symbol 1", group = "chart")
symbol2 = input.symbol(defval ="BINANCE:ETHUSDT.P", title="Symbol 2")
is_period = input.bool(defval=true, title = "use period?")
period = input.timeframe(defval="1D", title="Period")

para1 = request.security(symbol1, timeframe.period, close)
para2 = request.security(symbol2, timeframe.period, close)

timeFlag = timeframe.change(period)
var session = timeframe.in_seconds(period)/timeframe.in_seconds(timeframe.period)
var index_bar = 1
if(is_period)
    if(timeFlag)
        index_bar := 1
    else
        index_bar := index_bar+1
else
    index_bar := bar_index

para1_procent = (para1-para1[index_bar])*100/para1[index_bar]
para2_procent = (para2-para2[index_bar])*100/para2[index_bar]
plot(para1_procent, title="para1", color=color.new(#FF8C00, 0), linewidth=2)
plot(para2_procent, title="para2", color=color.new(#00CED1, 0), linewidth=2)

spread = math.abs(para1_procent - para2_procent)
corr = ta.correlation(para1_procent, para2_procent, index_bar)

// new chart strart //

multiplier = input.int(defval = 1, title="multiplier", minval = 1, group = "new chart")
new_symbol = symbol1+"/"+symbol2+"*"+str.tostring(multiplier)
[o, c, h, l] = request.security(new_symbol, timeframe.period, [open, close, high, low])
new_symbol_procent = (c - c[index_bar])*100/c[index_bar]
plot(new_symbol_procent, title="new chart procent", color=color.red, linewidth=2)
// new chart end  //

// z score start //

Length = input.int(defval=20, title="Length", group="z score")

basis = ta.sma(c, Length)
zscore = (c - basis) / ta.stdev(c, Length)

// z score end  // 

// atr z score start //
atrlen = input.int(10, minval=1, title='ATR Length', group = "atr z score")
lookback = input.int(100, minval=1, title='Longterm Average Lookback')

pine_atr(length, h ,l, c) =>
    trueRange = na(h[1])? h-l : math.max(math.max(h - l, math.abs(h - c[1])), math.abs(l - c[1]))
    ta.rma(trueRange, length)

atrAvg = pine_atr(atrlen, h ,l, c)

atrStdDev = (atrAvg - pine_atr(lookback, h ,l, c)) / ta.stdev(atrAvg, lookback)

// atr z score end  //

plotchar(corr, "corr", "", location = location.top)
plotchar(zscore, "z score", "", location = location.top)
plotchar(spread, "spread", "", location = location.top)
plotchar(atrStdDev, "atr z score", "", location = location.top)
//plotchar(ta.cross(corr_alert, corr), "test", "", location = location.top)

// alert start //
corr_alert = input.float(defval=0, title="corr", group="Alert")
z_score_alert = input.float(defval=0, title="z score")
spread_alert = input.float(defval=0, title="spread")
atr_z_score_alert = input.float(defval=0, title="atr z score")

alertcondition(corr_alert != 0 ? ta.cross(corr_alert, corr) : true and z_score_alert != 0 ? ta.cross(z_score_alert, zscore) or ta.cross((z_score_alert*-1), zscore) : true  and spread_alert != 0 ? ta.cross(spread_alert, spread) : true  and atr_z_score_alert != 0 ? ta.cross(atr_z_score_alert, atrStdDev) or ta.cross((atr_z_score_alert*-1), atrStdDev) : true, title="Arbitrage Spread")

// alert end  //