Code
library(quantmod)
library(tidyverse)
library(yahoofinancer)
library(Quandl)
library(plotly)
library(finreportr)
library(rvest)
with quantmod
Tony Duan
$frameId
[1] "6576FD94B556CF175D378FD965D36EB5"
$loaderId
[1] "C6A16C6616D37C0A2741BA44ADF3A92D"
[1] "An error occurred: Error in onRejected(...): code: -32000\n message: Cannot take screenshot with 0 height.\n"
Rows: 32
Columns: 6
$ breakdown <chr> "HTML_TAG_START HTML_TAG_END\nTotal Revenue", "Cost of Rev…
$ ttm <chr> "27,635,000", "14,446,000", "13,189,000", "5,114,000", "8,…
$ x10_31_2024 <chr> "27,176,000", "14,279,000", "12,897,000", "5,030,000", "7,…
$ x10_31_2023 <chr> "26,517,000", "14,133,000", "12,384,000", "4,730,000", "7,…
$ x10_31_2022 <chr> "25,785,000", "13,792,000", "11,993,000", "4,209,000", "7,…
$ x10_31_2021 <chr> "23,063,000", "12,149,000", "10,914,000", "3,714,000", "7,…
data002=data001 %>% mutate(breakdown=breakdown %>% str_remove_all('HTML_TAG_START HTML_TAG_END')
,ttm=round(as.numeric(gsub(",","",ttm)))
,year_2024=round(as.numeric(gsub(",","",x10_31_2024)))
,year_2022=round(as.numeric(gsub(",","",x10_31_2022)))
,year_2021=round(as.numeric(gsub(",","",x10_31_2021)))
,year_2023=round(as.numeric(gsub(",","",x10_31_2023)))
) %>% select(-x10_31_2023,-x10_31_2022,-x10_31_2021,-x10_31_2024)
Rows: 32
Columns: 6
$ breakdown <chr> "\nTotal Revenue", "Cost of Revenue", "Gross Profit", "\nOpe…
$ ttm <dbl> 27635000, 14446000, 13189000, 5114000, 8075000, -107000, NA,…
$ year_2024 <dbl> 27176000, 14279000, 12897000, 5030000, 7867000, 285000, NA, …
$ year_2022 <dbl> 25785000, 13792000, 11993000, 4209000, 7784000, -189000, 400…
$ year_2021 <dbl> 23063000, 12149000, 10914000, 3714000, 7200000, -118000, -31…
$ year_2023 <dbl> 26517000, 14133000, 12384000, 4730000, 7654000, 62000, NA, 7…
breakdown | ttm | year_2024 | year_2022 | year_2021 | year_2023 | |
---|---|---|---|---|---|---|
2 | Total Revenue | 27635000 | 27176000 | 25785000 | 23063000 | 26517000 |
3 | Cost of Revenue | 14446000 | 14279000 | 13792000 | 12149000 | 14133000 |
4 | Gross Profit | 13189000 | 12897000 | 11993000 | 10914000 | 12384000 |
5 | Operating Expense | 5114000 | 5030000 | 4209000 | 3714000 | 4730000 |
6 | Operating Income | 8075000 | 7867000 | 7784000 | 7200000 | 7654000 |
7 | Net Non Operating Interest Income Expense | -107000 | 285000 | -189000 | -118000 | 62000 |
8 | Other Income Expense | NA | NA | 4000 | -311000 | NA |
9 | Pretax Income | 7968000 | 8152000 | 7599000 | 6771000 | 7716000 |
10 | Tax Provision | 1625000 | 975000 | 1074000 | 883000 | 860000 |
11 | Net Income Common Stockholders | 6343000 | 7177000 | 6525000 | 5888000 | 6856000 |
12 | Diluted NI Available to Com Stockholders | 6343000 | 7177000 | 6525000 | 5888000 | 6856000 |
13 | Basic EPS | 8 | 9 | 7 | 6 | 8 |
14 | Diluted EPS | 8 | 9 | 7 | 6 | 8 |
15 | Basic Average Shares | 822750 | 827000 | 871000 | 910000 | 840000 |
16 | Diluted Average Shares | 829500 | 834000 | 877000 | 919000 | 845000 |
17 | Total Operating Income as Reported | 8075000 | 7867000 | 7788000 | 6889000 | 7654000 |
18 | Total Expenses | 19560000 | 19309000 | 18001000 | 15863000 | 18863000 |
19 | Net Income from Continuing & Discontinued Operation | 6343000 | 7177000 | 6525000 | 5888000 | 6856000 |
20 | Normalized Income | 6343000 | 7177000 | 6521564 | 6158570 | 6856000 |
21 | Interest Income | 145000 | 532000 | 39000 | 118000 | 300000 |
22 | Interest Expense | 252000 | 247000 | 228000 | 236000 | 238000 |
23 | Net Interest Income | -107000 | 285000 | -189000 | -118000 | 62000 |
24 | EBIT | 8220000 | 8399000 | 7827000 | 7007000 | 7954000 |
25 | EBITDA | 8626000 | 8791000 | 8271000 | 7401000 | 8469000 |
26 | Reconciled Cost of Revenue | 14446000 | 14279000 | 13792000 | 12149000 | 14133000 |
27 | Reconciled Depreciation | 406000 | 392000 | 444000 | 394000 | 515000 |
28 | Net Income from Continuing Operation Net Minority Interest | 6343000 | 7177000 | 6525000 | 5888000 | 6856000 |
29 | Total Unusual Items Excluding Goodwill | NA | NA | 4000 | -311000 | NA |
30 | Total Unusual Items | NA | NA | 4000 | -311000 | NA |
31 | Normalized EBITDA | 8626000 | 8791000 | 8267000 | 7712000 | 8469000 |
32 | Tax Rate for Calcs | 0 | 0 | 0 | 0 | 0 |
33 | Tax Effect of Unusual Items | NA | NA | 564 | -40430 | NA |
https://github.com/rsquaredacademy/yahoofinancer
https://github.com/sewardlee337/finreportr
https://www.youtube.com/watch?v=uVHGgSXtQmE
---
title: "Financial data"
subtitle: "with quantmod"
author: "Tony Duan"
execute:
warning: false
error: false
format:
html:
toc: true
toc-location: right
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
code-copy: true
---
```{r}
library(quantmod)
library(tidyverse)
library(yahoofinancer)
library(Quandl)
library(plotly)
library(finreportr)
library(rvest)
```
# stock price
```{r}
nifty_50 <- Index$new('^NSEI')
data001=nifty_50$get_history(start = '2023-01-01', interval = '1d') %>% unnest(cols = adj_close)
a=head(data001$adj_close,1)[[1]]
data001b=data001 %>% mutate(adj_close2=adj_close/a)
```
```{r}
length(unlist(data001$adj_close))
length(data001$adj_close)
```
```{r}
test=data001$adj_close %>% unlist()
```
```{r}
aapl <- Ticker$new('aapl')
data002=aapl$get_history(start = '2023-01-01', interval = '1d') %>% mutate(adj_close=adj_close %>% as.numeric())
a=head(data002$adj_close,1)[[1]]
data002b=data002 %>% mutate(adj_close2=adj_close/a)
```
```{r}
# initialize plot
fig <- plot_ly()
# add data from first dataframe Df1
fig <- fig %>%
add_lines(data=data001b, name="NSEI50", x = ~date, y = ~adj_close2)
# add data from second dataframe Df2
fig <- fig %>%
add_lines(data=data002b, name="apple", x = ~date, y = ~adj_close2)
# show figure
fig
```
# financials income statement
```{r}
library(chromote)
library(httr)
```
```{r}
url='https://finance.yahoo.com/quote/AMAT/financials'
```
```{r}
b <- ChromoteSession$new()
```
```{r}
# In a web browser, open a viewer for the headless browser. Works best with
# Chromium-based browsers.
#b$view()
b$Page$navigate(url)
```
```{r}
x <- b$DOM$getDocument()
```
```{r}
b$screenshot("sidebar.png")
```
```{r}
page=b$Runtime$evaluate("document.querySelector('html').outerHTML")$result$value %>%
read_html()
```
```{r}
b$close()
```
```{r}
col1=page%>%
html_elements(".row .column:nth-child(1)") %>% html_text2() %>% head(33)
```
```{r}
col2=page%>%
html_elements(".row .column:nth-child(2)") %>% html_text2()
```
```{r}
col3=page%>%
html_elements(".row .column:nth-child(3)") %>% html_text2()
```
```{r}
col4=page%>%
html_elements(".row .column:nth-child(4)") %>% html_text2()
```
```{r}
col5=page%>%
html_elements(".row .column:nth-child(5)") %>% html_text2()
```
```{r}
col6=page%>%
html_elements(".row .column:nth-child(6)") %>% html_text2()
```
```{r}
data001=data.frame(col1,col2,col3,col4,col5,col6)
```
```{r}
library(janitor )
colnames(data001) <- data001[1,]
data001 <- data001[-1, ] %>%clean_names()
```
```{r}
glimpse(data001)
```
```{r}
data002=data001 %>% mutate(breakdown=breakdown %>% str_remove_all('HTML_TAG_START HTML_TAG_END')
,ttm=round(as.numeric(gsub(",","",ttm)))
,year_2024=round(as.numeric(gsub(",","",x10_31_2024)))
,year_2022=round(as.numeric(gsub(",","",x10_31_2022)))
,year_2021=round(as.numeric(gsub(",","",x10_31_2021)))
,year_2023=round(as.numeric(gsub(",","",x10_31_2023)))
) %>% select(-x10_31_2023,-x10_31_2022,-x10_31_2021,-x10_31_2024)
```
```{r}
glimpse(data002)
```
```{r}
options(scipen = 999)
library(knitr)
kable(data002)
```
# resource:
https://github.com/rsquaredacademy/yahoofinancer
https://github.com/sewardlee337/finreportr
https://www.youtube.com/watch?v=uVHGgSXtQmE