编辑:修复比想象的要容易,我刚刚初始化投资组合、账户和订单太晚了,因为我的数据开始得更早。以下使代码运行。我不会删除我的问题,因为我几乎没有找到任何个人期货的例子。
## 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,
name = "sigThreshold",
arguments = list(
threshold = 70,
column = "RSI",
relationship = "gt",
cross = TRUE
),
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,
name = 'ruleSignal',
arguments = list(
sigcol = "LongExit",
sigval = TRUE,
orderqty = "all",
ordertype = "market",
orderside = "long",
replace = FALSE
),
label = "longExit",
enabled = TRUE,
type = "exit"
)
## Apply the Strategy to the Portfolio ----
# Apply strategy to the portfolio
t1 <- Sys.time()
results <- applyStrategy(stratName, portfolios = portName, 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 包及其相关包的生态系统对我来说看起来超级强大,但是使用真实数据而不是另一个 yahoo-finance 示例开始使用它比我想象的要难,而且缺少小插曲或 stackoverflow 问题也无济于事,所以我想我会添加一个。