R/q_return_stats_one.R

Defines functions q_return_stats_one

q_return_stats_one <- function(ticker,benchmark){

  # Returns Data -----------------------------------
  ticker_return <- q_return(ticker,
                            start_date = Sys.Date() - years(2),
                            frequency = "monthly")



  bench_return <- q_return(benchmark,
                           start_date = Sys.Date() - years(2),
                           frequency = "monthly") %>%
    select(date,bench_return = monthly_return)




  combined_returns <- left_join(ticker_return, bench_return, by = c("date" = "date"))




  annualized_returns <- ticker_return %>%
    tq_performance(Ra = monthly_return,
                   performance_fun = table.AnnualizedReturns) %>%
    gather(key,value) %>%
    left_join(performance_labels, by = "key") %>%
    select(label,value) %>%
    mutate(value = round(value,2)) %>%
    arrange(label)



  capm <- combined_returns %>%
    tq_performance(Ra = monthly_return,
                   Rb = bench_return,
                   performance_fun = table.CAPM) %>%
    gather(key,value) %>%
    slice(1:7) %>%
    left_join(performance_labels, by = "key") %>%
    select(label,value) %>%
    mutate(value = round(value,2)) %>%
    arrange(label)


  information_ratio <- combined_returns %>%
    tq_performance(Ra = monthly_return,
                   Rb = bench_return,
                   performance_fun = table.InformationRatio) %>%
    gather(key,value) %>%
    left_join(performance_labels, by = "key") %>%
    select(label,value) %>%
    mutate(value = round(value,2)) %>%
    arrange(label)

  specific_risk <- combined_returns %>%
    tq_performance(Ra = monthly_return,
                   Rb = bench_return,
                   performance_fun = table.SpecificRisk) %>%
    gather(key,value) %>%
    left_join(performance_labels, by = "key") %>%
    select(label,value) %>%
    mutate(value = round(value,2)) %>%
    arrange(label)

  x <- bind_rows(annualized_returns,
                 capm,
                 information_ratio,
                 specific_risk) %>%
    kable() %>%
    group_rows(index = c("Annualized Returns" = 3,
                         "CAPM" = 7,
                         "Information Ratio" = 3,
                         "Specific Risk" = 3))

  gsub("<thead>.*</thead>", "", x)

}
zac-garland/equityresearch documentation built on July 30, 2020, 2:29 p.m.