#' Calculate Gap strategies' daily universe info
#'
#' @param dbcon_str Connection string for Sharadar or QuoteMedia.
#' @param this_date Date to retrieve.
#' @param parmas Best parameters from bakctest results for the day.
#'
#' @return Daily universe info by data.table format
#' @export
calcGapStrategyDailyUniverse <- function(dbcon_str, this_date, params,
filter = TRUE) {
# Abbreviation
p <- params
# Date values
this_date <- as.Date(this_date)
# Get data from DB
symbols <- getActiveSymbols(dbcon_str, this_date, this_date)
columns <- c("close", "sma", "sd", "avg.tover", "open.gap.coef")
data <- getSliceData(dbcon_str, columns, this_date, this_date,
symbols, TRUE, p$sma_len, p$sd_len,
p$ato_len, p$ogc_len)
# Symbol info
info <- getSymbolInfo(dbcon_str)[, c("ticker", "name", "exchange")]
data.table::setnames(info, "ticker", "symbol")
# Build columns --------------------------------------------------------------
# close
close <- data$close %>% t() %>% data.table::data.table(rownames(.), .)
data.table::setnames(close, c("symbol", "close"))
info <- info[close, on = "symbol"]
# SMA
if (!is.null(data$sma)) {
sma <- data$sma[[1]] %>% t() %>% data.table::data.table(rownames(.), .)
data.table::setnames(sma, c("symbol", "sma"))
info <- info[sma, on = "symbol"]
}
# SD
sd = data$sd[[1]] %>% t() %>% data.table::data.table(rownames(.), .)
data.table::setnames(sd, c("symbol", "sd"))
info <- info[sd, on = "symbol"]
# Average turnover
ato <- data$avg.tover[[1]] %>% t() %>% data.table::data.table(rownames(.), .)
data.table::setnames(ato, c("symbol", "ato"))
info <- info[ato, on = "symbol"]
# Open Gap Coef
if (!is.null(data$open.gap.coef)) {
ogc <- data$open.gap.coef[[1]] %>% t() %>%
data.table::data.table(rownames(.), .)
data.table::setnames(ogc, c("symbol", "ogc"))
info <- info[ogc, on = "symbol"]
}
# Symbol mapping from Sharadar to IQFeed -------------------------------------
path <- paste0(system.file(package = "myrlib"), "/mapping/sharadar_iqf.csv")
map <- read.csv(path)
for (i in 1:nrow(map)) {
s_symbol <- map[i, "sharadar"]
i_symbol <- map[i, "iqf"]
info[symbol == s_symbol]$symbol <- i_symbol
}
# Filter rows ----------------------------------------------------------------
if (filter) {
# SMA
if (p$sma_len != 0) {
info <- info[close >= sma]
}
# SD
info <- info[sd >= p$sd_thres]
# ATO
info <- info[p$ato_l_thres <= ato & ato <= p$ato_h_thres]
# OGC
if (p$ogc_len != 0) {
info <- info[ogc <= p$ogc_thres]
}
# Min entry
info <- info[close >= p$min_thres]
}
return (info)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.