finreportr
12finreportr
- Financial Data from U.S. Securities and Exchange Commission 팩키지를 통해 기본적인 재무정보를 이해한다.
CompanyInfo()
: 회사정보AnnualReports()
: 사업보고서GetIncome()
: 손익계산서GetBalanceSheet()
: 대차대조표GetCashFlow()
: 현금흐름표미국 증권시장에서 거래되고 있는 반도체 업체를 NYSE와 NASDAQ으로 나눠 업체목록을 작성해 보자.
library(tidyverse)
library(rvest)
## NYSE 반도체 상장사
nyse_semicon_url <- "https://topforeignstocks.com/stock-lists/the-complete-list-of-semiconductor-stocks-trading-on-the-nyse/"
nyse_semicon_tickers <- nyse_semicon_url %>%
read_html() %>%
html_nodes(css = '#tablepress-2661') %>%
html_table(header = TRUE, fill = TRUE) %>%
.[[1]]
nyse_semicon_df <- nyse_semicon_tickers %>%
janitor::clean_names() %>%
as_tibble() %>%
mutate(exchange = "NYSE")
## NASDAQ 반도체 상장사
nasdaq_semicon_url <- "https://topforeignstocks.com/stock-lists/the-complete-list-of-semiconductors-stocks-trading-on-nasdaq/"
nasdaq_semicon_tickers <- nasdaq_semicon_url %>%
read_html() %>%
html_nodes(xpath = '//*[@id="tablepress-2894"]') %>%
html_table(header = TRUE, fill = TRUE) %>%
.[[1]]
nasdaq_semicon_df <- nasdaq_semicon_tickers %>%
janitor::clean_names() %>%
as_tibble() %>%
mutate(exchange = "NASDAQ")
semicon_df <- bind_rows(nyse_semicon_df, nasdaq_semicon_df) %>%
select(exchange, ticker, name)
semicon_df
# A tibble: 93 x 3
exchange ticker name
<chr> <chr> <chr>
1 NYSE ASX ASE Technology Holding Co., Ltd.
2 NYSE DQ DAQO New Energy Corp.
3 NYSE IPHI Inphi Corporation
4 NYSE JKS JinkoSolar Holding Company Limited
5 NYSE MX MagnaChip Semiconductor Corporation
6 NYSE MXL MaxLinear, Inc
7 NYSE NPTN NeoPhotonics Corporation
8 NYSE SOL Renesola Ltd.
9 NYSE SQNS Sequans Communications S.A.
10 NYSE STM STMicroelectronics N.V.
# … with 83 more rows
finreportr
팩키지 CompanyInfo()
함수를 사용해서 회사정보를 가져온다.
library(finreportr)
semicon_fin_df <- map_df(semicon_df$ticker, CompanyInfo)
semicon_fin_list <- list()
for(i in 1:nrow(semicon_df)) {
cat(i, ": \n")
if(semicon_df$ticker[i] %in% c("LPTH", "MLNX")) {
next
} else {
semicon_fin_list[[i]] <- CompanyInfo(semicon_df$ticker[i])
}
}
semicon_fin_df <- map_df(semicon_fin_list, rbind)
semicon_fin_df %>%
write_rds("data/semicon_fin_df.rds")
상장된 반도체 업체 기본정보를 표를 통해 일별한다.
시가총액(Market Capitalization)을 가져오기가 좀 까다롭게 되어 Stackoverflow, “Financial Analysis in R - hoping to add Market Cap, P/FCF, EV/EBITDA, and Shareholder Yield to dataset”을 참조하여 시총을 가져온다.
library(quantmod)
library(rvest)
library(XML)
# start_date <- as.Date("2019-12-01")
# end_date <- as.Date("2019-12-31")
# getSymbols(c("JPM", "WFC"), src = "yahoo", from = start_date, to = end_date)
get_marcap <- function(ticker) {
url <- glue::glue("http://finviz.com/quote.ashx?t={ticker}")
webpage <- readLines(url)
html <- htmlTreeParse(webpage, useInternalNodes = TRUE, asText = TRUE)
tableNodes <- getNodeSet(html, "//table")
df <- readHTMLTable(tableNodes[[9]],
header = c("data1", "data2", "data3", "data4", "data5", "data6",
"data7", "data8", "data9", "data10", "data11", "data12")) %>%
select(metric = data1, value = data2) %>%
filter(str_detect(metric, "Cap"))
return(df)
}
safe_get_marcap <- safely(get_marcap)
semicon_marcap_list <- map(semicon_df$ticker, ~ safe_get_marcap(.x))
semicon_marcap_list %>%
write_rds("data/semicon_marcap_list.rds")
시가총액 금액을 기계가 처리할 수 있는 형태로 숫자형으로 변형시켜 표형태로 추출한다.
semicon_marcap_list <-
read_rds("data/semicon_marcap_list.rds")
semicon_marcap_rs_list <- map(semicon_marcap_list, "result")
marcap_value <- pluck(semicon_marcap_rs_list, "value") %>%
map_chr(toString)
semicon_df %>%
mutate(marcap = marcap_value) %>%
mutate(unit = str_extract(marcap, ".$"),
value = parse_number(marcap)) %>%
mutate(market_cap = ifelse(unit == "B", value * 10^9, value * 10^6)) %>%
arrange(desc(market_cap)) %>%
# top_n(10, wt=market_cap) %>%
reactable::reactable()
TSMC가 가장 높은 시총을 갖는 것이 확인되어 TSMC 손익계산서를 가져오는 대신 인텔로 대신하여 손익계산서를 가져온다.
이번에는 인텔의 대차대조표를 가져와보자.
인텔의 현금흐름표를 가져와보자.
filing.name filing.date accession.no
1 10-K 2020-01-24 0000050863-20-000011
2 10-K 2019-02-01 0000050863-19-000007
3 10-K 2018-02-16 0000050863-18-000007
4 10-K 2017-02-17 0000050863-17-000012
5 10-K 2016-02-12 0000050863-16-000105
6 10-K 2015-02-13 0000050863-15-000015
7 10-K 2014-02-14 0000050863-14-000020
8 10-K 2013-02-19 0001193125-13-065416
9 10-K 2012-02-23 0001193125-12-075534
10 10-K 2011-02-18 0000950123-11-015783
11 10-K 2010-02-22 0000950123-10-015237
12 10-K 2009-02-23 0000891618-09-000047
13 10-K 2008-02-20 0000891618-08-000106
14 10-K 2007-02-26 0000891618-07-000111
15 10-K 2006-02-27 0000891618-06-000089
16 10-K 2005-02-22 0001193125-05-033162
17 10-K/A 2004-02-24 0001193125-04-028053
18 10-K 2004-02-23 0001193125-04-027430
19 10-K 2003-03-11 0001047469-03-008257
20 10-K 2002-03-13 0000912057-02-009698
21 10-K 2001-03-13 0000912057-01-503434
22 10-K 2000-03-23 0001012870-00-001562
23 10-K 1999-03-26 0001047469-99-011450
24 10-K 1998-03-27 0000050863-98-000031
25 10-K 1997-03-28 0000050863-97-000020
26 10-K 1996-03-29 0000050863-96-000002
27 10-K 1995-03-28 0000050863-95-000004
28 10-K 1994-03-25 0000891618-94-000087
데이터 과학자 이광춘 저작
kwangchun.lee.7@gmail.com