#install package and library fOptions # install.packages("fOptions") library(fOptions) # #In order to obtain the price of a call or put option by the Black-Scholes formula we can #use the function GBSOption, with the appropriate parameters, like in the following examples #X is the strike, b=r with no dividends and b=r-q with dividend yield q. GBSOption(TypeFlag = "c", S = 20, X = 22, Time = 6/12, r = 0.10, b = 0.10, sigma =0.2) #The following example is to price a put option with the same parameters GBSOption(TypeFlag = "p", S = 20, X = 22, Time = 6/12, r = 0.10, b = 0.10, sigma =0.2) # #Example of the price of a call with dividend yield q=0.02 GBSOption(TypeFlag = "c", S = 20, X = 22, Time = 6/12, r = 0.10, b = 0.08, sigma =0.2) # #Example of writing the R code for a function that implements the Black-Scholes formula #for a put option (x represents the value of the underlying asset S at time t) put.price <- function(x = 1, t = 0, T = 1, r = 1, q = 1, sigma = 1, K=1){d2 <- (log(x/K) + (r - q - 0.5 * sigma^2) * (T - t))/(sigma * sqrt(T - t)) d1 <- d2 + sigma * sqrt(T - t) K * exp(-r * (T - t)) * pnorm(-d2) - x * exp(-q * (T - t)) * pnorm(-d1) } # #Example of using this function # put.price(x = 10, t = 1/12, T =6/12, r = 0.07, q = 0.03, K = 11, sigma = 0.3) ##Another example put.price(x = 20, t = 0, T =6/12, r = 0.10, q = 0, K = 22, sigma = 0.2)