R/q_return_stats_full.R

Defines functions q_return_stats_full

q_return_stats_full <- function(ticker,benchmark,start_date,end_date){

  ticker_return <- q_return(ticker,
                            start_date = start_date,
                            end_date = end_date,
                            frequency = "monthly")



  bench_return <- q_return(benchmark,
                           start_date = start_date,
                           end_date = end_date,
                           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)



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

  drawdowns_ratio <- ticker_return %>%
    tq_performance(Ra = monthly_return,
                   performance_fun = table.DrawdownsRatio) %>%
    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,
                 downside_risk,
                 drawdowns_ratio,
                 information_ratio,
                 specific_risk) %>%
    kable() %>%
    group_rows("Annualized Return Statistics",1,3) %>%
    group_rows("CAPM",4,10) %>%
    group_rows("Downside Risk",11,21) %>%
    group_rows("Drawdown Ratios",22,28) %>%
    group_rows("Information Ratio",29,31) %>%
    group_rows("Specific Risk",32,34)

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


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