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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.