library("fOptions")
## Warning: package 'fOptions' was built under R version 3.4.4
## Loading required package: timeDate
## Loading required package: timeSeries
## Warning: package 'timeSeries' was built under R version 3.4.4
## Loading required package: fBasics
## Warning: package 'fBasics' was built under R version 3.4.4
# Black Scholes option pricing
GBSOption(TypeFlag = "c", S = 537.4, X = 500, Time = 0.25, r = 0.06477, sigma = 0.282885, b = 0.08 )
## 
## Title:
##  Black Scholes Option Valuation 
## 
## Call:
##  GBSOption(TypeFlag = "c", S = 537.4, X = 500, Time = 0.25, r = 0.06477, 
##      b = 0.08, sigma = 0.282885)
## 
## Parameters:
##           Value:  
##  TypeFlag c       
##  S        537.4   
##  X        500     
##  Time     0.25    
##  r        0.06477 
##  b        0.08    
##  sigma    0.282885
## 
## Option Price:
##  58.76074 
## 
## Description:
##  Wed Apr 11 05:27:28 2018
# Cox Ross Rubinstein Model 
CRRBinomialTreeOption(TypeFlag = "ce", S = 537.4, X = 500, Time = 1/4, r = 0.06477, b = 0.08, sigma = 0.282885, n = 3)@price
## [1] 57.76515
CRRBinomialTreeOption(TypeFlag = "ce", S = 537.4, X = 500, Time = 1/4, r = 0.06477, b = 0.08, sigma = 0.282885, n = 3)
## 
## Title:
##  CRR Binomial Tree Option 
## 
## Call:
##  CRRBinomialTreeOption(TypeFlag = "ce", S = 537.4, X = 500, Time = 1/4, 
##      r = 0.06477, b = 0.08, sigma = 0.282885, n = 3)
## 
## Parameters:
##           Value:  
##  TypeFlag ce      
##  S        537.4   
##  X        500     
##  Time     0.25    
##  r        0.06477 
##  b        0.08    
##  sigma    0.282885
##  n        3       
## 
## Option Price:
##  57.76515 
## 
## Description:
##  Wed Apr 11 05:27:28 2018
CRRTree <- BinomialTreeOption(TypeFlag = "ce" , S = 537.4 , X = 500 , Time = 1/4 , r = 0.06477 , sigma = 0.282885 , b = 0.08 , n = 3 )
BinomialTreePlot(CRRTree , dy = 1 , xlab = "Time steps" , ylab = "Number of up steps" , xlim = c(0,4))
title("call option tree")

#connection between models

prices <- sapply(1:200 , function(n){CRRBinomialTreeOption(TypeFlag = "ce" ,Time = 1/4 ,  S = 537.4 , X = 500 , sigma = 0.282885 , r = 0.06477 , b = 0.08 , n = n )@price} )

price <- GBSOption(TypeFlag = "c", S = 537.4, X = 500, Time = 0.25, r = 0.06477, sigma = 0.282885, b = 0.08 )@price


plot(1:200, prices, xlab = 'Number of steps', ylab = ' option Prices', type = "l")
abline(h = price, col = 'red')
legend("bottomright", legend = c('CRR-price', 'BS-price'), col = c('black', 'red'), pch = 19)

# Greeks
sapply( c("delta" , "rho" , "gamma" , "vega" , "theta"), function(greek)
  GBSGreeks(Selection = greek, TypeFlag = "c", S = 537.4 , X = 500 , r = 0.06477 ,   b = 0.08 , sigma = 0.282885 , Time = 1/4 ))
##         delta           rho         gamma          vega         theta 
##   0.767805694  88.464510295   0.004059336  82.908722735 -76.110837410
deltas <- sapply(c(1/4, 1/20, 1/50), function(t)
          sapply(500:1500, function(S)
          GBSGreeks(Selection = 'delta', TypeFlag = "c",
          S = S, X = 500, Time = t, r = 0.06477, b = 0.08, sigma = 0.282885)))

plot(500:1500, deltas[, 1], ylab = 'Delta of call option', xlab = "Price of the underlying (S)", type = 'l')
lines(500:1500, deltas[, 2], col = 'blue')
lines(500:1500, deltas[, 3], col = 'red')
legend("bottomright", legend = c('t=1/4', 't=1/20', 't=1/50'), col = c('black', 'blue', 'red'), pch = 19)

# straddle
straddles <- sapply(c('c', 'p'), function(type)
                    sapply(500:1500, function(S)
                           GBSGreeks(Selection = 'delta', TypeFlag = type, S = S, X = 500, Time = 1/4, r = 0.06477, b = 0.08, sigma = 0.282885)))

plot(500:1500, rowSums(straddles), type = 'l', xlab = 'Price of the underlying (S)', ylab = 'Delta of straddle')