-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbino tree.R
More file actions
62 lines (60 loc) · 2.09 KB
/
bino tree.R
File metadata and controls
62 lines (60 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#'@title Construct a binowmial Tree
#'@description
#'\code{bino} Constructs the binomial tree for an american option and calculates
#'the option price by taking type,strike price(k),Strike Price(st),volatility(vol),
#'dividend yield(yield),Risk free rate of return(rf),time to maturity(time),n is the number of steps
#'@param type for call option 0 for put option 1
#'@param k strike price
#'@param st stock price
#'@param volatility vol volatility(e.g 30 for 30%)
#'@param yield dividend yield(e,g 3 for 3%)
#'@param rf risk free rate(e.g 6.5 for 6.5%)
#'@param time time to maturity(e.g 3 for 3 months)
#'@param n number of steps(e.g 2 for 2 steps)
bino<-function(type,k,st,vol,yield,rf,time,n){
m_price<-matrix(nrow=n+1,ncol=n+1)
m_payoff<-matrix(nrow=n+1,ncol=n+1)
u<-exp((vol/100)*sqrt(time/(12*n)))
print(u)
d<-1/u
print(d)
deltaT<-time/(12*n)
p<-(exp((rf-yield)*deltaT/100)-d)/(u-d)
print(p)
q<-1-p
price<-st
n<-n+1
for(i in 1:n){
m_price[i,i]<-st*(d^(i-1))
if(type==1){
m_payoff[i,i]<-max(m_price[i,i]-k,0)
}
else{
m_payoff[i,i]<-max(k-m_price[i,i],0)
}
price<-m_price[i,i]
for(j in i:n){
m_price[i,j]<-price
if(type==1){
m_payoff[i,j]<-max(m_price[i,j]-k,0)
}
else{
m_payoff[i,j]<-max(k-m_price[i,j],0)
}
price<-price*u
}
}
m_final<-matrix(nrow=n,ncol=n)
for(i in n:1){
for(j in 1:(i-1)){
m_final[j,i-1]<-max(exp(-rf*deltaT/100)*(m_payoff[j,i]*p+m_payoff[j+1,i]*q),m_payoff[j,i-1])
}
}
print(q)
print(m_payoff)
print(m_final)
#if fOptions is not installed
#install.packages("fOptions")
library(fOptions)
BinomialTreePlot(m_final,xlab="n",ylab="Option Value",xlim=c(0,8))
}