if (FALSE) {
install_github("tmk-c/myrlib")
library(myrlib)
#### Load data from plus DB --------------------------------------------------
start.date <- "2011-01-01"
end.date <- "2011-12-31"
sma.lens <- seq(0, 250, 10)
sd.lens <- seq(10, 250, 10)
ato.lens <- seq(10, 250, 10)
ogc.lens <- seq(0, 250, 10)
# 3min for all 3years
columns <- c("open", "high", "low", "close", "adj.open", "roc.pc2to",
"sma", "sd", "avg.tover", "open.gap.coef")
symbols <- getActiveSymbols(settings$sharadar_plus.sqli, start.date, end.date)
data <- getSliceData(settings$sharadar_plus.sqli, columns, start.date,
end.date, symbols, TRUE, sma.lens, sd.lens,
ato.lens, ogc.lens)
#### Create data and single backtest -----------------------------------------
start_date <- "2009-01-01"
end_date <- "2009-12-31"
range <- paste(start_date, end_date, sep = "::")
# Long
l_params <- data.frame(
range = range,
sma_len = 0,
sd_len = 50,
ato_len = 200,
ogc_len = 0,
side = "Long",
sd_thres = 0.01,
ato_l_thres = 10000000,
ato_h_thres = Inf,
ogc_thres = 0,
stop_thres = 0.3,
min_thres = 10,
slippage = 0.001,
num_trades = 10,
lot = 10000,
stringsAsFactors = FALSE
)
# Short
s_params <- l_params
s_params$side <- "Short"
# Load data
path <- "/largedata/data_2009_sma0_sd50_ato200_ogc0.rds"
data <- readRDS(paste0(system.file(package = "myrlib"), path))
# Get data
# columns <- c("open", "high", "low", "close", "adj.open", "roc.pc2to",
# "sma", "sd", "avg.tover", "open.gap.coef")
# symbols <- getActiveSymbols(settings$sharadar.sqli, start_date, end_date)
# data <- getSliceData(settings$sharadar.sqli, columns, start_date, end_date,
# symbols, TRUE, l_params$sma_len, l_params$sd_len,
# l_params$ato_len, l_params$ogc_len)
# Save data
# saveRDS(data, paste0(system.file(package = "myrlib"), path))
# Long
l_result <- calcGapStrategy(l_params, data, output = "full")
l_result$plot
l_result$trans
l_result$equity_curve
# Short
s_result <- calcGapStrategy(s_params, data, output = "full")
s_result$plot
s_result$trans
s_result$equity_curve
# Combine result
trans <- rbindlist(list(l_result$trans, s_result$trans))
ec <- calcEquityCurveFromTrans(trans, data$adj.open)
perf <- calcPerformance(ec)
plot <- buildEquityCurvePlot(ec, perf)
plot
ec
### Parallel backtest --------------------------------------------------------
start_date = "2009-01-01"
end_date = "2009-12-31"
range = paste(start_date, end_date, sep = "::")
sma_lens = c(0, 10, 20)
sd_lens = c(50, 100)
ato_lens = c(100, 200)
ogc_lens = 0
side = "Long"
sd_thres = c(0.01, 0.02)
ato_l_thres = c(10000000, 20000000)
ato_h_thres = Inf
ogc_thres = 0
stop_thres = c(0.3, 0.5)
min_thres = c(10, 15)
slippage = c(0.001)
num_trades = c(10)
lot = c(10000)
# Get data
columns <- c("open", "high", "low", "close", "adj.open", "roc.pc2to",
"sma", "sd", "avg.tover", "open.gap.coef")
symbols <- getActiveSymbols(settings$sharadar.sqli, start_date, end_date)
data <- getSliceData(settings$sharadar.sqli, columns, start_date, end_date,
symbols, TRUE, sma_lens, sd_lens, ato_lens, ogc_lens)
# Save data
path <- "/largedata/data_2009_sma0_sd50_ato200_ogc0.rds"
saveRDS(data, paste0(system.file(package = "myrlib"), path))
data_param_set <- lapply(range, generateDataParams, sma_lens, sd_lens,
ato_lens, ogc_lens)
param_set <- do.call("rbind", lapply(data_param_set, generateStrategyParamSet,
side, sd_thres, ato_l_thres, ato_h_thres, ogc_thres, stop_thres, min_thres,
slippage, num_trades, lot))
# Params to backtests
param_set <- getParamSetDiff(settings$strategy.sqli, param_set)
calcGapStrategyAllParams(settings$sharadar.sqli, settings$strategy.sqli,
ranges, sma_lens, sd_lens, ato_lens, ogc_lens,
side, sd_thres, ato_l_thres, ato_h_thres, ogc_thres, stop_thres,
min_thres, slippage, num_trades, lot)
### Parameter sweep ----------------------------------------------------------
ranges = getAnnualRanges(2009, 2017)
sma_lens = 0 #seq(0, 250, 10)
sd_lens = seq(10, 250, 10)
ato_lens = 200 #seq(10, 250, 10)
ogc_lens = seq(0, 250, 10)
side = "Long"
sd_thres = seq(0, 0.05, 0.01)
ato_l_thres = c(10000000)
ato_h_thres = Inf
ogc_thres = 0 #seq(0, -0.01, -0.005)
stop_thres = seq(0.1, 1, 0.1)
min_thres = c(10)
slippage = c(0.001)
num_trades = c(10)
lot = c(10000)
# All params
data_param_set <- lapply(ranges, generateDataParams, sma_lens, sd_lens,
ato_lens, ogc_lens)
param_set <- do.call("rbind", lapply(data_param_set, generateStrategyParamSet,
side, sd_thres, ato_l_thres, ato_h_thres, ogc_thres, stop_thres, min_thres,
slippage, num_trades, lot))
# Params to backtests
param_set <- getParamSetDiff(settings$strategy.sqli, param_set)
# Estimate time
num_data <- length(data_param_set)
data_prep <- 160
num_cores <- 64
per_param <- 3
ttl_time <- (num_data * data_prep) +
((nrow(param_set) / num_cores) * per_param)
print(paste0(round(ttl_time / 60 / 60, 1), "H"))
s <- proc.time()
calcGapStrategyAllParams(settings$sharadar_plus.sqli, settings$strategy.sqli,
ranges, sma_lens, sd_lens, ato_lens, ogc_lens,
side, sd_thres, ato_l_thres, ato_h_thres, ogc_thres, stop_thres,
min_thres, slippage, num_trades, lot)
proc.time() - s
### Walk forward analysis ----------------------------------------------------
# Best params from DB
long_params <- getGapStrategyBestParamByScore(settings$strategy.sqli,
"Long", last_only = FALSE)
short_params <- getGapStrategyBestParamByScore(settings$strategy.sqli,
"Short", last_only = FALSE)
params <- rbind(long_params, short_params)
wfa_result <- calcGapStrategyWFA(params)
wfa_result$plot
wfa_result$trans
wfa_result$perf
wfa_result$params
### Daily setup --------------------------------------------------------------
# Variables
this_date <- "2018-11-28"
# Latest best params from DB
long_params <- getGapStrategyBestParamByScore(settings$strategy.sqli,
"Long", last_only = TRUE)
short_params <- getGapStrategyBestParamByScore(settings$strategy.sqli,
"Short", last_only = TRUE)
# Universe
info <- calcGapStrategyDailyUniverse(settings$sharadar.sqli,
this_date, long_params)
# Save results
file <- paste0("daily_universe_", Sys.Date(), ".csv")
path <- paste0("~/Dropbox/strat_dev/Gap_Strategy/Universe/", file)
write.csv(info, path, row.names = FALSE)
### Misc ---------------------------------------------------------------------
con <- DBI::dbConnect(RSQLite::SQLite(), settings$sharadar_plus.sqli)
src <- dplyr::tbl(con, "equity_prices")
tickers <- src %>%
dplyr::select(ticker) %>% dplyr::collect() %>% dplyr::pull() %>% unique()
DBI::dbDisconnect(con)
info <- getSymbolInfo(settings$sharadar_plus.sqli)
a <- getDailyOHLCV(settings$sharadar_plus.sqli, "A")
info[ticker == symbol]$last_price_date
info[ticker == symbol]$first_price_date
a <- fread(paste0("~/tmp/sma/", symbol, ".csv"))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.