Skip to content

Instantly share code, notes, and snippets.

@addiversitas
addiversitas / basicLeafletMap.R
Created October 24, 2020 14:12
basic leaflet map
library(leaflet)
leaflet() %>% setView(lng = 16.36, lat = 48.21, zoom = 10) %>% addTiles()
@addiversitas
addiversitas / ivFullCode.R
Created October 18, 2020 16:12
full code example for implied volatility surface
#load packages
library(quantmod)
library(rvest)
library(reshape2)
library(plotly)
library(akima)
#get underlying stock info and last trade date
symbol <- "AAPL"
priceInfo <- getQuote(symbol)
@addiversitas
addiversitas / surfacePlot.R
Last active October 18, 2020 16:12
example of implied volatility surface plot
library(plotly)
xaxx <- list(
gridcolor='rgb(255, 255, 255)',
zerolinecolor='rgb(255, 255, 255)',
showbackground=TRUE,
backgroundcolor='rgb(230, 230,230)',
title = "Moneyness"
)
@addiversitas
addiversitas / interpolationCalls.R
Created October 18, 2020 15:46
interpolation example for calls
#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)
@addiversitas
addiversitas / ivCalculationDiv.R
Last active October 18, 2020 13:52
iv calculation for APPL calls and puts including dividend yield
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")
@addiversitas
addiversitas / ivCalculation.R
Created October 18, 2020 13:22
iv calculation for APPL calls and puts
#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
@addiversitas
addiversitas / scrapeOptionChain.R
Last active September 2, 2021 00:40
option chain web scraping example for AAPL
#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`)
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)
@addiversitas
addiversitas / generalizedBlackScholesMerton.R
Created October 17, 2020 21:39
generalized black scholes merton implementation
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
@addiversitas
addiversitas / improvedCorrelationPlot.R
Last active October 5, 2020 11:36
full code for the improved correlation plot
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)]