require("readxl")# pour la fonction read_excel
## Loading required package: readxl
## Warning: package 'readxl' was built under R version 3.5.1
library(RCurl)# pour télécharger les données à partir d'URL. 
## Warning: package 'RCurl' was built under R version 3.5.1
## Loading required package: bitops
url2<-"http://mickael-clevenot.fr/wp-content/uploads/2017/09/don_fin_2018A.xlsx"
download.file(url2, destfile = "./data2.xlsx",mode = 'wb')
cac_3 <- read_excel("./data2.xlsx")#
cac_31<-as.data.frame(cac_3)#
head(cac_31)
##         Date     CAC    AXA  sano
## 1 2015-09-28 4357.05 21.275 84.44
## 2 2015-09-29 4343.73 21.080 83.09
## 3 2015-09-30 4455.29 21.635 84.89
## 4 2015-10-01 4426.54 21.535 84.61
## 5 2015-10-02 4458.88 21.620 84.98
## 6 2015-10-05 4616.90 22.500 87.62
# Dans R la gestion des dates ,c'est un peu compliqué
date<-as.Date(cac_31$Date)# On prend la colonne des dates et on explique à R qu'il s'agit de date.
#date
cac<-cac_31$CAC
axa<-cac_31$AXA
san<-cac_31$sano
R_cac1<-NULL # On crée les variables vides pour y placer le résultat des boucles ci-dessous. 
R_cac2<-NULL
R_axa1<-NULL
R_axa2<-NULL
R_san1<-NULL
R_san2<-NULL

n1<-length(cac)-1
for(i in 1:n1) R_cac1[i] <- ((cac[i+1]/cac[i])-1)*100 # On réalise une boucle afin de calculer le rendement du CAC.
for(i in 1:n1) R_cac2[i] <- log(cac[i+1]/cac[i])*100 
n2<-length(axa)-1
for(i in 1:n2) R_axa1[i] <- ((axa[i+1]/axa[i])-1)*100
for(i in 1:n2) R_axa2[i] <- log(axa[i+1]/axa[i])*100 
n3<-length(san)-1
for(i in 1:n3) R_san1[i] <- ((san[i+1]/san[i])-1)*100
for(i in 1:n3) R_san2[i] <- log(san[i+1]/san[i])*100  

# Calcus des alphas et des bêtas. 
lm1<-lm(R_san2~R_cac2)
summary(lm1)
## 
## Call:
## lm(formula = R_san2 ~ R_cac2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -7.1161 -0.5134 -0.0080  0.5198  4.5156 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.03569    0.03360  -1.062    0.288    
## R_cac2       0.81286    0.03330  24.411   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9339 on 771 degrees of freedom
## Multiple R-squared:  0.4359, Adjusted R-squared:  0.4352 
## F-statistic: 595.9 on 1 and 771 DF,  p-value: < 2.2e-16
leg1<-lm1$coefficients
lm2<-lm(R_axa2~R_cac2)
summary(lm2)
## 
## Call:
## lm(formula = R_axa2 ~ R_cac2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.9460  -0.5096   0.0045   0.5589   4.6169 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.02731    0.03967  -0.688    0.491    
## R_cac2       1.29329    0.03931  32.899   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.102 on 771 degrees of freedom
## Multiple R-squared:  0.584,  Adjusted R-squared:  0.5835 
## F-statistic:  1082 on 1 and 771 DF,  p-value: < 2.2e-16
class(leg1)
## [1] "numeric"
as.character(leg1)
## [1] "-0.035694126677719" "0.812858293807062"
# Simple Scatterplot

plot(R_san2, R_cac2, main="Sensibilité au CAC 40",
     xlab="rdt CAC40 ", ylab="rdt Sanofi", pch=8,cex=0.3,)
abline(lm1,col="blue")

text(-3,-5, paste("R_san2 =", round(lm1$coefficients[1],3),"+ R_cac2 *",round(lm1$coefficients[2],3)),col="blue",cex=0.6)
text(-3,-6,paste("R² ajusté",round(summary(lm1)$adj.r.squared,3)),col="blue",cex=0.6)
grid(NULL,NULL)

rdt<-cbind(R_cac1,R_cac2,R_axa1,R_axa2,R_san1,R_san2)# fonction de concatenation en colonne. 



head(rdt)# on vérifie si ça marche
##          R_cac1     R_cac2     R_axa1     R_axa2     R_san1     R_san2
## [1,] -0.3057114 -0.3061797 -0.9165687 -0.9207951 -1.5987684 -1.6116865
## [2,]  2.5682996  2.5358728  2.6328273  2.5987650  2.1663257  2.1431943
## [3,] -0.6453003 -0.6473914 -0.4622140 -0.4632855 -0.3298386 -0.3303838
## [4,]  0.7305932  0.7279373  0.3947063  0.3939294  0.4373006  0.4363472
## [5,]  3.5439393  3.4825871  4.0703053  3.9896497  3.1066133  3.0593348
## [6,]  0.9473889  0.9429294  1.0888889  1.0830032 -0.3652134 -0.3658820
date<-as.Date(cac_31$Date)# On prend la colonne des dates et on explique à R qu'il s'agit de date.
dato<-date[(2:774)]
plot(R_cac2~dato)



#Graphique complet avec beaucoup d'option à décortiquer.
plot(R_cac1~dato,xaxt="n",ylab="Rendements",xlab="Dates",type="l", panel.first = grid(),main="Rendements du CAC 40")

at1<-seq(ISOdate(2015,09,29), ISOdate(2018,09,21), by = "90 days")
axis.Date(side=1, ax,  at=at1,las=2,  format= "%b %y", panel.first = grid(),cex.axis = .5)

#Graphique 2  plus complet avec beaucoup d'option à décortiquer.
plot(R_cac1~dato,xaxt="n",ylab="Rendements",xlab="Dates",type="l", panel.first = grid(),main="Rendements du CAC 40",lines(R_axa2~dato,col="red"))

at1<-seq(ISOdate(2015,09,29), ISOdate(2018,09,21), by = "90 days")
axis.Date(side=1, ax,  at=at1,las=2,  format= "%b %y", panel.first = grid(),cex.axis = .5)

plot.ts(cbind(R_cac2,R_axa2,R_san2))

za<-cbind(R_cac2,R_axa2,R_san2)
plot.ts(za)

ts.plot(za,gpars= list(col=rainbow(3)))
grid(NULL,NULL)
legend(350, -7, legend=c("AXA ","SANOFI","CAC 40"),
       col=c("green", "blue","red"), lty=1:2, cex=0.8)

ts.plot(za,col=c("green","red","blue"))
grid(NULL,NULL)
legend(350, -7, legend=c("CAC 40 ","AXA","SANOFI"),
col=c("green","red","blue"), lty=1:2, cex=0.8)



#install.packages("devtools")
library(devtools)
## Warning: package 'devtools' was built under R version 3.5.1
## Warning: package 'usethis' was built under R version 3.5.1
#install_github("R-Finance/xtsExtra")
library(xts)
## Warning: package 'xts' was built under R version 3.5.1
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.5.1
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

library(xtsExtra)
## 
## Attaching package: 'xtsExtra'
## The following object is masked from 'package:xts':
## 
##     plot.xts
library(quantmod)
## Warning: package 'quantmod' was built under R version 3.5.1
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols( "^DJI" )
## '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.
## 
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
## 
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## [1] "DJI"
x <- DJI[,4]
plot(x)

head(x)
##            DJI.Close
## 2007-01-03  12474.52
## 2007-01-04  12480.69
## 2007-01-05  12398.01
## 2007-01-08  12423.49
## 2007-01-09  12416.60
## 2007-01-10  12442.16
getSymbols( "SNY" )
## [1] "SNY"
plot(SNY[,4])