r - 如何使用 apply.paramset 将符号列表添加为参数集?
问题描述
我得到了使用 apply.paramset 的建议,它期望参数集由 add.distribution 定义,从文档的外观来看,它与指标和规则一起使用。所以......如果我想并行化符号集。我该怎么做?这是我现在的代码
查看 if(FALSE) 阻止我卡在哪里
library(quantstrat)
library(future.apply)
library(scorecard)
autoregressor1 = function(x){
if(NROW(x)<12){ result = NA} else{
y = ROC(Ad(x))
y = na.omit(y)
step1 = ar.yw(y)
step2 = predict(step1,newdata=y,n.ahead=1)
step3 = step2$pred[1]+1
step4 = (step3*last(Ad(x))) - last(Ad(x))
result = step4
}
return(result)
}
autoregressor = function(x){
ans = rollapply(x,26,FUN = autoregressor1,by.column=FALSE)
return (ans)}
reset_quantstrat <- function() {
if (! exists(".strategy")) .strategy <<- new.env(parent = .GlobalEnv)
if (! exists(".blotter")) .blotter <<- new.env(parent = .GlobalEnv)
if (! exists(".audit")) .audit <<- new.env(parent = .GlobalEnv)
suppressWarnings(rm(list = ls(.strategy), pos = .strategy))
suppressWarnings(rm(list = ls(.blotter), pos = .blotter))
suppressWarnings(rm(list = ls(.audit), pos = .audit))
FinancialInstrument::currency("USD")
}
reset_quantstrat()
initDate <- '2010-01-01'
startDate <- '2011-01-01'
endDate <- '2019-08-10'
symbolstring1 <- c('^SP500TR','GOOG')
getSymbols(symbolstring1,from=startDate,to=endDate,adjust=TRUE,src='yahoo')
symbolstring1 <- c('SP500TR','GOOG')
.orderqty <- 1
.txnfees <- 0
reset_quantstrat()
search()
i=1
#here I'd like to use symbolstring as a subset in apply.paramset
symbolstring=as.character(symbolstring1[i])
try(rm.strat(strategyName),silent=TRUE)
try(rm(envir=FinancialInstrument:::.instrument),silent=TRUE)
for (name in ls(FinancialInstrument:::.instrument)){rm_instruments(name,keep.currencies = FALSE)}
currency('USD')
stock(symbolstring,currency='USD',multiplier=1)
init_equity <- 50000
Sys.setenv(TZ="UTC")
portfolioName <- accountName <- strategyName <- paste0("FirstPortfolio",i)
initPortf(name = portfolioName,
symbols = symbolstring,
initDate = initDate)
initAcct(name = accountName,
portfolios = portfolioName,
initDate = initDate,
initEq = init_equity)
initOrders(portfolio = portfolioName,
symbols = symbolstring,
initDate = initDate)
strategy(strategyName, store = TRUE)
add.indicator(
strategy = strategyName,
name = "autoregressor",
arguments = list(
x = quote(mktdata)),
label = "arspread")
add.signal(
strategy = strategyName,
name = "sigThreshold",
arguments = list(
threshold = 0.25,
column = "arspread",
relationship = "gte",
cross = TRUE),
label = "Selltime")
add.signal(
strategy = strategyName,
name = "sigThreshold",
arguments = list(
threshold = 0.1,
column = "arspread",
relationship = "lt",
cross = TRUE),
label = "cashtime")
add.signal(
strategy = strategyName,
name = "sigThreshold",
arguments = list(
threshold = -0.1,
column = "arspread",
relationship = "gt",
cross = TRUE),
label = "cashtime")
add.signal(
strategy = strategyName,
name = "sigThreshold",
arguments = list(
threshold = -0.25,
column = "arspread",
relationship = "lte",
cross = TRUE),
label = "Buytime")
#Entry Rule Long
add.rule(strategyName,
name = "ruleSignal",
arguments = list(
sigcol = "Buytime",
sigval = TRUE,
orderqty = .orderqty,
ordertype = "market",
orderside = "long",
pricemethod = "market",
replace = TRUE,
TxnFees = -.txnfees
#,
#osFUN = osMaxPos
),
type = "enter",
path.dep = TRUE,
label = "Entry")
add.rule(strategyName,
name = "ruleSignal",
arguments = list(
sigcol = "Selltime",
sigval = TRUE,
orderqty = .orderqty,
ordertype = "market",
orderside = "short",
pricemethod = "market",
replace = TRUE,
TxnFees = -.txnfees
#,
#osFUN = osMaxPos
),
type = "enter",
path.dep = TRUE,
label = "Entry")
summary(getStrategy(strategyName))
if(FALSE)
{
add.distribution(strategyName,
paramset.label = "forestopt", #The label we will use when we want to run this optimisation in paramset
component.type = "symbol", # The custom function is of indicator type (not other alternatives including signal or rule)
component.label = "symbolstring", #this is the name of your custom function
variable = list(symbolstring = symbolstring1),
label = "myForestOptLabel") #choose whatever you want
resultsopt <- apply.paramset(strategyName,
paramset.label = "forestopt",
portfolio.st = strategyName,
account.st = strategyName,
nsamples = 0)
}
results <- applyStrategy(strategy= strategyName, portfolios = portfolioName)
getTxns(Portfolio=portfolioName, Symbol=symbolstring)
mktdata
updatePortf(portfolioName)
dateRange <- time(getPortfolio(portfolioName)$summary)[-1]
updateAcct(portfolioName,dateRange)
updateEndEq(accountName)
print(plot(tail(getAccount(portfolioName)$summary$End.Eq,-1), main = "Portfolio Equity"))
这是我得到的错误。看起来它遍历了 3 个符号,但我不确定我应该如何创建参数集
[[1]] <simpleError in install.param.combo(strategy, param.combo, paramset.label): symbolstring: 策略FirstPortfolio中没有这样的符号规则>
[[2]] <simpleError in install.param.combo(strategy, param.combo, paramset.label): symbolstring: 策略FirstPortfolio中没有这样的符号规则>
[[3]] <simpleError in install.param.combo(strategy, param.combo, paramset.label): symbolstring: no such symbol rule in strategy FirstPortfolio>
解决方案
推荐阅读
- python - 为什么 MDRaisedButton 在 Android 上不起作用?
- .net - 如何在 blazor webassembly 的多个项目中使用相同的登录页面?
- php - PHP:将 X.pdf 连接到所选目录中的 X.png
- reactjs - 里面有没有使用Formik道具
- prolog - 为什么我会收到“单例变量”警告和不正确的结果?
- javascript - app脚本,file.makeCopy创建一个新文件后,如何访问这个新文件
- python - 从 Python 中的文件夹中按顺序读取帧的问题
- perl - 从文本文件中提取某些行,跳过其他行
- python - 根据工作簿的名称向每个 Excel 工作簿添加日期列。使用 python 和熊猫
- assembly - 查找字符串中的子字符串及其位置