bergant / finstr

:chart_with_upwards_trend: Financial statements in R
250 stars 69 forks source link

Outstanding shares from 10k or 10q #6

Open dmaupin12 opened 7 years ago

dmaupin12 commented 7 years ago

Great package! Is there a way to pull outstanding shares from the qs and ks? Its usually listed on the first page of the report so I'm sure it's there, just haven't figured out how to access it.

bergant commented 7 years ago

There is no special support (yet) in finstr. But, when the id of the XBRL concept is known, it is possible to extract it from the XBRL directly. The sec.gov page (interactive data) exposes the concept id when clicked on the description in the table.

To extract the value from XBRL, join the element (concept) to the fact (value) and context (period, dimensions). Here is an example for the dei_EntityCommonStockSharesOutstanding in 10-K:

# Apple 10-K report:
doc_url <- "https://www.sec.gov/Archives/edgar/data/320193/000162828016020309/aapl-20160924.xml"

# download and parse XBRL
library(XBRL)
old_o <- options(stringsAsFactors = FALSE)
xbrl_data <- xbrlDoAll(doc_url)
options(old_o)

library(dplyr)

xbrl_data$element %>% 
  filter(elementId == "dei_EntityCommonStockSharesOutstanding") %>% 
  left_join(xbrl_data$fact, by = "elementId") %>% 
  left_join(xbrl_data$context, by = "contextId") %>% 
  mutate(value = as.numeric(fact) * 10 ^ as.numeric(decimals)) %>% 
  select(elementId, value, unitId, endDate)

#                                elementId   value unitId    endDate
# 1 dei_EntityCommonStockSharesOutstanding 5332313 shares 2016-10-14
dmaupin12 commented 7 years ago

excellent, thanks for the help.

Satya2IB commented 7 years ago

Thanks bergant for this excellent package working smoothly with SEC filings!

I am particularly looking for debt as reported in the balance sheet and debt breakup as given in the Notes to Financial Statement. The id of the XBRL concept is known: us-gaap_LongTermDebtNoncurrent

I ran into some error while running the query:

install.packages("devtools") devtools::install_github("bergant/finstr")

Library(XBRL) xbrl_url17 <- https://www.sec.gov/Archives/edgar/data/320193/000032019317000009/aapl-20170701.xml

old.o <- options(stringsAsFactors = FALSE) xbrl_data17 <- xbrlDoAll(xbrl_url17) options(old.o)

library(dplyr)

xbrl_data17$element %>% filter(elemntId =="us-gaap_LongTermDebtNoncurrent") %>% left_join(xbrl_data17$fact, by = "elementId") %>% left_join(xbrl_data17$context, by = "contextId") %>% mutate(value = as.numeric(fact) * 10 ^ as.numeric(decimals)) %>% select(elementId, value, unitId, endDate) Error in filter(elemntId == "us-gaap_LongTermDebtNoncurrent") : object 'elemntId' not found

Can you pls help on this? How can I get this debt number as well as debt breakup as pasted from the footnotes

image

Kind regards,

bergant commented 7 years ago

First check the missing e in elemntId (in the filter command...)

Satya2IB commented 7 years ago

Thanks for pointing this. Still, I am not very much sure why I am getting an error : "Error in filter_impl(.data, quo) : Result must have length 20798, not 166384"

Pls see my code below

install.packages("devtools") devtools::install_github("bergant/finstr")

# Parsing AAPL 10Q ended July 2017 from SEC with XBRL package library(XBRL) xbrl_url17q <- "https://www.sec.gov/Archives/edgar/data/320193/000032019317000009/aapl-20170701.xml"

old.o <- options(stringsAsFactors = FALSE) xbrl_data17 <- xbrlDoAll(xbrl_url17q) options(old.o)

# after identifying the relevant element ID for long term debt: "us-gaap_LongTermDebtNoncurrent"

xbrl_data17$element %>% filter(xbrl_data17$element == "us-gaap_LongTermDebtNoncurrent") %>% left_join(xbrl_data17$fact, by = "elementID") %>% left_join(xbrl_data17$context, by = "contextID") %>% mutate(value = as.numeric (fact) * 10 ^ as.numeric(decimals)) %>% select( elementID, value, unitID, endDate)

This still shows me the error: Error in filter_impl(.data, quo) : Result must have length 20798, not 166384

Best,

bergant commented 7 years ago

The following code

xbrl_data17$element %>%
  filter(elementId == "us-gaap_LongTermDebtNoncurrent") %>%
  left_join(xbrl_data17$fact, by = "elementId") %>%
  left_join(xbrl_data17$context, by = "contextId") %>%
  mutate(value = as.numeric (fact) * 10 ^ as.numeric(decimals)) %>%
  select( elementId, value, unitId, endDate)

will return only values for the us-gaap_LongTermDebtNoncurrent element:

#>                        elementId value unitId    endDate
#> 1 us-gaap_LongTermDebtNoncurrent 75427    usd 2016-09-24
#> 2 us-gaap_LongTermDebtNoncurrent 89864    usd 2017-07-01

For debt summary table you should find all elements from "http://www.apple.com/role/DebtSummaryOfTermDebtDetails" role, find their hierarchy and add facts and contexts.

Maybe we can start like this (this will not create a final table):

relations <- 
  finstr::xbrl_get_relations(xbrl_data17, role_id = "http://www.apple.com/role/DebtSummaryOfTermDebtDetails", lbase = "presentation")

elements <-
  data.frame( 
    elementId = with(relations, unique(c(fromElementId, toElementId))),
    stringsAsFactors = FALSE
  )  %>%
  dplyr::left_join(xbrl_data17$element, by = c("elementId")) %>%
  dplyr::left_join(relations, by = c("elementId" = "toElementId")) %>%
  dplyr::left_join(xbrl_data17$label, by = c("elementId")) %>%
  dplyr::filter(labelRole == "http://www.xbrl.org/2003/role/label") %>% 
  dplyr::transmute(elementId, parentId = fromElementId, order, type, balance, labelString)

elements_h <- finstr::get_elements_h(elements)

elements_h %>% 
  filter(type != "nonnum:domainItemType") %>% 
  left_join(xbrl_data17$fact, by = "elementId") %>% 
  left_join(xbrl_data17$context, by = "contextId") %>%
  select(id, elementId, order, labelString, fact, unitId, startDate, endDate, type, contextId) %>% 
  arrange(id)

# ...
joeymizrahi commented 6 years ago

Hi, I want to extract the Balance Sheet,Cash flow and income statement tables from 10-k and 10-q filings. can this package help me?