# How to backtest strategy in portfolio of stocks using SIT R?

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()

tickers = spl('AAPL')
getSymbols(tickers, src = 'yahoo', from = '2000-01-01', env = data, auto.assign = 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?

• 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. – Ric May 7 '18 at 7:22
• 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)? – Ric May 7 '18 at 7:24