This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("shiny") | |
library("plotly") | |
library("data.table") | |
dates <- seq(as.Date("2019-02-01"),length=12,by="months") - 1 | |
books <- c(17,3,3,18,20,19,15,12,17,4,18,7) | |
laptops <- c(13,5,8,12,27,32,31,9,22,11,12,30) | |
tvs <- c(30,41,22,11,4,33,16,17,8,49,14,50) | |
data <- data.frame(dates = dates, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("shiny") | |
library("plotly") | |
library("data.table") | |
dates <- seq(as.Date("2019-02-01"),length=12,by="months") - 1 | |
books <- c(17,3,3,18,20,19,15,12,17,4,18,7) | |
laptops <- c(13,5,8,12,27,32,31,9,22,11,12,30) | |
tvs <- c(30,41,22,11,4,33,16,17,8,49,14,50) | |
data <- data.frame(dates = dates, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(plotly) | |
library(data.table) | |
data <- mtcars[,c(1,3:7)] | |
corrdata <- cor(data) | |
#do this before the transformation! | |
corrdata[upper.tri(corrdata, diag = TRUE)] <- NA | |
corrdata <- corrdata[-1, -ncol(corrdata)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gBSM <- function(S, X, sigma, r, q, ttm, type){ | |
#S = stock price | |
#X = strike price | |
#sigma = volatility | |
#r = risk free interest rate | |
#q = dividend yield | |
#ttm = time to maturity in days | |
#type = option type | |
b <- r - q |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
volOptimFun <- function(sigma, price, S, K, r, q, ttm, type){ | |
abs(price - gBSM(S, K, sigma, r, q, ttm, type)) | |
} | |
optimize(volOptimFun, interval = c(0, 2), price = price, S = S, K = K, r = r, q = q, ttm = ttm, type = type) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#load packages | |
library(quantmod) | |
library(rvest) | |
#get underlying stock info and last trade date | |
symbol <- "AAPL" | |
priceInfo <- getQuote(symbol) | |
lastPrice <- priceInfo$Last | |
date <- as.Date(priceInfo$`Trade Time`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#generalized black scholes merton model | |
gBSM <- function(S, X, sigma, r, q, ttm, type){ | |
#S = stock price | |
#X = strike price | |
#sigma = volatility | |
#r = risk free interest rate | |
#q = dividend yield | |
#ttm = time to maturity in days | |
#type = option type | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
divYield <- getQuote(symbol, what = yahooQF("Dividend Yield"))$`Dividend Yield` | |
if(is.na(divYield)){divYield <- 0} | |
#calculating IV | |
calls$iv <- apply(calls, 1, getIV, S = lastPrice, r = 0.0011, q = divYield, type = "call") | |
puts$iv <- apply(puts, 1, getIV, S = lastPrice, r = 0.0011, q = divYield, type = "put") | |
#create grids | |
library(reshape2) | |
ivGridCalls <- acast(calls, ttm ~ moneyness, value.var = "iv") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#get coordinates of NAs in grid | |
toInterpolate <- which(is.na(ivGridCalls)) | |
coords <- cbind(toInterpolate%%dim(ivGridCalls)[1], toInterpolate%/%dim(ivGridCalls)[1] + 1) | |
coords[coords[,1] == 0, 2] <- coords[coords[,1] == 0, 2] - 1 | |
coords[coords[,1] == 0, 1] <- dim(ivGridCalls)[1] | |
#loop through NAs and interpolate | |
for(i in 1:nrow(coords)){ | |
#get the coordinates of a 10x10 area around the missing value | |
x1 <- max(coords[i,1] - 10, 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(plotly) | |
xaxx <- list( | |
gridcolor='rgb(255, 255, 255)', | |
zerolinecolor='rgb(255, 255, 255)', | |
showbackground=TRUE, | |
backgroundcolor='rgb(230, 230,230)', | |
title = "Moneyness" | |
) |
OlderNewer