#' Plot Chart with Trades and Trade Summary from a Backtest Parameter Screen
#'
#' @param screen_results list containing backtest parameter screen results
#' @param par1 screen parameter 1
#' @param par2 screen parameter 2
#'
#' @import xts
#' @importFrom magrittr %>% %<>% %$%
#' @return
#' @export
#'
#' @examples plot_trades(strategy_screen_results)
plot_trades <- function (screen_results,
par1 = 100,
par2 = 5) {
par1 %<>% as.character()
par2 %<>% as.character()
symbols <- screen_results[[par1]][[par2]]$returns %>%
colnames() %>% .[!.%in% "CORR"]
for (i in seq_along(symbols)) {
w <- screen_results[[par1]][[par2]]$weights[, symbols[i]]
ret <- screen_results[[par1]][[par2]]$returns[, symbols[i]]
pr <- stock_prices[[symbols[i]]] %>% quantmod::Cl() %>%
.[paste0(index(w)[1], "::")]
tstats <- tradr::calc_trade_stats(ret, w)
df <- cbind(pr, w) %>%
na.locf() %>%
`colnames<-`(c("pr", "w")) %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "Date") %>%
dplyr::mutate(Date = as.Date(Date)) %>%
dplyr::mutate(w = w*max(pr, na.rm = TRUE)/max(w, na.rm = TRUE)) %>%
dplyr::select(Date, pr, w) %>%
tidyr::gather(key = "variable", value = "value", -Date)
main_title <- paste0(
symbols[i], " \n",
tradr:::stock_universe %>% dplyr::filter(ISIN == symbols[i]) %$% Name)
print(df %>% ggplot2::ggplot(., ggplot2::aes(x = Date, y = value)) +
ggplot2::geom_line(aes(color = variable), size = 0.5) +
ggplot2::ggtitle(main_title) +
ggplot2::scale_color_manual(values = c("#00AFBB", "#E7B800")) +
ggplot2::theme_minimal() +
ggpmisc::annotate(geom = "table",
x = df$Date[length(df$Date)*0.01],
y = max(pr, na.rm = TRUE)*0.975,
label = list(tstats),
vjust = 1, hjust = 0)
)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.