4
$\begingroup$

I am creating and testing strategies in R code and using systemic investor toolbox(SIT) package as the backtesting tool. I copied a SIT backtesting code from a website and made small changes to make below code and its working fine.

#backtesing long Apple
#long when fast MA is greater than slow MA and rsi less than 50 else exit
library(quantmod)
library(SIT)
data <- new.env()

# Load historical data and adjusts for splits and dividends
tickers = spl('AAPL')
getSymbols(tickers, src = 'yahoo', from = '2000-01-01', env = data, auto.assign = T)    
for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)  

#Calculate the moving averages and lag them one day to prevent lookback bias
close<-Cl(data[['AAPL']])
MAF <- lag(SMA(close,50))  
MAC <- lag(SMA(close,100))
rsi<- RSI(close,25)

#Sets backtesting environment
bt.prep(data, align='remove.na') 
prices = data$prices   

#Create a empty list for attaching the models to at a later stage
models = list()

#Specify the weights to be used in the backtest
data$weight[] = NA #Zero out any weights from previous
data$weight[] = ifelse(MAF>MAC&rsi<50,1,0)  #If price of SPY is above the SMA then buy

#Call the function to run the backtest given the data, which contains the prices and weights.
models$technical_model = bt.run.share(data, trade.summary=T)

#Plot equity curve and export the trades list to csv
plot(models$technical_model$equity, main="Equity Curve")

Currently to check the quality of my strategy; I backtest using above code against 10 randomly handpicked stocks and indexs in my portfolio (AAPL,GOOG, GE,GS,PFE,AA,SPY,^GSPC,XOM,C) and then manually take averages of the results(eg drawdown, sharpe, profit factor etc) to check the strategy viability. But doing this takes lots of my time and energy . How can I backtest my strategy against my whole protofolio instead of one by one in SIT to get a best estimate of my strategy. What kind of modifications should I do to the above code?

$\endgroup$
2
  • $\begingroup$ The SIT code was written in the course of a blog series (systematicinvestor.wordpress.com) you might find a portfolio view there as well. Otherwise if your question is: I got code for a single stock how can I use it for many, then this is not a QSE question. $\endgroup$
    – Richi Wa
    Commented May 7, 2018 at 7:22
  • $\begingroup$ If I get it wrong, then it is just trivial: you got code for one stock. There are many ways to use it for a bunch of stocks. Moreover: of what nature is your strategy? Does it deliver signals for single stocks or for many (like ranking stocks or building portfolios)? $\endgroup$
    – Richi Wa
    Commented May 7, 2018 at 7:24

1 Answer 1

1
$\begingroup$

I suppose you could try:

  1. Populating your 'tickers' vector with the symbolnames of all the rest of the stocks in your portfolio that you want to measure all at once.
  2. Use SIT::bt.apply.matrix or a similar function whose main argument is the 'prices' variable to get your signals applied to all 'tickers' you declared earlier in beginning.
  3. Run and measure.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.