If making the financial analysis of the stock exchange data, the following packages are needed:
#install.packages("PerformanceAnalytics", repos = "http://cran.us.r-project.org")
#install.packages("dplyr", repos = "http://cran.us.r-project.org")
#install.packages("tidyquant", repos = "http://cran.us.r-project.org")
#install.packages("quantmod", repos = "http://cAs iran.us.r-project.org")
#install.packages("tseries", repos = "http://cran.us.r-project.org")
#install.packages("tidyverse", repos = "http://cran.us.r-project.org")
library(PerformanceAnalytics) #useful package !!!
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
library(quantmod) # useful package
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(tidyquant) # useful
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(randtests) # for runs test
# other libraries
library(ggplot2)
library(dplyr)
##
## ######################### 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: 'dplyr'
## The following objects are masked from 'package:xts':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tseries)
##
## Attaching package: 'tseries'
## The following object is masked from 'package:randtests':
##
## runs.test
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ purrr 1.0.2 ✔ tibble 3.2.1
## ✔ readr 2.1.5 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
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(hrbrthemes)
library(xts)
library(knitr)
library(kableExtra)
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
##
## The following object is masked from 'package:purrr':
##
## some
##
## The following object is masked from 'package:dplyr':
##
## recode
library(mathjaxr)
library(zoo)
rm(list=ls())
If downloading the stock-exchange data, we use the quantmod package command “getSymbols”. COmmand “Ad” enables extraction of just Adjusted closing prices of the day.
# Load the required package
library(quantmod)
# Set dates for data retrieval
start_date <- "2010-01-01"
end_date <- "2022-12-31"
symbol <-"F"
# Download the data for Ford (ticker symbol: F)
getSymbols(symbol, src = "yahoo", from = start_date, to = end_date, auto.assign = TRUE)
## [1] "F"
# Extract the adjusted close prices
adot.close <- Ad(getSymbols(symbol, src = "yahoo", from = start_date, to = end_date, auto.assign = FALSE))
title <- paste(symbol)
plot(adot.close, col="red", main=title)
valtozas <- diff(adot.close)
median_valtozas <- median(valtozas, na.rm = TRUE) # Compute median excluding NA
valtozas[is.na(valtozas)] <- median_valtozas # replacing missing returns by their estimates by median
acf(valtozas,main="Autocorrelations of Price differences")
The above given Autocorrelation function is a function, which assigns
each lag a correlation between actual return and a lagged return by a
specified number of periods (varying from 1 to 30 ). If the correlations
would be within the band between two blue dotted lines, we could accept
the hypothesis that price movements of the F are not information weakly
efficient. On the other side, lags 7 and 15 seem to be statistically
significant (at 5 percent significance level). Remember, it can by just
demonstration of the Type 1 Error !!! There are just 2 cases of 30 -
that is why we are prone to reject the alternative hypothesis. We do not
have any information to reject \(H_0\),
prices can be considered as weakly efficient.
To test the weak form of the efficiency, we can use Runs test. We are using the diferenced time series as in the previous example. Therafter, we substitute
# Load the required package
library(randtests)
# Perform the Runs Test
#runs_test_result <- runs.test(as.vector(valtozas))
# Print the result
#print(runs_test_result)
\(H_0: \quad \text{Price movements reflect a weak form of the efficiency}\) \(H_1: \quad \text{Price movements deny the weak form of the efficiency}\)
In our case p-value is high, we reject the alternative, we deny the alternative hypothesis of absenting the weak form of the efficiency.
All public (but not inside) information is calculated into a stock’s current price. Neither fundamental nor technical analysis can be used to achieve superior gains.
Test Event Studies: These studies examine the stock price reaction to new public information, such as earnings announcements, to determine how quickly and accurately prices adjust.
Both the public and inside information is calculated into a stock current price.