library(tidyquant)
library(tidyverse)

Import Stocks

# Import data
from = today() - years(1)
Stock <- tq_get(c("TGT", "WMT", "BIG"), get = "stock.prices", from = from)
Stock
## # A tibble: 753 x 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 TGT    2018-05-09  68.7  70.1  68.3  69.9 4122900     67.6
##  2 TGT    2018-05-10  69.8  70.5  69.2  70.2 3574200     67.9
##  3 TGT    2018-05-11  69.9  70.3  69.3  70.2 3032200     67.9
##  4 TGT    2018-05-14  70.6  73.1  70.6  72.9 7312700     70.5
##  5 TGT    2018-05-15  72.3  73.4  72.2  73.1 5000000     71.3
##  6 TGT    2018-05-16  74.0  75.7  73.9  75.2 8596200     73.4
##  7 TGT    2018-05-17  75.2  76.4  75.1  75.8 5476000     74.0
##  8 TGT    2018-05-18  76.2  76.6  75.6  75.9 4317600     74.1
##  9 TGT    2018-05-21  76.6  77.2  76.3  76.9 5073900     75.0
## 10 TGT    2018-05-22  77    77.6  75.2  75.5 7550100     73.6
## # … with 743 more rows

Moving Average 15 day

stock<-
 Stock%>%
  tq_mutate(select = close, mutate_fun = SMA, n = 15)%>%
  tq_mutate(select = close, mutate_fun = runSD, n = 15)%>%
  rename(SD = value)
stock
## # A tibble: 753 x 10
##    symbol date        open  high   low close   volume adjusted   SMA    SD
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl> <dbl> <dbl>
##  1 TGT    2018-05-09  68.7  70.1  68.3  69.9  4122900     67.6    NA    NA
##  2 WMT    2018-05-09  82.6  83.7  82    83.1 32265200     81.2    NA    NA
##  3 BIG    2018-05-09  40.2  40.4  39.5  40.4  1523700     39.1    NA    NA
##  4 TGT    2018-05-10  69.8  70.5  69.2  70.2  3574200     67.9    NA    NA
##  5 WMT    2018-05-10  82.6  83.8  82.0  82.7 15857600     81.3    NA    NA
##  6 BIG    2018-05-10  40.4  41.3  40.1  41.0   942100     39.6    NA    NA
##  7 TGT    2018-05-11  69.9  70.3  69.3  70.2  3032200     67.9    NA    NA
##  8 WMT    2018-05-11  82.7  83.5  81.9  83.4  9934800     82.0    NA    NA
##  9 BIG    2018-05-11  40.8  41.6  40.5  41.5  1051900     40.2    NA    NA
## 10 TGT    2018-05-14  70.6  73.1  70.6  72.9  7312700     70.5    NA    NA
## # … with 743 more rows

Moving Average 20 Day

stock<-
 Stock%>%
  tq_mutate(select = close, mutate_fun = SMA, n = 20)%>%
  tq_mutate(select = close, mutate_fun = runSD, n = 20)%>%
  rename(SD = value)
stock
## # A tibble: 753 x 10
##    symbol date        open  high   low close   volume adjusted   SMA    SD
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl> <dbl> <dbl>
##  1 TGT    2018-05-09  68.7  70.1  68.3  69.9  4122900     67.6    NA    NA
##  2 WMT    2018-05-09  82.6  83.7  82    83.1 32265200     81.2    NA    NA
##  3 BIG    2018-05-09  40.2  40.4  39.5  40.4  1523700     39.1    NA    NA
##  4 TGT    2018-05-10  69.8  70.5  69.2  70.2  3574200     67.9    NA    NA
##  5 WMT    2018-05-10  82.6  83.8  82.0  82.7 15857600     81.3    NA    NA
##  6 BIG    2018-05-10  40.4  41.3  40.1  41.0   942100     39.6    NA    NA
##  7 TGT    2018-05-11  69.9  70.3  69.3  70.2  3032200     67.9    NA    NA
##  8 WMT    2018-05-11  82.7  83.5  81.9  83.4  9934800     82.0    NA    NA
##  9 BIG    2018-05-11  40.8  41.6  40.5  41.5  1051900     40.2    NA    NA
## 10 TGT    2018-05-14  70.6  73.1  70.6  72.9  7312700     70.5    NA    NA
## # … with 743 more rows

Moving Average 50 Day

stock<-
 Stock%>%
  tq_mutate(select = close, mutate_fun = SMA, n = 50)%>%
  tq_mutate(select = close, mutate_fun = runSD, n = 50)%>%
  rename(SD = value)
stock
## # A tibble: 753 x 10
##    symbol date        open  high   low close   volume adjusted   SMA    SD
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl> <dbl> <dbl>
##  1 TGT    2018-05-09  68.7  70.1  68.3  69.9  4122900     67.6    NA    NA
##  2 WMT    2018-05-09  82.6  83.7  82    83.1 32265200     81.2    NA    NA
##  3 BIG    2018-05-09  40.2  40.4  39.5  40.4  1523700     39.1    NA    NA
##  4 TGT    2018-05-10  69.8  70.5  69.2  70.2  3574200     67.9    NA    NA
##  5 WMT    2018-05-10  82.6  83.8  82.0  82.7 15857600     81.3    NA    NA
##  6 BIG    2018-05-10  40.4  41.3  40.1  41.0   942100     39.6    NA    NA
##  7 TGT    2018-05-11  69.9  70.3  69.3  70.2  3032200     67.9    NA    NA
##  8 WMT    2018-05-11  82.7  83.5  81.9  83.4  9934800     82.0    NA    NA
##  9 BIG    2018-05-11  40.8  41.6  40.5  41.5  1051900     40.2    NA    NA
## 10 TGT    2018-05-14  70.6  73.1  70.6  72.9  7312700     70.5    NA    NA
## # … with 743 more rows

Moving Average 100 Day

stock<-
 Stock%>%
  tq_mutate(select = close, mutate_fun = SMA, n = 100)%>%
  tq_mutate(select = close, mutate_fun = runSD, n = 100)%>%
  rename(SD = value)
stock
## # A tibble: 753 x 10
##    symbol date        open  high   low close   volume adjusted   SMA    SD
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl> <dbl> <dbl>
##  1 TGT    2018-05-09  68.7  70.1  68.3  69.9  4122900     67.6    NA    NA
##  2 WMT    2018-05-09  82.6  83.7  82    83.1 32265200     81.2    NA    NA
##  3 BIG    2018-05-09  40.2  40.4  39.5  40.4  1523700     39.1    NA    NA
##  4 TGT    2018-05-10  69.8  70.5  69.2  70.2  3574200     67.9    NA    NA
##  5 WMT    2018-05-10  82.6  83.8  82.0  82.7 15857600     81.3    NA    NA
##  6 BIG    2018-05-10  40.4  41.3  40.1  41.0   942100     39.6    NA    NA
##  7 TGT    2018-05-11  69.9  70.3  69.3  70.2  3032200     67.9    NA    NA
##  8 WMT    2018-05-11  82.7  83.5  81.9  83.4  9934800     82.0    NA    NA
##  9 BIG    2018-05-11  40.8  41.6  40.5  41.5  1051900     40.2    NA    NA
## 10 TGT    2018-05-14  70.6  73.1  70.6  72.9  7312700     70.5    NA    NA
## # … with 743 more rows

High Moving Average Visual

stock%>%
  ggplot(aes(x=date,y=high,color=symbol))+geom_line()+labs(subtitle ="Moving Average High")

Low Moving Average Visual

stock%>%
  ggplot(aes(x=date,y=low,color=symbol))+geom_line()+labs(subtitle ="Moving Average Low")

Analysis

Based upon our analysis of the stocks short term and long term moving averages, we have concluded that Walmart would be the most promising stock to invest in. Walmart portrays an upward moving average trend, allowing us as investors to believe there will be an increase in stock prices. Although the two other stocks would be cheaper to buy, they show a downward trend, concluding that they will decrease in price and become a less profitable stock to invest in. Target will be the second best option as their moving average dips but makes a slight increase again, showing a possible future increase.

Monthly Returns

from = today() - years(50)
Stocks <- tq_get(c("TGT","WMT", "BIG"), get = "stock.prices", from = from) %>%
    group_by(symbol)
Stocks 
## # A tibble: 30,188 x 8
## # Groups:   symbol [3]
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 TGT    1980-03-17 0.818 0.831 0.818 0.823 1771200   0.0272
##  2 TGT    1980-03-18 0.823 0.831 0.820 0.826  230400   0.0273
##  3 TGT    1980-03-19 0.826 0.833 0.826 0.826  364800   0.0273
##  4 TGT    1980-03-20 0.826 0.831 0.823 0.826 1483200   0.0273
##  5 TGT    1980-03-21 0.826 0.831 0.823 0.831 2179200   0.0275
##  6 TGT    1980-03-24 0.828 0.828 0.810 0.820 2145600   0.0271
##  7 TGT    1980-03-25 0.818 0.818 0.802 0.802  480000   0.0265
##  8 TGT    1980-03-26 0.802 0.807 0.802 0.805  115200   0.0266
##  9 TGT    1980-03-27 0.805 0.807 0.802 0.802  600000   0.0265
## 10 TGT    1980-03-28 0.815 0.828 0.815 0.823  974400   0.0272
## # … with 30,178 more rows
returns_monthly <-
  Stocks %>%
   tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "monthly")


returns_monthly
## # A tibble: 1,441 x 3
## # Groups:   symbol [3]
##    symbol date       monthly.returns
##    <chr>  <date>               <dbl>
##  1 TGT    1980-03-31         0.0126 
##  2 TGT    1980-04-30        -0.0406 
##  3 TGT    1980-05-30         0.143  
##  4 TGT    1980-06-30         0.00854
##  5 TGT    1980-07-31         0.110  
##  6 TGT    1980-08-29         0.0178 
##  7 TGT    1980-09-30        -0.00999
##  8 TGT    1980-10-31        -0.141  
##  9 TGT    1980-11-28         0.0118 
## 10 TGT    1980-12-31         0.0175 
## # … with 1,431 more rows

Visual of Monthly Returns for Walmart, Target, and Big Lots

returns_monthly%>%
  ggplot(aes(x=monthly.returns,fill=symbol))+geom_density(alpha=0.3)

Analyze

returns_monthly%>%
  summarise(returns_avg=mean(monthly.returns))
## # A tibble: 3 x 2
##   symbol returns_avg
##   <chr>        <dbl>
## 1 BIG         0.0160
## 2 TGT         0.0206
## 3 WMT         0.0187

Target has the highest average monthly returns, showing that it would be a promising investment, however Walmart’s average monthly return is close behind. If we look at this in conjunction with the moving average, Walmart is on a steady incline while target is in a downward trend, this would confirm that Walmart would be the best stock to invest in.