Price Spread Indicator v2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jroche1973

//@version=5
indicator("Price Spread Indicator v3", overlay=false)
i_sym = input.symbol("SPY", "Symbol")
sym_close = request.security(i_sym, 'D', close)

i_lookback1 = input.int(60, "MA/Days lookback value 1:")
i_lookback2 = input.int(90, "MA/Days lookback value 2:")
i_lookback3 = input.int(200, "MA/Days lookback value 3:")
i_lookback4 = input.int(18, "Short MA/Alt. Days look back")

chk_ratio1= input.float(-0.25, "Spread ratio LB 1") 
chk_ratio2= input.float(-1.1, "Spread ratio LB 2") 
chk_ratio3 = input.float(-1.4, "Spread ratio LB 3") // -1.3 is been derived from backtests to maximise P&L
chk_ratio4 = input.float(-8.6, "Spread ratio LB 4")

//close lookbacks 
cl1 = sym_close[i_lookback1]
cl2 = sym_close[i_lookback2]
cl3 = sym_close[i_lookback3]
cl4= sym_close[i_lookback4]

//Calculate spread 
spread1 = ((sym_close-cl1)/sym_close)*100 //60 day default
spread2 = ((sym_close-cl2)/sym_close)*100 //90 day default
spread3 = ((sym_close-cl3)/sym_close)*100 //200 day default
spread4 = ((sym_close-cl4)/sym_close)*100 //alternative catch all 18 day default

//Test Conditions
cond_1 = spread1 < chk_ratio1?true:false //is 60 day spread higher than our check spread
//and
cond_2 = spread2 < chk_ratio2?true:false
//and
cond_3 = spread3 < chk_ratio3?true:false
//or
cond_4 = spread4 < chk_ratio4?true:false

multi_cond = cond_1 and cond_2 and cond_3?true:false

cls_plt = plot(sym_close, color=color.blue, linewidth = 2)
bgcolor(multi_cond or cond_4? #FF9999:na) 
