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

//@version=5
indicator("Spread Chart")

masterQuantity = input.int(1)

tickerName = input.symbol("M2K1!")
tickerQuantity = input.float(-1, step=1)
tickerPointvalue = input.float(5, minval=0)

secondTickerName = input.symbol("ZB1!")
secondTickerQuantity = input.float(0, step=1)
secondTickerPointvalue = input.float(1000, minval=0)

invert = input.bool(false)

debug = input.bool(false)

rowColor(qty) => qty>0? color.green : color.red

var table tableInfo = na
if na(tableInfo)
    rowCount = 2 + (tickerQuantity!=0 ? 1 : 0) + (secondTickerQuantity!=0 ? 1 : 0)
    tableInfo := table.new(position.top_right, 3, rowCount, frame_color=color.gray, frame_width=1, border_color=color.gray, border_width=1)
    tableTextColor = color.white
    table.cell(tableInfo, 0, 0, "Qty", text_color=tableTextColor)
    table.cell(tableInfo, 1, 0, "Ticker", text_color=tableTextColor)
    table.cell(tableInfo, 2, 0, syminfo.currency+"/Handle", text_color=tableTextColor)
    table.cell(tableInfo, 0, 1, str.tostring(math.sign(invert?-1:1)*masterQuantity), text_color=rowColor(masterQuantity))
    table.cell(tableInfo, 1, 1, syminfo.tickerid, text_color=rowColor(masterQuantity))
    table.cell(tableInfo, 2, 1, str.tostring(syminfo.pointvalue), text_color=rowColor(masterQuantity))
    currentRow = 2
    if tickerQuantity != 0
        table.cell(tableInfo, 0, currentRow, str.tostring(math.sign(invert?-1:1)*tickerQuantity), text_color=rowColor(tickerQuantity))
        table.cell(tableInfo, 1, currentRow, tickerName, text_color=rowColor(tickerQuantity))
        table.cell(tableInfo, 2, currentRow, str.tostring(tickerPointvalue), text_color=rowColor(tickerQuantity))
        currentRow += 1
    if secondTickerQuantity != 0
        table.cell(tableInfo, 0, currentRow, str.tostring(math.sign(invert?-1:1)*secondTickerQuantity), text_color=rowColor(secondTickerQuantity))
        table.cell(tableInfo, 1, currentRow, secondTickerName, text_color=rowColor(secondTickerQuantity))
        table.cell(tableInfo, 2, currentRow, str.tostring(secondTickerPointvalue), text_color=rowColor(secondTickerQuantity))


tickerHigh = request.security(tickerName, timeframe.period, high)
tickerLow = request.security(tickerName, timeframe.period, low)
tickerOpen = request.security(tickerName, timeframe.period, open)
tickerClose = request.security(tickerName, timeframe.period, close)
// tickerPointvalue = request.security(secondTickerName, timeframe.period, syminfo.pointvalue)

barOpen = masterQuantity*open*syminfo.pointvalue + tickerQuantity*tickerOpen*tickerPointvalue
barClose = masterQuantity*close*syminfo.pointvalue + tickerQuantity*tickerClose*tickerPointvalue
barHigh = masterQuantity*high*syminfo.pointvalue + tickerQuantity*tickerHigh*tickerPointvalue
barLow = masterQuantity*low*syminfo.pointvalue + tickerQuantity*tickerLow*tickerPointvalue

secondTickerHigh = request.security(secondTickerName, timeframe.period, high)
secondTickerLow = request.security(secondTickerName, timeframe.period, low)
secondTickerOpen = request.security(secondTickerName, timeframe.period, open)
secondTickerClose = request.security(secondTickerName, timeframe.period, close)
// secondTickerPointvalue = request.security(secondTickerName, timeframe.period, syminfo.pointvalue)

barOpen += secondTickerQuantity*secondTickerOpen*secondTickerPointvalue
barClose += secondTickerQuantity*secondTickerClose*secondTickerPointvalue
barHigh += secondTickerQuantity*secondTickerHigh*secondTickerPointvalue
barLow += secondTickerQuantity*secondTickerLow*secondTickerPointvalue

if invert
    barOpen *= -1
    barClose *= -1
    barHigh *= -1
    barLow *= -1
    
plotbar(barOpen, barHigh, barLow, barClose, color=barOpen>barClose?color.red:(barOpen<barClose?color.green:color.white))
plot(debug?tickerClose:na, color=color.red)
plot(debug?tickerQuantity*tickerClose:na, color=color.green)
plot(debug?tickerQuantity*tickerClose*tickerPointvalue:na, color=color.blue)
