Options Theoritcal Price
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Mohit_Kakkar08


//@version=5

indicator('Options Theoritcal Price', precision=2, overlay=true)


tableposition=input.string("top_right",title="Table position", options=["top_left", "top_center", "top_right", "middle_left", "middle_center", "middle_right", "bottom_left", "bottom_center", "bottom_right"])
tabpos= tableposition== "top_left"? position.top_left:tableposition== "top_center"?position.top_center : tableposition== "top_right"? position.top_right : tableposition== "middle_left"? position.middle_left : tableposition== "middle_center"? position.middle_center : tableposition== "middle_right"? position.middle_right : tableposition== "bottom_left"? position.bottom_left : tableposition== "bottom_center"? position.bottom_center: tableposition== "bottom_right"? position.bottom_right: position.top_right

s = input.int(1000, 'Spot Price')
k = input(1000.0, 'Strike Price-1')
k9 = input(1000.0, 'Strike Price-2')
r0 = input(6.0, 'Risk Free Rate %')
t0 = input(60.0, 'Days to maturity')
a0 = input(22.0, 'Strike-1 Implied Volatility %')
a_9 = input(22.0, 'Strike-2 Implied Volatility %')



r = r0 / 100
t = t0 / 365
a = a0 / 100

a__9 = a_9 / 100

ln = math.log(s / k)
d1 = (ln + (r + math.pow(a, 2) / 2) * t) / (a * math.sqrt(t))
d2 = d1 - a * math.sqrt(t)

e = 2.71828182846
a1 = 1.26551223
a2 = 1.00002368
a3 = 0.37409196
a4 = 0.09678418
a5 = 0.18628806
a6 = 0.27886807
a7 = 1.13520398
a8 = 1.48851587
a9 = 0.82215223
a10 = 0.17087277

x1 = d1 / math.sqrt(2)
t1 = 1 / (1 + 0.5 * math.abs(x1))
n1 = -math.pow(x1, 2) - a1 + a2 * t1 + a3 * math.pow(t1, 2) + a4 * math.pow(t1, 3) - a5 * math.pow(t1, 4) + a6 * math.pow(t1, 5) - a7 * math.pow(t1, 6) + a8 * math.pow(t1, 7) - a9 * math.pow(t1, 8) + a10 * math.pow(t1, 9)
T1 = t1 * math.pow(e, n1)
erf1 = x1 < 0 ? T1 - 1 : 1 - T1
Nd1 = 0.5 + 0.5 * erf1

x2 = d2 / math.sqrt(2)
t2 = 1 / (1 + 0.5 * math.abs(x2))
n2 = -math.pow(x2, 2) - a1 + a2 * t2 + a3 * math.pow(t2, 2) + a4 * math.pow(t2, 3) - a5 * math.pow(t2, 4) + a6 * math.pow(t2, 5) - a7 * math.pow(t2, 6) + a8 * math.pow(t2, 7) - a9 * math.pow(t2, 8) + a10 * math.pow(t2, 9)
T2 = t2 * math.pow(e, n2)
erf2 = x2 < 0 ? T2 - 1 : 1 - T2
Nd2 = 0.5 + 0.5 * erf2


C = Nd1 * s - Nd2 * k * math.pow(e, -r * t)
P = k * math.pow(e, -r * t) * (1 - Nd2) - s * (1 - Nd1)






ln_9 = math.log(s / k9)
d1_9 = (ln_9 + (r + math.pow(a__9, 2) / 2) * t) / (a__9 * math.sqrt(t))
d2_9 = d1_9 - a * math.sqrt(t)



x1_9 = d1_9 / math.sqrt(2)
t1_9 = 1 / (1 + 0.5 * math.abs(x1_9))
n1_9 = -math.pow(x1_9, 2) - a1 + a2 * t1_9 + a3 * math.pow(t1_9, 2) + a4 * math.pow(t1_9, 3) - a5 * math.pow(t1_9, 4) + a6 * math.pow(t1_9, 5) - a7 * math.pow(t1_9, 6) + a8 * math.pow(t1_9, 7) - a9 * math.pow(t1_9, 8) + a10 * math.pow(t1_9, 9)
T1_9 = t1_9 * math.pow(e, n1_9)
erf1_9 = x1_9 < 0 ? T1_9 - 1 : 1 - T1_9
Nd1_9 = 0.5 + 0.5 * erf1_9

x2_9 = d2_9 / math.sqrt(2)
t2_9 = 1 / (1 + 0.5 * math.abs(x2_9))
n2_9 = -math.pow(x2_9, 2) - a1 + a2 * t2_9 + a3 * math.pow(t2_9, 2) + a4 * math.pow(t2_9, 3) - a5 * math.pow(t2_9, 4) + a6 * math.pow(t2_9, 5) - a7 * math.pow(t2_9, 6) + a8 * math.pow(t2_9, 7) - a9 * math.pow(t2_9, 8) + a10 * math.pow(t2_9, 9)
T2_9 = t2_9 * math.pow(e, n2_9)
erf2_9 = x2_9 < 0 ? T2_9 - 1 : 1 - T2_9
Nd2_9 = 0.5 + 0.5 * erf2_9


C_9 = Nd1_9 * s - Nd2_9 * k9 * math.pow(e, -r * t)
P_9 = k * math.pow(e, -r * t) * (1 - Nd2_9) - s * (1 - Nd1_9)



x0= timeframe.period

var table indicatorTable = table.new(tabpos, 20, 20, frame_color=color.black, frame_width=1,border_color=color.black, border_width=2)
if barstate.islast
    // symbols
    table.cell(indicatorTable, 0, 0, syminfo.ticker, bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 0, 1, text="CALL-1", text_halign=text.align_center, bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 0, 2, text="PUT-1", text_halign=text.align_center, bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 0, 3, text="CALL-2", text_halign=text.align_center, bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 0, 4, text="PUT-2", text_halign=text.align_center, bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 0, 7, text="Straddle-1", text_halign=text.align_center, bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 0, 8, text="Straddle-2", text_halign=text.align_center, bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 0, 5, text="Call Spread", text_halign=text.align_center, bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 0, 6, text="PUT Spread", text_halign=text.align_center, bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 0, 9, text="PE-1 & CE-2 Strangle", text_halign=text.align_center, bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 0, 10, text="CE-1 & PE-2 Strangle", text_halign=text.align_center, bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 1, 0, text='Strike Price', bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 1, 1, str.tostring(k), text_halign=text.align_center, bgcolor=color.red, text_color=color.white)
    table.cell(indicatorTable, 1, 2, str.tostring(k), text_halign=text.align_center, bgcolor=color.green, text_color=color.white)
    table.cell(indicatorTable, 1, 3, str.tostring(k9), text_halign=text.align_center, bgcolor=color.red, text_color=color.white)
    table.cell(indicatorTable, 1, 4, str.tostring(k9), text_halign=text.align_center, bgcolor=color.green, text_color=color.white)
    table.cell(indicatorTable, 2, 0, text='Premium', bgcolor=color.black, text_color=color.white)
    table.cell(indicatorTable, 2, 1, str.tostring(math.round(C,2)), text_halign=text.align_center, bgcolor=color.red, text_color=color.white)
    table.cell(indicatorTable, 2, 2, str.tostring(math.round(P,2)), text_halign=text.align_center, bgcolor=color.green, text_color=color.white)
    table.cell(indicatorTable, 2, 3, str.tostring(math.round(C_9,2)), text_halign=text.align_center, bgcolor=color.red, text_color=color.white)
    table.cell(indicatorTable, 2, 4, str.tostring(math.round(P_9,2)), text_halign=text.align_center, bgcolor=color.green, text_color=color.white)
    table.cell(indicatorTable, 1, 7, str.tostring(math.round(C,2) + math.round(P,2)), text_halign=text.align_center, bgcolor=color.blue, text_color=color.white)
    table.cell(indicatorTable, 1, 8, str.tostring(math.round(C_9,2) + math.round(P_9,2)), text_halign=text.align_center, bgcolor=color.blue, text_color=color.white)
    table.merge_cells(indicatorTable, 1, 7, 2, 7)
    table.merge_cells(indicatorTable, 1, 8, 2, 8)
    table.cell(indicatorTable, 1, 5, str.tostring(math.round(C,2) - math.round(C_9,2)), text_halign=text.align_center, bgcolor=color.orange, text_color=color.white)
    table.cell(indicatorTable, 1, 6, str.tostring(math.round(P,2) - math.round(P_9,2)), text_halign=text.align_center, bgcolor=color.orange, text_color=color.white)
    table.merge_cells(indicatorTable, 1, 5, 2, 5)
    table.merge_cells(indicatorTable, 1, 6, 2, 6)
    table.cell(indicatorTable, 1, 9, str.tostring(math.round(P,2) + math.round(C_9,2)), text_halign=text.align_center, bgcolor=color.fuchsia, text_color=color.white)
    table.cell(indicatorTable, 1, 10, str.tostring(math.round(C,2) + math.round(P_9,2)), text_halign=text.align_center, bgcolor=color.fuchsia, text_color=color.white)
    table.merge_cells(indicatorTable, 1, 9, 2, 9)
    table.merge_cells(indicatorTable, 1, 10, 2, 10)