File: OptionSurfaces.R

package info (click to toggle)
rquantlib 0.4.17-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,308 kB
  • sloc: cpp: 3,690; sh: 69; makefile: 6; ansic: 4
file content (80 lines) | stat: -rw-r--r-- 2,408 bytes parent folder | download | duplicates (4)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

## RQuantLib Demo for (European) Option surfaces
## Dirk Eddelbuettel, September 2005
## $Id: OptionSurfaces.R,v 1.1 2005/10/12 03:42:45 edd Exp $

OptionSurface <- function(EOres, label, fov=60) {
    axis.col <- "black"
    text.col <- axis.col
    ylab <- label
    xlab <- "Underlying"
    zlab <- "Volatility"
    y <- EOres

    ## clear scene:
    clear3d()
    clear3d(type="bbox")
    clear3d(type="lights")

    ## setup env:
    ##  bg3d(color="#887777")
    bg3d(color="#DDDDDD")
    light3d()

    rgl.viewpoint(fov=fov)
    ##rgl.bg(col="white", fogtype="exp2")
    ##rgl.bg(col="black", fogtype="exp2")
    ##rgl.bg(col="black", fogtype="exp")
    ##rgl.bg(col="white", fogtype="exp")

    x <- (1:nrow(y))
    z <- (1:ncol(y))
    x <- (x-min(x))/(max(x)-min(x))
    y <- (y-min(y))/(max(y)-min(y))
    z <- (z-min(z))/(max(z)-min(z))
    rgl.surface(x, z, y, alpha=0.6, lit=TRUE, color="blue")
    rgl.lines(c(0,1), c(0,0), c(0,0), col=axis.col)
    rgl.lines(c(0,0), c(0,1), c(0,0), col=axis.col)
    rgl.lines(c(0,0),c(0,0), c(0,1), col=axis.col)
    rgl.texts(1,0,0, xlab, adj=1, col=text.col)
    rgl.texts(0,1,0, ylab, adj=1, col=text.col)
    rgl.texts(0,0,1, zlab, adj=1, col=text.col)

    ## add grid (credit's to John Fox scatter3d)
    xgridind <- round(seq(1, nrow(y), length=25))
    zgridind <- round(seq(1, ncol(y), length=25))
    rgl.surface(x[xgridind], z[zgridind], y[xgridind,zgridind],
                color="darkgray", alpha=0.5, lit=TRUE,
                front="lines", back="lines")

    ## animate (credit to rgl.viewpoint() example)
    start <- proc.time()[3]
    while ((i <- 36*(proc.time()[3]-start)) < 360) {
        rgl.viewpoint(i,i/8);
    }
}

RQuantLib.demo.OptionSurfaces <- function() {

    und.seq <- seq(10, 200, by = 2.5)
    vol.seq <- seq(0.05, 2, by = 0.025)

    cat("Calculating surface ...")
    EOarr <- EuropeanOptionArrays("call", underlying = und.seq, strike = 100,
                                  dividendYield = 0.01, riskFreeRate = 0.03,
                                  maturity = 1, volatility = vol.seq)
    cat(" done.\n")

    rgl.open()
    OptionSurface(EOarr$value, "Value")
    OptionSurface(EOarr$delta, "Delta")
    OptionSurface(EOarr$gamma, "Gamma")
    OptionSurface(EOarr$vega, "Vega")
    OptionSurface(EOarr$theta, "Theta")

}

require(rgl,quiet=TRUE)
require(RQuantLib,quiet=TRUE)

RQuantLib.demo.OptionSurfaces()