Home > R > Loading Historical Stock Data

Loading Historical Stock Data

Historical Stock Data is critical for testing your investment strategies. I illustrated all my back-test examples with getSymbols function from quantmod package. For example, following is a back-test comparison for a few portfolio allocation methods:

###############################################################################
# Load Systematic Investor Toolbox (SIT)
# http://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
setInternet2(TRUE)
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
    source(con)
close(con)

    #*****************************************************************
    # Load historical data
    #****************************************************************** 
    load.packages('quantmod')
        
    tickers = spl('UUP,EMB,HYG')
    
    data <- new.env()

    # load historical data, getSymbols from quantmod
        getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)    

    # prepare data for back test
        for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)                            
    bt.prep(data, align='remove.na')
    
    #*****************************************************************
    # Code Strategies
    #******************************************************************
    obj = portfolio.allocation.helper(data$prices, periodicity = 'months', lookback.len = 250, 
        min.risk.fns = list(EW=equal.weight.portfolio,
                        RP=risk.parity.portfolio,
                        MV=min.var.portfolio,
                        MC=min.corr.portfolio)
    ) 
    
    models = create.strategies(obj, data)$models
                    
    #*****************************************************************
    # Create Report
    #******************************************************************        
    strategy.performance.snapshoot(models, T)

The getSymbols function, from quantmod package, downloads historical stock prices from Yahoo Fiance. I often get questions about alternative ways to load data. For example, let’s say you don’t want to download prices each time you need to run a back-test, instead you are storing historical price files locally and want to use in them instead. Following are 3 examples to load historical prices from files saved on your computer:

  • 1. Load Historical Stock data from the csv files you saved from Yahoo Fiance
  • 2. Load Historical Stock data from your custom files
  • 3. Load Historical Stock from one csv file, where each column represents one stock

Just substitute any of the samples below with following line of code in my original back-test example:

    # load historical data, getSymbols from quantmod
        getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)    

For the first method I created a getSymbols.sit function that loads historical stock prices from csv files you saved from Yahoo Fiance. Here is an example:

    stock.folder = 'c:\\Stocks\\Data\\'

    # if you saved yahoo historical price files localy
    getSymbols.sit(tickers, src = 'yahoo', from = '1980-01-01', env = data, auto.assign = T, stock.folder = stock.folder)

For the second method, please load your historical price files with read.xts function. You will need to make sure that that one of the column headers is labeled “Close”. Here is an example:

    stock.folder = 'c:\\Stocks\\Data\\'

    # custom format historical price files
    for(n in tickers) {
        data[[n]] = read.xts(paste(stock.folder, n, '.csv', sep=''), format='%m/%d/%Y')
    } 

For the third method, please load your historical prices from one file. (i.e. each column is one stock) Here is an example:

    stock.folder = 'c:\\Stocks\\Data\\'

    # read from one csv file, column headers are tickers
    filename = 'histdata.csv'
    all.data = read.xts(paste(stock.folder, filename, sep=''), format='%m/%d/%Y')
    for(n in names(all.data)) {
        data[[n]] = all.data[,n]
        colnames(data[[n]]) = 'Close'
        data[[n]]$Adjusted = data[[n]]$Open = data[[n]]$High = data[[n]]$Low = data[[n]]$Close
    }

Have fun with your data and let me know if you run into any problems.

About these ads
Categories: R
  1. Fraz7a
    December 1, 2013 at 8:04 pm

    Just wanted to say that I am very impressed by this blog and very grateful that you share this with us. You’ve got me hooked.
    I have started to get myself up to speed in R as I realise possibilities are endless (comparing it to VBA which is all I used so far).

    I must say that some of your functions are very tough to crack because of the very short abbreviations you use. But maybe that is because I am not a natural programmer.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 246 other followers

%d bloggers like this: