﻿

# Option pricing in R

Mar 26th, 2012
270
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # see more: http://www.reakkt.com/2012/03/diverging-on-option-prices.html
2.
3. library(fOptions)
4. library(RQuantLib)
5.
6. # S - spot
7. # K - strike price
8. # rf - risk free rate
9. # TTM - time to maturity
10. # sigma - volatility
11. BSCall <- function(S,K,rf,TTM,sigma) {
12.
13.   d1 <- (log(S/K)+(rf+sigma^2/2)*TTM) / (sigma*sqrt(TTM))
14.
15.   d2 <- d1-sigma*sqrt(TTM)
16.
17.   pnorm(d1)*S-pnorm(d2)*K*exp(-rf*TTM)
18.
19. }
20.
21. BSPut <- function(S,K,rf,TTM,sigma) {
22.
23.   K*exp(-rf*TTM)-S+BSCall(S,K,rf,TTM,sigma)
24.
25. }
26.
27. # q - dividend yield
28. BSCall.dividend <- function(S,K,rf,q,TTM,sigma) {
29.
30.   F = S * exp((rf-q)*TTM)
31.
32.   d1 <- (log(F/K)+(sigma^2/2)*TTM) / (sigma*sqrt(TTM))
33.
34.   d2 <- d1-sigma*sqrt(TTM)
35.
36.   exp(-rf*TTM)*( (F*pnorm(d1))-K*pnorm(d2) )
37.
38. }
39.
40. BSPut.dividend <-  function(S,K,rf,q,TTM,sigma) {
41.
42.   F = S * exp((rf-q)*TTM)
43.
44.   d1 <- (log(F/K)+(sigma^2/2)*TTM) / (sigma*sqrt(TTM))
45.
46.   d2 <- d1-sigma*sqrt(TTM)
47.
48.   exp(-rf*TTM)*( (K*pnorm(-d2))-F*pnorm(-d1) )
49.
50. }
51.
52. BSCall(S=5,K=7,rf=0.045,TTM=0.5,sigma=0.27)
53.
54. BSPut(S=10,K=7,rf=0.045,TTM=0.5,sigma=0.27)
55.
56. BSCall.dividend(S=5,K=7,rf=0.045,q=0.02,TTM=0.5,sigma=0.27)
57.
58. BSPut.dividend(S=10,K=7,rf=0.045,q=0.02,TTM=0.5,sigma=0.27)
59.
60. ###
61.
62. xSpot   = 601042.9378
63. xStrike = 254417.3841
64. xRf     = 0.14395813
65. xDiv    = 0.11951086
66. xTTM    = 6.22885299
67. xSigma  = 1.04225573
68.
69. BSPut.dividend(S=xSpot,K=xStrike,rf=xRf,q=xDiv,TTM=xTTM,sigma=xSigma)
70.
71. GBSOption(TypeFlag="p",S=xSpot,X=xStrike,Time=xTTM,r=xDiv,b=xRf,sigma=xSigma)@price
72.
73. EuropeanOption(type="put",underlying=xSpot,strike=xStrike,dividendYield=xDiv,riskFreeRate=xRf,maturity=xTTM,volatility=xSigma)\$value
RAW Paste Data