## ----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", "tidyr", "purrr", "ggplot2", "scales"))
# 
# library(yahoofinancer)
# library(dplyr)
# library(tidyr)
# library(purrr)
# library(ggplot2)
# library(scales)

## ----fetch-recommendations----------------------------------------------------
# aapl_obj <- Ticker$new("AAPL")
# 
# related <- aapl_obj$recommendations
# 
# related
# #>   symbol   score
# #> 1   AMZN 0.20319
# #> 2   TSLA 0.19162
# #> 3   GOOG 0.17865
# #> 4   META 0.17095
# #> 5   MSFT 0.15560

## ----sort-recommendations-----------------------------------------------------
# top_related <- related |>
#   arrange(desc(score))
# 
# top_related
# #>   symbol   score
# #> 1   AMZN 0.20319
# #> 2   TSLA 0.19162
# #> ...

## ----plot-recommendations-----------------------------------------------------
# ggplot(top_related, aes(x = score, y = reorder(symbol, score))) +
#   geom_col(fill = "#1f77b4", width = 0.65) +
#   scale_x_continuous(labels = percent_format(accuracy = 1)) +
#   labs(
#     title    = "Securities Most Associated with AAPL",
#     subtitle = "Yahoo Finance related-symbol relevance scores",
#     x        = "Relevance score",
#     y        = NULL,
#     caption  = "Source: Yahoo Finance via yahoofinancer"
#   ) +
#   theme_minimal(base_size = 12) +
#   theme(panel.grid.major.y = element_blank())

## ----key-levels---------------------------------------------------------------
# ins <- aapl_obj$technical_insights
# 
# levels_tbl <- tibble::tibble(
#   level = c("Support", "Resistance", "Stop loss"),
#   price = c(
#     ins$instrumentInfo$keyTechnicals$support,
#     ins$instrumentInfo$keyTechnicals$resistance,
#     ins$instrumentInfo$keyTechnicals$stopLoss
#   )
# )
# 
# levels_tbl
# #> # A tibble: 3 × 2
# #>   level        price
# #>   <chr>        <dbl>
# #> 1 Support       417.
# #> 2 Resistance    507.
# #> 3 Stop loss     397.

## ----outlooks-----------------------------------------------------------------
# te <- ins$instrumentInfo$technicalEvents
# 
# outlooks_tbl <- tibble::tibble(
#   horizon = c("Short term", "Intermediate term", "Long term"),
#   direction = c(
#     te$shortTermOutlook$direction,
#     te$intermediateTermOutlook$direction,
#     te$longTermOutlook$direction
#   ),
#   score = c(
#     te$shortTermOutlook$score,
#     te$intermediateTermOutlook$score,
#     te$longTermOutlook$score
#   ),
#   description = c(
#     te$shortTermOutlook$scoreDescription,
#     te$intermediateTermOutlook$scoreDescription,
#     te$longTermOutlook$scoreDescription
#   )
# )
# 
# outlooks_tbl
# #> # A tibble: 3 × 4
# #>   horizon           direction score description
# #>   <chr>             <chr>     <dbl> <chr>
# #> 1 Short term        Bullish       3 Strong Bullish Evidence
# #> 2 Intermediate term Bullish       3 Strong Bullish Evidence
# #> 3 Long term         Bearish       1 Weak Bearish Evidence

## ----valuation----------------------------------------------------------------
# ins$instrumentInfo$valuation$description
# #> [1] "Near Fair Value"
# 
# ins$instrumentInfo$valuation$discount
# #> [1] "8%"

## ----company-snapshot---------------------------------------------------------
# snap <- ins$companySnapshot
# 
# snapshot_tbl <- tibble::tibble(
#   dimension = names(snap$company),
#   Company   = unlist(snap$company),
#   Sector    = unlist(snap$sector)
# )
# 
# snapshot_long <- snapshot_tbl |>
#   pivot_longer(
#     cols = c(Company, Sector),
#     names_to = "scope",
#     values_to = "score"
#   )
# 
# snapshot_long
# #> # A tibble: 12 × 3
# #>    dimension        scope  score
# #>    <chr>            <chr>  <dbl>
# #>  1 innovativeness   Company 0.977
# #>  2 innovativeness   Sector  0.5
# #>  3 hiring           Company 0.966
# #>  4 hiring           Sector  0.5
# #> # i 8 more rows

## ----plot-snapshot------------------------------------------------------------
# ggplot(snapshot_long, aes(x = score, y = reorder(dimension, score))) +
#   geom_point(aes(color = scope, size = scope), position = position_dodge(width = 0.5)) +
#   scale_color_manual(values = c("Company" = "#1f77b4", "Sector" = "#9aa5ad")) +
#   scale_size_manual(values = c("Company" = 3.4, "Sector" = 2.4), guide = "none") +
#   scale_x_continuous(labels = percent_format(accuracy = 1), limits = c(0, 1)) +
#   labs(
#     title    = "Company Scorecard vs. Sector Median",
#     subtitle = paste("Sector:", snap$sectorInfo),
#     x        = "Score",
#     y        = NULL,
#     color    = NULL,
#     caption  = "Source: Yahoo Finance insights via yahoofinancer"
#   ) +
#   theme_minimal(base_size = 12) +
#   theme(
#     panel.grid.major.y = element_blank(),
#     legend.position = "top"
#   )

## ----research-call------------------------------------------------------------
# call_tbl <- tibble::tibble(
#   provider     = ins$recommendation$provider,
#   rating       = ins$recommendation$rating,
#   target_price = ins$recommendation$targetPrice
# )
# 
# call_tbl
# #> # A tibble: 1 × 3
# #>   provider       rating target_price
# #>   <chr>          <chr>         <dbl>
# #> 1 Argus Research BUY             620

## ----report-headlines---------------------------------------------------------
# head(map_chr(ins$reports, "title"), n = 3)
# #> [1] "Last week featured another monumental run for the stock market, as the major indices recaptured ..."
# #> [2] "Daily - Vickers Top Buyers & Sellers for 03/10/2026"
# #> ...

## ----watchlist-fanout---------------------------------------------------------
# watchlist <- Tickers$new(c("AAPL", "MSFT", "GOOG"))
# 
# related_all <- watchlist$recommendations |>
#   as_tibble()
# 
# related_all
# #> # A tibble: 15 x 3
# #>    symbol recommended_symbol   score
# #>    <chr>  <chr>               <dbl>
# #>  1 AAPL   AMZN              0.203
# #>  2 AAPL   TSLA              0.192
# #>  3 AAPL   GOOG              0.179
# #>  4 MSFT   AAPL              0.212
# #>  5 MSFT   ORCL              0.187
# #> # i 10 more rows

## ----watchlist-top-picks------------------------------------------------------
# best_per_source <- related_all |>
#   group_by(symbol) |>
#   arrange(desc(score), .by_group = TRUE) |>
#   slice_head(n = 1) |>
#   ungroup()
# 
# best_per_source
# #> # A tibble: 3 x 3
# #>   symbol recommended_symbol score
# #>   <chr>  <chr>              <dbl>
# #> 1 AAPL   AMZN               0.203
# #> 2 MSFT   AAPL               0.212
# #> 3 GOOG   META               0.196

## ----full-workflow------------------------------------------------------------
# library(yahoofinancer)
# library(dplyr)
# library(ggplot2)
# library(scales)
# 
# # 1. Fetch and plot related-symbol recommendations
# aapl_obj <- Ticker$new("AAPL")
# top_related <- aapl_obj$recommendations |>
#   arrange(desc(score))
# 
# ggplot(top_related, aes(x = score, y = reorder(symbol, score))) +
#   geom_col(fill = "#1f77b4", width = 0.65) +
#   scale_x_continuous(labels = percent_format(accuracy = 1)) +
#   labs(
#     title    = "Securities Most Associated with AAPL",
#     subtitle = "Yahoo Finance related-symbol relevance scores",
#     x        = "Relevance score",
#     y        = NULL,
#     caption  = "Source: Yahoo Finance via yahoofinancer"
#   ) +
#   theme_minimal(base_size = 12) +
#   theme(panel.grid.major.y = element_blank())
# 
# # 2. Extract technical insights and key support/resistance levels
# ins <- aapl_obj$technical_insights
# 
# levels_tbl <- tibble::tibble(
#   level = c("Support", "Resistance", "Stop loss"),
#   price = c(
#     ins$instrumentInfo$keyTechnicals$support,
#     ins$instrumentInfo$keyTechnicals$resistance,
#     ins$instrumentInfo$keyTechnicals$stopLoss
#   )
# )
# levels_tbl
# 
# # 3. Extract research recommendation and price target
# call_tbl <- tibble::tibble(
#   provider     = ins$recommendation$provider,
#   rating       = ins$recommendation$rating,
#   target_price = ins$recommendation$targetPrice
# )
# call_tbl

