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)
metaplatform_url <- "https://finviz.com/quote.ashx?t=META&ty=c&ta=1&p=d"
metaplatform_page <- read_html(metaplatform_url)

metaplatform_data <- metaplatform_page %>%
  html_nodes("table.snapshot-table2") %>%
  html_table(fill = TRUE)

metaplatform_fins <- as.data.frame(metaplatform_data[[1]])
print(metaplatform_fins)
##                  X1           X2            X3     X4            X5     X6
## 1             Index NDX, S&P 500           P/E  26.15     EPS (ttm)  23.92
## 2        Market Cap     1585.21B   Forward P/E  21.77    EPS next Y  28.73
## 3            Income       62.36B           PEG   2.28    EPS next Q   5.24
## 4             Sales      164.50B           P/S   9.64    EPS this Y  5.68%
## 5           Book/sh        72.07           P/B   8.68    EPS next Y 13.96%
## 6           Cash/sh        30.85           P/C  20.28   EPS next 5Y 11.47%
## 7     Dividend Est. 1.74 (0.28%)         P/FCF  29.32   EPS past 5Y 29.99%
## 8      Dividend TTM 1.50 (0.24%)   Quick Ratio   2.98 Sales past 5Y 19.06%
## 9  Dividend Ex-Date Mar 14, 2025 Current Ratio   2.98   EPS Y/Y TTM 61.97%
## 10        Employees        74067       Debt/Eq   0.27 Sales Y/Y TTM 21.94%
## 11     Option/Short    Yes / Yes    LT Debt/Eq   0.26       EPS Q/Q 50.43%
## 12   Sales Surprise        2.94%  EPS Surprise 18.71%     Sales Q/Q 20.63%
## 13            SMA20       -8.47%         SMA50 -4.37%        SMA200 10.38%
##               X7         X8             X9             X10          X11
## 1    Insider Own     13.72%   Shs Outstand           2.19B    Perf Week
## 2  Insider Trans     -0.54%      Shs Float           2.19B   Perf Month
## 3       Inst Own     67.61%    Short Float           1.26% Perf Quarter
## 4     Inst Trans      1.06%    Short Ratio            1.85  Perf Half Y
## 5            ROA     24.66% Short Interest          27.48M    Perf Year
## 6            ROE     37.14%      52W Range 414.50 - 740.91     Perf YTD
## 7            ROI     27.07%       52W High         -15.56%         Beta
## 8   Gross Margin     81.68%        52W Low          50.94%     ATR (14)
## 9   Oper. Margin     42.41%       RSI (14)           36.52   Volatility
## 10 Profit Margin     37.91%          Recom            1.41 Target Price
## 11        Payout      8.38%     Rel Volume            1.41   Prev Close
## 12      Earnings Jan 29 AMC     Avg Volume          14.89M        Price
## 13        Trades                    Volume      21,375,672       Change
##            X12
## 1       -6.37%
## 2      -11.24%
## 3        1.96%
## 4       22.02%
## 5       27.63%
## 6        6.86%
## 7         1.25
## 8        23.86
## 9  4.44% 3.17%
## 10      762.80
## 11      627.93
## 12      625.66
## 13      -0.36%
getSymbols("META", src = "yahoo", from = "2015-01-01", to = Sys.Date())
## [1] "META"
metaplatform_prices <- Cl(META)  # Extract closing prices
autoplot(metaplatform_prices) + ggtitle("metaplatform Stock Prices")

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

pe_ratio <- metaplatform_fins[metaplatform_fins$X1 == "P/E", "X2"]
debt_eq_ratio <- metaplatform_fins[metaplatform_fins$X1 == "Debt/Eq", "X2"]
roe <- metaplatform_fins[metaplatform_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: "