tidyquant part 2

## Download financial data with tidyquant
## Perform various data manipulations with dplyr
## plot data with ggplot
## Athanassios Stavrakoudis 
## 29 Oct 2021

library(tidyquant)
library(tidyverse)

# get daily data for a stock, here FB
tq_get("FB")

# define from / to dates
tq_get("FB", from = "2015-01-01", to = Sys.Date())

# store in a variable for further use
FB <- tq_get("FB", from = "2015-01-01", to = Sys.Date())

# FB line chart of closing value
FB %>%
  ggplot(aes(x = date, y = close)) +
  geom_line(color = "blue") +
  labs(title = "Facebook", x = "", y = "Closing Price") + 
  theme_tq(base_size = 15, base_family = "NimbusSan") 

# FB bar chart
FB %>%
  ggplot(aes(x = date, y = close)) +
  geom_barchart(aes(open = open, high = high, low = low, close = close)) +
  labs(title = "Facebook", y = "Closing Price", x = "Date") + 
  theme_tq(base_size = 15, base_family = "NimbusSan") 

# last 10 values 
FB %>%
  tail(10) %>% 
  ggplot(aes(x = date, y = close)) +
  geom_barchart(aes(open = open, high = high, low = low, close = close)) +
  labs(title = "Facebook", y = "Closing Price", x = "Date") + 
  theme_tq(base_size = 15, base_family = "NimbusSan") 

# candlesticks
FB %>%
  ggplot(aes(x = date)) +
  geom_candlestick(aes(open = open, close = close, high = high, low = low)) +
  labs(title = "Facebbok") +
  theme_tq(base_size = 15, base_family = "NimbusSan") 

# candlesticks, last 4 weeks
FB %>% 
  filter(date >= max(date) - weeks(4)) %>% 
  ggplot(aes(x = date)) +
  geom_candlestick(aes(open = open, close = close, high = high, low = low)) +
  labs(title = "Facebbok") +
  theme_tq(base_size = 15, base_family = "NimbusSan") 

# compute daily returns
FB %>% 
  tq_mutate(select     = close, 
            mutate_fun = periodReturn, 
            period     = "daily", 
            col_rename = "return_d")

# see all options
tq_mutate_fun_options() 

# see help
?quantmod::periodReturn

# continuous returns
FB %>% 
  mutate(return_d = log( close / lag(close) ) )

# discrete returns
FB %>% 
  mutate(return_d = (close - lag(close)) / close)



# histogram of daily returns
FB %>% 
  tq_mutate(select     = close, 
            mutate_fun = periodReturn, 
            period     = "daily", 
            col_rename = "return_d") %>%
  ggplot(aes(x = return_d)) +
  geom_histogram(binwidth = 0.01, color = "darkblue", fill = "lightblue") +
  theme_tq(base_size = 15, base_family = "NimbusSan") 

# frequency polygon of daily returns
FB %>% 
  tq_mutate(select     = close, 
            mutate_fun = periodReturn, 
            period     = "daily", 
            col_rename = "return_d") %>%
  ggplot(aes(x = return_d)) +
  geom_freqpoly(binwidth = 0.01, color = "darkblue") +
  theme_tq(base_size = 15, base_family = "NimbusSan") 


# histogram + frequency polygon of daily returns
FB %>% 
  tq_mutate(select     = close, 
            mutate_fun = periodReturn, 
            period     = "daily", 
            col_rename = "return_d") %>%
  ggplot(aes(x = return_d)) +
  geom_histogram(binwidth = 0.01, color = NA, fill = "lightblue") +
  geom_freqpoly(binwidth = 0.01, color = "darkblue") +
  theme_tq(base_size = 15, base_family = "NimbusSan") 


# compute moving average
FB %>%
  tq_mutate(select = close, mutate_fun = SMA, n = 5) 

# compute and plot moving averages at 50 and 100 data pontis
FB2 <- FB %>%
  tq_mutate(select = close, mutate_fun = SMA, n = 050) %>%
  rename(SMA_050 = SMA) %>%
  tq_mutate(select = close, mutate_fun = SMA, n = 100) %>%
  rename(SMA_100 = SMA)

# plot moving averages 
ggplot(FB2, aes(x = date)) +
  geom_line(aes(y = close, colour = "close"), size = 1) +
  geom_line(aes(y = SMA_050, colour = "SMA_050"), size = 1.2) +
  geom_line(aes(y = SMA_100, colour = "SMA_100"), size = 1.2) +
  labs(colour = "", y = "close / SMA") +
  theme_tq(base_size = 15, base_family = "NimbusSan") 


# daily to monthly
FB %>%
  tq_transmute(select     = close, 
               mutate_fun = to.period, 
               period     = "months")

# check with this
FB %>% filter(date == '2015-01-30')

# monthly averages of close price
FB %>%
  tq_transmute(select     = close, 
               mutate_fun = apply.monthly, 
               FUN        = mean, 
               col_rename = "mean_close") 

# non quand, classic approach
FB %>% 
  mutate(date = floor_date(date, unit = "months")) %>% 
  group_by(date) %>% 
  summarise(close = mean(close))

Συνδεθείτε για περισσότερες δυνατότητες αλληλεπίδρασης,
σχολιασμοί, εξωτερικοί σύνδεσμοι, βοήθεια, ψηφοφορίες, αρχεία, κτλ.

Creative Commons License
Εκπαιδευτικό υλικό από τον Αθανάσιο Σταυρακούδη σας παρέχετε κάτω από την άδεια Creative Commons Attribution-NonCommercial-ShareAlike 4.0 License.
Σας παρακαλώ να ενημερωθείτε για κάποιους επιπλέον περιορισμούς
http://stavrakoudis.econ.uoi.gr/stavrakoudis/?iid=401.