帮助,为什么这种简单的方法不起作用?
我正在尝试围绕其中一个quantstrat演示(bbands)创建一个函数,并将股票代码作为参数传递。 我想我正在尝试围绕一段工作代码创建一个函数,并用通过该函数传递的参数替换一个硬编码常量。在哪里首先看,这是不同类或环境的问题吗?
以下可再现代码:
try_BBands <- function(stock)
{
########## body of function is demo from quantstrat package
require(quantstrat)
suppressWarnings(rm("order_book.bbands",pos=.strategy))
suppressWarnings(rm("account.bbands","portfolio.bbands",pos=.blotter))
suppressWarnings(rm("account.st","portfolio.st","stock.str","stratBBands","initDate","initEq",'start_t','end_t'))
########## here I replaced the following line:
########## stock.str = 'IBM'
stock.str <- stock
# we'll pass these
SD = 2 # how many standard deviations, traditionally 2
N = 20 # how many periods for the moving average, traditionally 20
currency('USD')
stock(stock.str,currency='USD',multiplier=1)
initDate='2006-12-31'
initEq=1000000
portfolio.st='bbands'
account.st='bbands'
initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
initAcct(account.st,portfolios='bbands', initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)
addPosLimit(portfolio.st, stock.str, initDate, 200, 2 ) #set max pos
stratBBands <- strategy("bbands")
#one indicator
stratBBands <- add.indicator(strategy = stratBBands, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'))
#add signals:
stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")
stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")
stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("High","Low","mavg"),relationship="op"),label="Cross.Mid")
# lets add some rules
stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='enter')
stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='enter')
stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='exit')
#alternately, to exit at the opposite band, the rules would be...
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Lo.gt.UpperBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Hi.lt.LowerBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
#TODO add thresholds and stop-entry and stop-exit handling to test
getSymbols(stock.str,from=initDate, to='2012-07-05', index.class=c('POSIXt','POSIXct'))
start_t<-Sys.time()
out<-try(applyStrategy(strategy=stratBBands , portfolios='bbands',parameters=list(sd=SD,n=N)) )
# look at the order book
#getOrderBook('bbands')
end_t<-Sys.time()
#print("strat execution time:")
#print(end_t-start_t)
#start_t<-Sys.time()
updatePortf(Portfolio='bbands',Dates=paste('::',as.Date(Sys.time()),sep=''))
#end_t<-Sys.time()
#print("updatePortf execution time:")
#print(end_t-start_t)
chart.Posn(Portfolio='bbands',Symbol=stock.str)
#plot(add_BBands(on=1,sd=SD,n=N))
}
try_BBands('AAPL')
返回的错误是:
Error in get(symbol) : object 'AAPL' not found
建议非常感谢!
答案 0 :(得分:0)
解决! 我必须在getSymbols调用中包含env = globalenv()。 显然,在我的函数中创建的xts对象并没有存储在全局环境中,我猜想。
以下行可以解决问题:
getSymbols(stock.str,from=initDate, to='2012-07-05', index.class=c('POSIXt','POSIXct'), env=globalenv())