Here's the code I'm running
library(quantmod)
library(tseries)
Stocks={}
companies=c("IOC.BO","BPCL.BO","ONGC.BO","HINDPETRO.BO","GAIL.BO")
for(i in companies){
Stocks[i]=getSymbols(i)
}
I'm trying to get a list of dataframes that are obtained from getSymbols to get stored in Stocks.
The problem is that getSymbols directly saves the dataframes to the global environment Stocks only saves the characters in companies in the list.
How do I save the dataframes in the Global Environment to a list?
Any help is appreciated.. Thanks in advance!
Another option is lapply
library(quantmod)
Stocks <- lapply(companies, getSymbols, auto.assign = FALSE)
Stocks <- setNames(Stocks, companies)
from ?getSymbols
auto.assign : should results be loaded to env If FALSE, return results instead. As of 0.4-0, this is the same as setting env=NULL. Defaults to TRUE
Using a for loop you could do
companies <- c("IOC.BO", "BPCL.BO", "ONGC.BO", "HINDPETRO.BO", "GAIL.BO")
Stocks <- vector("list", length(companies))
for(i in seq_along(companies)){
Stocks[[i]] <- getSymbols(name, auto.assign = FALSE)
}
Stocks
In my version of quantmod (0.4.0) it was necessary to set env=NULL in the functions' parameters, then the entire data frame is returned
Use the following argument as getSymbols(i, auto.assign=FALSE)
Related
Hi I am trying to download data of multiple stocks from yahoo finance using the quantmod package in R. My method is to create a function using the getSymbols function, and use lapply to apply this to a vector of ticker symbols. Below is my code
get_data <- function(x){getSymbols(x,
from = "2017-01-01",
to = "2021-03-15",
auto.assign = FALSE)}
tickers <- c("AAPL","GOOG","FB","TSLA")
mydata <- lapply(tickers,get_data)
running the last step does not give me the data, just a list with all the tickers stored as characters.
Would appreciate if anyone could tell me where my code is going wrong. Cheers
Here's the code I'm running
library(quantmod)
library(tseries)
Stocks={}
companies=c("IOC.BO","BPCL.BO","ONGC.BO","HINDPETRO.BO","GAIL.BO")
for(i in companies){
Stocks[i]=getSymbols(i)
}
I'm trying to get a list of dataframes that are obtained from getSymbols to get stored in Stocks.
The problem is that getSymbols directly saves the dataframes to the global environment Stocks only saves the characters in companies in the list.
How do I save the dataframes in the Global Environment to a list?
Any help is appreciated.. Thanks in advance!
Another option is lapply
library(quantmod)
Stocks <- lapply(companies, getSymbols, auto.assign = FALSE)
Stocks <- setNames(Stocks, companies)
from ?getSymbols
auto.assign : should results be loaded to env If FALSE, return results instead. As of 0.4-0, this is the same as setting env=NULL. Defaults to TRUE
Using a for loop you could do
companies <- c("IOC.BO", "BPCL.BO", "ONGC.BO", "HINDPETRO.BO", "GAIL.BO")
Stocks <- vector("list", length(companies))
for(i in seq_along(companies)){
Stocks[[i]] <- getSymbols(name, auto.assign = FALSE)
}
Stocks
In my version of quantmod (0.4.0) it was necessary to set env=NULL in the functions' parameters, then the entire data frame is returned
Use the following argument as getSymbols(i, auto.assign=FALSE)
I'm trying to collect mutual fund performance data via open and close prices from quantmod. I have scraped a list of 5000 some funds and am trying to loop through and get one open and close price for each fund. I'm having difficulty calling the xts object yielded by getSymbols() as it is called invisibly into the environment. Since the object is stored as its ticker name, I tried calling it by its ticker name.
Code so far:
## loop thru list and use quantmod to calculate performance from 1/2/14 to 12/31/14
for(i in 1:4881){
ticker <- tickernames[i]
getSymbols(ticker)
Open <- ticker["2014-01-02",1]
Close <- ticker["2014-12-31",4]
performance2014[i] = (Open - Close)/Open
}
Is there a way I can call the object using ls()?
The key is to set the auto.assign argument to FALSE in getSymbols. This way you deactivate getSymbols automatic assignment to the global environment.
Here's an example that should guide you through step by step:
require(quantmod)
#Vector of symbols to fetch prices for
symbols <- c('MSFT','SBUX','GOOGL')
#Initialize a list to store the fetched prices
myList <- list()
#Loop through symbols, fetch prices, and store in myList
myList <-lapply(symbols, function(x) {getSymbols(x,auto.assign=FALSE)} )
#Housekeeping
names(myList) <- symbols
#Access MSFT prices
myList[['MSFT']]
#Access SBUX prices
myList[['SBUX']]
#Access GOOGL prices
myList[['GOOGL']]
Hope this answered your question.
I'm learning R this semester and this is my first assignment. I want to retrieve monthly Adjusted stock quotes within a set date range using a for loop. And once I am able to do that I want to merge all the data into a data frame.
My code so far retrieves daily stock quotes for 5 stock symbols within a set date range, it assigns the object to the environment specified, and places only the .Adjusted column in the list.
Could someone point me in a better direction in obtaining the monthly quotes and am I on the right track with my code.
Thanks.
#Packages
library(quantmod)
#Data structure that contains stock quote objects
ETF_Data <- new.env()
#Assign dates to set range for stock quotes
sDate <- as.Date("2007-08-31")
eDate <- as.Date("2014-09-04")
#Assign a vector of ticker symbols.
ticker_symbol <- c("IVW","JKE","QQQ","SPYG","VUG")
#Assign number of ticker symbols.
total_ticker_symbols <- length(ticker_symbol)
#Assign empty list to for each object contained in my environment.
Temp_ETF_Data <- list()
#Assign integer value to counter.
counter <- 1L
#Loop and retrieve each ticker symbols quotes from Yahoo's API
for(i in ticker_symbol)
{
getSymbols(
i,
env = ETF_Data,
reload.Symbols = FALSE,
from = sDate,
to = eDate,
verbose = FALSE,
warnings = TRUE,
src = "yahoo",
symbol.lookup = TRUE)
#Add only Adjusted Closing Prices for each stock or object into list.
Temp_ETF_Data[[i]] <- Ad(ETF_Data[[i]])
if (counter == length(ticker_symbol))
{
#Merge all the objects of the list into one object.
ETF_Adj_Daily_Quotes <- do.call(merge, Temp_ETF_Data)
ETF_Adj_Monthly_Quotes <- ETF_Adj_Daily_Quotes[endpoints(ETF_Adj_Daily_Quotes,'months')]
}
else
{
counter <- counter + 1
}
}
There's no need for the for loop. You can loop over all the objects in the environment with eapply:
getSymbols(ticker_symbol, env=ETF_Data, from=sDate, to=eDate)
# Extract the Adjusted column from all objects,
# then merge all columns into one object
ETF_Adj_Data <- do.call(merge, eapply(ETF_Data, Ad))
# then extract the monthly endpoints
Monthly_ETF_Adj_Data <- ETF_Adj_Data[endpoints(ETF_Adj_Data,'months')]
I know that this is an old question but this answer might help potential future users seeking a better answer.
quantmod has now introduced an additional parameter to the getSymbols function called periodicity which can take the values of daily, weekly, monthly.
I tested out the following and it seems to work as desired:
getSymbols("EURGBP=X", from = starting, src = 'yahoo', periodicity = 'monthly')
just use
to.monthly(your_ticker)
I have about 2000 or so stock tickers that I loaded into R using:
companyList <- read.csv("~/Downloads/companylist.csv")
#saved all 2000 symbols inside the variable "ticker"
ticker <- as.vector(as.character(companyList[1:2000, 'Symbol']))
StockData <- new.env()
data <- getSymbols(ticker, env = stockData)
I can't seem to figure out how to run calculations on each stocks data without doing manually.
You can use eapply:
eapply(stockData, FUN=calcul)