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

//@version=5
indicator("Spread of Scripts", precision=6, overlay=false)

draw_label(_x, _y, _text, _xloc, _yloc, color, _style, _textcolor, _size, _textalign, _tooltip) =>
    dlabel = label.new(x=_x, y=_y, text=_text, xloc=_xloc, yloc=_yloc, color=color, style=_style, textcolor=_textcolor, size=_size, 
         textalign=_textalign, tooltip=_tooltip)
    label.delete(dlabel[1])

tifrm = input.timeframe("", title="Timeframe")

method = input.string("Difference", title="Method of Spread", options=["Divide", "Difference"])

s1 = input.symbol("NSE:NIFTY1!", title="Symbol 1", confirm=true) 
s2 = input.symbol("NSE:NIFTY", title="Symbol 2", confirm=true)

s1sec = request.security(s1, tifrm, close)
s2sec = request.security(s2, tifrm, close) 

spread = method == "Divide" ? s1sec / s2sec : s1sec - s2sec

col = method == "Divide" ? (spread>1 ? color.green : color.red) : spread>0 ? color.green : color.red

plot(spread, title="Spread", color=col, style=plot.style_line, linewidth=2) 

plot(method == "Divide" ? 1 : 0, title="Zero", color=color.black) 

draw_label(bar_index[0], spread, '' + str.tostring(spread, '0.000000'), xloc.bar_index, yloc.price, col, label.style_label_left, 
     color.white, size.normal, text.align_left, '')

