问题描述
我得到了使用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,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,arguments = list(
threshold = 0.1,relationship = "lt",label = "cashtime")
add.signal(
strategy = strategyName,arguments = list(
threshold = -0.1,relationship = "gt",arguments = list(
threshold = -0.25,relationship = "lte",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,arguments = list(
sigcol = "Selltime",orderside = "short",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,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]]
[[2]]
[[3]]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)