Search code examples
rdataframextsquantmod

QuantMod: How do I get data in a tabular format?


I created a for loop for 100 companies and plotted Bollinger bands, volume, commodity channel index, MACD, and relative strength index. For all the metrics I mentioned how can I convert that table into a table?

For loop Script:

for (stock in ls(stockEnv)){
  chartSeries(stockEnv[[stock]], theme="white", name=stock,
              TA="addVo();addBBands();addCCI();addSMA(20, col='blue');
        addSMA(5, col='red');addMACD();addRSI();addROC()", subset='last 30 days')     
}

The plot is attached. What I am trying to do is bind all the data together for the other 99 stock tickers I have into a tabular format for the metrics you see in the image. enter image description here


Solution

  • I found your code was sourced from this SO answer.

    I think this approach should work for you.

    library(quantmod)
    library(data.table)
    stockEnv <- new.env()
    stocks <- c("AAPL","MSFT","FB")
    getSymbols(stocks, src='yahoo', env=stockEnv)
    
    datalist <- list()
    for(stock in ls(stockEnv)){
    table <- as.data.frame(stockEnv[[stock]])
    date = rownames(table)
    rownames(table) <- NULL
    colnames(table) <- c("Open","High","Low","Close","Volume","Adjusted")
    bound.table <- data.frame(Symbol = stock, date ,table)
    datalist[[stock]] <-  bound.table
    }
    Result <- rbindlist(datalist,fill=TRUE)  
    Result
    #      Symbol       date      Open      High       Low     Close    Volume  Adjusted
    #   1:   AAPL 2007-01-03  12.32714  12.36857  11.70000  11.97143 309579900  10.39169
    #   2:   AAPL 2007-01-04  12.00714  12.27857  11.97429  12.23714 211815100  10.62234
    #   3:   AAPL 2007-01-05  12.25286  12.31428  12.05714  12.15000 208685400  10.54669
    #   4:   AAPL 2007-01-08  12.28000  12.36143  12.18286  12.21000 199276700  10.59878
    #   5:   AAPL 2007-01-09  12.35000  13.28286  12.16429  13.22429 837324600  11.47922
    #  ---                                                                              
    #8652:   MSFT 2020-03-30 152.44000 160.60001 150.01000 160.23000  63420300 160.23000
    #8653:   MSFT 2020-03-31 159.39999 164.78000 156.56000 157.71001  77927200 157.71001
    #8654:   MSFT 2020-04-01 153.00000 157.75000 150.82001 152.11000  57969900 152.11000
    #8655:   MSFT 2020-04-02 151.86000 155.48000 150.36000 155.26000  49630700 155.26000
    #8656:   MSFT 2020-04-03 155.10001 157.38001 152.19000 153.83000  41212700 153.83000
    

    These lines of code will add the Bollinger bands.

    Result[,(c("dn","mavg","up","pctB")):= 
             apply(BBands(.SD),2,function(x){as.list(x)}),
           by = "Symbol",
           .SDcols = c("High","Low","Close")]
    

    You can also easily add any of the other TTR functions results. You just need to know the columns it accepts as inputs and how many it outputs.

    For MACD:

    Result[,(c("macd","signal")):= 
             apply(MACD(.SD,type="EMA"),2,function(x){as.list(x)}),
           by = "Symbol",
           .SDcols = c("Close")]
    

    Note there is a slight variation for single column outputs like CCI.

    Thus, for Commodity Channel Index.

    Result[,(c("CCI")):= list(as.vector(CCI(.SD))),by = "Symbol", .SDcols = c("High","Low","Close")]
    

    Or for Relative Strength Index

    Result[,(c("RSI")):= list(as.vector(RSI(.SD, maType="EMA"))),by = "Symbol", .SDcols = c("Close")]
    

    See the help for all other TTR functions, help(SMA) for example.