Black-Scholes – R Code

This is a simple R code for calculating the price of a European call or put option using the Black-Scholes-Merton closed-form solution. The inputs can be easily modified in the corresponding block (initial stock price, strike price, annual volatility, time, risk-free rate and dividend yield).

## European call / put option

# clear environment and console
rm(list = ls())
cat("\014")

# inputs
S = 10 # stock price
K = 10 # strike price
sigma = 0.2 # volatility
t = 1.5 # number of years
r = 0.02 # risk-free rate
d = 0 # expected dividend yield

# calculate d1 & d2
d1 = (log(S/K) + (r - d + 0.5*sigma^2)*t) / (sigma * sqrt(t))
d2 = d1 - sigma * sqrt(t)

# calculate call price
c = S * pnorm(d1) * exp(-d * t) - K * pnorm(d2) * exp(-r * t)
c

# calculate put price
p = K * pnorm(-d2) * exp(-r * t) - S * pnorm(-d1) * exp(-d * t)
p

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s