install.packages(c("tidyverse", "rvest", "quantmod", "ggplot2", "xts", "dplyr"))
## Installing packages into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(rvest)
##
## Attaching package: 'rvest'
##
## The following object is masked from 'package:readr':
##
## guess_encoding
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(ggplot2)
library(xts)
library(dplyr)
netflix_url <- "https://finviz.com/quote.ashx?t=NFLX"
netflix_page <- read_html(netflix_url)
netflix_data <- netflix_page %>%
html_nodes("table.snapshot-table2") %>%
html_table(fill = TRUE)
netflix_fins <- as.data.frame(netflix_data[[1]])
print(netflix_fins)
## X1 X2 X3 X4 X5 X6
## 1 Index NDX, S&P 500 P/E 44.94 EPS (ttm) 19.83
## 2 Market Cap 381.18B Forward P/E 29.28 EPS next Y 30.44
## 3 Income 8.71B PEG 1.97 EPS next Q 5.73
## 4 Sales 38.88B P/S 9.80 EPS this Y 25.12%
## 5 Book/sh 57.84 P/B 15.41 EPS next Y 22.68%
## 6 Cash/sh 22.41 P/C 39.76 EPS next 5Y 22.77%
## 7 Dividend Est. - P/FCF 55.07 EPS past 5Y 36.85%
## 8 Dividend TTM - Quick Ratio 1.22 Sales past 5Y 14.25%
## 9 Dividend Ex-Date - Current Ratio 1.22 EPS Y/Y TTM 65.72%
## 10 Employees 14000 Debt/Eq 0.73 Sales Y/Y TTM 15.28%
## 11 Option/Short Yes / Yes LT Debt/Eq 0.64 EPS Q/Q 102.15%
## 12 Sales Surprise 1.37% EPS Surprise 1.71% Sales Q/Q 15.40%
## 13 SMA20 -10.53% SMA50 -6.21% SMA200 14.32%
## X7 X8 X9 X10 X11
## 1 Insider Own 0.72% Shs Outstand 427.76M Perf Week
## 2 Insider Trans -20.28% Shs Float 424.69M Perf Month
## 3 Inst Own 83.75% Short Float 1.66% Perf Quarter
## 4 Inst Trans 0.04% Short Ratio 1.84 Perf Half Y
## 5 ROA 16.64% Short Interest 7.04M Perf Year
## 6 ROE 38.43% 52W Range 542.01 - 1064.50 Perf YTD
## 7 ROI 21.50% 52W High -16.29% Beta
## 8 Gross Margin 45.88% 52W Low 64.41% ATR (14)
## 9 Oper. Margin 26.47% RSI (14) 33.40 Volatility
## 10 Profit Margin 22.41% Recom 1.78 Target Price
## 11 Payout 0.00% Rel Volume 2.02 Prev Close
## 12 Earnings Jan 21 AMC Avg Volume 3.83M Price
## 13 Trades Volume 7,725,055 Change
## X12
## 1 -9.12%
## 2 -11.87%
## 3 -1.23%
## 4 31.11%
## 5 48.89%
## 6 -0.02%
## 7 1.58
## 8 34.26
## 9 4.72% 3.15%
## 10 1081.36
## 11 906.36
## 12 891.11
## 13 -1.68%
getSymbols("NFLX", src = "yahoo", from = "2020-01-01", to = Sys.Date())
## [1] "NFLX"
netflix_prices <- Cl(NFLX) # Extract closing prices
autoplot(netflix_prices) + ggtitle("Netflix Stock Prices")

netflix_returns <- dailyReturn(NFLX, type = "log")
ggplot(data = as.data.frame(netflix_returns), aes(x = index(NFLX), y = coredata(netflix_returns))) +
geom_line() +
ggtitle("Netflix Daily Log Returns") +
xlab("Date") +
ylab("Log Returns")

pe_ratio <- netflix_fins[netflix_fins$X1 == "P/E", "X2"]
debt_eq_ratio <- netflix_fins[netflix_fins$X1 == "Debt/Eq", "X2"]
roe <- netflix_fins[netflix_fins$X1 == "ROE", "X2"]
print(paste("P/E Ratio:", pe_ratio))
## [1] "P/E Ratio: "
print(paste("Debt/Equity Ratio:", debt_eq_ratio))
## [1] "Debt/Equity Ratio: "
print(paste("Return on Equity:", roe))
## [1] "Return on Equity: "