yahoo.read <- function(url){
dat <- read.table(url,header=TRUE,sep=",")
df <- dat[,c(1,5)]
df$Date <- as.Date(as.character(df$Date))
return(df)}
library(readxl)
AAPL <- read_excel("C:/Users/supre/Desktop/AAPL.xlsx")
View(AAPL)
TWTR <- read_excel("C:/Users/supre/Desktop/TWTR.xlsx")
View(AAPL)
MSFT <- read_excel("C:/Users/supre/Desktop/MSFT.xlsx")
View(AAPL)
aapl <- AAPL
twtr <- TWTR
msft <- MSFT
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
ggplot(aapl,aes(Date,Close)) +
geom_line(aes(color="aapl")) +
geom_line(data=twtr,aes(color="twtr")) + geom_line(data=msft,aes(color="msft"))+
labs(color="Legend") +
scale_colour_manual("", breaks = c("aapl", "twtr","msft"),
values = c("blue", "red","yellow")) +
ggtitle("Closing Stock Prices: Apple, Twitter & Microsoft") +
theme(plot.title = element_text(lineheight=.7, face="bold"))+ theme_solarized(light = FALSE)
yahoo1.read <- function(url){
dat <- read.table(url,header=TRUE,sep=",")
dm <- dat[,c(1,6)]
dm$Date <- as.Date(as.character(dm$Date))
return(dm)}
aapl <-AAPL
twtr <-TWTR
msft <-MSFT
ggplot(aapl,aes(Date,Volume)) +
geom_line(aes(color="aapl")) +
geom_line(data=twtr,aes(color="twtr")) + geom_line(data=msft,aes(color="msft"))+
labs(color="Legend") +
scale_colour_manual("", breaks = c("aapl", "twtr","msft"),
values = c("blue", "black","red")) +
ggtitle("Stock Volume Comparission: Apple, Twitter & Microsoft") +
theme(plot.title = element_text(lineheight=.7, face="bold"))
require(quantmod)
symbols <- c("AAPL", "TWTR","MSFT")
c<- getSymbols(symbols)
#define training set
startT <- "2016-01-01"
endT <- "2017-01-01"
rangeT <- paste(startT,"::",endT,sep ="")
tAAPL <- AAPL[,6][rangeT]
tTWTR <- TWTR[,6][rangeT]
tMSFT <- MSFT[,6][rangeT]
#define out of sample set
startO <- "2016-02-01"
endO <- "2017-12-01"
rangeO <- paste(startO,"::",endO,sep ="")
oAAPL <- AAPL[,6][rangeO]
oTWTR <- TWTR[,6][rangeO]
oMSFT <- MSFT[,6][rangeO]
#compute price differences on in-sample data
pdtAAPL <- diff(tAAPL)[-1]
pdtTWTR <- diff(tTWTR)[-1]
pdtMSFT <- diff(tMSFT)[-1]
#build the model
model <- lm(pdtAAPL ~ pdtTWTR - 1)
#extract the hedge ratio
hr <- as.numeric(model$coefficients[1])
spreadT <- tAAPL - hr * tTWTR
spreadT1 <- tAAPL - hr * tMSFT
spreadT2 <- tTWTR - hr * tMSFT
#compute statistics of the spread
meanT <- as.numeric(mean(spreadT,na.rm=TRUE))
sdT <- as.numeric(sd(spreadT,na.rm=TRUE))
upperThr <- meanT + 1 * sdT
lowerThr <- meanT - 1 * sdT
hist(spreadT, col = "blue", breaks = 100, main = "Spread Histogram (AAPL vs. TWTR)")
abline(v = meanT, col = "red", lwd = 2)
require(quantmod)
symbols <- c("AAPL", "TWTR","MSFT")
c<- getSymbols(symbols)
#define training set
startT <- "2016-01-01"
endT <- "2017-01-01"
rangeT <- paste(startT,"::",endT,sep ="")
tAAPL <- AAPL[,6][rangeT]
tTWTR <- TWTR[,6][rangeT]
tMSFT <- MSFT[,6][rangeT]
#define out of sample set
startO <- "2016-02-01"
endO <- "2017-12-01"
rangeO <- paste(startO,"::",endO,sep ="")
oAAPL <- AAPL[,6][rangeO]
oTWTR <- TWTR[,6][rangeO]
oMSFT <- MSFT[,6][rangeO]
#compute price differences on in-sample data
pdtAAPL <- diff(tAAPL)[-1]
pdtTWTR <- diff(tTWTR)[-1]
pdtMSFT <- diff(tMSFT)[-1]
#build the model
model <- lm(pdtAAPL ~ pdtMSFT - 1)
#extract the hedge ratio
hr <- as.numeric(model$coefficients[1])
spreadT <- tAAPL - hr * tMSFT
spreadT1 <- tAAPL - hr * tMSFT
spreadT2 <- tTWTR - hr * tMSFT
#compute statistics of the spread
meanT <- as.numeric(mean(spreadT,na.rm=TRUE))
sdT <- as.numeric(sd(spreadT,na.rm=TRUE))
upperThr <- meanT + 1 * sdT
lowerThr <- meanT - 1 * sdT
hist(spreadT, col = "blue", breaks = 100, main = "Spread Histogram (AAPL vs. MSFT)")
abline(v = meanT, col = "red", lwd = 2)
require(quantmod)
symbols <- c("AAPL", "TWTR","MSFT")
c<- getSymbols(symbols)
#define training set
startT <- "2016-01-01"
endT <- "2017-01-01"
rangeT <- paste(startT,"::",endT,sep ="")
tAAPL <- AAPL[,6][rangeT]
tTWTR <- TWTR[,6][rangeT]
tMSFT <- MSFT[,6][rangeT]
#define out of sample set
startO <- "2016-02-01"
endO <- "2017-12-01"
rangeO <- paste(startO,"::",endO,sep ="")
oAAPL <- AAPL[,6][rangeO]
oTWTR <- TWTR[,6][rangeO]
oMSFT <- MSFT[,6][rangeO]
#compute price differences on in-sample data
pdtAAPL <- diff(tAAPL)[-1]
pdtTWTR <- diff(tTWTR)[-1]
pdtMSFT <- diff(tMSFT)[-1]
#build the model
model <- lm(pdtTWTR ~ pdtMSFT - 1)
#extract the hedge ratio
hr <- as.numeric(model$coefficients[1])
spreadT <- tTWTR - hr * tMSFT
spreadT1 <- tAAPL - hr * tMSFT
spreadT2 <- tTWTR - hr * tMSFT
#compute statistics of the spread
meanT <- as.numeric(mean(spreadT,na.rm=TRUE))
sdT <- as.numeric(sd(spreadT,na.rm=TRUE))
upperThr <- meanT + 1 * sdT
lowerThr <- meanT - 1 * sdT
hist(spreadT, col = "blue", breaks = 100, main = "Spread Histogram (TWTR vs. MSFT)")
abline(v = meanT, col = "red", lwd = 2)