## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## ----load-libraries, message=FALSE, warning=FALSE-----------------------------
# # Install required packages if not already installed:
# # install.packages(c("yahoofinancer", "dplyr", "ggplot2", "zoo", "scales"))
# 
# library(yahoofinancer)
# library(dplyr)
# library(ggplot2)
# library(zoo)
# library(scales)

## ----download-prices----------------------------------------------------------
# # Fetch 1 year of daily historical data for Apple Inc. (AAPL)
# aapl <- yf_download_prices(
#   tickers  = "AAPL",
#   period   = "1y",
#   interval = "1d"
# )
# 
# head(aapl)
# #> # A tibble: 6 × 8
# #>   symbol date                 open  high   low close adj_close   volume
# #>   <chr>  <dttm>              <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
# #> 1 AAPL   2025-08-18 13:30:00  232.  233.  230.  231.      230. 41235600
# #> 2 AAPL   2025-08-19 13:30:00  231.  233.  229.  231.      230. 38945200
# #> 3 AAPL   2025-08-20 13:30:00  230.  230.  226.  226.      225. 45120300
# #> 4 AAPL   2025-08-21 13:30:00  226.  227.  224.  225.      224. 39870100
# #> 5 AAPL   2025-08-22 13:30:00  226.  229.  225.  228.      227. 42319800
# #> 6 AAPL   2025-08-25 13:30:00  226.  229.  226.  227.      226. 37651000

## ----compute-sma--------------------------------------------------------------
# aapl_trend <- aapl |>
#   arrange(date) |>
#   mutate(
#     sma_50 = rollmean(close, k = 50, fill = NA, align = "right")
#   )
# 
# tail(aapl_trend)
# #> # A tibble: 6 × 9
# #>   symbol date                 open  high   low close adj_close   volume sma_50
# #>   <chr>  <dttm>              <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>  <dbl>
# #> 1 AAPL   2026-08-11 13:30:00  224.  226.  223.  225.      225. 40124300   221.
# #> 2 AAPL   2026-08-12 13:30:00  225.  228.  224.  227.      227. 43105200   221.
# #> 3 AAPL   2026-08-13 13:30:00  227.  230.  226.  229.      229. 48721100   222.
# #> 4 AAPL   2026-08-14 13:30:00  229.  231.  228.  230.      230. 42390800   222.
# #> 5 AAPL   2026-08-15 13:30:00  230.  232.  229.  231.      231. 39912000   223.
# #> 6 AAPL   2026-08-18 13:30:00  231.  233.  230.  232.      232. 41050000   223.

## ----plot-price-sma-----------------------------------------------------------
# # Convert POSIXct timestamp to Date for clean daily axis scaling
# ggplot(aapl_trend, aes(x = as.Date(date))) +
#   # Closing price line
#   geom_line(aes(y = close, color = "Closing Price"), linewidth = 0.85) +
#   # 50-Day Moving Average line
#   geom_line(aes(y = sma_50, color = "50-Day SMA"), linewidth = 1.05, na.rm = TRUE) +
#   # Custom colors and formatting
#   scale_color_manual(
#     name = "Series",
#     values = c("Closing Price" = "#1f77b4", "50-Day SMA" = "#e6550d")
#   ) +
#   scale_x_date(date_breaks = "2 months", date_labels = "%b %Y") +
#   scale_y_continuous(labels = dollar_format()) +
#   labs(
#     title = "Apple Inc. (AAPL) — 1-Year Price History",
#     subtitle = "Daily Closing Price with 50-Day Simple Moving Average (SMA-50)",
#     x = "Date",
#     y = "Price (USD)",
#     caption = "Source: Yahoo Finance via yahoofinancer"
#   ) +
#   theme_minimal(base_size = 12) +
#   theme(
#     legend.position = "top",
#     plot.title = element_text(face = "bold", size = 14),
#     panel.grid.minor = element_blank()
#   )

## ----full-workflow------------------------------------------------------------
# library(yahoofinancer)
# library(dplyr)
# library(ggplot2)
# library(zoo)
# library(scales)
# 
# # 1. Download 1 year of daily historical prices
# aapl <- yf_download_prices("AAPL", period = "1y", interval = "1d")
# 
# # 2. Compute 50-day Simple Moving Average
# aapl_trend <- aapl |>
#   arrange(date) |>
#   mutate(sma_50 = rollmean(close, k = 50, fill = NA, align = "right"))
# 
# # 3. Plot price chart with SMA overlay
# ggplot(aapl_trend, aes(x = as.Date(date))) +
#   geom_line(aes(y = close, color = "Closing Price"), linewidth = 0.85) +
#   geom_line(aes(y = sma_50, color = "50-Day SMA"), linewidth = 1.05, na.rm = TRUE) +
#   scale_color_manual(
#     name = "Series",
#     values = c("Closing Price" = "#1f77b4", "50-Day SMA" = "#e6550d")
#   ) +
#   scale_x_date(date_breaks = "2 months", date_labels = "%b %Y") +
#   scale_y_continuous(labels = dollar_format()) +
#   labs(
#     title = "Apple Inc. (AAPL) — 1-Year Price History",
#     subtitle = "Daily Closing Price with 50-Day Simple Moving Average (SMA-50)",
#     x = "Date",
#     y = "Price (USD)",
#     caption = "Source: Yahoo Finance via yahoofinancer"
#   ) +
#   theme_minimal(base_size = 12) +
#   theme(
#     legend.position = "top",
#     plot.title = element_text(face = "bold", size = 14),
#     panel.grid.minor = element_blank()
#   )

## ----custom-dates-------------------------------------------------------------
# aapl_custom <- yf_download_prices("AAPL", start = "2024-01-01", end = "2024-12-31")

## ----r6-demo------------------------------------------------------------------
# aapl_obj <- Ticker$new("AAPL")
# 
# # Inspect security metadata and valuation measures
# aapl_obj$currency
# aapl_obj$valuation_measures
# 
# # Historical prices via R6
# aapl_obj$get_history(period = "1y", interval = "1d")

