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:
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(ggplot2)
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(kableExtra)
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
library(TTR) # For moving averages
ticker <- "MSFT" # Microsoft Corp.
finviz_url <- paste0("https://finviz.com/quote.ashx?t=", ticker)
webpage <- read_html(finviz_url)
data <- webpage %>%
html_nodes("table.snapshot-table2") %>%
html_table(fill = TRUE)
if (length(data) > 0) {
financials <- data[[1]]
colnames(financials) <- c("Metric", "Value")
} else {
financials <- data.frame(Metric = NA, Value = NA)
}
# Display Financials as a Table
kable(financials, caption = paste("Key Financials for", ticker))
Metric | Value | NA | NA | NA | NA | NA | NA | NA | NA | NA | NA |
---|---|---|---|---|---|---|---|---|---|---|---|
Index | DJIA, NDX, S&P 500 | P/E | 30.65 | EPS (ttm) | 12.42 | Insider Own | 1.47% | Shs Outstand | 7.43B | Perf Week | -2.03% |
Market Cap | 2829.30B | Forward P/E | 25.46 | EPS next Y | 14.95 | Insider Trans | -0.09% | Shs Float | 7.32B | Perf Month | -8.47% |
Income | 92.75B | PEG | 2.14 | EPS next Q | 3.23 | Inst Own | 73.27% | Short Float | 0.73% | Perf Quarter | -12.99% |
Sales | 261.80B | P/S | 10.81 | EPS this Y | 11.46% | Inst Trans | 1.36% | Short Ratio | 2.38 | Perf Half Y | -6.81% |
Book/sh | 40.71 | P/B | 9.35 | EPS next Y | 13.65% | ROA | 18.47% | Short Interest | 53.63M | Perf Year | -5.35% |
Cash/sh | 9.63 | P/C | 39.54 | EPS next 5Y | 14.33% | ROE | 34.29% | 52W Range | 381.00 - 468.35 | Perf YTD | -9.71% |
Dividend Est. | 3.27 (0.86%) | P/FCF | 40.40 | EPS past 5Y | 18.45% | ROI | 23.60% | 52W High | -18.74% | Beta | 0.99 |
Dividend TTM | 3.16 (0.83%) | Quick Ratio | 1.34 | Sales past 5Y | 14.40% | Gross Margin | 69.41% | 52W Low | -0.11% | ATR (14) | 9.86 |
Dividend Ex-Date | Feb 20, 2025 | Current Ratio | 1.35 | EPS Y/Y TTM | 12.38% | Oper. Margin | 44.96% | RSI (14) | 32.76 | Volatility | 2.89% 2.05% |
Employees | 228000 | Debt/Eq | 0.34 | Sales Y/Y TTM | 15.04% | Profit Margin | 35.43% | Recom | 1.30 | Target Price | 508.26 |
Option/Short | Yes / Yes | LT Debt/Eq | 0.30 | EPS Q/Q | 10.23% | Payout | 25.42% | Rel Volume | 1.29 | Prev Close | 393.31 |
Sales Surprise | 1.11% | EPS Surprise | 3.54% | Sales Q/Q | 12.27% | Earnings | Jan 29 AMC | Avg Volume | 22.52M | Price | 380.59 |
SMA20 | -5.33% | SMA50 | -8.62% | SMA200 | -10.47% | Trades | Volume | 10,083,307 | Change | -3.23% |
# Get Stock Data from Yahoo Finance
getSymbols(ticker, src = "yahoo", from = Sys.Date() - 180, to = Sys.Date())
## [1] "MSFT"
# Calculate Daily Returns
stock_data <- get(ticker)
stock_data$Daily_Return <- dailyReturn(Cl(stock_data))
# Display Summary Statistics
summary(stock_data$Daily_Return)
## Index Daily_Return
## Min. :2024-09-11 Min. :-0.0618090
## 1st Qu.:2024-10-23 1st Qu.:-0.0068034
## Median :2024-12-05 Median : 0.0004097
## Mean :2024-12-07 Mean :-0.0004965
## 3rd Qu.:2025-01-22 3rd Qu.: 0.0068312
## Max. :2025-03-07 Max. : 0.0413069
# Calculate 20-day Moving Average
stock_data$Moving_Avg <- SMA(Cl(stock_data), n = 20)
# Plot Stock Price & Moving Average
chartSeries(stock_data,
theme = chartTheme("white"),
name = paste("Stock Price of", ticker),
TA = "addSMA(n=20, col='red')")
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.