Reputation: 1933
very new to R and to TTR. I've created what I assume is a vector of all the stock symbols retrieved by the TTR package in R with the following:
stockDataFrame <- stockSymbols()
symbs <- stockDataFrame[["Symbol"]]
So I've got all the symbols in the variable "symbs". How do I apply the TTR function "getYahooData" on each element of this vector, especially since the function requires not only the stock symbol but also start, end, freq, type, adjust, and quiet argument values?
Upvotes: 2
Views: 526
Reputation: 49810
Just use quantmod which will load TTR for you. And you won't hurt @JoshuaUlrich's feelings because he's also devloper on the quantmod project. Check out the examples on the website: http://www.quantmod.com/
getSymbols
is a sort of "generic" function which has "methods" for different data sources. By default, src=yahoo
which means that getSymbols.yahoo
is called. This can handle a vector of Symbols.
By default auto.assign=TRUE
which means that the returned data is assigned in the .GlobalEnv
(by default), and the names of the objects that were assigned are returned.
> library(quantmod)
> getSymbols(symbs[1:5])
[1] "AA-P" "AAU" "ACU" "ACY" "ADGE"
> head(ACY)
ACY.Open ACY.High ACY.Low ACY.Close ACY.Volume ACY.Adjusted
2007-01-03 6.58 7.15 6.58 6.99 31700 6.99
2007-01-04 7.24 7.35 7.15 7.35 18600 7.35
2007-01-05 7.39 7.55 7.25 7.45 15200 7.45
2007-01-08 7.32 7.35 7.01 7.26 16100 7.26
2007-01-09 7.15 7.50 7.15 7.16 12100 7.16
2007-01-10 7.30 7.45 7.21 7.24 13700 7.24
If you don't really want to download data for 6,000+ stocks, there's a convenient alternative: attachSymbols
makes all of those Symbols available on-demand with lazy evaluation. The first time you use a Symbol, it will be downloaded and cached so that next time you use it it will be read from memory (or disk depending on which arguments you use with attachSymbols
).
> attachSymbols()
> tail(GS)
GS.Open GS.High GS.Low GS.Close GS.Volume GS.Adjusted
2012-11-26 119.16 120.95 118.50 120.94 3371000 120.43
2012-11-27 120.50 121.34 118.38 118.41 3875200 117.91
2012-11-28 117.25 119.33 116.57 119.33 3811200 119.33
2012-11-29 120.00 120.45 118.52 118.73 2980800 118.73
2012-11-30 117.96 119.44 117.69 117.79 4264300 117.79
2012-12-03 118.42 119.64 118.07 118.40 3613900 118.40
Upvotes: 3
Reputation: 121568
You can use plyr package to loop through your symbol list.
I am not sure that getYahooData can load the data, for all the symbols. I think it should exist better method to use TTR. But here I show the use of plyr package. It is a generic method.
I call llply to get all the data for the first 6 market symbols between two dates (you can add other function argument,..)
library(plyr)
ll <- llply(symbs[1:6],getYahooData ,start=20081201, end=20081207)
$`AA-P`
Open High Low Close Volume Unadj.Close Div Split Adj.Div
2008-12-01 49.39233 49.39233 49.39233 49.39233 243.9650 60.25 NA NA NA
2008-12-02 49.39233 49.39233 49.39233 49.39233 243.9650 60.25 NA NA NA
2008-12-03 49.39233 49.39233 49.39233 49.39233 0.0000 60.25 NA NA NA
2008-12-04 49.80223 51.64676 49.80223 51.64676 487.9300 63.00 NA NA NA
2008-12-05 49.79403 49.79403 49.79403 49.79403 121.9825 60.74 NA NA NA
$AAU
Open High Low Close Volume
2008-12-01 0.49 0.53 0.49 0.50 57100
2008-12-02 0.52 0.54 0.51 0.52 33400
2008-12-03 0.50 0.54 0.49 0.49 52800
2008-12-04 0.49 0.49 0.44 0.44 62400
2008-12-05 0.44 0.48 0.42 0.47 87600
$ACU
Open High Low Close Volume Unadj.Close Div Split Adj.Div
2008-12-01 6.168596 6.168596 5.823880 5.823880 3196.838 6.42 NA NA NA
2008-12-02 5.987166 6.077881 5.814809 5.987166 8157.448 6.60 NA NA NA
2008-12-03 6.068810 6.068810 5.896452 5.987166 1984.244 6.60 NA NA NA
Upvotes: 1