Uploaded by Zz z

9007 code Clear all the memories

advertisement
#Clear all the memories
rm(list=ls(all=TRUE))
#change the working directory to that of the current file
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#load packages
library(moments)
library(tseries)
library(bizdays)
par(mfrow = c(2, 3))
# read the raw data of underlying
SPX_Emini <- read.csv("./sampleData/spxEminiEx20200619.csv", header=T,as.is = T)
SPX_Emini$TradeDate=as.Date(SPX_Emini$TradeDate, format = "%d/%m/%Y")
plot(SPX_Emini$TradeDate,SPX_Emini$X19.06.2020,col="red", type="l", xlab = "Trade
Date", ylab = "Settlement Price", main = "SPX E-mini futures daily settlement price during
years")
SPX_Option_Ex0619
<-
read.csv("./sampleData/spxOptionDataEx20200619.csv",
header=T,as.is = T)
SPX_Option_Ex0619$TradeDate <- trimws(SPX_Option_Ex0619$TradeDate)
SPX_Option_Ex0619$TradeDate=as.Date(SPX_Option_Ex0619$TradeDate,
"%Y%m%d")
format
=
plot(x = SPX_Option_Ex0619$TradeDate, y = SPX_Option_Ex0619$price, col = "red", type
= "l", xlab = "Trade Date", ylab = "Price", main = " SPX Option Daily Average Price During
Years")
SPY_Option_Ex0619
<-
read.csv("./sampleData/spyOptionDataEx20200619.csv",
header=T,as.is = T)
SPY_Option_Ex0619$TradeDate <- trimws(SPY_Option_Ex0619$TradeDate)
SPY_Option_Ex0619$TradeDate=as.Date(SPY_Option_Ex0619$TradeDate,
format
=
"%Y%m%d")
plot(x = SPY_Option_Ex0619$TradeDate, y = SPY_Option_Ex0619$price, col = "red", type
= "l", xlab = "Trade Date", ylab = "Price", main = " SPX Option Daily Average Price During
Years ")
1.SPX 2.SPY 3.Difference 4.Percentage
#Clear all the memories
rm(list=ls(all=TRUE))
#change the working directory to that of the current file
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#load packages
library(moments)
library(tseries)
library(bizdays)
par(mfrow = c(2, 2))
# read the raw data of underlying
underlying <- read.csv("./sampleData/underlyingSampleData.csv", header=T,as.is = T)
# sample size
N<-length(underlying$SPX)
underlying$TradeDate=as.Date(underlying$TradeDate, format = "%Y-%m-%d")
# calculate simple and log returns of SPX
underlying$logRetSPX=c(NA,log(underlying$SPX[2:N]/underlying$SPX[1:N-1]))
underlying$logRetSPY=c(NA,log(underlying$SPY[2:N]/underlying$SPY[1:N-1]))
underlying=na.omit(underlying)
plot(underlying$TradeDate,underlying$SPX,col="black", type="l", xlab = "Year", ylab =
"SPX", main = "SPX during years")
plot(underlying$TradeDate,underlying$SPY*10,col="black", type="l", xlab = "Year", ylab =
"SPY", main = "SPY during years")
underlying$diff=underlying$SPX-underlying$SPY*10
plot(underlying$TradeDate,underlying$diff,col="red", type="l", xlab = "Year", ylab =
"Difference", main = "Difference on SPX vs SPY during years")
underlying$diffP=((underlying$SPX/(underlying$SPY*10))-1)
plot(underlying$TradeDate,underlying$diffP,col="red", type="l", xlab = "Year", ylab =
"Difference in percentage", main = "Difference on SPX vs SPY in percentage during years")
两条核密度曲线
rm(list=ls(all=TRUE))
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
library(moments)
library(gridExtra)
library(grid)
underlying <- read.csv("./sampleData/underlyingSampleData.csv", header=T, as.is = T)
N <- length(underlying$SPX)
underlying$TradeDate <- as.Date(underlying$TradeDate, format = "%Y-%m-%d")
underlying$logRetSPX <- c(NA, log(underlying$SPX[2:N]/underlying$SPX[1:N-1]))
underlying$logRetSPY <- c(NA, log(underlying$SPY[2:N]/underlying$SPY[1:N-1]))
underlying <- na.omit(underlying)
d <- density(underlying$logRetSPX)
d1 <- density(underlying$logRetSPY)
xAxis <- seq(-0.15, 0.1, by=0.0001)
plot(d, xlim = c(-0.05, 0.05), xlab = "logReturn", ylab = "Density", main = "SPX density of
logReturn")
lines(xAxis, dnorm(xAxis, mean(underlying$logRetSPX),
sqrt(var(underlying$logRetSPX))), col="green")
xAxis <- seq(-0.15, 0.1, by=0.0001)
plot(d1, xlim = c(-0.05, 0.05), xlab = "logReturn", ylab = "Density", main = "SPY density of
logReturn")
lines(xAxis, dnorm(xAxis, mean(underlying$logRetSPY),
sqrt(var(underlying$logRetSPY))), col="blue")
jarque.bera.test(underlying$logRetSPX)
jarque.bera.test(underlying$logRetSPY)
收益率及统计数据
rm(list=ls(all=TRUE))
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
library(moments)
library(gridExtra)
library(grid)
underlying <- read.csv("./sampleData/underlyingSampleData.csv", header=T, as.is = T)
N <- length(underlying$SPX)
underlying$TradeDate <- as.Date(underlying$TradeDate, format = "%Y-%m-%d")
underlying$logRetSPX <- c(NA, log(underlying$SPX[2:N]/underlying$SPX[1:N-1]))
underlying$logRetSPY <- c(NA, log(underlying$SPY[2:N]/underlying$SPY[1:N-1]))
underlying <- na.omit(underlying)
d <- density(underlying$logRetSPX)
d1 <- density(underlying$logRetSPY)
moments_df <- data.frame(
Measure = c("Mean", "Variance", "Standard Deviation", "Skewness", "Kurtosis"),
SPX = c(
mean(underlying$logRetSPX)*252,
var(underlying$logRetSPX),
sqrt(var(underlying$logRetSPX)*252),
skewness(underlying$logRetSPX),
kurtosis(underlying$logRetSPX)
),
SPY = c(
mean(underlying$logRetSPY)*252,
var(underlying$logRetSPY),
sqrt(var(underlying$logRetSPY)*252),
skewness(underlying$logRetSPY),
kurtosis(underlying$logRetSPY)
)
)
table_grob <- tableGrob(moments_df)
gpar_params <- gpar(fill = "transparent", col = "black")
table_grob$theme$fill$gpar <- gpar_params
table_grob$theme$g$gpar <- gpar_params
grid.newpage()
grid.draw(table_grob)
rm(list=ls(all=TRUE))
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
library(moments)
library(gridExtra)
library(grid)
underlying <- read.csv("./sampleData/underlyingSampleData.csv", header=T, as.is = T)
underlying$expiryDate<-underlying$TradeDate
###################################################################
# read the S&P 500 Emini cross section data on 20200110
spEminiCross=read.csv("./sampleData/spxEminiTrade20200110.csv", header=T,as.is =
T,check.names=FALSE)
spEminiCross$TradeDate=as.Date(spEminiCross$TradeDate, format = "%d/%m/%Y")
spotIndex=underlying[underlying$TradeDate==spEminiCross$TradeDate,'SPX']
deliveryDate=as.Date(colnames(spEminiCross)[2:length(colnames(spEminiCross))],
format = "%d/%m/%Y")
# calculate business days
business_calendar <- create.calendar('my_calendar', weekdays = c('saturday','sunday'))
ttm=bizdays(spEminiCross$TradeDate, deliveryDate, cal = business_calendar)/252
# cost of carry
log(spEminiCross[,2:length(colnames(spEminiCross))]/spotIndex)/ttm
###################################################################
# read the S&P 500 Emini time series data
spEminiTS=read.csv("./sampleData/spxEminiEx20200619.csv",
header=T,as.is
=
T,check.names=FALSE)
spEminiTS$TradeDate=as.Date(spEminiTS$TradeDate, format = "%d/%m/%Y")
# basis
spEminiTS=merge(x=spEminiTS,y=underlying[c('TradeDate','SPX')],by="TradeDate",all.x
=TRUE)
spEminiTS$Basis=spEminiTS$`19/06/2020`-spEminiTS$SPX
spEminiTS$BasisP=spEminiTS$SPX/spEminiTS$`19/06/2020`-1
plot(spEminiTS$TradeDate,spEminiTS$Basis, type="l")
plot(spEminiTS$TradeDate,spEminiTS$BasisP, type="l")
###################################################################
# read the SPX cross section data on 20200110
spxOptionCross=read.csv("./sampleData/spxOptionData20200110.csv", header=T,as.is =
T,check.names=FALSE)
spxOptionCross$TradeDate <- trimws(spxOptionCross$TradeDate)
spxOptionCross$TradeDate=as.Date(spxOptionCross$TradeDate, format = "%Y%m%d")
spxOptionCross$expiryDate <- trimws(spxOptionCross$expiryDate)
spxOptionCross$expiryDate=as.Date(spxOptionCross$expiryDate, format = "%Y%m%d")
# basis
spxOptionCross=merge(x=spxOptionCross,y=underlying[c('expiryDate','SPX')],by="expiry
Date",all.x=TRUE)
spxOptionCross$Basis=spxOptionCross$`strike`-spxOptionCross$SPX
spxOptionCross$BasisP=spxOptionCross$SPX/spxOptionCross$`strike`-1
plot(spxOptionCross$strike,spxOptionCross$Basis, type="l")
plot(spxOptionCross$strike,spxOptionCross$BasisP, type="l")
###################################################################
# read the SPX Panel data on 20200110
spxOptionPD=read.csv("./sampleData/spxOptionDataEx20200619.csv", header=T,as.is =
T,check.names=FALSE)
spxOptionPD$TradeDate <- trimws(spxOptionPD$TradeDate)
spxOptionPD$TradeDate=as.Date(spxOptionPD$TradeDate, format = "%Y%m%d")
spxOptionPD$expiryDate <- trimws(spxOptionPD$expiryDate)
spxOptionPD$expiryDate=as.Date(spxOptionPD$expiryDate, format = "%Y%m%d")
summary(spxOptionPD)
# basis
spxOptionPD=merge(x=spxOptionPD,y=underlying[c('TradeDate','SPX')],by="TradeDate"
,all.x=TRUE)
spxOptionPD$Basis=spxOptionPD$`strike`-spxOptionPD$SPX
spxOptionPD$BasisP=spxOptionPD$SPX/spxOptionPD$`strike`-1
plot(spxOptionPD$strike,spxOptionPD$Basis, type="l")
plot(spxOptionPD$strike,spxOptionPD$BasisP, type="l")
Download