I just started with quantmod package. If I want to select stocks based on their recent performance, then I need to loop through all the stocks in, say, NYSE. So I need:
- get all the stock symbols
- select those symbols that do have data
- loop through them one by one(say, each time, download the stock data as $X$, and do some analysis. Then loop to next one, and set it as $X$)
My question is: how can I do these? Here is part of my thought:
use stockSymbols() function in TTR package:
AllSym <- stockSymbols(). The first column ofAllSymare all the potential symbols.Use
getSymbols(AllSym[i,1])to loop all the stocki:
Questions here:
Not all the symbols from TTR have data. Some may have error when using
getSymbols. I actually do not care about this certain stock. So how can I continue with the loop? [How can I use thetryfunction?]The
getSymbolsfunction will automatically make the stock symbols as the variables. How can I copy(or set) it to $X$(instead of something like $APPL$)?How can I loop trough the variables with their names saved as strings in a vector? For example, if we do use
getSymbolsfunction to get data with name as the stock symbol. How can I loop through those stocks?[Note that their variable name are stored in first columnAllSymas string]
Thank you very much!
try()function – Rime Sep 18 '17 at 08:45passunless you want to do something like print a statement and the stock that caused the error. – Shanemeister Apr 14 '18 at 17:06