Stock Analysis Theory

Financial Mathematics 1

1 Daily stock prices

1.1 Obtaining data from R packages

Necessary packages in use:

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
## # dplyr from breaking base R's lag() function.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'xts'
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## 
## 
## Attaching package: 'PerformanceAnalytics'
## 
## The following object is masked from 'package:graphics':
## 
##     legend
## 
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(quantmod)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(DT)

1: Write a R code to obtain stock prices for 4 companies: NETFLIX, TESLA, VINFAST, GENERAL, MOTORS in the latest 4 months.

start_date <- as.Date("2024-01-17")
end_date <- as.Date("2024-05-17")
symbols <- c("NFLX", "TSLA", "VFS", "GM")

prices <- tq_get(symbols, from = start_date, to = end_date)

prices_filtered <- prices %>%
  filter(symbol == "VFS") %>%
  mutate(arithmetic_return = (adjusted - lag(adjusted)) / lag(adjusted),
         log_return = log(adjusted) - log(lag(adjusted))) %>%
  select(symbol, date, adjusted, arithmetic_return, log_return) %>%
  na.omit()

datatable(prices_filtered)

2. Write a R code to plot stock prices for 4 companies: NETFLIX, TESLA, VINFAST, GENERAL MOTORS in the latest 4 months.

start_date <- as.Date("2024-01-17")
end_date <- as.Date("2024-05-17")
symbols <- c("NFLX", "TSLA", "VFS", "GM")

prices <- tq_get(symbols, from = start_date, to = end_date)

prices_filtered <- prices %>%
  filter(symbol == "VFS") %>%
  mutate(arithmetic_return = (adjusted - lag(adjusted)) / lag(adjusted),
         log_return = log(adjusted) - log(lag(adjusted))) %>%
  select(symbol, date, adjusted, arithmetic_return, log_return) %>%
  na.omit()

datatable(prices_filtered)

2 Daily stock return

  1. Write a R code to compute and visualize the daily arithmetic return for VINFAST in the last 4 months.
daily_arithmetic_return_4m <- prices %>%
  filter(date >= start_date, date <= end_date, symbol == "VFS") %>%
  mutate(daily_arithmetic_return = (adjusted - lag(adjusted)) / lag(adjusted)) %>%
  select(symbol, date, adjusted, daily_arithmetic_return) %>%
  na.omit()

datatable(daily_arithmetic_return_4m)
daily_arithmetic_return_4m %>%
  ggplot(aes(x = date, y = daily_arithmetic_return, color = symbol)) +
  geom_line() +
  labs(title = "Daily Arithmetic Return for VINFAST", x = "Date", y = "Arithmetic Return") +
  theme_minimal()

  1. Write a R code to compute and visualize the daily log return for VINFAST in the last 4 months.
daily_log_returns_4m <- prices %>% filter(date < "2024-05-18", date >= "2024-01-18", symbol == "VFS") %>%
    group_by(symbol) %>%
    mutate(daily_log_return=log(adjusted)-log(lag(adjusted,1))) %>% 
    select(symbol, date, adjusted,daily_log_return)%>%
    na.omit() # Remove rows with NA values in arithmetic_return

daily_log_returns_4m %>% datatable()
daily_log_returns_4m %>% ggplot(aes(x=date, y=daily_log_return, col=symbol))+geom_line()+facet_grid(symbol~.)

3 Daily Stock volatility

3.1 Code practice

Compute and visualize the sample daily volatility for 4 stocks NETFLIX, TESLA, VINFAST, GENERAL MOTORS in the last 4 months. Which one has the greatest volatility?

data= daily_log_returns_4s_4m <- prices %>% 
  filter(date <"2024-05-18", date >="2024-01-18", symbol %in%c( "NFLX", "TSLA", "VFS", "GM")) %>%
  group_by(symbol)%>% mutate(daily_log_return=log(adjusted)-log(lag(adjusted,1))) %>%   
  select(symbol, date, adjusted, daily_log_return)%>%na.omit()

data %>% 
  select(symbol,date,daily_log_return)%>% 
  pivot_wider(names_from = symbol, values_from = daily_log_return) ## showing 4 stocks by 4 columns
## # A tibble: 83 × 5
##    date           NFLX     TSLA      VFS        GM
##    <date>        <dbl>    <dbl>    <dbl>     <dbl>
##  1 2024-01-19 -0.00487  0.00146  0.0619   0.0265  
##  2 2024-01-22  0.00570 -0.0161   0.0263  -0.00536 
##  3 2024-01-23  0.0133   0.00163 -0.00325 -0.00255 
##  4 2024-01-24  0.102   -0.00628 -0.0382  -0.0152  
##  5 2024-01-25  0.0310  -0.129    0.0349   0.0132  
##  6 2024-01-26  0.0149   0.00339 -0.0299   0.000569
##  7 2024-01-29  0.00937  0.0411   0.0282   0.00595 
##  8 2024-01-30 -0.0227   0.00345 -0.0182   0.0751  
##  9 2024-01-31  0.00224 -0.0227  -0.00837  0.0169  
## 10 2024-02-01  0.00601  0.00835 -0.00337  0.00180 
## # ℹ 73 more rows
data %>% ggplot(aes(x=date, y=daily_log_return, col=symbol))+geom_line()+facet_grid(symbol~.)

data%>% 
  mutate(diff=daily_log_return-mean(daily_log_return)) %>% 
  summarize(volatility = sqrt(sum(diff^2) / (n() - 1)))
## # A tibble: 4 × 2
##   symbol volatility
##   <chr>       <dbl>
## 1 GM         0.0159
## 2 NFLX       0.0221
## 3 TSLA       0.0366
## 4 VFS        0.0634
data=daily_log_returns_4s_4m %>% filter(date< "2024-05-18", date >="2024-01-18", symbol %in%c( "NFLX", "TSLA", "VFS", "GM"))
data %>% datatable()
data%>% 
  mutate(diff=daily_log_return-mean(daily_log_return)) %>% 
  summarize(volatility = sqrt(sum(diff^2) / (n() - 1)))
## # A tibble: 4 × 2
##   symbol volatility
##   <chr>       <dbl>
## 1 GM         0.0159
## 2 NFLX       0.0221
## 3 TSLA       0.0366
## 4 VFS        0.0634

4 Daily covariance between two stocks

Consider 4 stocks NETFLIX, TESLA, VINFAST, GENERAL MOTORS in the last 4 months.

a) Plot the scatter plot for each pair of stocks’ log returns?

data1=daily_log_returns_4s_4m %>% select(symbol,date,daily_log_return)%>% pivot_wider(names_from = symbol, values_from = daily_log_return)

data1
## # A tibble: 83 × 5
##    date           NFLX     TSLA      VFS        GM
##    <date>        <dbl>    <dbl>    <dbl>     <dbl>
##  1 2024-01-19 -0.00487  0.00146  0.0619   0.0265  
##  2 2024-01-22  0.00570 -0.0161   0.0263  -0.00536 
##  3 2024-01-23  0.0133   0.00163 -0.00325 -0.00255 
##  4 2024-01-24  0.102   -0.00628 -0.0382  -0.0152  
##  5 2024-01-25  0.0310  -0.129    0.0349   0.0132  
##  6 2024-01-26  0.0149   0.00339 -0.0299   0.000569
##  7 2024-01-29  0.00937  0.0411   0.0282   0.00595 
##  8 2024-01-30 -0.0227   0.00345 -0.0182   0.0751  
##  9 2024-01-31  0.00224 -0.0227  -0.00837  0.0169  
## 10 2024-02-01  0.00601  0.00835 -0.00337  0.00180 
## # ℹ 73 more rows
plot(data1$NFLX[-1],data1$TSLA[-1])

plot(data1$NFLX[-1],data1$VFS[-1])

plot(data1$NFLX[-1],data1$GM[-1])

plot(data1$VFS[-1],data1$TSLA[-1])

plot(data1$VFS[-1],data1$GM[-1])

plot(data1$GM[-1],data1$TSLA[-1])

b) Compute the sample daily covariance for each pair of 4 stocks.

data2=data1 %>% filter(date < "2024-05-18", date >="2023-02-18")  
data2 %>% datatable()
# The sample daily covariance of NFLX and VFS
## Method 1:
 sum((data2$NFLX-mean(data2$NFLX))*(data2$VFS - mean(data2$VFS))) / (nrow(data2) - 1)
## [1] 0.0001859198