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

//@version=5

// @description TradingView Library for showing option prices on the chart
library("RS_TD_Library_2", true)


// @function Shows the table of profit/loss of all options to the given underlying
// @param sumShortPut Sum of all short put options
// @param sumLongPut Sum of all long put options
// @param sumShortCall Sum of all short call options
// @param sumLongCall Sum of all long call options
// @param sumTotal Sum of all kind of options
// @param smallLayout Usage of a small table layout
export showTable(float sumShortPut, float sumLongPut, float sumShortCall, float sumLongCall, float sumTotal, bool smallLayout) =>
    var _text_size = smallLayout ? size.small : size.normal
    var table panel = table.new("top_right", 3, 6, border_width = 1)
    table.cell(panel, 0, 0, syminfo.ticker, bgcolor = color.new(color.black, 30), text_color = color.white, text_size = _text_size)
    table.cell(panel, 2, 0, "Result", bgcolor = color.new(color.black, 30), text_color = color.white, text_size = _text_size)

    table.cell(panel, 0, 1, "ShortPut", bgcolor = color.new(color.gray, 30), text_halign = text.align_left, text_size = _text_size)
    bgColor = sumShortPut >= 0.0 ? color.green : color.red
    table.cell(panel, 2, 1, str.tostring(sumShortPut, format.mintick), text_color = color.black, bgcolor = bgColor, text_halign = text.align_right, text_size = _text_size)

    table.cell(panel, 0, 2, "LongPut", bgcolor = color.new(color.gray, 30), text_halign = text.align_left, text_size = _text_size)
    bgColor := sumLongPut >= 0.0 ? color.green : color.red
    table.cell(panel, 2, 2, str.tostring(sumLongPut, format.mintick), text_color = color.black, bgcolor = bgColor, text_halign = text.align_right, text_size = _text_size)

    table.cell(panel, 0, 3, "ShortCall", bgcolor = color.new(color.gray, 30), text_halign = text.align_left, text_size = _text_size)
    bgColor := sumShortCall >= 0.0 ? color.green : color.red
    table.cell(panel, 2, 3, str.tostring(sumShortCall, format.mintick), text_color = color.black, bgcolor = bgColor, text_halign = text.align_right, text_size = _text_size)

    table.cell(panel, 0, 4, "LongCall", bgcolor = color.new(color.gray, 30), text_halign = text.align_left, text_size = _text_size)
    bgColor := sumLongCall >= 0.0 ? color.green : color.red
    table.cell(panel, 2, 4, str.tostring(sumLongCall, format.mintick), text_color = color.black, bgcolor = bgColor, text_halign = text.align_right, text_size = _text_size)

    table.cell(panel, 0, 5, "Total", bgcolor = color.new(color.black, 30), text_color = color.white, text_halign = text.align_left, text_size = _text_size)
    bgColor := sumTotal >= 0.0 ? color.green : color.red
    table.cell(panel, 2, 5, str.tostring(sumTotal, format.mintick), text_color = color.black, bgcolor = bgColor, text_halign = text.align_right, text_size = _text_size)


// @function Shows a debugging label on each bar
// @param _text Text to show on the label
export debugging_labelOnEachBar(string _text) =>
    label.new(bar_index, high, str.tostring(_text))


// @function Prints a line at the strike of the option
// @param date1 Buying date of the option
// @param date2 Selling date of the option
// @param strike Strike of the option
// @param color Color of the line
export showLine(int date1, int date2, float strike, color color) =>
    //debugging_labelOnEachBar(str.tostring(date1))
    // Note: line has no argument "toolTip"
    line.new(date1, y1=strike, x2=date2, y2=strike, extend=extend.none, color = color, width = 4, xloc = xloc.bar_time)


// @function Prints a label at the strike of the option
// @param date Buying/Selling date of the option
// @param strike Strike of the option
// @param color Color of the line
// @param tooltip ToolTip of the option with the detailed prices
export showLabel(int date, float strike, color color, string toolTip) =>
    //debugging_labelOnEachBar(str.tostring(date))
    label.new(date, strike, xloc = xloc.bar_time, color = color, style=label.style_label_right, tooltip = toolTip)



// example
color COLOR_LONG_PUT   = #B2DFDB
color COLOR_LONG_CALL  = #B2DFDB
color COLOR_SHORT_CALL = #FFCDD2
color COLOR_SHORT_PUT  = #FFCDD2

// example
showOptions_AAPL() =>
    // option 1
    showLine(timestamp("25 Feb 2022"), timestamp("04 Mar 2022"), 160.0, COLOR_SHORT_PUT)
    showLabel(timestamp("25 Feb 2022"), 160.0, COLOR_SHORT_PUT, "Short Put\nAAPL 04MAR22 160 P\nQuantity: -1\nOpen: $1.1 (25 Feb 2022)\nClose: $0.0 (04 Mar 2022)")
    // option 2
    showLine(timestamp("07 Oct 2022"), timestamp("14 Oct 2022"), 130.0, COLOR_SHORT_PUT)
    showLabel(timestamp("07 Oct 2022"), 130.0, COLOR_SHORT_PUT, "Short Put\nAAPL 14OCT22 130 P\nQuantity: -1\nOpen: $0.47 (07 Oct 2022)\nClose: $0.0 (14 Oct 2022)")
    // option 3
    showLine(timestamp("22 Sep 2022"), timestamp("03 Oct 2022"), 160.0, COLOR_SHORT_CALL)
    showLabel(timestamp("22 Sep 2022"), 160.0, COLOR_SHORT_CALL, "Short Call\nAAPL 21OCT22 160 C\nQuantity: -1\nOpen: $3.2 (22 Sep 2022)\nClose: $0.25 (03 Oct 2022)")
    // option 4
    showLine(timestamp("22 Sep 2022"), timestamp("04 Oct 2022"), 162.5, COLOR_LONG_CALL)
    showLabel(timestamp("22 Sep 2022"), 162.5, COLOR_LONG_CALL, "Long Call\nAAPL 21OCT22 162.5 C\nQuantity: 1\nOpen: $2.37 (22 Sep 2022)\nClose: $0.22 (04 Oct 2022)")
    // option 5
    showLine(timestamp("12 Sep 2022"), timestamp("21 Oct 2022"), 130.0, COLOR_LONG_PUT)
    showLabel(timestamp("12 Sep 2022"), 130.0, COLOR_LONG_PUT, "Long Put\nAAPL 21OCT22 130 P\nQuantity: 1\nOpen: $0.4 (12 Sep 2022)\nClose: $0.0 (21 Oct 2022)")
    // option 6
    showLine(timestamp("12 Sep 2022"), timestamp("19 Oct 2022"), 140.0, COLOR_SHORT_PUT)
    showLabel(timestamp("12 Sep 2022"), 140.0, COLOR_SHORT_PUT, "Short Put\nAAPL 21OCT22 140 P\nQuantity: -1\nOpen: $0.86 (12 Sep 2022)\nClose: $0.5 (19 Oct 2022)")

// example
if barstate.islast and syminfo.ticker == "AAPL"
    showOptions_AAPL()
    showTable(243.0, -40.0, 320.0, -237.0, 286.0, false)

