Option Pricing

Models uses pydantic to create a data model for option inputs. Refer the following implementation of the Black Scholes amd Monte Carlo option pricing methods.

import numpy as np
from tabulate import tabulate
from quantmod.models import (
    OptionInputs,
    OptionType,
    ExerciseStyle,
    BarrierType,
    BinomialOptionPricing,
    BlackScholesOptionPricing,
    MonteCarloOptionPricing,
)


# Get the option inputs parameters
inputs=OptionInputs(
    spot=100,
    strike=100,
    ttm=1,
    rate=0.05,
    volatility=0.2,
    callprice=8.0
    )


# 1. Black Scholes Option Pricing
# Initialize the BS Option Pricing Engine
option = BlackScholesOptionPricing(inputs=inputs)

# Print the BS option price
header = ['Option Price', 'Delta', 'Gamma', 'Theta', 'Vega', 'Rho', 'IV']
table = [[option.call_price, option.call_delta, option.gamma, option.call_theta, option.vega, option.call_rho, option.impvol],]
print(tabulate(np.around(table, 4), headers=header))


# 2. Binomial Option Pricing
# Define input parameters
inputs=OptionInputs(
    spot=100,
    strike=100,
    ttm=1,
    rate=0.05,
    volatility=0.2
    )

# Instantiate the BinomialOptionPricing Engine
binomial = BinomialOptionPricing(
    inputs=inputs,
    nsteps=4,
    option_type=OptionType.PUT,
    exercise_style=ExerciseStyle.AMERICAN,
    output="delta",
)

# Print the tree values
print(f"\nOption {binomial.output} tree:")
print(binomial.binomialoption)

# Plot the tree
print(f"\n\nPlot the tree:")
binomial.plot_tree()

# 3. Monte Carlo Option Pricing
# Instantiate the MonteCarloPricing class
mc = MonteCarloOptionPricing(
    inputs=inputs,
    nsims=50000,
    timestep=252,
    option_type=OptionType.CALL,
    exercise_style=ExerciseStyle.BARRIER,
    barrier_level=150,
    barrier_rebate=0,
    barrier_type=BarrierType.UP_AND_OUT,
)

print(f"\nMonte Carlo price for {mc.exercise_style} {mc.option_type} option:")
print(f"Option Price: {mc.option_price:.4f}")