下面是可复制的代码:
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)")
输出:
chart.Posn(portfolio.st,Symbol = symbol,Dates =“ 2017-01-01 :: 2018-06-24”,: 没有交易/排名图表
R版本3.4.4(2018-03-15)平台:x86_64-pc-linux-gnu(64位) 运行于:Ubuntu 18.04 LTS
getTxns(portfolio.st, symbol)
输出: Txn数量Txn价格价格Txn费用Txn值Txn平均成本净值Txn实现的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)
时遇到了相同的错误(无交易/无图表位置),如本link
sessionInfo()
R版本3.4.4(2018-03-15)平台:x86_64-pc-linux-gnu(64位) 运行于:Ubuntu 18.04 LTS
Matrix产品:默认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
语言环境:1 LC_CTYPE = pt_BR.UTF-8 LC_NUMERIC = C
LC_TIME = pt_BR.UTF-8 [4] LC_COLLATE = zh_CN.UTF-8
LC_MONETARY = pt_BR.UTF-8 LC_MESSAGES = zh_CN.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附加的基本软件包:1统计信息图形grDevices utils
数据集方法基础其他附带的软件包: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通过名称空间(未附加)加载:1 quadprog_1.5-5
点阵_0.20-35 codetools_0.2-15 MASS_7.3-49 grid_3.4.4
[6] curl_3.2 boot_1.3-20迭代器_1.0.9工具_3.4.4
yaml_2.1.19 [11]编译器_3.4.4
答案 0 :(得分:0)
这是0.14.5版的错误。它已在版本0.14.6中修复。