Skip to content

Instantly share code, notes, and snippets.

@addiversitas
addiversitas / basicChart.R
Last active September 7, 2020 14:10
basic plotly chart in a shiny app
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,
@addiversitas
addiversitas / finalChart.R
Created September 7, 2020 18:21
end result after improving the plotly output
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,
@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)]
@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
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 / 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`)
@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 / 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 / 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 / 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"
)