如何解决R quantstrat 期货示例 - “交易必须按顺序添加”
编辑:修复比想象的要容易,我刚刚初始化投资组合、账户和订单太晚了,因为我的数据开始得更早。以下使代码运行。我不会删除我的问题,因为我几乎找不到任何关于个人期货的例子。
## set up quantstrat environment ----
initDate <- '2010-01-01'
startDate <- '2010-01-01'
startEquity <- 10E6
endDate <- '2012-12-31'
我一直在寻找一个永远在单个期货上运行的 quantstrat 示例,不幸的是,我没有找到任何东西,尽管我浏览了包中的所有示例、演示等,Guy Yollin 的网站等。因此,我决定自己尝试一下。我使用 VX 期货,加载它们并尝试对它们运行简单的 RSI 策略。代码的许多部分是从不同的站点复制的,但它在 applyStrategy
之前一直有效。
library(FinancialInstrument)
library(quantstrat)
library(qmao)
library(quantmod)
## download sample data ----
dl_data <- T # change to T for downloading the data
if(dl_data){
getSymbols("VX",src='cfe',Months = 1:12,Years = 2011:2012)
to_store <- c(ls()[grepl('VX',ls())])
saveSymbols.days(Symbols = to_store,base_dir = 'data/VX')
}
## new blotter and strategy environments
.blotter <- new.env()
.strategy <- new.env()
## load VX futures ----
vx_names <- gsub(pattern = '.RData',replacement = '',list.files(path = 'data/VX/'))
currency('USD')
for(i in vx_names){
future(primary_id = i,currency = 'USD',multiplier = 1000,tick_size = 0.05,underlying_id = NULL)
}
getSymbols.FI(Symbols = vx_names,dir = 'data/VX',extension = "rda",from = '2010-12-01',to = '2013-12-31')
## set up quantstrat environment ----
initDate <- '2010-12-01'
startDate <- '2011-01-01'
startEquity <- 10E6
endDate <- '2012-12-31'
portName <- stratName <- acctName <- 'futures_example'
rm.strat(stratName)
# initialize portfolio,account and orders
initPortf(portName,symbols = vx_names,initDate = startDate)
initacct(acctName,portfolios = portName,initDate = startDate,initEq = startEquity)
initOrders(portfolio = portName,startDate = startDate)
# Initialize and store the strategy
strategy(stratName,store = TRUE)
# Define RSI Indicator
add.indicator(
strategy = stratName,name = "RSI",arguments = list(price = quote(Cl(mktdata)),maType = "EMA"),label = "RSI"
)
# The applyIndicators function allow to observe the indicators for a specific symbol while making progress in building the strategy
test <- applyIndicators(stratName,mktdata = OHLC(VX_G11))
tail(test,10)
# add signals
add.signal(
strategy = stratName,name = "sigThreshold",arguments = list(
threshold = 30,column = "RSI",relationship = "lt",cross = TRUE
),label = "LongEntry"
)
add.signal(
strategy = stratName,arguments = list(
threshold = 70,relationship = "gt",label = "LongExit"
)
# With applySignal(),it is possible to observe the signal columns as well as the remaining information of the mktdata object.
sig <- applySignals(stratName,mktdata)
# Add Strategy Rules
add.rule(
strategy = stratName,name = 'ruleSignal',arguments = list(
sigcol = "LongEntry",sigval = TRUE,orderqty = 100,ordertype = "market",orderside = "long",replace = FALSE
),label = "longEnter",enabled = TRUE,type = "enter"
)
add.rule(
strategy = stratName,arguments = list(
sigcol = "LongExit",orderqty = "all",label = "longExit",type = "exit"
)
## Apply the Strategy to the Portfolio ----
# Apply strategy to the portfolio
t1 <- Sys.time()
results <- applyStrategy(stratName,symbols = vx_names)
t2 <- Sys.time()
print(t2 - t1)
运行时报错
Error in addTxn(Portfolio = portfolio,Symbol = symbol,TxnDate = txntime,:
Transactions must be added in order. TxnDate (2010-12-30 01:00:00) is before last transaction in portfolio (2011-01-01) for VX_F11
在谷歌上搜索时,我发现了一个旧错误,它应该已经在我的吸墨纸版本中修复了,我怀疑它与期货有关,期货在某个时间或设置到期。任何提示为什么这不起作用?我如何告诉 quantstrat 期货仅在有限的时间内“存在”?
总的来说,quantstrat 包及其相关包的生态系统对我来说看起来非常强大,但是使用真实数据而不是另一个雅虎财经示例开始使用它比我想象的要难,而且缺少小插曲或 stackoverflow 问题并没有没用,所以我想我会添加一个。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。