library(rtsdata)
library(readxl)
library(ggplot2)
library(dplyr)
library(kableExtra)
library(formattable)
library(ggfortify)
library(highcharter)
library(quantmod)
library(viridisLite)
library(forecast)
library(highcharter)
library(reticulate)
purple <- c("#2C2C54")
pink <- c("#A40E4C")
blue <- c("#2E86AB")
yellow <- c("#FF9C00")
lila <- c("#E3DFFF")
brown <- c("#C3979F")
grey <- c("#BFBFBF")
white <- c("#FFFFFF")
colfunc <- colorRampPalette(c(purple,pink,blue))
Ordres <- read_excel("/Users/theotimebourgeois/Documents/Documents/Comptes.xlsx", sheet = "Mes ordres", range = cell_cols("A:F"))
Ordres$Date <- as.Date(as.POSIXct(Ordres$Date))
Ordres$BuyValuation <- Ordres$Prix*Ordres$Quantité
Ordres$BuyValuation <- (Ordres$BuyValuation + Ordres$BuyValuation*Ordres$Frais) %>% round(2)
Index <- c("^GSPC","^FCHI")
Historical_index <- matrix(nrow=0,ncol=7) %>%
as.data.frame() %>%
`colnames<-`(c("Open","High","Low","Close","Volume","Adjusted","Ticker"))
for (t in 1:length(Index)){
Historical_data_temp <-
ds.getSymbol.yahoo(Index[t],from = "1995-01-01") %>%
as.data.frame() %>%
`colnames<-`(c("Open","High","Low","Close","Volume","Adjusted"))
Historical_data_temp$Ticker <- Index[t]
Historical_index <- rbind(Historical_index,Historical_data_temp)
}
Historical_index$Date <- Historical_index %>% rownames() %>% as.Date()
ggplot(Historical_index, aes(x = Date, y = Close, col = Ticker))+
geom_line()+
scale_color_manual(values=colfunc(3))+
geom_hline(yintercept = 0, col = grey)+
theme(text=element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.position = "right",
axis.line = element_line(colour = purple))+
labs(title="Evolution of the main stock market indices",
subtitle=paste0("From ",Historical_index$Date %>% min() %>% format("%Y")," until today"),
y="Value", x="Date")
Historical_index <- matrix(nrow=0,ncol=7) %>%
as.data.frame() %>%
`colnames<-`(c("Open","High","Low","Close","Volume","Adjusted","Ticker"))
for (t in 1:length(Index)){
Historical_data_temp <-
ds.getSymbol.yahoo(Index[t],from = "2021-10-01") %>%
as.data.frame() %>%
`colnames<-`(c("Open","High","Low","Close","Volume","Adjusted"))
Historical_data_temp$Ticker <- Index[t]
Historical_data_temp$Percentage <- Historical_data_temp$Close - (Historical_data_temp$Close %>% min())
Historical_data_temp$Percentage <- 100*Historical_data_temp$Percentage/(Historical_data_temp$Percentage %>% max())
Historical_index <- rbind(Historical_index,Historical_data_temp)
}
Historical_index$Date <- Historical_index %>% rownames() %>% as.Date()
ggplot(Historical_index, aes(x = Date, y = Percentage, col = Ticker))+
geom_line()+
scale_color_manual(values=colfunc(3))+
theme(text=element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.position = "right",
axis.line = element_line(colour = purple))+
labs(title="Evolution of the main stock market indices",
subtitle=paste0("From ",Historical_index$Date %>% min() %>% format("%Y")," until today"),
y="Value", x="Date")
options("getSymbols.warning4.0"=FALSE)
options("getSymbols.yahoo.warning"=FALSE)
MyPortfolio <- Ordres$Ticker %>% unique() %>%
as.data.frame() %>%
`colnames<-`(c("Ticker"))
for (t in 1:nrow(MyPortfolio)){
MyPortfolio$Date[t] <-
Ordres$Date[MyPortfolio$Ticker[t]==Ordres$Ticker] %>% min()
MyPortfolio$Quantité[t] <-
Ordres$Quantité[MyPortfolio$Ticker[t]==Ordres$Ticker] %>% sum()
MyPortfolio$BuyValuation[t] <-
Ordres$BuyValuation[MyPortfolio$Ticker[t]==Ordres$Ticker] %>% sum()
}
MyPortfolio$Date <- MyPortfolio$Date %>% as.Date()
Historical_data <- matrix(nrow=0,ncol=7) %>%
as.data.frame() %>%
`colnames<-`(c("Open","High","Low","Close","Volume","Adjusted","Ticker"))
for (t in 1:nrow(MyPortfolio)){
Historical_data_temp <-
ds.getSymbol.yahoo(MyPortfolio$Ticker[t],
from = MyPortfolio$Date[t],
to = Sys.Date()) %>%
as.data.frame() %>%
`colnames<-`(c("Open","High","Low","Close","Volume","Adjusted"))
Historical_data_temp$Ticker <- MyPortfolio$Ticker[t]
Historical_data <- rbind(Historical_data,Historical_data_temp)
}
Historical_data$Date <- Historical_data %>% rownames() %>% as.Date()
for (t in 1:nrow(MyPortfolio)){
Historical_data_temp <-
Historical_data[MyPortfolio$Ticker[t]==Historical_data$Ticker,]
MyPortfolio$LastClose[t] <-
Historical_data_temp$Close[Historical_data_temp$Date==Historical_data_temp$Date %>% max()]
}
MyPortfolio$Valuation <- MyPortfolio$Quantité*MyPortfolio$LastClose
MyPortfolio$Result <- MyPortfolio$Valuation-MyPortfolio$BuyValuation
ResultTotal <- MyPortfolio$Result %>% sum()
write.csv(MyPortfolio,"/Users/theotimebourgeois/Documents/Documents/MyPortfolio.csv", row.names = FALSE)
MyPortfolio %>%
mutate(Result= color_tile(pink, purple)(Result)) %>%
kable(escape = F, align = c("l", "c", "c", "c", "c", "c", "c")) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = T) %>%
column_spec(1, bold = T) %>%
column_spec(7, bold = T, color = white)
| Ticker | Date | Quantité | BuyValuation | LastClose | Valuation | Result |
|---|---|---|---|---|---|---|
| BX4.PA | 2021-11-30 | 1250 | 1714.60 | 1.2302 | 1537.75 | -176.85 |
| RMS.PA | 2022-01-12 | 1 | 1390.54 | 1202.0000 | 1202.00 | -188.54 |
| TFI.PA | 2022-01-20 | 100 | 859.32 | 8.9850 | 898.50 | 39.18 |
for (t in 1:nrow(Ordres)){
Ordres$Action[t] <- Ordres$Quantité[t] * if (Ordres$Sens[t]=="Vente") {-1} else {1}
Historical_data$Action[Ordres$Date[t]==Historical_data$Date & Ordres$Ticker[t]==Historical_data$Ticker] <- Ordres$Action[t]
Historical_data$Valuation[Ordres$Date[t]==Historical_data$Date & Ordres$Ticker[t]==Historical_data$Ticker] <- Ordres$Prix[t]
Historical_data$BuyValuation[Ordres$Date[t]==Historical_data$Date & Ordres$Ticker[t]==Historical_data$Ticker] <- Ordres$BuyValuation[t]
}
Historical_data$Action[is.na(Historical_data$Action)] <- 0
Historical_data$BuyValuation[is.na(Historical_data$BuyValuation)] <- 0
Historical_data$Action2[1] <- Historical_data$Action[1]
Historical_data$BuyValuation2[1] <- Historical_data$BuyValuation[1]
for (t in 2:nrow(Historical_data)){
Historical_data$Action2[t] <-
if(Historical_data$Ticker[t]!=Historical_data$Ticker[t-1]){
Historical_data$Action[t]
}else{
if (Historical_data$Action[t]==0){
Historical_data$Action2[t-1]
} else {
Historical_data$Action2[t-1]+Historical_data$Action[t]
}}}
for (t in 2:nrow(Historical_data)){
Historical_data$BuyValuation2[t] <-
if(Historical_data$Ticker[t]!=Historical_data$Ticker[t-1]){
Historical_data$BuyValuation[t]
}else{
if (Historical_data$BuyValuation[t]==0){
Historical_data$BuyValuation2[t-1]
} else {
Historical_data$BuyValuation2[t-1]+Historical_data$BuyValuation[t]
}}}
for (t in 1:nrow(Historical_data)){
Historical_data$ActualPrice[t] <-
if(is.na(Historical_data$Valuation[t])){
Historical_data$Close[t]
}else{
Historical_data$Valuation[t]
}}
Historical_data$ActualValuation <- Historical_data$ActualPrice*Historical_data$Action2
Historical_data$Result <- Historical_data$ActualValuation - Historical_data$BuyValuation2
ggplot(Historical_data, aes(x = Date, y = Result, col = Ticker))+
geom_line()+
scale_color_manual(values=colfunc(3))+
geom_hline(yintercept = 0, col = grey)+
theme(text=element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.position = "right",
axis.line = element_line(colour = purple))+
labs(title="Earnings per share over time",
subtitle="From the end of 2021 until today",
y="Result", x="Date")
Historical_data$Percentage <- 100*Historical_data$Result/Historical_data$BuyValuation2
ggplot(Historical_data, aes(x = Date, y = Percentage, col = Ticker))+
geom_line()+
scale_color_manual(values=colfunc(3))+
geom_hline(yintercept = 0, col = grey)+
theme(text=element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.position = "right",
axis.line = element_line(colour = purple))+
labs(title="Earnings per share in percentage over time",
subtitle="From the end of 2021 until today",
y="Percentage", x="Date")
Historical_summary <- Historical_data %>%
group_by(Date) %>%
summarise(Result = sum(Result), BuyValuation = sum(BuyValuation2)) %>%
transform(Percentage = 100*Result / BuyValuation)
ggplot(Historical_summary, aes(x = Date, y = Result))+
geom_area(fill = purple)+
geom_hline(yintercept = 0, col = grey)+
theme(text=element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.position = "right",
axis.line = element_line(colour = purple))+
labs(title="Result of my portfolio over time",
subtitle="From the end of 2021 until today",
y="Result", x="Date")
Ticker <- MyPortfolio$Ticker[2]
MainTicker <- getSymbols(Ticker, auto.assign=FALSE, from="2020-01-01", to=Sys.Date())
cols <- colfunc(3)
highchart(type="stock") %>%
hc_add_series(MainTicker) %>%
hc_add_series(SMA(na.omit(Cl(MainTicker)),n=50),name="SMA(50)") %>%
hc_add_series(SMA(na.omit(Cl(MainTicker)),n=200),name="SMA(200)") %>%
hc_title(text=paste0("<b>",Ticker," Price Candle Stick Chart 2021-2022</b>")) %>%
hc_colors(cols)
MainTicker <- getSymbols(Ticker, auto.assign=FALSE, from="2010-01-01", to=Sys.Date())
# Number of period we want to forecast
n <- 360*5
# Splitting the data
train <- head(Cl(MainTicker), length(Cl(MainTicker))-n)
test <- tail(Cl(MainTicker), n)
fc_na <- naive(train, h=n)
autoplot(fc_na, col = purple)+
autolayer(ts(test, start=length(train)), series = "Test Data", color = pink)+
theme(text=element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.position = "right",
axis.line = element_line(colour = purple))+
labs(title="Forecast from naive method",
subtitle="From the end of 2021 until today",
y="Value", x="Date")
# Create the Model
model_s <- auto.arima(train)
# Forecast n periods of the data
fc_s <- forecast(model_s, h=n)
# Plot the result
autoplot(fc_s, col = purple)+
autolayer(ts(test, start= length(train)), series="Test Data", color = pink)+
theme(text=element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.position = "right",
axis.line = element_line(colour = purple))+
labs(title="Forecast from ARIMA method",
subtitle="From the end of 2021 until today",
y="Value", x="Date")
d.arima <- auto.arima(train)
d.forecast <- forecast(d.arima, level = c(75), h = 30)
autoplot(d.forecast, col = purple, predict.colour = pink)+
theme(text=element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.position = "right",
axis.line = element_line(colour = purple))+
labs(title="Forecast from ARIMA method",
subtitle="From the end of 2021 until today",
y="Value", x="Date")
#I import a picture and I set the size and the float
knitr::include_graphics("PhotoThéotime.svg")
Analysis conducted by Théotime Bourgeois
Master of Science - Data Science & Organizational Behavior
by Burgundy School of Business
# from yahoo_fin.stock_info import get_data
# import yahoo_fin.stock_info as si
# import numpy as np
# import yfinance as yf
# from yahoofinancials import YahooFinancials
# from yahoo_fin.stock_info import *
#
# MyPortfolio = r.MyPortfolio
#
# for ticker in MyPortfolio.Ticker.unique():
# print(ticker)
#
#
# ticker = yf.Ticker('TFI.PA')
# Reco = ticker.recommendations
#
# Reco = ticker.recommendations
# Reco['Ticker'] = 'AAPL'