1. Load libraries
# 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"

1.3 Download from library

con <- dbConnect(RMySQL::MySQL(),
                 dbname= "quantmei_library",
                 host = "91.208.99.2",
                 port = 1210,
                 user = "quantmei_library",
                 password = "Antonio2357!")
OHLC_DAILY_P_IV_SPX <- dbReadTable(con, "OHLC_DAILY_P_IV_SPX")
  1. Created calculated values Name Description
    R_co range at close of close to open C - O R-hl range of high to low at close H - L E_h expected H O * (1+(v/SQRT(252)) E_l expected L O * (1-(v/SQRT(252)) H_E_h diff from H to E.H [O * (1+v)] - H L_E_l diff from E.L to L [O * (1-v)] - L R_2 theoretical range expected Library Call back (given constant) S_UP upper bear spread strike E.H +/- CONSTANT S_DW lower bull spread strike E.L +/- CONSTANT C_ER.H calculated expected error for high C_ER.L calculated expected error for low IV_d daily IV as a function of the yearly [[ IV Y]]/SQRT(252) H_o high from open deviation [O * (1+v)] - (H - O) L_o low from open deviation [O * (1+v)] - (H - O) E_h_o expected high dev from open E_L_o expected low dev from open
OHLC_DAILY_P_IV_SPX$R_co <- OHLC_DAILY_P_IV_SPX$SPX.Close.x - OHLC_DAILY_P_IV_SPX$SPX.Open.x      
OHLC_DAILY_P_IV_SPX$R_hl <- OHLC_DAILY_P_IV_SPX$SPX.High.x - OHLC_DAILY_P_IV_SPX$SPX.Low.x
OHLC_DAILY_P_IV_SPX$IV_d <- OHLC_DAILY_P_IV_SPX$SPX.Open.y / sqrt(252)
OHLC_DAILY_P_IV_SPX$E_h <- OHLC_DAILY_P_IV_SPX$SPX.Open.x * ( 1 + OHLC_DAILY_P_IV_SPX$IV_d)
OHLC_DAILY_P_IV_SPX$E_l <- OHLC_DAILY_P_IV_SPX$SPX.Open.x * ( 1 - OHLC_DAILY_P_IV_SPX$IV_d)
OHLC_DAILY_P_IV_SPX$H_E_h <- OHLC_DAILY_P_IV_SPX$E_h - OHLC_DAILY_P_IV_SPX$SPX.High.x
OHLC_DAILY_P_IV_SPX$L_E_l <- OHLC_DAILY_P_IV_SPX$E_h - OHLC_DAILY_P_IV_SPX$SPX.Low.x
OHLC_DAILY_P_IV_SPX$H_o <- OHLC_DAILY_P_IV_SPX$SPX.High.x - OHLC_DAILY_P_IV_SPX$SPX.Open.x
OHLC_DAILY_P_IV_SPX$L_o <- OHLC_DAILY_P_IV_SPX$SPX.Open.x - OHLC_DAILY_P_IV_SPX$SPX.Low.x
OHLC_DAILY_P_IV_SPX$E_h_o <- OHLC_DAILY_P_IV_SPX$E_h - OHLC_DAILY_P_IV_SPX$SPX.Open.x
OHLC_DAILY_P_IV_SPX$E_l_o <- OHLC_DAILY_P_IV_SPX$SPX.Open.x - OHLC_DAILY_P_IV_SPX$SPX.Low.x
OHLC_DAILY_P_IV_SPX$E_R_hl <- OHLC_DAILY_P_IV_SPX$E_h - OHLC_DAILY_P_IV_SPX$E_l
OHLC_DAILY_P_IV_SPX$Diff_R <- OHLC_DAILY_P_IV_SPX$E_R_hl - OHLC_DAILY_P_IV_SPX$R_hl

Time series of [ Close - Open ] over time

# classic plot :
ggplot(OHLC_DAILY_P_IV_SPX, aes(x=Date, y=R_co)) +
    geom_point(col="blue", size = 1) +
    geom_line(data=OHLC_DAILY_P_IV_SPX, aes(x=Date, y=SPX.Open.y), col="yellow", size = 1)
## geom_path: Each group consists of only one observation. Do you need to
## adjust the group aesthetic?

Frequency distribution of R_o

hist(OHLC_DAILY_P_IV_SPX$R_co)

hist(OHLC_DAILY_P_IV_SPX$R_co,breaks = 50 , freq=TRUE)

Frequency distribution of Daily IV

hist(OHLC_DAILY_P_IV_SPX$SPX.Open.y,breaks = 50 , freq=TRUE)

Cumulative histogram of IV

hist( OHLC_DAILY_P_IV_SPX$SPX.Open.y ) -> h # do a histogram of y and assign its info to h

h$counts <- cumsum(h$counts) # replace the cell freq.s by cumulative freq.s
plot( h, labels = TRUE ) # plot a cumulative histogram of y

Cumulative histogram of IV v2
eom_point(size = 0.01)

Time series of range to expected range

ggplot(OHLC_DAILY_P_IV_SPX, aes(x=Date, OHLC_DAILY_P_IV_SPX$R_hl), col = "blue") +
    geom_point(size = 0.1) +
    geom_point(aes(x=Date, OHLC_DAILY_P_IV_SPX$E_R_hl), col = "red", size = 0.1)

Time series of realised to expcted range

ggplot(OHLC_DAILY_P_IV_SPX, aes(x=Date, OHLC_DAILY_P_IV_SPX$Diff_R), col = "blue") +
    geom_point(size = 0.1) +
    geom_point(aes(x=Date, OHLC_DAILY_P_IV_SPX$IV_d), col = "red", size = 0.1)

overlay plot #1

# p <- ggplot(OHLC_DAILY_P_IV_SPX, aes(x=Date, OHLC_DAILY_P_IV_SPX$R_hl)) +
# geom_point() +
# geom_line(OHLC_DAILY_P_IV_SPX, aes(x=Date, OHLC_DAILY_P_IV_SPX$IV_d))
# Create a simple secondary axis
# p + scale_y_continuous(sec.axis = sec_axis(~.+10))
p <- ggplot(OHLC_DAILY_P_IV_SPX, aes(x=Date, y=R_co)) +
    geom_point(col="blue", size = 0.1) +
    geom_point(data=OHLC_DAILY_P_IV_SPX, aes(x=Date, y=SPX.Open.y), col="red", size = 0.1)
p + scale_y_continuous(sec.axis = sec_axis(~./1000000))