R quantstrat:尽管有所有信号,但仍未完成交易

R quantstrat: no transactions done despite all the signals

下面是可重现的代码:

library(quantstrat)

start_date <- as.Date("2017-02-02")
end_date <- as.Date("2018-06-24")
init_date <- as.Date("2017-01-01")
init_equity <- "50000"
adjustment <- TRUE
symbol <- "PETR4.SA"


getSymbols(symbol, src = "yahoo",from = start_date, to=end_date, adjust = adjustment)


portfolio.st <- "basic_port"
account.st <- "basic_account"
strategy.st <- "basic_strategy"


rm.strat(portfolio.st)
rm.strat(account.st)

stock(symbol, currency = currency("BRL"), multiplier = 1)
initPortf(name = portfolio.st, symbols = symbol, initDate = init_date)
initAcct(name = account.st, portfolios = portfolio.st, initDate = init_date, initEq =init_equity)
initOrders(portfolio.st, symbol, init_date)
strategy(strategy.st, store = TRUE)

add.indicator(strategy = strategy.st, name = "SMA", arguments = list(x = quote(Cl(mktdata)), n=10), label ="nFast")
add.indicator(strategy = strategy.st, name = "SMA", arguments = list(x = quote(Cl(mktdata)), n=30), label = "nSlow")

add.signal(strategy = strategy.st, name= "sigCrossover", arguments =  list(columns = c("nFast", "nSlow"), relationship = "gte"), label = "long")
add.signal(strategy = strategy.st, name= "sigCrossover", arguments =  list(columns = c("nFast", "nSlow"), relationship = "lt"), label = "short")

#Add rules for entering positions
#enter long position
add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "long",
                          sigval = TRUE,
                          orderqty = 100,
                          ordertype = "stoplimit",
                          orderside = "long",
                          threshold = "0.0005",
                          prefer = "High",
                          TxnFees = -.8,
                          replace = FALSE),
         type = "enter",
         label = "EnterLong")

#enter short position
add.rule(strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "short",
                          sigval = TRUE,
                          orderqty = -100,
                          ordertype = "stoplimit",
                          threshold = -0.005, 
                          orderside = "short", 
                          replace = FALSE, 
                          TxnFees = -.8, 
                          prefer = "Low"),
         type = "enter",
         label = "EnterShort")

#Add rules for exiting opened postions
#exit long positions
add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "short", 
                          sigval = TRUE, 
                          orderside = "long", 
                          ordertype = "market", 
                          orderqty = "all", 
                          TxnFees = -.8, 
                          replace = TRUE), 
         type = "exit", 
         label = "Exit2SHORT")

#exit short positions
add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "long", 
                          sigval = TRUE, 
                          orderside = "short", 
                          ordertype = "market", 
                          orderqty = "all", 
                          TxnFees = -.8, 
                          replace = TRUE), 
         type = "exit", 
         label = "Exit2LONG")

#Apply strategy
applyStrategy(strategy.st, portfolios = portfolio.st,debug = TRUE)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)

#Chart
chart.Posn(portfolio.st, Symbol = symbol, Dates="2017-01-01::2018-06-24", 
           TA="add_SMA(n = 10, col = 2); add_SMA(n = 30, col = 4)")

输出:

Error in chart.Posn(portfolio.st, Symbol = symbol, Dates = "2017-01-01::2018-06-24", : no transactions/positions to chart

R version 3.4.4 (2018-03-15) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 18.04 LTS

getTxns(portfolio.st, symbol)

输出: Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL 2016-12-31 21:00:00 0 0 0 0 0 0

然而,检查 mktdata 对象,我们发现进行 10 次交易的信号:

mktdata[mktdata$long == 1 | mktdata$short==1]

有趣的是,我在 运行 demo('bbands', ask=FALSE) 时得到了相同的错误(没有 transactions/positions 图表),如

中所述
sessionInfo()

R version 3.4.4 (2018-03-15) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 18.04 LTS

Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale: LC_CTYPE=pt_BR.UTF-8 LC_NUMERIC=C
LC_TIME=pt_BR.UTF-8 [4] LC_COLLATE=en_US.UTF-8
LC_MONETARY=pt_BR.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=pt_BR.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C

attached base packages: stats graphics grDevices utils
datasets methods base

other attached packages: quantstrat_0.14.5
foreach_1.4.4 blotter_0.14.2 [4] PerformanceAnalytics_1.5.2.2 FinancialInstrument_1.3.0
quantmod_0.4-13 [7] TTR_0.23-3
xts_0.10-2 zoo_1.8-2

loaded via a namespace (and not attached): quadprog_1.5-5
lattice_0.20-35 codetools_0.2-15 MASS_7.3-49 grid_3.4.4
[6] curl_3.2 boot_1.3-20 iterators_1.0.9 tools_3.4.4
yaml_2.1.19 [11] compiler_3.4.4

这是 0.14.5 版的错误。它已在 0.14.6 版本中修复。

来源:https://github.com/braverock/quantstrat/issues/88