library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
#library(qmao)
#install.packages("qmao", repos="http://r-forge.r-project.org", type="source")

#devtools::install_github("gsee/qmao")

FINO <- new.env()

getSymbols(c('AAPL','MSFT','FB',"^IXIC","^GSPC"), env = FINO, src = "yahoo",from = as.Date("2014-11-28") ,to= as.Date("2019-11-27"))
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "AAPL"  "MSFT"  "FB"    "^IXIC" "^GSPC"
head(FINO$GSPC)
##            GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume
## 2014-11-28   2074.78   2075.76  2065.06    2067.56  2504640000
## 2014-12-01   2065.78   2065.78  2049.57    2053.44  4159010000
## 2014-12-02   2053.77   2068.77  2053.77    2066.55  3686650000
## 2014-12-03   2067.45   2076.28  2066.65    2074.33  3612680000
## 2014-12-04   2073.64   2077.34  2062.34    2071.92  3408340000
## 2014-12-05   2072.78   2079.47  2070.81    2075.37  3419620000
##            GSPC.Adjusted
## 2014-11-28       2067.56
## 2014-12-01       2053.44
## 2014-12-02       2066.55
## 2014-12-03       2074.33
## 2014-12-04       2071.92
## 2014-12-05       2075.37
sp_500<-FINO$GSPC[,6]
apl<-FINO$AAPL[,6]
msft<-FINO$MSFT[,6]
fb<-FINO$FB[,6]
nas<-FINO$IXIC[,6]

plot(sp_500)

plot(apl)

plot(msft)

plot(fb)

plot(nas)

class(sp_500)
## [1] "xts" "zoo"
r_sp500<-dailyReturn(sp_500)
plot(r_sp500)

r_apl<-dailyReturn(apl)
plot(r_apl)

r_msft<-dailyReturn(msft)
plot(r_msft)

r_fb<-dailyReturn(fb)
plot(r_fb)

r_nas<-dailyReturn(nas)
plot(r_nas)

lm1<-lm(r_apl~r_sp500)
summary(lm1)
## 
## Call:
## lm(formula = r_apl ~ r_sp500)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.069247 -0.005641 -0.000286  0.005495  0.072916 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.0003683  0.0003279   1.123    0.262    
## r_sp500     1.2412129  0.0384171  32.309   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.01162 on 1256 degrees of freedom
## Multiple R-squared:  0.4539, Adjusted R-squared:  0.4534 
## F-statistic:  1044 on 1 and 1256 DF,  p-value: < 2.2e-16
lm2<-lm(r_msft~r_sp500)
summary(lm2)
## 
## Call:
## lm(formula = r_msft ~ r_sp500)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.075601 -0.004638  0.000070  0.004267  0.100936 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.0006308  0.0002725   2.315   0.0208 *  
## r_sp500     1.3118909  0.0319197  41.100   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.009655 on 1256 degrees of freedom
## Multiple R-squared:  0.5735, Adjusted R-squared:  0.5732 
## F-statistic:  1689 on 1 and 1256 DF,  p-value: < 2.2e-16
lm3<-lm(r_apl~r_sp500)
summary(lm3)
## 
## Call:
## lm(formula = r_apl ~ r_sp500)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.069247 -0.005641 -0.000286  0.005495  0.072916 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.0003683  0.0003279   1.123    0.262    
## r_sp500     1.2412129  0.0384171  32.309   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.01162 on 1256 degrees of freedom
## Multiple R-squared:  0.4539, Adjusted R-squared:  0.4534 
## F-statistic:  1044 on 1 and 1256 DF,  p-value: < 2.2e-16
lm4<-lm(r_nas~r_sp500)
summary(lm4)
## 
## Call:
## lm(formula = r_nas ~ r_sp500)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0171607 -0.0018186  0.0001498  0.0019396  0.0127154 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1.010e-04  9.057e-05   1.115    0.265    
## r_sp500     1.142e+00  1.061e-02 107.641   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.003209 on 1256 degrees of freedom
## Multiple R-squared:  0.9022, Adjusted R-squared:  0.9021 
## F-statistic: 1.159e+04 on 1 and 1256 DF,  p-value: < 2.2e-16
plot(as.numeric(r_apl), as.numeric(r_sp500), main="Sensibilité au SP500",
     xlab="rdt SP500 ", ylab="rdt APPLe", pch=20,cex=0.08,col="red")
abline(lm1,col="black")

text(-0.05,0.04, paste("R_apl =", round(lm1$coefficients[1],3),"+ R_sp500 *",round(lm1$coefficients[2],3)),col="blue",cex=0.6)
text(-0.065,0.03,paste("R² ajusté",round(summary(lm1)$adj.r.squared,3)),col="blue",cex=0.6)
grid(NULL,NULL)

APl_100<-NULL
Nas_100<-NULL
FB_100<-NULL
SP_100<-NULL
MSFT_100<-NULL

n1<-length(sp_500)

#for(i in 2:n1) R_CAC[i-1] <- (CAC40[i]-CAC40[i-1])/CAC40[i-1]*100 #
for(i in 1:n1) SP_100[i] <- (as.numeric(sp_500[i])/as.numeric(sp_500[1]))*100 #

for(i in 1:n1) Nas_100[i] <- (as.numeric(nas[i])/as.numeric(nas[1]))*100 #
for(i in 1:n1) FB_100[i] <- (as.numeric(fb[i])/as.numeric(fb[1]))*100 #
for(i in 1:n1) MSFT_100[i] <- (as.numeric(msft[i])/as.numeric(msft[1]))*100 #
for(i in 1:n1) APl_100[i] <- (as.numeric(apl[i])/as.numeric(apl[1]))*100 #

plot.ts(MSFT_100,main="Base des 5 titres",ylab="base 100",xlab="Temps")
lines(SP_100,col="purple")
lines(Nas_100,col="red")
lines(FB_100,col="blue")
lines(APl_100,col="orange")

grid(NULL,NULL)