如何使用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,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]]

iCMS 回答:如何使用apply.paramset将符号列表添加为参数集?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1714644.html

大家都在问