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
Related
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 am currently writing a code to download timeseries (which will then be converted into csv-files) to conduct an event study upon.
The following code (part of the complete code) I wrote:
tickers = c("^AEX", "^ATX", "^BFX", "^FCHI", "^FTSE", "^GDAXI", "^IBEX", "^OMX","^OMXH25", "^OSEAX", "^SSMI", "FTSEMIB.MI")
Aggregate <- getSymbols(tickers,
from = "2014-01-01",
to = "2021-12-31")
na.omit(Aggregate,"iz",interp="linear")
Ticker <- Aggregate
Ticker
class(Ticker)
data1 <-as.data.frame(Ticker)
data1
class(data1)
data2 <- data1 # Duplicate data frame
data2 # Print new data frame
AEX <- ^AEX
write.zoo(AEX,"//Users/TEST/Library/CloudStorage/OneDrive-Personal/Event Study Basis\\AEX.csv",index.name="Date",sep=",")
As the tickers of the indices (^AEX, ^ATX etc.) all possess a "^", which Excel doesn't "eat" I want to make sure the dataframe I want to export to a .csv file does not possess this "^". For a different analysis, the code worked (different tickers) now I get an error every time I try to run it.
My questions:
which command will solve my problem? --> Converting ^AEX into AEX so Excel eats it :)
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)
library(quantmod)
library(rpart)
library(rpart.plot)
library(stocks)
today <- Sys.Date()
kq <- getSymbols("^kq11", from = today-100, to = today, auto.assign = F)
R's quantmod package provide getSymbols function that call stock price information.
But I can not call many stock data to use loop. Because I don't know yahoo symbol list.
Can I get all yahoo KOSPI(Korea Composite Stock Price Index) KOSDAQ(Korea Securities Dealers Automated Quotations) symbol list to use getsymbol function in R?
today <- Sys.Date()
getSymbols("KRX:KOSPI200",src="google",auto.assign=F,from='2015-01-01','today')
I would like to cycle through a list of tickers, get their financials and export them to CSV files in a folder on my desktop. However, I have been having trouble with an error in R related to viewFinancials() in the Quantmod package. The code and error are shown below.
And so, my question is how to assign a variable as an object of class financial so that my loop runs properly? Or if anyone has another alternative, I would be excited to hear it!
Here is the error message:
Error in viewFinancials(co.f, "BS", "Q") :
‘x’ must be of type ‘financials’
Here is the code I am working on:
tickers <- c('AAPL','ORCL','MSFT')
for(i in 1:length(tickers)){
co <- tickers[1]
#co.f <- paste(co,".f",sep='') #First attempt, was worth a try
co.f <- getFin(co, auto.assign=T) # automatically assigns data to "co.f" object
BS.q<-viewFinancials(co.f,'BS',"Q") # quarterly balance sheet
IS.q<-viewFinancials(co.f,"IS","Q") # quarterly income statement
CF.q<-viewFinancials(co.f,"CF","Q") # quarterly cash flow statement
BS<-viewFinancials(co.f,"BS","A") # annual balance sheet
IS<-viewFinancials(co.f,"IS","A") # annual income statement
CF<-viewFinancials(co.f,"CF","A") # annual cash flow statement
d<-Sys.Date()
combinedA <- rbind(BS,IS,CF)
combinedQ <- rbind(BS.q,IS.q,CF.q)
BSAfile <- paste('/Users/dedwards/Desktop/RFinancials/',d,' ',co,'_BS_A.csv',sep='')
BSQfile <- paste('/Users/dedwards/Desktop/RFinancials/',d,' ',co,'_BS_Q.csv',sep='')
write.csv(combinedA, file = BSAfile, row.names=TRUE)
write.csv(combinedQ, file = BSQfile, row.names=TRUE)
}
co.f contains the name of the object in the workspace that actually contains the financials object. To actually use that object you need to call get(co.f)
obj <- get(co.f)
# now you can use obj where you were previously trying to use co.f
Alternatively it looks like
co.f <- getFin(co, auto.assign = FALSE)
also works and is probably more straight forward.
Rather than writing a loop, you might consider the tidyquant package which enables multiple stocks to be passed to the tq_get() function. Setting tq_get(get = "financials") will allow you to download the financials for multiple stocks. Here's and example:
library(tidyquant)
c("FB", "AMZN", "NFLX", "GOOG") %>%
tq_get(get = "financials")
This returns a nested data frame of all the financial statement data (income statement, balance sheet, cashflow) in both annual and quarterly periods. You can use the unnest() function to peel away the layers.
If you need to save the data, you can unnest then write to a csv using the write_csv() function.