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)

tesla_url <- "https://finviz.com/quote.ashx?t=TSLA"
tesla_page <- read_html(tesla_url)

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

tesla_fins <- as.data.frame(tesla_data[[1]])
print(tesla_fins)
##                  X1           X2            X3      X4            X5      X6
## 1             Index NDX, S&P 500           P/E  128.87     EPS (ttm)    2.04
## 2        Market Cap      844.88B   Forward P/E   68.55    EPS next Y    3.83
## 3            Income        7.13B           PEG    4.77    EPS next Q    0.53
## 4             Sales       97.69B           P/S    8.65    EPS this Y  19.21%
## 5           Book/sh        22.67           P/B   11.59    EPS next Y  32.83%
## 6           Cash/sh        11.52           P/C   22.80   EPS next 5Y  27.04%
## 7     Dividend Est.            -         P/FCF  235.93   EPS past 5Y       -
## 8      Dividend TTM            -   Quick Ratio    1.61 Sales past 5Y  34.02%
## 9  Dividend Ex-Date            - Current Ratio    2.02   EPS Y/Y TTM -52.81%
## 10        Employees       125665       Debt/Eq    0.19 Sales Y/Y TTM   0.95%
## 11     Option/Short    Yes / Yes    LT Debt/Eq    0.14       EPS Q/Q -71.01%
## 12   Sales Surprise       -5.69%  EPS Surprise  -5.72%     Sales Q/Q   2.15%
## 13            SMA20      -17.36%         SMA50 -29.34%        SMA200  -6.59%
##               X7         X8             X9             X10          X11
## 1    Insider Own     12.91%   Shs Outstand           3.22B    Perf Week
## 2  Insider Trans     -0.23%      Shs Float           2.80B   Perf Month
## 3       Inst Own     48.26%    Short Float           2.19% Perf Quarter
## 4     Inst Trans      4.14%    Short Ratio            0.71  Perf Half Y
## 5            ROA      6.24% Short Interest          61.50M    Perf Year
## 6            ROE     10.52%      52W Range 138.80 - 488.54     Perf YTD
## 7            ROI      8.56%       52W High         -46.23%         Beta
## 8   Gross Margin     17.86%        52W Low          89.24%     ATR (14)
## 9   Oper. Margin      7.94%       RSI (14)           26.76   Volatility
## 10 Profit Margin      7.30%          Recom            2.58 Target Price
## 11        Payout      0.00%     Rel Volume            1.18   Prev Close
## 12      Earnings Jan 29 AMC     Avg Volume          86.40M        Price
## 13        Trades                    Volume     102,369,640       Change
##            X12
## 1      -10.35%
## 2      -30.54%
## 3      -25.25%
## 4       19.72%
## 5       45.33%
## 6      -34.96%
## 7         2.60
## 8        19.24
## 9  6.73% 5.76%
## 10      378.39
## 11      263.45
## 12      262.67
## 13      -0.30%
getSymbols("TSLA", src = "yahoo", from = "2020-01-01", to = Sys.Date())
## [1] "TSLA"
tesla_prices <- Cl(TSLA)

autoplot(tesla_prices) + ggtitle("Tesla Stock Prices")

tesla_returns <- dailyReturn(TSLA, type = "log")

ggplot(data = as.data.frame(tesla_returns), aes(x = index(TSLA), y = coredata(tesla_returns))) +
  geom_line() +
  ggtitle("Tesla Daily Log Returns") +
  xlab("Date") +
  ylab("Log Returns")

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