Hint: What is the assignment? What class is this assignment from? Who is the instructor? If it’s not a class assignment, describe your research topic.
This is taken from Joe’s Financial Management final project. The instructor is Botoa An, and the main lesson for the project is to learn real world skills on investing knowledge such as how to invest wisely, making smart buying and selling decisions. To display real world investing knowledge we chose Simple Moving Average. A simple moving average (SMA) is an arithmetic moving average calculated by adding recent closing prices and then dividing that by the number of time periods in the calculation average.
library(tidyquant)
library(tidyverse)
from = today() - years(1)
T <- tq_get("TSLA", get = "stock.prices", from = from)
T
## # A tibble: 251 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-12-06 356. 367. 351. 363. 7842500 363.
## 2 2018-12-07 369 379. 358. 358. 11511200 358.
## 3 2018-12-10 360 366. 353. 365. 6613500 365.
## 4 2018-12-11 370. 372. 360. 367. 6308800 367.
## 5 2018-12-12 369. 372. 365. 367. 5027000 367.
## 6 2018-12-13 370. 377. 367. 377. 7365900 377.
## 7 2018-12-14 375 378. 364. 366. 6337600 366.
## 8 2018-12-17 362 366. 344. 348. 7674000 348.
## 9 2018-12-18 351. 352. 334. 337. 7100000 337.
## 10 2018-12-19 338. 347. 330. 333. 8274200 333.
## # … with 241 more rows
from = today() - years(1)
G <- tq_get("GM", get = "stock.prices", from = from)
G
## # A tibble: 251 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-12-06 35.7 35.8 34.9 35.7 14533700 34.3
## 2 2018-12-07 35.4 35.9 34.5 34.7 9236700 33.3
## 3 2018-12-10 34.5 34.8 33.5 34.4 12320000 33.0
## 4 2018-12-11 35.5 35.8 34.7 34.7 12531800 33.3
## 5 2018-12-12 35.2 36.0 35.2 35.7 10324900 34.2
## 6 2018-12-13 36.0 36.2 35.1 35.1 9001000 33.7
## 7 2018-12-14 35 35.7 34.8 35.1 8415300 33.7
## 8 2018-12-17 34.9 35.6 34.5 34.7 10674200 33.3
## 9 2018-12-18 35.0 35.5 34.7 34.9 8492400 33.5
## 10 2018-12-19 35.0 36.2 34.7 34.9 11882000 33.5
## # … with 241 more rows
from = today() - years(1)
F <- tq_get("F", get = "stock.prices", from = from)
F
## # A tibble: 251 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-12-06 9.12 9.12 8.92 9.02 53963600 8.46
## 2 2018-12-07 9.02 9.13 8.8 8.82 43534800 8.27
## 3 2018-12-10 8.83 8.85 8.38 8.52 56499700 7.99
## 4 2018-12-11 8.72 8.8 8.5 8.54 42294500 8.01
## 5 2018-12-12 8.64 8.73 8.59 8.64 40602700 8.10
## 6 2018-12-13 8.67 8.7 8.46 8.5 34200400 7.97
## 7 2018-12-14 8.52 8.68 8.46 8.52 35613300 7.99
## 8 2018-12-17 8.48 8.72 8.41 8.5 44469900 7.97
## 9 2018-12-18 8.55 8.68 8.41 8.47 38263000 7.94
## 10 2018-12-19 8.46 8.65 8.27 8.32 75272900 7.80
## # … with 241 more rows
T <-
T %>%
tq_mutate(select = close, mutate_fun = SMA, n = 15) %>%
rename(SMA.short = SMA) %>%
tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
rename(SMA.long = SMA)
T
## # A tibble: 251 x 9
## date open high low close volume adjusted SMA.short SMA.long
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-12-06 356. 367. 351. 363. 7842500 363. NA NA
## 2 2018-12-07 369 379. 358. 358. 11511200 358. NA NA
## 3 2018-12-10 360 366. 353. 365. 6613500 365. NA NA
## 4 2018-12-11 370. 372. 360. 367. 6308800 367. NA NA
## 5 2018-12-12 369. 372. 365. 367. 5027000 367. NA NA
## 6 2018-12-13 370. 377. 367. 377. 7365900 377. NA NA
## 7 2018-12-14 375 378. 364. 366. 6337600 366. NA NA
## 8 2018-12-17 362 366. 344. 348. 7674000 348. NA NA
## 9 2018-12-18 351. 352. 334. 337. 7100000 337. NA NA
## 10 2018-12-19 338. 347. 330. 333. 8274200 333. NA NA
## # … with 241 more rows
G <-
G %>%
tq_mutate(select = close, mutate_fun = SMA, n = 15) %>%
rename(SMA.short = SMA) %>%
tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
rename(SMA.long = SMA)
G
## # A tibble: 251 x 9
## date open high low close volume adjusted SMA.short SMA.long
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-12-06 35.7 35.8 34.9 35.7 14533700 34.3 NA NA
## 2 2018-12-07 35.4 35.9 34.5 34.7 9236700 33.3 NA NA
## 3 2018-12-10 34.5 34.8 33.5 34.4 12320000 33.0 NA NA
## 4 2018-12-11 35.5 35.8 34.7 34.7 12531800 33.3 NA NA
## 5 2018-12-12 35.2 36.0 35.2 35.7 10324900 34.2 NA NA
## 6 2018-12-13 36.0 36.2 35.1 35.1 9001000 33.7 NA NA
## 7 2018-12-14 35 35.7 34.8 35.1 8415300 33.7 NA NA
## 8 2018-12-17 34.9 35.6 34.5 34.7 10674200 33.3 NA NA
## 9 2018-12-18 35.0 35.5 34.7 34.9 8492400 33.5 NA NA
## 10 2018-12-19 35.0 36.2 34.7 34.9 11882000 33.5 NA NA
## # … with 241 more rows
F <-
F %>%
tq_mutate(select = close, mutate_fun = SMA, n = 15) %>%
rename(SMA.short = SMA) %>%
tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
rename(SMA.long = SMA)
F
## # A tibble: 251 x 9
## date open high low close volume adjusted SMA.short SMA.long
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-12-06 9.12 9.12 8.92 9.02 53963600 8.46 NA NA
## 2 2018-12-07 9.02 9.13 8.8 8.82 43534800 8.27 NA NA
## 3 2018-12-10 8.83 8.85 8.38 8.52 56499700 7.99 NA NA
## 4 2018-12-11 8.72 8.8 8.5 8.54 42294500 8.01 NA NA
## 5 2018-12-12 8.64 8.73 8.59 8.64 40602700 8.10 NA NA
## 6 2018-12-13 8.67 8.7 8.46 8.5 34200400 7.97 NA NA
## 7 2018-12-14 8.52 8.68 8.46 8.52 35613300 7.99 NA NA
## 8 2018-12-17 8.48 8.72 8.41 8.5 44469900 7.97 NA NA
## 9 2018-12-18 8.55 8.68 8.41 8.47 38263000 7.94 NA NA
## 10 2018-12-19 8.46 8.65 8.27 8.32 75272900 7.80 NA NA
## # … with 241 more rows
stock_longt <-
T %>%
select(date, close, SMA.short, SMA.long) %>%
gather(key = "type", value = "price", close:SMA.long)
stock_longt
## # A tibble: 753 x 3
## date type price
## <date> <chr> <dbl>
## 1 2018-12-06 close 363.
## 2 2018-12-07 close 358.
## 3 2018-12-10 close 365.
## 4 2018-12-11 close 367.
## 5 2018-12-12 close 367.
## 6 2018-12-13 close 377.
## 7 2018-12-14 close 366.
## 8 2018-12-17 close 348.
## 9 2018-12-18 close 337.
## 10 2018-12-19 close 333.
## # … with 743 more rows
stock_longg <-
G %>%
select(date, close, SMA.short, SMA.long) %>%
gather(key = "type", value = "price", close:SMA.long)
stock_longg
## # A tibble: 753 x 3
## date type price
## <date> <chr> <dbl>
## 1 2018-12-06 close 35.7
## 2 2018-12-07 close 34.7
## 3 2018-12-10 close 34.4
## 4 2018-12-11 close 34.7
## 5 2018-12-12 close 35.7
## 6 2018-12-13 close 35.1
## 7 2018-12-14 close 35.1
## 8 2018-12-17 close 34.7
## 9 2018-12-18 close 34.9
## 10 2018-12-19 close 34.9
## # … with 743 more rows
stock_longf <-
F %>%
select(date, close, SMA.short, SMA.long) %>%
gather(key = "type", value = "price", close:SMA.long)
stock_longf
## # A tibble: 753 x 3
## date type price
## <date> <chr> <dbl>
## 1 2018-12-06 close 9.02
## 2 2018-12-07 close 8.82
## 3 2018-12-10 close 8.52
## 4 2018-12-11 close 8.54
## 5 2018-12-12 close 8.64
## 6 2018-12-13 close 8.5
## 7 2018-12-14 close 8.52
## 8 2018-12-17 close 8.5
## 9 2018-12-18 close 8.47
## 10 2018-12-19 close 8.32
## # … with 743 more rows
stock_longt %>%
ggplot(aes(x = date, y = price, col = type)) +
geom_line() +
theme(legend.position="bottom") +
labs(title = "Moving Averages for Tesla",
x = NULL,
y = "Stock Prices")
stock_longg %>%
ggplot(aes(x = date, y = price, col = type)) +
geom_line() +
theme(legend.position="bottom") +
labs(title = "Moving Averages for GM",
x = NULL,
y = "Stock Prices")
stock_longf %>%
ggplot(aes(x = date, y = price, col = type)) +
geom_line() +
theme(legend.position="bottom") +
labs(title = "Moving Averages for Ford",
x = NULL,
y = "Stock Prices")