# Load required libraries
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)
# Scrape financial data from Finviz for EnerSys (ENS)
ens_url <- "https://finviz.com/quote.ashx?t=ENS"
ens_page <- read_html(ens_url)
ens_data <- ens_page %>%
html_nodes("table.snapshot-table2") %>%
html_table(fill = TRUE)
# Convert to DataFrame
ens_fins <- as.data.frame(ens_data[[1]])
# Get EnerSys stock price data from Yahoo Finance
getSymbols("ENS", src = "yahoo", from = "2020-01-01", to = Sys.Date())
## [1] "ENS"
# Extract closing prices
ens_prices <- Cl(ENS)
# Plot EnerSys stock prices
autoplot(ens_prices) + ggtitle("EnerSys Stock Prices")
# Calculate daily log returns
ens_returns <- dailyReturn(ENS, type = "log")
# Plot daily log returns
ggplot(data = as.data.frame(ens_returns), aes(x = index(ENS), y = coredata(ens_returns))) +
geom_line() +
ggtitle("EnerSys Daily Log Returns") +
xlab("Date") +
ylab("Log Returns")
# Extract key financial ratios from Finviz data
pe_ratio <- ens_fins[ens_fins$X1 == "P/E", "X2"]
debt_eq_ratio <- ens_fins[ens_fins$X1 == "Debt/Eq", "X2"]
roe <- ens_fins[ens_fins$X1 == "ROE", "X2"]
# Print financial ratios
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: "
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE
parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.