[R-SIG-Finance] quantmod‏ - getSymbols for csv files

rex rex at nosyntax.net
Thu Sep 15 20:58:12 CEST 2011


Jun Sheng Tan <thyreus56344612 at hotmail.com> [2011-09-14 02:28]:
>
>I'm a newbie and currently facing the same issue as this guy  http://pastebin.com/4bfURwBY Where the getSymbols is unable to detect the date on my excel .csv file , instead it listed (real-time) today's date as the date valueThe csv file I'm using is from yahoo http://chart.yahoo.com/table.csv?s=SBUX&a=0&b=01&c=2007&d=8&e=13&f=2011&g=d&q=q&y=0&z=SBUX&x=.csv
>Original file :
>  Open  High   Low Close   Volume Adj.Close2011-09-12 36.69 37.88 36.69 37.65  8122500     37.652011-09-09 38.62 38.75 37.05 37.31 10514600     37.312011-09-08 38.98 39.39 38.69 38.92  7886900     38.922011-09-07 38.19 39.37 37.96 39.18  8398900     39.182011-09-06 36.52 37.84 36.42 37.75  6135000     37.75
>> getSymbols('SBUX',src='csv',return='timeSeries')[1] "SBUX"> SBUXGMT           SBUX.Open SBUX.High SBUX.Low SBUX.Close SBUX.Volume SBUX.Adjusted2011-09-14     36.69     37.88    36.69      37.65     8122500         37.652011-09-14     38.62     38.75    37.05      37.31    10514600         37.312011-09-14     38.98     39.39    38.69      38.92     7886900         38.922011-09-14     38.19     39.37    37.96      39.18     8398900         39.18
>Does anyone know have to have the date column to list the correct date from the csv file?

~$ R
R version 2.13.0 (2011-04-13)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)
[...]
[Previously saved workspace restored]

> library(quantmod)
Loading required package: Defaults
Loading required package: xts
Loading required package: zoo

Attaching package: 'zoo'

The following object(s) are masked from 'package:base':

      as.Date

Loading required package: TTR
> getSymbols('SBUX')
[1] "SBUX"
> head(SBUX)                                                                                                                                              
             SBUX.Open SBUX.High SBUX.Low SBUX.Close SBUX.Volume  SBUX.Adjusted
2007-01-03     35.60     36.20    34.64      35.25     9017300     34.39                                                                               
2007-01-04     35.37     35.50    34.79      35.29     7328000     34.43                                                                               
2007-01-05     35.12     35.29    34.97      35.14     6565700     34.28                                                                               
2007-01-08     34.91     35.26    34.90      35.01     5807200     34.16                                                                               
2007-01-09     35.00     35.39    34.70      34.86     7152500     34.01
2007-01-10     34.70     34.78    34.17      34.75     9329200     33.90
> tail(index(SBUX))
[1] "2011-09-07" "2011-09-08" "2011-09-09" "2011-09-12" "2011-09-13"
"2011-09-14"
> 

As you can see from the above, there's no need for a CSV file, but if
you want to read the data from a local CSV file that looks like::

~$ less subx.csv
Date,Open,High,Low,Close,Volume,Adj Close
2011-09-13,37.71,38.31,37.67,38.19,6159400,38.19
2011-09-12,36.69,37.88,36.69,37.65,8122500,37.65
2011-09-09,38.62,38.75,37.05,37.31,10514600,37.31
[...]

Then, in R do:

> tmp <- read.csv('subx.csv')
> subx = xts(tmp[,-1],as.Date(tmp[,1],"%Y-%m-%d"))
> head(subx)
             Open  High   Low Close    Volume Adj.Close
1992-06-26 21.00 22.25 20.50 21.50 112281600      0.66
1992-06-29 21.75 23.50 21.25 23.00  29504000      0.70
1992-06-30 23.50 23.75 22.00 22.25  17449600      0.68
1992-07-01 22.50 23.00 21.75 22.75   9270400      0.69
1992-07-02 23.00 23.00 22.25 22.75   7155200      0.69
1992-07-06 22.50 22.75 22.25 22.75   3056000      0.69
> 

HTH,

-rex
-- 
Q:  Why do mountain climbers rope themselves together?
A:  To prevent the sensible ones from going home.



More information about the R-SIG-Finance mailing list