[英]What is causing this error message when creating a portfolio in Quantstrat for R?
I have been following this tutorial to learn about backtesting using Quantstrat.我一直在关注本教程以了解使用 Quantstrat 进行回测。
https://timtrice.github.io/backtesting-strategies/basic-strategy.html https://timtrice.github.io/backtesting-strategies/basic-strategy.html
I ran into trouble at the start of part 5. The only thing I have done differently that I can see is that I imported my OHLC data from a csv rather than from Yahoo directly.我在第 5 部分开始时遇到了麻烦。我能看到的唯一不同之处是我从 csv 导入了我的 OHLC 数据,而不是直接从 Yahoo 导入。 This is because I have been having trouble with Quantmod recently.
这是因为我最近在使用 Quantmod 时遇到了麻烦。 The csv was downloaded directly from Yahoo finance.
csv直接从雅虎财经下载。 A sample is at the very bottom of the page.
样本位于页面的最底部。
My code is below.我的代码如下。 I do not get any error messages until the start of block 2. At that point, I get the following error message.
在块 2 开始之前,我没有收到任何错误消息。此时,我收到以下错误消息。
Error in portfolio$symbols[[instrument]] <- new.env(hash = TRUE) :
wrong args for environment subassignment
I made sure the csv was imported properly, and it displays an xts with the expected dimensions.我确保 csv 已正确导入,并显示具有预期尺寸的 xts。 Beyond that, I am not sure where to go.
除此之外,我不确定 go 在哪里。 I tried searching for anyone with a similar error code, but I did not see an obvious cause for the problem.
我尝试搜索具有类似错误代码的任何人,但我没有看到问题的明显原因。
If anyone has any suggestions, I would appreciate it.如果有人有任何建议,我将不胜感激。
install.packages("devtools") # if not installed
install.packages("FinancialInstrument") #if not installed
install.packages("PerformanceAnalytics") #if not installed
install.packages('quantmod')
# next install blotter from GitHub
devtools::install_github("braverock/blotter")
# next install quantstrat from GitHub
devtools::install_github("braverock/quantstrat")
library(devtools)
library(FinancialInstrument)
library(PerformanceAnalytics)
library(quantstrat)
library(TTR)
library(blotter)
library(quantmod)
Sys.setenv(TZ = "UTC")
currency('USD')
init_date <- "2010-01-04"
start_date <- "2010-01-05"
end_date <- "2019-12-27"
init_equity <- 1e4 # $10,000
adjustment <- TRUE
symbols<-c(IEZ)
getSymbols.csv("IEZ",
env=globalenv(),
dir="C:\\Users\\NEW USER\\Downloads",
return.class = "xts",
extension="csv",index.class="POSIXct")
symbols<-IEZ
stock('IEZ',
currency = "USD",
multiplier = 1)
portfolio.st <- "Port.Luxor"
account.st <- "Acct.Luxor"
strategy.st <- "Strat.Luxor"
rm.strat(portfolio.st) # remove old strategies
rm.strat(account.st)
initPortf(name = portfolio.st,
symbols = symbols,
initDate = init_date)
initAcct(name = account.st,
portfolios = portfolio.st,
initDate = init_date,
initEq = init_equity)
initOrders(portfolio = portfolio.st,
symbols = IEZ,
initDate = init_date)
strategy(strategy.st, store = TRUE)
##Adding Indicators
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(C(mktdata)),
n = 30),
label = "nSlow")
#Adding Signals
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")
##Adding Rules
add.rule(strategy = strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "long",
sigval = TRUE,
orderqty = 100,
ordertype = "stoplimit",
orderside = "long",
threshold = 0.0005,
prefer = "High",
TxnFees = -10,
replace = FALSE),
type = "enter",
label = "EnterLONG")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "short",
sigval = TRUE,
orderqty = -100,
ordertype = "stoplimit",
threshold = -0.005,
orderside = "short",
replace = FALSE,
TxnFees = -10,
prefer = "Low"),
type = "enter",
label = "EnterSHORT")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "short",
sigval = TRUE,
orderside = "long",
ordertype = "market",
orderqty = "all",
TxnFees = -10,
replace = TRUE),
type = "exit",
label = "Exit2SHORT")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "long",
sigval = TRUE,
orderside = "short",
ordertype = "market",
orderqty = "all",
TxnFees = -10,
replace = TRUE),
type = "exit",
label = "Exit2LONG")
cwd <- getwd()
setwd("~/")
results_file <- paste("results", strategy.st, "RData", sep = ".")
if( file.exists(results_file) ) {
load(results_file)
} else {
results <- applyStrategy(strategy.st, portfolios = portfolio.st)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)
if(checkBlotterUpdate(portfolio.st, account.st, verbose = TRUE)) {
save(list = "results", file = results_file)
save.strategy(strategy.st)
}
}
setwd(cwd)
IEZ.Open IEZ.High IEZ.Low IEZ.Close IEZ.Volume IEZ.Adjusted
2010-01-04 43.84 44.61 43.55 44.61 38.23743 382700
2010-01-05 44.68 45.58 44.54 45.51 39.00885 202500
2010-01-06 45.47 46.59 45.40 46.52 39.87460 283100
2010-01-07 46.37 46.73 45.93 46.66 39.99459 238800
2010-01-08 46.45 47.71 46.45 47.56 40.76603 173800
2010-01-11 47.93 48.09 46.65 47.05 40.32888 232000
Not sure all that environment code is necessary.不确定所有环境代码都是必要的。
Replace this block替换此块
cwd <- getwd()
setwd("~/")
results_file <- paste("results", strategy.st, "RData", sep = ".")
if( file.exists(results_file) ) {
load(results_file)
} else {
results <- applyStrategy(strategy.st, portfolios = portfolio.st)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)
if(checkBlotterUpdate(portfolio.st, account.st, verbose = TRUE)) {
save(list = "results", file = results_file)
save.strategy(strategy.st)
}
}
setwd(cwd)
With this:有了这个:
applyStrategy(strategy.st, portfolios = portfolio.st)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)
And the strategy should run as expected.并且该策略应该按预期运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.