Search code examples
rquantlib

namespace locked for RQuantLib


Running this on RStudio/Windows. The last line fails with:

Error in registerNames(names, package, ".global", add) : The namespace for package "RQuantLib" is locked; no changes in the global variables list may be made.

library(RQuantLib)

myStrike <- 1240
myPrice <- 1410

volVec <- c(0.1, 0.2, 0.3, 0.4, 0.5)
priceVec <- c(1500,1400,1300,1200,1100)
myType <- "put"
rfRate <- 0.02
maturity <- 360/360
vol <- 0.3


EO = EuropeanOption(type = myType,price = myPrice,strike = myStrike, dividendYield = 0,riskFreeRate = rfRate, maturity = maturity,volatility = vol )
EOres = EuropeanOptionArrays(type = myType,price = priceVec,strike = myStrike, dividendYield = 0,riskFreeRate = rfRate, maturity = maturity,volatility = volVec )
summary(EO)
plotOptionSurface(EOres)

Solution

  • Thanks for asking this question, and apologies for missing it for a few days. An issue ticket at GitHub would have been ok too.

    The code you post (and possibly quoted from old documentation of mine...) is buggy. The second argument to both EuropeanOption() and EuropeanOptionArrays() is now underlying, not price. So we make this

    library(RQuantLib)
    
    myStrike <- 1240
    myPrice <- 1410
    
    volVec <- c(0.1, 0.2, 0.3, 0.4, 0.5)
    priceVec <- c(1500,1400,1300,1200,1100)
    myType <- "put"
    rfRate <- 0.02
    maturity <- 360/360
    vol <- 0.3
    
    
    EO <- EuropeanOption(type = myType, underlying = myPrice,strike = myStrike,
                         dividendYield = 0, riskFreeRate = rfRate, maturity = maturity,
                         volatility = vol )
    EOres <- EuropeanOptionArrays(type = myType, underlying = priceVec,strike = myStrike, 
                                  dividendYield = 0,riskFreeRate = rfRate, maturity = maturity,
                                  volatility = volVec )
    summary(EO)
    plotOptionSurface(EOres)
    

    Next, in the function plotOptionSurface() we need to comment out the call to globalVariables(), this can no longer be in a function body. So make it eg

    pos <- function(EOres, ylabel="", xlabel="", zlabel="", fov=60) {
        if (requireNamespace("rgl", quietly=TRUE)) {
            #utils::globalVariables(c("clear3d", "bg3d", "ligh3d", "rgl.viewpoint", "rgl.surface", "tgl.texts"))
            axis.col <- "black"
            text.col <- axis.col
            ylab <- ylabel
            xlab <- xlabel
            zlab <- zlabel
            y <- EOres
    
            ## clear scene:
            rgl::clear3d()
            rgl::clear3d(type="bbox")
            rgl::clear3d(type="lights")
    
            ## setup env:
            rgl::bg3d(color="#DDDDDD")
            rgl::light3d()
    
            rgl::rgl.viewpoint(fov=fov)
    
            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::rgl.surface(x, z, y, alpha=0.6, lit=TRUE, color="blue")
            rgl::rgl.lines(c(0,1), c(0,0), c(0,0), col=axis.col)
            rgl::rgl.lines(c(0,0), c(0,1), c(0,0), col=axis.col)
            rgl::rgl.lines(c(0,0),c(0,0), c(0,1), col=axis.col)
            rgl::rgl.texts(1,0,0, xlab, adj=1, col=text.col)
            rgl::rgl.texts(0,1,0, ylab, adj=1, col=text.col)
            rgl::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::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::rgl.viewpoint(i,i/8);
            }
        } else {
            message("Please install the 'rgl' package before using this function.")
        }
    }
    

    Now we can call with element of the result list, eg

    pos(EOres[[1]])  # value
    

    and ditto for the others.