You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WFA does not work as expected for portfolios with more than one symbol.
Expected behavior
Perform parameter optimization and WFA across a portfolio of assets based on their own unique optimal paramater combinations.
Minimal, reproducible example
require(quantstrat)
suppressWarnings(rm("order_book.macd",pos=.strategy))
suppressWarnings(rm("account.macd","portfolio.macd",pos=.blotter))
suppressWarnings(rm("account.st","portfolio.st","stock.str","stratMACD","startDate","initEq",'start_t','end_t'))
#correct for TZ issues if they crop upoldtz<-Sys.getenv('TZ')
if(oldtz=='') {
Sys.setenv(TZ="GMT")
}
stock.str=c('AAPL','MSFT') # what are we trying it on#MA parameters for MACDfastMA=12slowMA=26signalMA=9maType="EMA"
currency('USD')
stock(stock.str,currency='USD',multiplier=1)
startDate='2006-12-31'initEq=1000000portfolio.st='macd'account.st='macd'
initPortf(portfolio.st,symbols=stock.str)
initAcct(account.st,portfolios=portfolio.st)
initOrders(portfolio=portfolio.st)
strat.st<-portfolio.st# define the strategy
strategy(strat.st, store=TRUE)
#one indicator
add.indicator(strat.st, name="MACD",
arguments=list(x=quote(Cl(mktdata)),
nFast=fastMA,
nSlow=slowMA),
label='_'
)
#two signals
add.signal(strat.st,name="sigThreshold",
arguments=list(column="signal._",
relationship="gt",
threshold=0,
cross=TRUE),
label="signal.gt.zero"
)
add.signal(strat.st,name="sigThreshold",
arguments=list(column="signal._",
relationship="lt",
threshold=0,
cross=TRUE),
label="signal.lt.zero"
)
##### add rules# entry
add.rule(strat.st,name='ruleSignal',
arguments=list(sigcol="signal.gt.zero",
sigval=TRUE,
orderqty=100,
ordertype='market',
orderside='long',
threshold=NULL),
type='enter',
label='enter',
storefun=FALSE
)
# exit
add.rule(strat.st,name='ruleSignal',
arguments=list(sigcol="signal.lt.zero",
sigval=TRUE,
orderqty='all',
ordertype='market',
orderside='long',
threshold=NULL,
orderset='exit2'),
type='exit',
label='exit'
)
#end rules####
getSymbols(stock.str,from=startDate, to='2014-06-01', src='yahoo')
start_t<-Sys.time()
out<-applyStrategy(strat.st , portfolios=portfolio.st,parameters=list(nFast=fastMA, nSlow=slowMA, nSig=signalMA,maType=maType),verbose=TRUE)
end_t<-Sys.time()
print(end_t-start_t)
start_t<-Sys.time()
updatePortf(Portfolio=portfolio.st,Dates=paste('::',as.Date(Sys.time()),sep=''))
end_t<-Sys.time()
print("trade blotter portfolio update:")
print(end_t-start_t)
# set tz as it was before the demo
Sys.setenv(TZ=oldtz)
require(foreach,quietly=TRUE)
require(iterators)
require(quantstrat)
#retrieve the strategy from the environment, since the 'macd' strategy uses store=TRUEstrategy.st<-'macd'### Set up Parameter Values.FastMA= (1:10)
.SlowMA= (5:25)
.nsamples=15#for random parameter sampling, less important if you're using doParallel or doMC### MA paramset
add.distribution(strategy.st,
paramset.label='MA',
component.type='indicator',
component.label='_', #this is the label given to the indicator in the stratvariable=list(n=.FastMA),
label='nFAST'
)
add.distribution(strategy.st,
paramset.label='MA',
component.type='indicator',
component.label='_', #this is the label given to the indicator in the stratvariable=list(n=.SlowMA),
label='nSLOW'
)
add.distribution.constraint(strategy.st,
paramset.label='MA',
distribution.label.1='nFAST',
distribution.label.2='nSLOW',
operator='<',
label='MA'
)
###wfportfolio<-"wf.macd"
initPortf(wfportfolio,symbols=stock.str)
initOrders(portfolio=wfportfolio)
wf_start<- Sys.time()
registerDoSEQ() # for debuggingwfresults<- walk.forward(strategy.st,
paramset.label='MA',
portfolio.st=wfportfolio,
account.st=account.st,
nsamples=.nsamples,
period='months',
k.training=36,
k.testing=12,
verbose=TRUE,
anchored=TRUE,
include.insamples=TRUE,
savewf=FALSE
)
wf_end<-Sys.time()
cat("\n Running the walk forward search: \n")
print(wf_end-wf_start)
cat(" Total trials:",.strategy$macd$trials,"\n")
wfa.stats<-wfresults$tradeStats
print(wfa.stats)
chart.forward(wfresults)
# resulting stats are identical for both symbols, AAPL and MSFT
apply.paramset and applyStrategy will now apply the appropriate mktdata related to each symbol in the portfolio, as opposed to just the first.
See #125
Adding dailyStats to the results output in apply.paramset which is called by walk.forward. When the number of symbols in the portfolio is >1, we will select dailyStats as the objective function.
TODO: make it more conditional on alternate user-specified objective functions.
See #125
Description
WFA does not work as expected for portfolios with more than one symbol.
Expected behavior
Perform parameter optimization and WFA across a portfolio of assets based on their own unique optimal paramater combinations.
Minimal, reproducible example
Session Info
The text was updated successfully, but these errors were encountered: