我一直在关注本教程,以了解有关使用Quantstrat进行回测的信息。
https://timtrice.github.io/backtesting-strategies/basic-strategy.html
在第5部分开始时遇到麻烦。我能做的唯一不同的事情是,我从csv而不是直接从Yahoo导入了OHLC数据。这是因为我最近在使用Quantmod时遇到了麻烦。 CSV是直接从Yahoo Finance下载的。示例位于页面的最底部。
我的代码如下。在第2块开始之前,我没有收到任何错误消息。这时,我收到以下错误消息。
Error in portfolio$symbols[[instrument]] <- new.env(hash = TRUE) :
wrong args for environment subassignment
我确保正确导入了csv,并且显示了具有预期尺寸的xts。除此之外,我不确定要去哪里。我尝试搜索具有类似错误代码的任何人,但没有发现问题的明显原因。
如果有人有任何建议,我将不胜感激。
第1块
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)
第2块
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,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,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,arguments = list(sigcol = "short",orderqty = -100,threshold = -0.005,orderside = "short",replace = FALSE,prefer = "Low"),label = "EnterSHORT")
add.rule(strategy.st,ordertype = "market",orderqty = "all",replace = TRUE),type = "exit",label = "Exit2SHORT")
add.rule(strategy.st,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)
CSV样本
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