# lirary load
Packages <- c("tidyverse", "DBI", "data.table", "readr", "IBrokers", "RMySQL", "ggplot2", "ggExtra")
lapply(Packages, library, character.only = TRUE)
## Warning: package 'tidyverse' was built under R version 3.5.2
## -- Attaching packages ------------------------------------------------------------------------------------------------------------------ tidyverse 1.2.1 --
## v ggplot2 3.1.0 v purrr 0.2.5
## v tibble 1.4.2 v dplyr 0.7.8
## v tidyr 0.8.2 v stringr 1.3.1
## v readr 1.3.1 v forcats 0.3.0
## Warning: package 'ggplot2' was built under R version 3.5.2
## Warning: package 'tidyr' was built under R version 3.5.2
## Warning: package 'readr' was built under R version 3.5.2
## Warning: package 'dplyr' was built under R version 3.5.2
## Warning: package 'forcats' was built under R version 3.5.2
## -- Conflicts --------------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## Warning: package 'DBI' was built under R version 3.5.2
## Warning: package 'data.table' was built under R version 3.5.2
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
##
## between, first, last
## The following object is masked from 'package:purrr':
##
## transpose
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'xts'
## The following objects are masked from 'package:data.table':
##
## first, last
## The following objects are masked from 'package:dplyr':
##
## first, last
## IBrokers version 0.9-10. Implementing API Version 9.64
##
## IBrokers comes with NO WARRANTY. Not intended for production use!
## See ?IBrokers for details.
## Warning: package 'RMySQL' was built under R version 3.5.2
## [[1]]
## [1] "forcats" "stringr" "dplyr" "purrr"
## [5] "readr" "tidyr" "tibble" "ggplot2"
## [9] "tidyverse" "RevoUtils" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "RevoUtilsMath"
## [17] "methods" "base"
##
## [[2]]
## [1] "DBI" "forcats" "stringr" "dplyr"
## [5] "purrr" "readr" "tidyr" "tibble"
## [9] "ggplot2" "tidyverse" "RevoUtils" "stats"
## [13] "graphics" "grDevices" "utils" "datasets"
## [17] "RevoUtilsMath" "methods" "base"
##
## [[3]]
## [1] "data.table" "DBI" "forcats" "stringr"
## [5] "dplyr" "purrr" "readr" "tidyr"
## [9] "tibble" "ggplot2" "tidyverse" "RevoUtils"
## [13] "stats" "graphics" "grDevices" "utils"
## [17] "datasets" "RevoUtilsMath" "methods" "base"
##
## [[4]]
## [1] "data.table" "DBI" "forcats" "stringr"
## [5] "dplyr" "purrr" "readr" "tidyr"
## [9] "tibble" "ggplot2" "tidyverse" "RevoUtils"
## [13] "stats" "graphics" "grDevices" "utils"
## [17] "datasets" "RevoUtilsMath" "methods" "base"
##
## [[5]]
## [1] "IBrokers" "xts" "zoo" "data.table"
## [5] "DBI" "forcats" "stringr" "dplyr"
## [9] "purrr" "readr" "tidyr" "tibble"
## [13] "ggplot2" "tidyverse" "RevoUtils" "stats"
## [17] "graphics" "grDevices" "utils" "datasets"
## [21] "RevoUtilsMath" "methods" "base"
##
## [[6]]
## [1] "RMySQL" "IBrokers" "xts" "zoo"
## [5] "data.table" "DBI" "forcats" "stringr"
## [9] "dplyr" "purrr" "readr" "tidyr"
## [13] "tibble" "ggplot2" "tidyverse" "RevoUtils"
## [17] "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "RevoUtilsMath" "methods" "base"
##
## [[7]]
## [1] "RMySQL" "IBrokers" "xts" "zoo"
## [5] "data.table" "DBI" "forcats" "stringr"
## [9] "dplyr" "purrr" "readr" "tidyr"
## [13] "tibble" "ggplot2" "tidyverse" "RevoUtils"
## [17] "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "RevoUtilsMath" "methods" "base"
##
## [[8]]
## [1] "ggExtra" "RMySQL" "IBrokers" "xts"
## [5] "zoo" "data.table" "DBI" "forcats"
## [9] "stringr" "dplyr" "purrr" "readr"
## [13] "tidyr" "tibble" "ggplot2" "tidyverse"
## [17] "RevoUtils" "stats" "graphics" "grDevices"
## [21] "utils" "datasets" "RevoUtilsMath" "methods"
## [25] "base"
# Connect to IB
library(IBrokers)
tws = twsConnect(port=7496)
isConnected(tws)
## [1] TRUE
twsConnectionTime(tws)
## [1] "20190416 17:02:02 Greenwich Mean Time"
# Define contract
ohlc_daily_spx = twsIND("SPX", "CBOE")
# Download ohlc of price of the underlying and the iv of the underlying
ohlc_daily_iv_spx_IB = reqHistoricalData(tws, ohlc_daily_spx, barSize = "1 day", duration = '15 Y', whatToShow = "OPTION_IMPLIED_VOLATILITY")
## TWS Message: 2 -1 2106 HMDS data farm connection is OK:ushmds.nj
## waiting for TWS reply on SPX .... done.
ohlc_daily_price_spx_IB = reqHistoricalData(tws, ohlc_daily_spx, barSize = "1 day", duration = '15 Y', whatToShow = "TRADES")
## waiting for TWS reply on SPX .... done.
# Convert with`as data table`
ohlc_daily_price_spx_dt <- as.data.table(ohlc_daily_price_spx_IB)
ohlc_daily_iv_spx_dt <- as.data.table(ohlc_daily_iv_spx_IB)
# Convert index to date format
ohlc_daily_price_spx_dt$index <- as.Date(as.POSIXct(ohlc_daily_price_spx_dt$index, 'GMT'))
ohlc_daily_iv_spx_dt$index <- as.Date(as.POSIXct(ohlc_daily_iv_spx_dt$index, 'GMT'))
# Merge into one table
ohlc_p_iv_spx <- inner_join(ohlc_daily_price_spx_dt, ohlc_daily_iv_spx_dt, by = "index")
# Rename index to Date
colnames(ohlc_p_iv_spx)[1] <- "Date"
ohlc_p_iv_spx$R_co <- ohlc_p_iv_spx$SPX.Close.x - ohlc_p_iv_spx$SPX.Open.x
ohlc_p_iv_spx$R_hl <- ohlc_p_iv_spx$SPX.High.x - ohlc_p_iv_spx$SPX.Low.x
ohlc_p_iv_spx$IV_d <- ohlc_p_iv_spx$SPX.Open.y / sqrt(252)
ohlc_p_iv_spx$E_h <- ohlc_p_iv_spx$SPX.Open.x * ( 1 + ohlc_p_iv_spx$IV_d)
ohlc_p_iv_spx$E_l <- ohlc_p_iv_spx$SPX.Open.x * ( 1 - ohlc_p_iv_spx$IV_d)
ohlc_p_iv_spx$H_E_h <- ohlc_p_iv_spx$E_h - ohlc_p_iv_spx$SPX.High.x
ohlc_p_iv_spx$H_E_h <- ohlc_p_iv_spx$E_h - ohlc_p_iv_spx$SPX.High.x
ohlc_p_iv_spx$H_o <- ohlc_p_iv_spx$SPX.High.x - ohlc_p_iv_spx$SPX.Open.x
ohlc_p_iv_spx$L_o <- ohlc_p_iv_spx$SPX.Open.x - ohlc_p_iv_spx$SPX.Low.x
ohlc_p_iv_spx$E_h_o <- ohlc_p_iv_spx$E_h - ohlc_p_iv_spx$SPX.Open.x
ohlc_p_iv_spx$E_l_o <- ohlc_p_iv_spx$SPX.Open.x - ohlc_p_iv_spx$SPX.Low.x
Plot the inial data
# classic plot :
ggplot(ohlc_p_iv_spx, aes(x=Date, y=R_co)) +
geom_point() +
geom_line(data=ohlc_p_iv_spx, aes(x=Date, y=SPX.Open.y), col="yellow")