-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinance_google.Rmd
122 lines (95 loc) · 3.18 KB
/
finance_google.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
title: "Stockmarket Google Finance"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Get and plot historical stokmarket data from Google Finance
As the yahoo finance no longer seems to work, an alternative using google data.
```{r}
# necessary libraries
library(readr)
library(ggplot2)
library(lubridate)
library(plotly)
```
## Function to get google finance data
Use the 'read_csv' function from 'readr'
```{r}
build_url <- function(sym, sy = 2005, sm = 1, sd = 1, ey, em, ed) {
stock_url <- paste0("http://finance.google.com/finance/historical",
"?q=", sym,
"&startdate=", paste(sm, sd, sy, sep = "+"),
"&enddate=", paste(em, ed, ey, sep = "+"),
"&output=csv")
return(stock_url)
}
google_stocks <- function(sym, current = TRUE, sy = 2005, sm = 1, sd = 1, ey, em, ed) {
# sy, sm, sd: start year, start month and start day
# ey, em, ed: end year, end month, and end day
# If TRUE, use the date as the enddate
if(current){
system_time <- as.character(Sys.time())
ey <- as.numeric(substr(system_time, start = 1, stop = 4))
em <- as.numeric(substr(system_time, start = 6, stop = 7))
ed <- as.numeric(substr(system_time, start = 9, stop = 10))
}
stockurl <- build_url(sym, sy, sm , sd , ey, em, ed)
require(readr)
query_result <- tryCatch(
read_csv(stockurl),
error = function(e) NULL
)
return(query_result)
}
```
## I want to read data from VW, Tesla, Daimler IBM and Apple
```{r, message=FALSE, warning=FALSE }
apple_data = google_stocks('AAPL', sy = 2010)
vw_data = google_stocks('SWX%3AVW', sy = 2010)
tesla_data = google_stocks('tsla', sy = 2010)
dai_data = google_stocks('DAI', sy = 2010)
ibm_data = google_stocks('ibm', sy = 2010)
```
## Label the data and combine them to one big data.frame
```{r}
apple_label <- rep("Apple", length(apple_data$Date))
apple_data$label <- apple_label
vw_label <- rep("VW", length(vw_data$Date))
vw_data$label <- vw_label
daimler_label <- rep("Daimler", length(dai_data$Date))
dai_data$label <- daimler_label
tesla_label <- rep("Tesla", length(tesla_data$Date))
tesla_data$label <- tesla_label
ibm_label <- rep("IBM", length(ibm_data$Date))
ibm_data$label <- ibm_label
shares_dac <- rbind(vw_data, dai_data, apple_data, ibm_data, tesla_data)
shares_dac$datum <- as.factor(shares_dac$Date)
shares_dax_order <- shares_dac[with(shares_dac, order(datum)),]
shares_dax_order$YM <- format(dmy(shares_dax_order$Date), "%Y-%m")
```
## Save the data for further processing
``` {r}
save(shares_dax_order, file="shares_dax_order.rda")
load("shares_dax_order.rda")
```
## And do the Plots
```{r stockmarket, echo=FALSE}
p <- ggplot(shares_dax_order, aes(YM, Close, fill=label))
p <- p + geom_boxplot()
p <- p + theme(axis.text.x = element_text(angle=45))
p <- p + theme(legend.position="bottom")
p <- p + scale_x_discrete(name ="Zeitraum")
p <- p + scale_y_discrete(name ="Schlusskurs")
p
```
## and save the plot as well
```{r}
ggsave("shares_order.png", width = 30, height = 20, units = "cm")
```
## Interactive graph
Using plotly
```{r}
ggplotly(p, tooltip = c("Stock"))
```