Applying a Technical Strategy Across a Stock Portfolio in SIT
Summary
The document asks how to evaluate a technical strategy across an entire stock portfolio using the Systematic Investor Toolbox in R. The example strategy enters when a lagged 50-day moving average exceeds a lagged 100-day moving average and the 25-day RSI is below 50, then otherwise holds no position. Adjusted prices are used, and the moving averages are lagged to reduce lookahead bias.
The proposed workflow is to load all desired ticker symbols together and apply the signals across the resulting price matrix with a SIT matrix backtesting function, such as `bt.apply.matrix`, then run and assess the results. This replaces testing securities one at a time and manually averaging individual performance measures. The answer is brief and does not specify how to aggregate portfolio returns, handle missing data, or account for portfolio weights, transaction costs, and survivorship bias, so those choices still need to be defined for a meaningful evaluation.
Key ideas
- Load all portfolio ticker symbols together to prepare a multi-security backtest.
- A matrix-based SIT function can apply strategy signals across the portfolio price data.
- Lagged moving averages help avoid using information unavailable at the time of a trade.
- Portfolio-level evaluation still depends on explicit choices about weights, costs, and data quality.
Tags
Full text
# How to backtest strategy in portfolio of stocks using SIT R?
# 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()
# 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?
## Answer by ogukku (score 1)
https://quant.stackexchange.com/a/36789
I suppose you could try:
- 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.
- 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.
- Run and measure.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.