Import stock prices

stocks <- tq_get(c("TSM", "SNDK", "NVDA"),
                 get = "stock.prices",
                 from = "2025-01-01",
                 to = "2026-09-08")
stocks
## # A tibble: 1,232 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 TSM    2025-01-02  197   204.  197.  202. 10951400     198.
##  2 TSM    2025-01-03  204.  209.  204.  209. 10261900     205.
##  3 TSM    2025-01-06  219.  222.  216.  220. 24339900     216.
##  4 TSM    2025-01-07  221.  221.  211.  211. 17010000     208.
##  5 TSM    2025-01-08  210.  210.  206.  207. 13262700     203.
##  6 TSM    2025-01-10  208.  210.  203.  208. 17265200     205.
##  7 TSM    2025-01-13  201.  204.  200.  201. 16612400     198.
##  8 TSM    2025-01-14  205.  206.  198.  201. 14182900     198.
##  9 TSM    2025-01-15  202.  209.  199.  207. 18286500     203.
## 10 TSM    2025-01-16  219.  222.  213.  215. 38539600     211.
## # ℹ 1,222 more rows

Plot stock prices

stocks %>%
    
    ggplot(aes(x = date, y = adjusted, color = symbol)) +
    geom_line()

Apply the dplyr verbs you learned in chapter 5

Filter rows

stocks %>% filter(adjusted > 24)
## # A tibble: 1,232 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 TSM    2025-01-02  197   204.  197.  202. 10951400     198.
##  2 TSM    2025-01-03  204.  209.  204.  209. 10261900     205.
##  3 TSM    2025-01-06  219.  222.  216.  220. 24339900     216.
##  4 TSM    2025-01-07  221.  221.  211.  211. 17010000     208.
##  5 TSM    2025-01-08  210.  210.  206.  207. 13262700     203.
##  6 TSM    2025-01-10  208.  210.  203.  208. 17265200     205.
##  7 TSM    2025-01-13  201.  204.  200.  201. 16612400     198.
##  8 TSM    2025-01-14  205.  206.  198.  201. 14182900     198.
##  9 TSM    2025-01-15  202.  209.  199.  207. 18286500     203.
## 10 TSM    2025-01-16  219.  222.  213.  215. 38539600     211.
## # ℹ 1,222 more rows
stocks %>% filter(date > 2024-01-02)
## # A tibble: 1,232 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 TSM    2025-01-02  197   204.  197.  202. 10951400     198.
##  2 TSM    2025-01-03  204.  209.  204.  209. 10261900     205.
##  3 TSM    2025-01-06  219.  222.  216.  220. 24339900     216.
##  4 TSM    2025-01-07  221.  221.  211.  211. 17010000     208.
##  5 TSM    2025-01-08  210.  210.  206.  207. 13262700     203.
##  6 TSM    2025-01-10  208.  210.  203.  208. 17265200     205.
##  7 TSM    2025-01-13  201.  204.  200.  201. 16612400     198.
##  8 TSM    2025-01-14  205.  206.  198.  201. 14182900     198.
##  9 TSM    2025-01-15  202.  209.  199.  207. 18286500     203.
## 10 TSM    2025-01-16  219.  222.  213.  215. 38539600     211.
## # ℹ 1,222 more rows
# Looking at days where the lowest price of the day, where higher than the mean of the high of all days, and where the closing price where higher than opening price (showing that the stock had momentum)
stocks %>% filter(low > mean(high), close > open)
## # A tibble: 143 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 TSM    2026-02-09  349.  360.  348.  355. 14594900     354.
##  2 TSM    2026-02-11  370.  380.  368.  374. 18651300     372.
##  3 TSM    2026-02-17  362.  366.  356.  364. 10063800     362.
##  4 TSM    2026-02-19  360.  363.  357.  360.  6346500     359.
##  5 TSM    2026-02-20  360.  372.  359.  371.  9009200     369.
##  6 TSM    2026-02-23  367.  373.  366.  370.  9550200     368.
##  7 TSM    2026-02-24  379.  389.  376.  386. 13269500     384.
##  8 TSM    2026-02-27  370.  377.  369.  375.  9186600     373.
##  9 TSM    2026-03-02  365.  373.  365.  369. 12571600     367.
## 10 TSM    2026-03-03  350.  356.  344.  353. 18580200     351.
## # ℹ 133 more rows

Arange Rows

stocks %>% arrange(desc(low))
## # A tibble: 1,232 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 SNDK   2026-06-22 2293. 2354. 2251. 2274.  9977600    2274.
##  2 SNDK   2026-06-25 2238. 2348. 2092. 2335  15030200    2335 
##  3 SNDK   2026-06-30 2110. 2281. 2070  2274. 11454300    2274.
##  4 SNDK   2026-06-26 2170. 2256. 2063. 2091. 16867100    2091.
##  5 SNDK   2026-06-18 2045. 2192. 2029  2185. 12178300    2185.
##  6 SNDK   2026-06-15 2101. 2120. 2021. 2108.  9156400    2108.
##  7 SNDK   2026-07-01 2085. 2130. 2002. 2032. 11056500    2032.
##  8 SNDK   2026-06-16 2134. 2167. 1980. 1992.  9588500    1992.
##  9 SNDK   2026-06-23 2008. 2060  1950. 1964. 12868300    1964.
## 10 SNDK   2026-06-17 2075. 2075. 1938  1959.  9182500    1959.
## # ℹ 1,222 more rows
stocks %>% arrange(low)
## # A tibble: 1,232 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 SNDK   2025-04-07  29.2  33.0  27.9  31.5  6238600     31.5
##  2 SNDK   2025-04-11  30.6  32.0  28.3  32.0  4856000     32.0
##  3 SNDK   2025-04-04  35.7  35.9  28.4  30.1 11428600     30.1
##  4 SNDK   2025-04-21  31.1  31.1  28.9  29.8  2579400     29.8
##  5 SNDK   2025-04-22  30.3  31.1  29.3  29.6  2883700     29.6
##  6 SNDK   2025-04-23  32.0  32.8  30.2  30.4  2750200     30.4
##  7 SNDK   2025-04-24  30.8  33.0  30.3  32.3  2590500     32.3
##  8 SNDK   2025-04-17  31.8  32.7  30.4  31.3  2057200     31.3
##  9 SNDK   2025-04-10  34.6  35.0  30.5  31.1  4153400     31.1
## 10 SNDK   2025-04-16  32.3  33.1  30.8  32.0  2321000     32.0
## # ℹ 1,222 more rows
stocks %>% arrange(desc(high), desc(low))
## # A tibble: 1,232 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 SNDK   2026-06-22 2293. 2354. 2251. 2274.  9977600    2274.
##  2 SNDK   2026-06-25 2238. 2348. 2092. 2335  15030200    2335 
##  3 SNDK   2026-06-30 2110. 2281. 2070  2274. 11454300    2274.
##  4 SNDK   2026-06-26 2170. 2256. 2063. 2091. 16867100    2091.
##  5 SNDK   2026-06-18 2045. 2192. 2029  2185. 12178300    2185.
##  6 SNDK   2026-06-16 2134. 2167. 1980. 1992.  9588500    1992.
##  7 SNDK   2026-07-01 2085. 2130. 2002. 2032. 11056500    2032.
##  8 SNDK   2026-06-15 2101. 2120. 2021. 2108.  9156400    2108.
##  9 SNDK   2026-06-29 2091. 2091. 1895  2050. 11251000    2050.
## 10 SNDK   2026-06-17 2075. 2075. 1938  1959.  9182500    1959.
## # ℹ 1,222 more rows
stocks %>% arrange(desc(close))
## # A tibble: 1,232 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 SNDK   2026-06-25 2238. 2348. 2092. 2335  15030200    2335 
##  2 SNDK   2026-06-22 2293. 2354. 2251. 2274.  9977600    2274.
##  3 SNDK   2026-06-30 2110. 2281. 2070  2274. 11454300    2274.
##  4 SNDK   2026-06-18 2045. 2192. 2029  2185. 12178300    2185.
##  5 SNDK   2026-06-15 2101. 2120. 2021. 2108.  9156400    2108.
##  6 SNDK   2026-06-26 2170. 2256. 2063. 2091. 16867100    2091.
##  7 SNDK   2026-06-29 2091. 2091. 1895  2050. 11251000    2050.
##  8 SNDK   2026-07-01 2085. 2130. 2002. 2032. 11056500    2032.
##  9 SNDK   2026-06-16 2134. 2167. 1980. 1992.  9588500    1992.
## 10 SNDK   2026-06-12 1891. 2022. 1865. 1980. 11926100    1980.
## # ℹ 1,222 more rows

Select Colums

stocks %>% select(open, close, date) %>%
    arrange(desc(close))
## # A tibble: 1,232 × 3
##     open close date      
##    <dbl> <dbl> <date>    
##  1 2238. 2335  2026-06-25
##  2 2293. 2274. 2026-06-22
##  3 2110. 2274. 2026-06-30
##  4 2045. 2185. 2026-06-18
##  5 2101. 2108. 2026-06-15
##  6 2170. 2091. 2026-06-26
##  7 2091. 2050. 2026-06-29
##  8 2085. 2032. 2026-07-01
##  9 2134. 1992. 2026-06-16
## 10 1891. 1980. 2026-06-12
## # ℹ 1,222 more rows
stocks %>% select(symbol:open, close) %>%
    filter(open > close) %>%
    arrange(open)
## # A tibble: 595 × 4
##    symbol date        open close
##    <chr>  <date>     <dbl> <dbl>
##  1 SNDK   2025-04-22  30.3  29.6
##  2 SNDK   2025-04-21  31.1  29.8
##  3 SNDK   2025-04-17  31.8  31.3
##  4 SNDK   2025-04-23  32.0  30.4
##  5 SNDK   2025-04-16  32.3  32.0
##  6 SNDK   2025-04-28  32.6  32.3
##  7 SNDK   2025-04-08  33    32.3
##  8 SNDK   2025-04-15  33.3  33.2
##  9 SNDK   2025-04-14  33.8  33.5
## 10 SNDK   2025-05-06  34.0  33.8
## # ℹ 585 more rows

Add columns

#Calculate the percentage gain during the day, and arranged it through highest daily gain to lowest
mutate(stocks,
       gain_procent = (close - open) /open*100) %>%
    select(symbol:open, gain_procent) %>%
    arrange(desc(gain_procent))
## # A tibble: 1,232 × 4
##    symbol date         open gain_procent
##    <chr>  <date>      <dbl>        <dbl>
##  1 SNDK   2026-01-06  289.          21.1
##  2 SNDK   2025-02-18   40.5         16.3
##  3 NVDA   2025-04-09   98.9         15.6
##  4 SNDK   2025-11-07  208.          14.9
##  5 SNDK   2025-04-09   31.9         14.6
##  6 SNDK   2026-08-13 1340.          14.1
##  7 SNDK   2026-03-09  517           13.9
##  8 SNDK   2025-10-23  147.          13.6
##  9 TSM    2025-04-09  140.          13.2
## 10 SNDK   2026-02-02  589.          13.0
## # ℹ 1,222 more rows

Summarize with groups

stocks %>%

#Average gain during day
mutate( 
       gain_procent = (close - open) /open*100) %>%
    
summarise(gain = mean(gain_procent))
## # A tibble: 1 × 1
##    gain
##   <dbl>
## 1 0.210
# Adding the new column to the data set (new)
stocks_gain <- stocks %>%
    mutate( 
       gain_procent = (close - open) /open*100)

group_by(stocks_gain, symbol) %>%
    summarise(gain = mean(gain_procent))
## # A tibble: 3 × 2
##   symbol     gain
##   <chr>     <dbl>
## 1 NVDA    0.0140 
## 2 SNDK    0.651  
## 3 TSM    -0.00658

Summarize and visualize by group

stocks_gain %>%
    filter(date >= max(date) - 365) %>%
    group_by(symbol) %>%
    mutate(avg_gain = mean(gain_procent)) %>%
    
    ggplot(aes(x = date, y = gain_procent, colour = symbol)) +
    geom_line(alpha = 0.6) +
    
    geom_hline(aes(yintercept = avg_gain, colour = symbol),
               linetype = "dashed") +
    coord_cartesian(ylim = c(-10, 10))

#Separating the stocks
stocks_gain %>%
    filter(date >= max(date) - 365) %>%
    group_by(symbol) %>%
    mutate(avg_gain = mean(gain_procent)) %>%
    
    ggplot(aes(x = date, y = gain_procent, colour = symbol)) +
    geom_line(alpha = 0.6) +
    
    geom_hline(aes(yintercept = avg_gain, colour = symbol),
               linetype = "dashed") +
    coord_cartesian(ylim = c(-10, 10)) +
  facet_wrap(~ symbol)

Grouping & Un-grouping

stocks_gain %>%
    group_by(symbol) %>%
    summarise(count = n())
## # A tibble: 3 × 2
##   symbol count
##   <chr>  <int>
## 1 NVDA     420
## 2 SNDK     392
## 3 TSM      420
stocks_gain %>%
    ungroup()
## # A tibble: 1,232 × 9
##    symbol date        open  high   low close   volume adjusted gain_procent
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>        <dbl>
##  1 TSM    2025-01-02  197   204.  197.  202. 10951400     198.        2.32 
##  2 TSM    2025-01-03  204.  209.  204.  209. 10261900     205.        2.21 
##  3 TSM    2025-01-06  219.  222.  216.  220. 24339900     216.        0.567
##  4 TSM    2025-01-07  221.  221.  211.  211. 17010000     208.       -4.44 
##  5 TSM    2025-01-08  210.  210.  206.  207. 13262700     203.       -1.36 
##  6 TSM    2025-01-10  208.  210.  203.  208. 17265200     205.        0.371
##  7 TSM    2025-01-13  201.  204.  200.  201. 16612400     198.        0.419
##  8 TSM    2025-01-14  205.  206.  198.  201. 14182900     198.       -1.82 
##  9 TSM    2025-01-15  202.  209.  199.  207. 18286500     203.        2.13 
## 10 TSM    2025-01-16  219.  222.  213.  215. 38539600     211.       -1.87 
## # ℹ 1,222 more rows
stocks_gain %>%
    mutate(gain_procent = (close - open)/ open
*100, month = format(date, "%Y-%m")) %>%
    group_by(symbol, month) %>%
    summarise(
        gain = mean(gain_procent, na.rm = TRUE)
    )
## # A tibble: 62 × 3
## # Groups:   symbol [3]
##    symbol month      gain
##    <chr>  <chr>     <dbl>
##  1 NVDA   2025-01 -0.507 
##  2 NVDA   2025-02 -0.0756
##  3 NVDA   2025-03 -0.291 
##  4 NVDA   2025-04  1.04  
##  5 NVDA   2025-05  0.400 
##  6 NVDA   2025-06  0.445 
##  7 NVDA   2025-07 -0.0154
##  8 NVDA   2025-08  0.150 
##  9 NVDA   2025-09  0.356 
## 10 NVDA   2025-10 -0.264 
## # ℹ 52 more rows
#Transpose the table = pivot wider
stocks_gain %>%
    group_by(symbol) %>%
    summarise(count = n())
## # A tibble: 3 × 2
##   symbol count
##   <chr>  <int>
## 1 NVDA     420
## 2 SNDK     392
## 3 TSM      420
stocks_gain %>%
    ungroup()
## # A tibble: 1,232 × 9
##    symbol date        open  high   low close   volume adjusted gain_procent
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>        <dbl>
##  1 TSM    2025-01-02  197   204.  197.  202. 10951400     198.        2.32 
##  2 TSM    2025-01-03  204.  209.  204.  209. 10261900     205.        2.21 
##  3 TSM    2025-01-06  219.  222.  216.  220. 24339900     216.        0.567
##  4 TSM    2025-01-07  221.  221.  211.  211. 17010000     208.       -4.44 
##  5 TSM    2025-01-08  210.  210.  206.  207. 13262700     203.       -1.36 
##  6 TSM    2025-01-10  208.  210.  203.  208. 17265200     205.        0.371
##  7 TSM    2025-01-13  201.  204.  200.  201. 16612400     198.        0.419
##  8 TSM    2025-01-14  205.  206.  198.  201. 14182900     198.       -1.82 
##  9 TSM    2025-01-15  202.  209.  199.  207. 18286500     203.        2.13 
## 10 TSM    2025-01-16  219.  222.  213.  215. 38539600     211.       -1.87 
## # ℹ 1,222 more rows
stocks_gain %>%
    mutate(gain_procent = (close - open)/ open
*100, month = format(date, "%Y-%m")) %>%
    group_by(symbol, month) %>%
    summarise(
        gain = mean(gain_procent, na.rm = TRUE)
    ) %>%
    pivot_wider(
        names_from = symbol,
        values_from = gain
    )
## # A tibble: 21 × 4
##    month      NVDA   SNDK      TSM
##    <chr>     <dbl>  <dbl>    <dbl>
##  1 2025-01 -0.507  NA     -0.00174
##  2 2025-02 -0.0756  1.28  -0.433  
##  3 2025-03 -0.291   0.616 -0.0977 
##  4 2025-04  1.04   -0.751  0.691  
##  5 2025-05  0.400   0.501  0.202  
##  6 2025-06  0.445   0.346  0.476  
##  7 2025-07 -0.0154 -0.311 -0.0368 
##  8 2025-08  0.150   0.797 -0.170  
##  9 2025-09  0.356   2.16   0.624  
## 10 2025-10 -0.264   1.23  -0.649  
## # ℹ 11 more rows