R quantstrat: no transactions done despite all the signals

大兔子大兔子 提交于 2019-12-25 01:32:14

问题


Below is the reproducible code:

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)")

Output:

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)

Output: 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

However, inspecting the mktdata object, we find signal to make transactions 10 times:

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

It is interesting that the same err (no transactions/positions to chart) I get when running demo('bbands', ask=FALSE), as described in this link

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: 1 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: 1 stats graphics grDevices utils
datasets methods base

other attached packages: 1 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): 1 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


回答1:


It was a bug with version 0.14.5. It's been fixed in version 0.14.6.

Source: https://github.com/braverock/quantstrat/issues/88



来源:https://stackoverflow.com/questions/51072873/r-quantstrat-no-transactions-done-despite-all-the-signals

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!