Option Calculator [elio27]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © elio27


//@version=5
indicator("Option Calculator [elio27]", shorttitle="Option Calc [elio27]", overlay=true)


// --- Inputs ---
type = input.string(defval="Call", options=["Call", "Put"], title="Option Type", confirm=true)
K = input.float(defval=0, title="Strike Price", confirm=true)
premium = input.float(defval=0, title="Premium", confirm=true)
qty = input.int(defval=1, title="Quantity", confirm=true)

labels = input.bool(defval=true, title="Show Labels")
offset = input.int(defval=15, title="Offset")
profitColor = input.color(defval=color.green, title="Positive Color")
lossColor = input.color(defval=color.red, title="Negative Color")



// --- Calculations ---
isExercised = false
isProfitable = false
breakEven = K
ret = 0.0

if type == "Call"
    isExercised := close >= K // A call option is exercised when the price of the asset is superior to the strike price
    breakEven += premium // The Break Even point of a call  is where the benefit of buying under the current price equals the premium paid
    isProfitable := close > breakEven // A call option is profitable, or "In The Money" when the price is above the Break Even point
    ret += math.max(close - K, 0) * qty // The return of a call is the Maximum of S-K and 0
    
else
    isExercised := close <= K // A put option is exercised when the price of the asset is inferior to the strike price
    breakEven -= premium // The Break Even point of a put  is where the benefit of seling above the current price equals the premium paid
    isProfitable := close < breakEven // A put option is profitable, or "In The Money" when the price is under the Break Even point
    ret += math.max(K - close, 0) * qty // The return of a put is the Maximum of K-S and 0

netReturn = ret - qty * premium // The net return of an option is its return minus the premium



// --- Outputs ---
line.new(bar_index, K, bar_index+offset, K, color = isExercised ? profitColor : lossColor, width=2)
plot(K, title="Strike Price", color = isExercised ? profitColor : lossColor, linewidth=2)
plot(breakEven, title="Break Even point", color=color.white, offset=offset, linewidth=2)
if barstate.islast and labels
    label.new(bar_index + offset, breakEven, "Break Even point", color=color.white)
    label.new(bar_index + offset, K, "Exercise price", color = isExercised ? profitColor : lossColor)

tbl = table.new(position.bottom_right, 2, 4, bgcolor = color.gray, frame_width = 2, frame_color = color.black)
table.cell(tbl, 0, 0, "State")
table.cell(tbl, 1, 0, isExercised ? "In The Money" : close==breakEven ? "At The Money" : "Out The Money", bgcolor = isExercised ? profitColor : lossColor)
table.cell(tbl, 0, 1, "Profitable")
table.cell(tbl, 1, 1, isProfitable ? "Yes" : "No", bgcolor = isProfitable ? profitColor : lossColor)
table.cell(tbl, 0, 2, "Return")
table.cell(tbl, 1, 2, str.tostring(ret), bgcolor = isExercised ? profitColor : lossColor)
table.cell(tbl, 0, 3, "Net Return")
table.cell(tbl, 1, 3, str.tostring(netReturn), bgcolor = isProfitable ? profitColor : lossColor)
