Skip to content

Instantly share code, notes, and snippets.

@addiversitas
Created October 17, 2020 21:39
Show Gist options
  • Save addiversitas/72b3e8634686faaaae9a1ec5015315e8 to your computer and use it in GitHub Desktop.
Save addiversitas/72b3e8634686faaaae9a1ec5015315e8 to your computer and use it in GitHub Desktop.
generalized black scholes merton implementation
gBSM <- function(S, X, sigma, r, q, ttm, type){
#S = stock price
#X = strike price
#sigma = volatility
#r = risk free interest rate
#q = dividend yield
#ttm = time to maturity in days
#type = option type
b <- r - q
t <- ttm/365.25
d1 <- (log(S / X) + (b + sigma ^ 2 / 2) * t) / (sigma * sqrt(t))
d2 <- d1 - sigma * sqrt(t)
if(type == "call"){
price <- S * exp((b - r) * t) * pnorm(d1) - X * exp(-r * t) * pnorm(d2)
}else if (type == "put"){
price <- (X * exp(-r * t) * pnorm(-d2) - S * exp((b - r) * t) * pnorm(-d1))
}
return(price)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment