Finance Club Portfolio

10 Year Historical Prices (Excluding “UVXY”)

stocks <- tq_get(c("HLIT", "ABR", "RYLD", "GM", "COPX", "AMLP", "XYLD", "FDRR", "DBP", "PRFT", "IUSV", "BSV", "FRC", "META", "JPM", "JNJ", "NXST", "SPY"),
                 get = "stock.prices",
                 from = "2013-02-01")
stocks
## # A tibble: 42,750 × 8
##    symbol date        open  high   low close volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl>
##  1 HLIT   2013-02-01  5.24  5.38  5.17  5.34 620900     5.34
##  2 HLIT   2013-02-04  5.27  5.36  5.24  5.29 524100     5.29
##  3 HLIT   2013-02-05  5.35  5.39  5.29  5.36 352000     5.36
##  4 HLIT   2013-02-06  5.31  5.35  5.25  5.35 359800     5.35
##  5 HLIT   2013-02-07  5.33  5.39  5.33  5.35 328700     5.35
##  6 HLIT   2013-02-08  5.38  5.38  5.32  5.35 314900     5.35
##  7 HLIT   2013-02-11  5.37  5.45  5.33  5.44 305900     5.44
##  8 HLIT   2013-02-12  5.46  5.5   5.4   5.46 408100     5.46
##  9 HLIT   2013-02-13  5.5   5.54  5.42  5.48 668400     5.48
## 10 HLIT   2013-02-14  5.45  5.73  5.45  5.7  832100     5.7 
## # … with 42,740 more rows

Plot stock prices

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

Applying the dyplyr verbs learned in Ch.5

Filter rows

# Days with low trading volume

stocks %>%
    filter(volume < 10000000) %>%
    ggplot(mapping = aes(x = date, y = volume, color = symbol)) +
    geom_boxplot() +
    coord_flip()

Arrange rows

# Arrange the stocks by descending order for highs & lows

stocks %>%
    arrange(desc(open), desc(close))
## # A tibble: 42,750 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 SPY    2022-01-04  479.  480.  476.  478.  71178700     470.
##  2 SPY    2021-12-30  478.  479   476.  476.  55329000     469.
##  3 SPY    2021-12-28  478.  479.  476.  477.  47274600     469.
##  4 SPY    2022-01-05  477.  478.  468.  468. 104538900     461.
##  5 SPY    2021-12-29  477.  479.  476.  477.  54503000     470.
##  6 SPY    2022-01-03  476.  478.  474.  478.  72668200     470.
##  7 SPY    2021-12-31  476.  477.  475.  475.  65237400     467.
##  8 SPY    2021-12-16  473.  473.  465.  466. 116568600     457.
##  9 SPY    2022-01-13  472.  473.  463.  465.  91173100     457.
## 10 SPY    2021-12-27  472.  477.  472.  477.  56808600     470.
## # … with 42,740 more rows

Select columns

# Selecting desired columns

select(stocks, 
       date, open, close, high, low)
## # A tibble: 42,750 × 5
##    date        open close  high   low
##    <date>     <dbl> <dbl> <dbl> <dbl>
##  1 2013-02-01  5.24  5.34  5.38  5.17
##  2 2013-02-04  5.27  5.29  5.36  5.24
##  3 2013-02-05  5.35  5.36  5.39  5.29
##  4 2013-02-06  5.31  5.35  5.35  5.25
##  5 2013-02-07  5.33  5.35  5.39  5.33
##  6 2013-02-08  5.38  5.35  5.38  5.32
##  7 2013-02-11  5.37  5.44  5.45  5.33
##  8 2013-02-12  5.46  5.46  5.5   5.4 
##  9 2013-02-13  5.5   5.48  5.54  5.42
## 10 2013-02-14  5.45  5.7   5.73  5.45
## # … with 42,740 more rows

Add columns

# Creating a new variable called "gain_loss"

mutate(stocks,
       gain_loss = close - open) %>% select (close, open, gain_loss)
## # A tibble: 42,750 × 3
##    close  open gain_loss
##    <dbl> <dbl>     <dbl>
##  1  5.34  5.24    0.100 
##  2  5.29  5.27    0.0200
##  3  5.36  5.35    0.0100
##  4  5.35  5.31    0.0400
##  5  5.35  5.33    0.0200
##  6  5.35  5.38   -0.0300
##  7  5.44  5.37    0.0700
##  8  5.46  5.46    0     
##  9  5.48  5.5    -0.0200
## 10  5.7   5.45    0.25  
## # … with 42,740 more rows

Summazrize groups

# Average

summarise(stocks, avg_high = mean(high, na.rm = TRUE))
## # A tibble: 1 × 1
##   avg_high
##      <dbl>
## 1     75.0
# Finding the average high for each asset

stocks %>%

    # Group by asset
    group_by(symbol) %>%
    
    # Calculate 
    summarise(avg_high = mean(high, na.rm = TRUE)) %>%

    # Sort it
    arrange(avg_high)
## # A tibble: 18 × 2
##    symbol avg_high
##    <chr>     <dbl>
##  1 HLIT       6.68
##  2 ABR       10.5 
##  3 RYLD      22.8 
##  4 COPX      25.4 
##  5 FDRR      34.3 
##  6 GM        37.8 
##  7 PRFT      37.9 
##  8 DBP       42.1 
##  9 XYLD      46.4 
## 10 IUSV      53.5 
## 11 AMLP      56.9 
## 12 BSV       80.1 
## 13 NXST      86.4 
## 14 JPM       96.3 
## 15 FRC       98.7 
## 16 JNJ      130.  
## 17 META     158.  
## 18 SPY      278.