Skip to content

Instantly share code, notes, and snippets.

@zachlim98
Created February 11, 2021 07:16
Show Gist options
  • Save zachlim98/d2ba6a554f7ba25afe739718768f6cb0 to your computer and use it in GitHub Desktop.
Save zachlim98/d2ba6a554f7ba25afe739718768f6cb0 to your computer and use it in GitHub Desktop.
BlackScholes <- function(S, K, r, T, sig, type){
if(type=="Call"){
d1 <- (log(S/K) + (r + sig^2/2)*T) / (sig*sqrt(T))
d2 <- d1 - sig*sqrt(T)
value <- S*pnorm(d1) - K*exp(-r*T)*pnorm(d2)
return(value)}
if(type=="Put"){
d1 <- (log(S/K) + (r + sig^2/2)*T) / (sig*sqrt(T))
d2 <- d1 - sig*sqrt(T)
value <- (K*exp(-r*T)*pnorm(-d2) - S*pnorm(-d1))
return(value)}
}
putPrice <- matrix(0,38,1000)
for (s in 1:ncol(gbm)){
for (t in 1:nrow(gbm)) {
putPrice[t,s] <- BlackScholes(gbm[t,s], 122.5, 0.01, (nrow(gbm)+1-t)/365, 0.37, "Put")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment