setwd("/Users/CHATKEAWPAISAL/Desktop/4 term 2/EE435/assignment2_Thunyaporn")
cat(rep("\n",50))  #clear R Console
install.packages("quantmod")  
install.packages("fBasics") 
install.packages("sn")  
install.packages("PerformanceAnalytics") 
install.packages("car") 
install.packages("tseries")  
install.packages("forecast")
library(quantmod)
library(fBasics)
library(sn)
library(PerformanceAnalytics)
library(car)
library(tseries)
library(forecast)
getSymbols("CAT", from="2000-01-03",to="2021-01-31")
dim(CAT)
head(CAT)
tail(CAT)
da_CAT= CAT
chartSeries(CAT,theme="white") 
price_CAT=da_CAT[,6]
plot(price_CAT,type='l')
logprice_CAT=log(price_CAT)
plot(logprice_CAT,type='l')
logreturn_CAT=diff(log(price_CAT))
simplereturn_CAT <-exp(logreturn_CAT)-1

getSymbols("AOT.BK", from="2000-01-03",to="2020-01-31")
dim(AOT.BK)
head(AOT.BK)
tail(AOT.BK)
da_AOT= AOT.BK
chartSeries(AOT.BK,theme="white") 
price_AOT=da_AOT[,6]
plot(price_AOT,type='l')
logprice_AOT=log(price_AOT)
plot(logprice_AOT,type='l')
logreturn_AOT=diff((log(price_AOT)))
simplereturn_AOT <-exp(logreturn_AOT)-1 

#1a)
par(mfrow=c(2,1))
plot(logreturn_CAT, type='l')
plot(simplereturn_CAT,type='l')

par(mfrow=c(2,1))
plot(logreturn_AOT, type='l')
plot(simplereturn_AOT,type='l')

#1b)
table.Stats(simplereturn_CAT)
table.Stats(simplereturn_AOT)

#1c)
newlogreturn_CAT <- logreturn_CAT[2:nrow(logreturn_CAT)]
newsimplereturn_CAT <- simplereturn_CAT[2:nrow(logreturn_CAT)]

par(mfrow=c(1,1))
hist(simplereturn_CAT, breaks = 100, col="slateblue")
chart.Histogram(simplereturn_CAT, methods = c("add.normal"))

par(mfrow=c(1,1))
qqnorm((newlogreturn_CAT))
qqline(newlogreturn_CAT, col = 2)
jarque.bera.test(newlogreturn_CAT)

newlogreturn_AOT <- logreturn_AOT[2:nrow(logreturn_AOT)]
newsimplereturn_AOT <- simplereturn_AOT[2:nrow(logreturn_AOT)]

par(mfrow=c(1,1))
hist(simplereturn_AOT, breaks = 100, col="slateblue")
chart.Histogram(simplereturn_AOT, methods = c("add.normal"))

par(mfrow=c(1,1))
qqnorm((newlogreturn_AOT))
qqline(newlogreturn_AOT, col = 2)
jarque.bera.test(newlogreturn_AOT)

#1d)
table.Stats(logreturn_CAT)
table.Stats(logreturn_AOT)

table.Stats(newlogreturn_CAT)
table.Stats(newlogreturn_AOT)

#1e) g)
t.test(newlogreturn_CAT)
t.test(newlogreturn_AOT)

#1f)
par(mfrow=c(1,1))
hist(logreturn_CAT, breaks = 100, col="slateblue")
chart.Histogram(logreturn_CAT, methods = c("add.normal"))

par(mfrow=c(1,1))
hist(logreturn_AOT, breaks = 100, col="slateblue")
chart.Histogram(logreturn_AOT, methods = c("add.normal"))

#1h)
T_CAT=length(newlogreturn_CAT)
S_CAT= skewness(newlogreturn_CAT)
tstS_CAT = S_CAT/sqrt(6/T)
tstS_CAT

pvS_CAT = 2*(pnorm(tstS_CAT))
pvS_CAT

T_AOT=length(newlogreturn_AOT)
S_AOT= skewness(newlogreturn_AOT)
tstS_AOT = S_AOT/sqrt(6/T)
tstS_AOT

pvS_AOT = 2*(1-pnorm(tstS_AOT))
pvS_AOT

#1i)
k_CAT = kurtosis(newlogreturn_CAT)
tstK_CAT = k_CAT/sqrt(24/T)
tstK_CAT

pv_CAT = 2*(1-pnorm(tstK_CAT))
pv_CAT

k_AOT = kurtosis(newlogreturn_AOT)
tstK_AOT = k_AOT/sqrt(24/T)
tstK_AOT

pv_AOT = 2*(1-pnorm(tstK_AOT))
pv_AOT