tidyquant introduction

Παραδείγματα χρήσης της βιβλιοθήκης tidyquant και ανάλυσης χρηματοοικονομικών δεδομένων.

library(tidyquant)
library(ggplot2)
library(writexl)
library(dplyr)

# stock index
tq_exchange_options()
tq_exchange("NYSE")

# get options
tq_index_options()
tq_index("SP500")

# print more
tq_index("SP500") %>% print(n=50)

# view all
tq_index("SP500") %>% View

# download
sp500 <- tq_index("SP500")

# make some data exploration
sp500 %>% str()
sp500 %>% filter(symbol=='FB') 
sp500 %>% filter(sector=='Information Technology') %>% nrow()
sp500 %>% select(company, weight) 
sp500 %>% filter(symbol=='FB') %>% select(company, weight) 
sp500 %>% select(weight) %>% sum()
sp500 %>% group_by(sector) %>% summarise(cnt=n())
sp500 %>% group_by(sector) %>% summarise(cnt=n()) %>% filter(cnt>50)
sp500 %>% group_by(sector) %>% summarise(weight=sum(weight)) 

# plot weight by sector
w <- sp500 %>% group_by(sector) %>% summarise(weight=sum(weight))
ggplot(w, aes(x=sector, y=weight)) + 
  geom_bar(stat="identity", fill="blue") +
  theme_tq() +
  theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
  geom_text(aes(label=round(weight,2)), vjust=1.6, color="white", size=3.5)

# export data to excel
write_xlsx(w, "sector_weights.xlsx")

#get metal prices from oanda
gold <- tq_get("gold", get="metal.prices")
ggplot(gold, aes(x=date, y=price)) + geom_line() + 
  labs(title = "gold price") +
  theme_tq()

#get stock prices from yahoo
fb  <- tq_get("FB", get="stock.prices", from="2014-01-01", to="2018-01-01")

# FB line chart
fb %>%
  ggplot(aes(x = date, y = close)) +
  geom_line(color="blue") +
  labs(title = "Facebook", y = "Closing Price", x = "") + 
  theme_tq()

# 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()

# zoom
end <- as_date("2018-01-01")
fb %>%
  ggplot(aes(x = date, y = close)) +
  geom_barchart(aes(open = open, high = high, low = low, close = close),
                color_up = "darkgreen", color_down = "darkred", size = 1) +
  labs(title = "Facebbok") + 
  coord_x_date(xlim = c(end - weeks(4), end),
               ylim = c(150, 200)) + 
  theme_tq()

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

# 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") +
  xlim(-0.1, 0.1) +
  theme_tq()

# compute moving average
fb %>%
  tq_mutate(select = close, mutate_fun = SMA, n = 10) %>%
  print(n=15)
  
# Candlestick
fb %>%
  ggplot(aes(x = date, y = close)) +
  geom_candlestick(aes(open = open, high = high, low = low, close = close),
                   color_up = "darkgreen", color_down = "darkred", 
                   fill_up  = "darkgreen", fill_down  = "darkred") +
  labs(title = "AAPL Candlestick Chart", 
       subtitle = "Zoomed in, Experimenting with Formatting",
       y = "Closing Price", x = "") + 
  coord_x_date(xlim = c(end - weeks(4), end),
               ylim = c(150, 200)) + 
  theme_tq()

# compute and plot moving averages
x <- 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)
ggplot(x, aes(x=date)) +
  geom_line(aes(y=close), colour="black") +
  geom_line(aes(y=SMA_050), colour="red") +
  geom_line(aes(y=SMA_100), colour="blue") +
  theme_bw()

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

# min max per quarter
max_close_FB <- fb %>%
  tq_transmute(select     = close, 
               mutate_fun = apply.quarterly, 
               FUN        = max, 
               col_rename = "max_close") 
min_close_FB <- fb %>%
  tq_transmute(select     = close, 
               mutate_fun = apply.quarterly, 
               FUN        = min, 
               col_rename = "min_close") 

mm_close_FB <- inner_join(min_close_FB, max_close_FB, by="date") 

# compute daily returns
Ra <- fb %>% 
  tq_transmute(select     = adjusted, 
            mutate_fun = periodReturn, 
            period     = "daily", 
            col_rename = "Ra")

tq_index("RUSSELL2000", use_fallback = TRUE) %>% 
  tq_get(get = "stock.prices", from = "2018-01-01")

Rb <- "RUSSELL2000" %>% tq_get(get  = "stock.prices",
                      from = "2014-01-01",
                      to   = "2018-01-01")  %>% 
  tq_transmute(select     = adjusted, 
            mutate_fun = periodReturn, 
            period     = "daily", 
            col_rename = "Rb")

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))

RaRb %>%
  tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM) %>% 
  t()

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

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