Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This post shows how to read all prices of constituents of a stock index given all symbols as string. It is a starting point of pairs trading backtest to collect daily stock prices of a given stock index.

# Read and Concatenate Prices of Constituents of a Stock Index

Pairs trading aims to select good performing pairs from a set of universe of a stock index. It is, therefore, a starting point of pairs trading backtest to collect daily stock prices of a given stock index.

Sometimes there are symbols from which price information rea not available. In this case, we should check the all components of the stock index. In case of Nasdaq 100, recent information can be found at

https://www.slickcharts.com/nasdaq100

### R code

The following R code retreives historical daily prices of constituents of the Nasdaq 100 index given all symbols of it as of 2022-07-29.

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 #========================================================## Quantitative ALM, Financial Econometrics & Derivatives # ML/DL using R, Python, Tensorflow by Sang-Heon Lee ## https://kiandlee.blogspot.com#——————————————————–## load prices of constituents of a stock index#========================================================# graphics.off(); rm(list = ls()) library(quantmod) #————————————————-# Components of the Nasdaq 100, as of 2022-07-29#————————————————-vstr_nasdaq100 <–     “AAPL,MSFT,AMZN,TSLA,GOOG,GOOGL,NVDA,META,PEP,COST,    AVGO,CMCSA,ADBE,CSCO,TMUS,QCOM,INTC,TXN,AMD,AMGN,    HON,INTU,NFLX,PYPL,ADP,SBUX,AMAT,MDLZ,ADI,ISRG,    CHTR,GILD,BKNG,VRTX,CSX,MU,FISV,LRCX,REGN,MNA,    ATVI,SNPS,KLAC,KDP,MNST,MAR,AEP,CDNS,PANW,NXPI,    ASML,FTNT,ORLY,PAYX,MRVL,KHC,ADSK,EXC,CTAS,ABNB,    MELI,AZN,XEL,CRWD,EA,MCHP,CTSH,LULU,DLTR,WBA,    DXCM,ILMN,SGEN,IDXX,JD,BIIB,ODFL,PCAR,LCID,BIDU,    WDAY,CPRT,VRSK,TEAM,ROST,FAST,ZM,DDOG,EBAY,SIRI,    PDD,ANSS,ZS,ALGN,MTCH,VRSN,CEG,NTES,SWKS,SPLK,    OKTA,DOCU” #——————————————-# split symbols and make vector#——————————————-nasdaq100_symbols <–     gsub(” “, “”, strsplit(vstr_nasdaq100,                            “\\s*,\\s*”)[[1]])nasdaq100_symbols #——————————————-# read price information of constituents#——————————————-sdate <– as.Date(“2020-07-01”)edate <– as.Date(“2022-06-30”)getSymbols(nasdaq100_symbols,from=sdate,to=edate) #——————————————-# collect only adjusted prices#——————————————-price <– NULLfor(i in 1:length(nasdaq100_symbols)) {    eval(parse(text=paste0(        “price <- cbind(price,",        nasdaq100_symbols[i],“[,6])”)))} # modify column name as only symbolcolnames(price) <–     gsub(“.Adjusted”, “”, colnames(price)) #——————————————-# print price time series of Components#——————————————-head(price)tail(price) Colored by Color Scripter cs

Running the above R code displays the status of data reading process as follows.

Finally, we can get the collection of individual stock prices, which are components of the Nasdaq 100 index