tidyquant part 3

## Download financial data with tidyquant
## Perform various data manipulations with dplyr
## join and pivot datasets, CAPM computation
## Athanassios Stavrakoudis 
## 29 Oct 2021

# load required libraries
library(tidyquant)
library(tidyverse)

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

# 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") 
mm_close_FB


# compute daily returns of Nasdaq and FB, adjusted prices
# Capital asset pricing model

NDAQ <- tq_get("NDAQ",
               get  = "stock.prices",
               from = "2015-01-01",
               to   = Sys.Date()) 

Ra <- FB %>% 
  tq_transmute(select     = adjusted, 
               mutate_fun = periodReturn, 
               period     = "daily", 
               col_rename = "rFB")
Rb <- NDAQ  %>% 
  tq_transmute(select     = adjusted, 
               mutate_fun = periodReturn, 
               period     = "daily", 
               col_rename = "rNDAQ")

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

# Compute CAPM
RaRb %>% 
  tq_performance(Ra = rFB, Rb = rNDAQ, performance_fun = table.CAPM)

# transpose the table results
RaRb %>% 
  tq_performance(Ra = rFB, Rb = rNDAQ, performance_fun = table.CAPM) %>% 
  t()

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

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