Is there some place I can programmatically download the ROIC and other data typically reported in a company's quarter report?
I know that I can access the daily price data of a stock from http://chart.yahoo.com/table.csv, but I can't find anything about the financial performance.
Thanks!
Intrinio provides that data in R using the httr package. You can follow the instructions here, I'll modify them here to get ROIC:
#Install httr, which you need to request data via API
install.packages("httr")
require("httr")
#Create variables for your usename and password, get those at intrinio.com/login
username <- "Your_API_Username"
password <- "Your_API_Password"
#Making an api call for roic. This puts together the different parts of the API call
base <- "https://api.intrinio.com/"
endpoint <- "data_point"
stock <- "T"
item1 <- "roic"
call1 <- paste(base,endpoint,"?","ticker","=", stock, "&","item","=",item1, sep="")
#Now we use the API call to request the data from Intrinio's database
ATT_roic <- GET(call1, authenticate(username,password, type = "basic"))
#That gives us the ROIC value, but it isn't in a good format so we parse it
test1 <- unlist(content(ATT_roic,"parsed"))
df <- data.frame(test1)
You can modify that code for any US ticker, and you can change roic out for hundreds of other financial metrics. If you want to pull historical roic, or roic for a specific date range, see the instructions I posted at the start of this answer.
Related
I am unable to even run the example code given on the botrnot documentation. Unsure what's happening.
# libraries
library(rtweet)
library(tweetbotornot)
# authentication for twitter API
auth <- rtweet_app()
auth_setup_default()
users <- c("kearneymw", "geoffjentry", "p_barbera",
"tidyversetweets", "rstatsbot1234", "RStatsStExBot")
## get most recent 10 tweets from each user
tmls <- get_timeline(users, n = 10)
## pass the returned data to botornot()
data <- botornot(tmls)
Expecting data frame titled data should be created and have an additional column that is the probability of the user being a bot. Instead I have this error.
?Error in botornot.data.frame(tmls) : "user_id" %in% names(x) is not TRUE
The table in the bottom of the documentation is what I'm hoping to achieve.
https://www.rdocumentation.org/packages/botrnot/versions/0.0.2
I have setup an API access key with a data provider of stock market data. With this key i am able to extract stock market data based on ticker code (E.g. APPL: Apple, FB: Facebook etc).
I am able to extract stock data on an individual ticker basis using R but I want to write a piece of code that extracts data based on the multiple stock tickers and puts them all in one data frame (the structure is the same for all stocks). I m not sure how to create a loop that updates the data frame each time stock data is extracted. I get an error called 'No encoding supplied: defaulting to UTF-8' which does not tell me much. A point in the right direction would be helpful.
I have the following code:
if (!require("httr")) {
install.packages("httr")
library(httr)
}
if (!require("jsonlite")) {
install.packages("jsonlite")
library(jsonlite)
}
stocks <- c("FB","APPL") #Example stocks actual stocks removed
len <- length(stocks)
url <- "URL" #Actual url removed
access_key <- "MY ACCESS KEY" #Actual access key removed
extraction <- lapply(stocks[1:len],function(i){
call1 <- paste(url,"?access_key=",access_key,"&","symbols","=",stocks[i],sep="")
get_prices <- GET(call1)
get_prices_text <- content(get_prices, "text")
get_prices_json <- fromJSON(get_prices_text, flatten = TRUE)
get_prices_df <- as.data.frame(get_prices_json)
return(get_prices_df)
}
)
file <- do.call(rbind,extraction)
I realised that this is not the most efficient way of doing this. A better way is to update the url to include multiple stocks rather then using a lapply function. I am therefore closing the question.
I am using the Quandl API in R to download the historical stock market data listed in NSE India.
The below code gives me the historical data for ICICI and PNB but needs manual entries for each stock to fetch the data. How do we download the historical data for all the stocks listed in NSE without writing these manual statements for each stock.
library(Quandl)
Quandl.api_key("API_Key")
## Download the data Set
ICICI = Quandl("NSE/ICICIBANK",collapse="daily",start_date="2018-01-01",type="raw")
PNB= Quandl("NSE/PNB",collapse="daily",start_date="2018-01-01",type="raw")
## Add another ("Stock") coloumn in Datasets using cbind command
ICICI<-cbind(ICICI,Stock="")
PNB<-cbind(PNB,Stock="")
## Paste the stock name in stock column
ICICI$Stock<-paste(ICICI$Stock,"ICICI",sep="")
PNB$Stock<-paste(PNB$Stock,"PNB",sep="")
## Consolidate under one dataset
Master_Data<-rbind(ICICI,PNB)
I do have a list of all the stocks name in an excel file as follows.
NSE/20MICRONS
NSE/3IINFOTECH
NSE/3MINDIA
NSE/A2ZMES
NSE/AANJANEYA
NSE/AARTIDRUGS
NSE/AARTIIND
NSE/AARVEEDEN
NSE/ABAN
NSE/ABB
NSE/ABBOTINDIA
NSE/ABCIL
NSE/ABGSHIP
Any help would be really appreciated.
Regards,
Akash
This worked for me, I hope it will be running for you too.
I have tested it.
Make a list (lyst in the code) items of all the companies for you want the data, like below. Then use lapply to save everything in a new list, like lyst_dwnld in the code.
If you want to avoid manually typing all that name as list, then you can save your excel sheet of names as a data frame and then use the same concept as below.
Code:
lyst <- list(icici = "NSE/ICICIBANK", pnb = "NSE/PNB")
lyst_dwnld <- lapply(names(lyst),
function(x)Quandl(lyst[[x]],
collapse="daily",
start_date="2018-01-01",type="raw"))
Output:
YOu can check if the data is downloaded or not, by quickly seeing the head.
> lapply(lyst_dwnld, head,2)
[[1]]
Date Open High Low Last Close Total Trade Quantity Turnover (Lacs)
1 2018-05-25 298.4 300.95 294.6 296.20 295.65 13541580 40235.19
2 2018-05-24 293.7 299.00 291.2 298.15 297.70 11489424 33952.28
[[2]]
Date Open High Low Last Close Total Trade Quantity Turnover (Lacs)
1 2018-05-25 81.95 84.55 81.30 83.60 83.35 19102160 15875.32
2 2018-05-24 80.70 82.50 80.05 82.35 82.10 19933989 16229.67
EDITED:
In you are unable to make a list using a dataframe, here is what you can do.
1) Read your data in excel to R dataframe using (readxl).
2) I have read a sample data and called it df.
df <- readxl::read_excel('path_where_excel_resides_with_name_of_excel_and_extension')
3) Name the column, something meaningful. I just used "name" here for this single column excel.
names(df) <- "name"
4) Use strsplit, to split the column(keep the original column), fetech only the second name out of it.
df$co_name <- lapply(strsplit(df$name, "/"),`[[`,2)
Now you can create the lyst object which is used in earlier code.
lyst <- as.list(df$name)
names(lyst) <- df$co_name
Please let me know in case this doesn't work out for you or in case of any issues. Thanks
The data is available until 2018 only via Quandl.
You can use BatchGetSymbols library in R to download further data. There is one issue that I noticed while downloading from Yahoo finance was that sometimes the data was missing for a few months in a year. Advised to use with caution before implementing any analysis and check the consistency of the data.
The following code is for reference:
if (!require(BatchGetSymbols)) install.packages('BatchGetSymbols')
#Package to download yahoo finance stock prices from internet directly
library(BatchGetSymbols)
first.date <- Sys.Date() - (365*5) #5 years back
last.date <- Sys.Date()
freq.data <- 'monthly' #change it as you like. example:'daily'
tickers <- c('RELIANCE.NS','TCS.NS')
# Reliance, Tcs
l.out <- BatchGetSymbols(tickers = tickers,
first.date = first.date,
last.date = last.date,
freq.data = freq.data,
cache.folder = file.path(your_directory_path,"stock_prices"))
str(l.out)
# To check download status of all stocks
l.out$df.control
# Actual Stock prices downloaded
head(l.out$df.tickers)
Alternate Method
I found this link during my web browsing and I am yet to implement it after understanding. Hopefully, this might help you further.
https://www.r-bloggers.com/extracting-eod-data-from-nse/
I'm pulling large sets of data for multiple sites and views from Google Analytics for processing in R. To streamline the process, I've added my most common queries to a function (so I only have to pass the profile ID and date range). Each query is stored as a local variable in the function, and is assigned to a dynamically-named global variable:
R version 3.1.1
library(rga)
library(stargazer)
# I would add a dataset, but my data is locked down by client agreements and I don't currently have any test sites configured.
profiles <- ga$getProfiles()
website1 <- profiles[1,]
start <- "2013-01-01"
end <- "2013-12-31"
# profiles are objects containing all the ID's, accounts #, etc.; start and end specify date range as strings (e.g. "2014-01-01")
reporting <- function(profile, start, end){
id <- profile[,1] #sets profile number from profile object
#rga function for building and submitting query to API
general <- ga$getData(id,
start.date = start,
end.date = end,
metrics = "ga:sessions")
... #additional queries, structured similarly to example above(e.g. countries, cities, etc.)
#transforms name of profile object to string
profileName <- deparse(substitute(profile))
#appends "Data" to profile object name
temp <- paste(profileName, "Data", sep="")
#stores query results as list
temp2 <- list(general,countries,cities,devices,sources,keywords,pages,events)
#assigns list of query results and stores it globally
assign(temp, temp2, envir=.GlobalEnv)
}
#call reporting function at head of report or relevant section
reporting(website1,start,end)
#returns list of data frames returned by the ga$getData(...), but within the list they are named "data.frame" instead of their original query name.
#generate simple summary table with stargazer package for display within the report
stargazer(website1[1])
I'm able to access these results through *website1*Data[1], but I'm handing the data off to collaborators. Ideally, they should be able to access the data by name (e.g. *website1*Data$countries).
Is there an easier/better way to store these results, and to make accessing them easier from within an .Rmd report?
There's no real reason to do the deparse in side the function just to assign a variable in the parent environment. If you have to call the reporting() function, just have that function return a value and assign the results
reporting <- function(profile, start, end){
#... all the other code
#return results
list(general=general,countries=countries,cities=cities,
devices=devices,sources=sources,keywords=keywords,
pages=pages,events=events)
}
#store results
websiteResults <- reporting(website1,start,end)
I am looking for ways to get company description, key statistics, chairman name from Yahoo Finance (or other financial website) using R, for example package quantmod.
There is oodles of info how to get current and historical prices etc, but this is not what I want.
best,
This R package does not support queries for Asian bourses. The problem appears to be with the underlying Yahoo APIs.
You can get that using Intrinio's API. Their data tag directory allows you to look up the tags you want, in your case, "long_description" and "ceo" will get you the data you want:
#Install httr, which you need to request data via API
install.packages("httr")
require("httr")
#Create variables for your usename and password, get those at intrinio.com/login
username <- "Your_API_Username"
password <- "Your_API_Password"
#Making an api call for roic. This puts together the different parts of the API call
base <- "https://api.intrinio.com/"
endpoint <- "data_point"
stock <- "T"
item1 <- "long_description"
item2 <- "ceo"
#Pasting them together to make the API call
call1 <- paste(base,endpoint,"?","ticker","=", stock, "&","item","=",item1, sep="")
call2 <- paste(base,endpoint,"?","ticker","=", stock, "&","item","=",item2, sep="")
#Now we use the API call to request the data from Intrinio's database
ATT_description <- GET(call1, authenticate(username,password, type = "basic"))
ATT_CEO <- GET(call2, authenticate(username,password, type = "basic"))
#That gives us the ROIC value, but it isn't in a good format so we parse it
test1 <- unlist(content(ATT_description,"parsed"))
test2 <- unlist(content(ATT_CEO,"parsed"))
#Then make your data frame:
df1 <- data.frame(test1)
df2 <- data.frame(test2)
#From here you can rbind or cbind, and create loops to get the same data for many tickers
You can get your API keys here. Full API documentation here. This is what the CEO dataframe looks like: