从txt文件生成 xts 对象。它包含5行:Open,High,Low,Close,S_Base。 S_Base是二进制信号,当Low = Close时为TRUE(等于1)。考虑制作55行的最小可重复示例。它有4个TRUE信号。
下面的量子代码在信号为TRUE时输入long,在信号为FALSE时输入为flat。这导致系统在打开后关闭位置。
该系统产生4次往返。它工作得很好。但是 blotter 包函数perTradeStats
会出现以下错误:
Error in NextMethod(.Generic) :
dims [product 2] do not match the length of object [0]
如果我将相同的代码应用于通过从Google下载代码生成的xts(使用一些简单的SMA交叉作为信号),则没有错误。我无法弄清楚为什么。下面是最小可重现示例的代码。提前感谢您的帮助。
# Install Packages
#install.packages("devtools")
#require(devtools)
#install_github("braverock/FinancialInstrument")
#install_github("joshuaulrich/xts") #needed as latest version on repository is older
#install_github("braverock/blotter")
#install.packages("quantstrat", repos="http://R-Forge.R-project.org")
#install_github("braverock/PerformanceAnalytics")
# Libraries
library(quantstrat)
library(knitr)
# Data
DTT3 <- structure(c(126.33, 126.37, 126.42, 126.3, 126.31, 126.31, 126.31,126.37, 126.41, 126.42, 126.38, 126.33, 126.38, 126.38, 126.36, 126.39, 126.42, 126.42, 126.43, 126.43, 126.42, 126.39, 126.47, 126.56, 126.53, 126.5, 126.43, 126.45, 126.5, 126.42, 126.39, 126.39, 126.37, 126.24, 126.21, 126.15, 126.21, 126.24, 126.24, 126.25, 126.25, 126.2, 126.18, 126.21, 126.15, 126.16, 126.03, 125.94, 125.97, 125.97, 125.7, 125.73, 125.73, 125.55, 126.38, 126.47, 126.43, 126.35, 126.32, 126.37, 126.37, 126.43, 126.44, 126.43, 126.39, 126.39, 126.42, 126.38, 126.38, 126.44, 126.44, 126.44, 126.44, 126.44, 126.42, 126.5, 126.56, 126.57, 126.55, 126.52, 126.56, 126.5, 126.52, 126.44, 126.43, 126.42, 126.41, 126.29, 126.22, 126.29, 126.24, 126.3, 126.25, 126.27, 126.3, 126.2, 126.25, 126.21, 126.2, 126.17, 126.03, 126.04, 126.02, 125.97, 126.04, 125.89, 125.81, 125.6, 126.32, 126.36, 126.3, 126.25, 126.26, 126.29, 126.29, 126.36, 126.4, 126.37, 126.32, 126.28, 126.36, 126.34, 126.36, 126.39, 126.36, 126.37, 126.41, 126.41, 126.35, 126.39, 126.43, 126.52, 126.48, 126.42, 126.4, 126.41, 126.36, 126.34, 126.35, 126.3, 126.22, 126.2, 126.14, 126.15, 126.15, 126.22, 126.18, 126.23, 126.15, 126.14, 126.15, 126.15, 126.09, 125.91, 125.87, 125.94, 125.9, 125.7, 125.67, 125.63, 125.54, 125.35, 126.37, 126.42, 126.3, 126.32, 126.3, 126.32, 126.36, 126.41, 126.42, 126.38, 126.33, 126.39, 126.38, 126.36, 126.38, 126.42, 126.44, 126.42, 126.44, 126.41, 126.4, 126.47, 126.55, 126.53, 126.5, 126.43, 126.45, 126.49, 126.42, 126.39, 126.38, 126.36, 126.25, 126.22, 126.16, 126.21, 126.23, 126.24, 126.25, 126.26, 126.21, 126.18, 126.2, 126.15, 126.16, 126.02, 125.95, 125.98, 125.97, 125.7, 125.73, 125.72, 125.55, 125.38, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), index = structure(c(1403483400, 1403484300, 1403485200, 1403486100, 1403487000, 1403487900, 1403488800, 1403489700, 1403490600, 1403491500, 1403492400, 1403493300, 1403494200, 1403495100, 1403496000, 1403496900, 1403497800, 1403498700, 1403499600, 1403500500, 1403501400, 1403502300, 1403503200, 1403504100, 1403505000, 1403505900, 1403506800, 1403507700, 1403508600, 1403509500, 1403510400, 1403511300, 1403512200, 1403513100, 1403514000, 1403514900, 1403515800, 1403516700, 1403517600, 1403518500, 1403519400, 1403520300, 1403521200, 1403522100, 1403523000, 1403523900, 1403524800, 1403525700, 1403526600, 1403527500, 1403528400, 1403529300, 1403530200, 1403531100), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(54L, 5L), .Dimnames = list(NULL, c("Open", "High", "Low", "Close", "S_Base")))
# Basics
Sys.setenv(TZ = "UTC")
currency("USD")
initeq <- 30000
# Symbols
basic_symbols <- function() {symbols <- c("DTT3")}
symbols <- basic_symbols()
future(symbols, currency = "USD", multiplier = 1000)
# Strategy/Portfolio/Account
strategy.st <- "basicstrat"
portfolio.st <- "basicstrat"
account.st <- "basicstrat"
rm.strat(strategy.st)
rm.strat(portfolio.st)
initPortf(portfolio.st, symbols = symbols)
initAcct(account.st, portfolios = portfolio.st, initEq = initeq)
initOrders(portfolio.st, symbols = symbols)
strategy(strategy.st, store = TRUE)
# Enter Long Signal
add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "S_Base",
threshold = 0,
relationship = "gt",
cross = TRUE),
label = "longsig")
# Go Flat Signal
add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "S_Base",
threshold = 0,
relationship = "eq",
cross = TRUE),
label = "flatsig")
# Enter Long Rule
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longsig",
sigval = TRUE,
ordertype = "market",
prefer = "Open",
orderside = "long",
orderqty = 1,
replace = FALSE),
type = "enter")
# Go Flat Rule
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "flatsig",
sigval = TRUE,
ordertype = "market",
prefer = "Open",
orderside = "long",
orderqty = "all",
replace = TRUE),
type = "exit")
# Apply Strategy
applyStrategy(strategy.st, portfolio.st)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)
# Table: Summary Trade Stats
tstats <- tradeStats(portfolio.st)
kable(t(tstats))
### NO ERRORS ABOVE - FOLLOWING CODE PRODUCES THE ERROR/ERRORS ###
# Table: Per Trade Stats
for(symbol in symbols) {
pts <- perTradeStats(portfolio.st, Symbol = "DTT3")
kable(pts, booktabs = TRUE, caption = symbol)}
kable(pts)
#Chart: Maximum Adverse Excursion
for(symbol in symbols) {
chart.ME(Portfolio = portfolio.st, Symbol = symbol, type = "MAE",
scale = "percent")}