brahma-investment-group/big-algo-framework

Black Scholes

Opened this issue · 1 comments

Here is a code snippet to calculate the option price and greeks using Black Scholes method. We can also look into other methods like Binomial mthod.

import numpy as np
from scipy.stats import norm

class BlackScholes():
    def __init__(self, S, K, r, T, sigma, option_right, action):
        self.S = S
        self.K = K
        self.r = r
        self.T = T
        self.sigma = sigma
        self.option_right = option_right
        self.action = action

        self.d1 = (np.log(self.S / self.K) + (self.r + 0.5 * self.sigma ** 2) * self.T) / (self.sigma * np.sqrt(self.T))
        self.d2 = (np.log(self.S / self.K) + (self.r - 0.5 * self.sigma ** 2) * self.T) / (self.sigma * np.sqrt(self.T))

    def get_option_price(self):
        if self.option_right == 'C':
            return self.S * norm.cdf(self.d1) - self.K * np.exp(-self.r * self.T) * norm.cdf(self.d2)
        else:
            return self.K * np.exp(-self.r * self.T) * norm.cdf(-self.d2) - self.S * norm.cdf(-self.d1)

    def get_delta(self):
        if self.option_right == "C":
            return norm.cdf(self.d1)
        else:
            return norm.cdf(self.d1) - 1

    def get_theta(self):
        N_d2 = norm.cdf(self.d2)

        if self.option_right == "C":
            theta = -(self.S * norm.pdf(self.d1) * self.sigma) / (2 * np.sqrt(self.T)) - self.r * self.K * np.exp(-self.r * self.T) * N_d2
        else:
            theta = (self.S * norm.pdf(self.d1) * self.sigma) / (2 * np.sqrt(self.T)) - self.r * self.K * np.exp(-self.r * self.T) * (1 - N_d2)

        return theta * (-1 if self.action == "BUY" else 1)

    def get_gamma(self):
        gamma = norm.pdf(self.d1) / (self.S * self.sigma * np.sqrt(self.T))
        return gamma * (1 if self.action == "BUY" else -1)

    def get_vega(self):
        vega = self.S * norm.pdf(self.d1) * np.sqrt(self.T)
        return vega * (1 if self.action == "BUY" else -1)

    def get_rho(self):
        if self.option_right == "C":
            rho = self.K * self.T * np.exp(-self.r * self.T) * norm.cdf(self.d2)
            return rho * (1 if self.action == "BUY" else -1)

        else:
            rho = -self.K * self.T * np.exp(-self.r * self.T) * norm.cdf(-self.d2)
            return rho * (1 if self.action == "SELL" else -1)

#Example
bs = BlackScholes(S=4, K=5, r=0.0459, T=30/365, sigma=0.901, option_right='P', action='SELL')
print("Option Price is: ${:.2f}".format(bs.get_option_price()))
print("Option Delta is: {:.2f}".format(bs.get_delta()))
print("Option Theta is: {:.2f}".format(bs.get_theta()))
print("Option Gamma is: {:.2f}".format(bs.get_gamma()))
print("Option Vega is: {:.2f}".format(bs.get_vega()))
print("Option Rho is: {:.2f}".format(bs.get_rho()))

We may want to consider: pyBlackScholesAnalytics
It will work with both North American and European options which is something the one above didn't take into consideration.
We could add it as a prerequisite and then have the ability to use it's capabilities internally.