Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

stock - R - Quantstart: Testing Multiple Equities Different Backtesting Periods

I am testing a strategy that uses 3 equities "XLF", "XLP", "XLE". My overall backtesting period starts from "2018-01-01". I can run the following code to test all these 3 equities starting "2018-01-01" to today’s date. What I would like to do is to test "XLF" only in 2018, "XLP" only in 2019 and "XLE" only in 2020 and then look at overall backtesting result. I can achieve this by changing initial dates and run it three times separately but at the end I have to aggregate results which is not ideal.

Would it be possible to do this in Quantstart without running this code three times?

library(quantstrat)
.blotter <<- new.env()
.strategy <<- new.env()
data(stratBBands) #load a test strategy
symbols = c("XLF", "XLP", "XLE")
initDate="2018-01-01"
initEq=100000

getSymbols(symbols
           , src='yahoo'
           , index.class=c("POSIXt"
                           ,"POSIXct")
           , from=initDate)
currency("USD")
stock(symbols, currency="USD", multiplier=1)
strategy.st <- "bbands"
portfolio.st <- "bbands"
account.st <- "bbands"

initPortf(name=portfolio.st, 
          symbols=symbols, 
          initDate=initDate, 
          currency='USD')
initAcct(name=account.st, 
         portfolios=portfolio.st,    
         initDate=initDate, 
         currency='USD', 
         initEq=initEq)
initOrders(portfolio=portfolio.st, 
           symbols=symbols,
           initDate=initDate)
out<-try(applyStrategy(strategy=stratBBands 
                       , portfolios=portfolio.st
                       , parameters=list(sd=2,n=60)))
question from:https://stackoverflow.com/questions/66066494/r-quantstart-testing-multiple-equities-different-backtesting-periods

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...