library(readxl)
library(here)
## here() starts at /Users/lynn/Desktop/SF FInal
Cleaned_Data<-here("SF Final", "03_data_processed", "Cleaned Data.xlsx")
Cleaned_Data <- read_excel("Cleaned Data.xlsx")
View(Cleaned_Data)
library(ggplot2)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ✔ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(tidyquant)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
##
## 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
##
##
## 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
vec<-c()
for(i in c(1:length(Cleaned_Data$`Opening Price`))){
if(Cleaned_Data$`Opening Price`[i] >= Cleaned_Data$`Closing Price`[i]){
group = 'Fall'
}else{
group = 'Rise'
}
vec<-c(vec,group)
}
Cleaned_Data$Group<-vec
a<-ggplot(data=Cleaned_Data,aes(x=Date))+
geom_errorbar(aes(ymin = `Minimum Price`, ymax=`Maximum Price`), size=0.5) +
geom_rect(aes(xmin =
Date-0.5, xmax =Date+0.5,
ymin = pmin(`Opening Price`,`Closing Price`), ymax = pmax(`Opening Price`, `Closing Price`), fill = Group))+
scale_fill_manual(values = rainbow(2,alpha = 0.4))+
labs(x= "Date", y= "Price", title = "AAA")+
theme(legend.position = c(0.9,0.9))
a

Cleaned_Data$candleLower<-pmin(Cleaned_Data$`Opening Price`,Cleaned_Data$`Closing Price`)
Cleaned_Data$candleUpper<-pmax(Cleaned_Data$`Opening Price`,Cleaned_Data$`Closing Price`)
Cleaned_Data$candleMiddle <- NA
Cleaned_Data$color <- "green"
Cleaned_Data$color[Cleaned_Data$`Closing Price`<Cleaned_Data$`Opening Price`] = "red"
BB<-geom_boxplot(data=Cleaned_Data,aes(lower=candleLower, middle=candleMiddle, upper=candleUpper, ymin=`Minimum Price`, ymax=`Maximum Price`,group=Date, fill=color))
BB
## mapping: lower = ~candleLower, middle = ~candleMiddle, upper = ~candleUpper, ymin = ~`Minimum Price`, ymax = ~`Maximum Price`, group = ~Date, fill = ~color
## geom_boxplot: outlier.colour = NULL, outlier.fill = NULL, outlier.shape = 19, outlier.size = 1.5, outlier.stroke = 0.5, outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5, varwidth = FALSE, na.rm = FALSE, orientation = NA
## stat_boxplot: na.rm = FALSE, orientation = NA
## position_dodge2
A<-ggplot(data=Cleaned_Data,aes(x = Date, y =`Closing Price`)) +
geom_candlestick(aes(open = `Opening Price`, high = `Maximum Price`, low = `Minimum Price`, close = `Closing Price`)) +
labs(title = "Candlestick Chart", y = "Closing Price", x = "Date") +
theme_tq()
A

###We want to apply a SMA(simple moving average), so we research the TTR function and we see that it accepts, n, the number of periods to average over. We see that the aesthetics required are x, a date, and y, a price. Since these are already in the main ggplot() function, we don’t need to add the aesthetics to the geom. We apply the moving average geoms after the candlestick geom to overlay the moving averages on top of the candlesticks. We add two moving average calls, one for the 50-day and the other for the 200-day. We add color = "red" and linetype = 5 to distinguish the 200-day from the 50-day.
B<-ggplot(data=Cleaned_Data,aes(x = Date, y = `Closing Price`)) +
geom_candlestick(aes(open = `Opening Price`, high = `Maximum Price`, low = `Minimum Price`, close = `Closing Price`)) +
geom_ma(ma_fun = SMA, n = 50, linetype = 5, size = 1.25) +
geom_ma(ma_fun = SMA, n = 150, color = "red", size = 1.25) +
labs(title = "Candlestick Chart",
subtitle = "50 and 150-Day SMA",
y = "Closing Price", x = "") +
theme_tq()
B

###We want an EMA(exponential moving averages), so we research the TTR function and we see that it accepts, n, the number of periods to average over, wilder a Boolean, and ratio arguments. We will use wilder = TRUE and go with the default for the ratio arg. We see that the aesthetics required are x, a date, and y, a price. Since these are already in the main ggplot() function, we don’t need to modify the geom. We are ready to apply after the bar chart geom.
C<-ggplot(data=Cleaned_Data,aes(x = Date, y = `Closing Price`)) +
geom_barchart(aes(open = `Opening Price`, high = `Maximum Price`, low = `Minimum Price`, close = `Closing Price`)) +
geom_ma(ma_fun = EMA, n = 50, wilder = TRUE, linetype = 5, size = 1.25) +
geom_ma(ma_fun = EMA, n = 150, wilder = TRUE, color = "red", size = 1.25) +
labs(title = "Bar Chart",
subtitle = "50 and 150-Day EMA",
y = "Closing Price", x = "") +
theme_tq()
C

Cleaned_Data$Date<-as.Date(Cleaned_Data$Date)
end <- as_date("2022-11-02")
end
## [1] "2022-11-02"
start<- as_date("2021-11-02")
start
## [1] "2021-11-02"
range_60_tbl <- Cleaned_Data %>%
tail(100) %>%
summarise(
max_high = max(`Maximum Price`),
min_low = min(`Minimum Price`)
)
range_60_tbl
## # A tibble: 1 × 2
## max_high min_low
## <dbl> <dbl>
## 1 61.1 38.5
#Applying BBands using a SMA
##Let’s do a basic example to add Bollinger Bands using a simple moving average. Because both the candlestick geom and the BBands geom use high, low and close prices, we move these aesthetics to the main ggplot() function to avoid duplication. We add BBands after the candlestick geom to overlay the BBands on top.
Cleaned_Data%>%
ggplot(aes(x = Date, y =`Closing Price` , open = `Opening Price`, high = `Maximum Price`, low = `Minimum Price`, close = `Closing Price`)) +
geom_candlestick() +
geom_bbands(ma_fun = SMA, sd = 2, n = 20) +
labs(title = "AAPL Candlestick Chart",
subtitle = "BBands with SMA Applied",
y = "Closing Price", x = "") +
coord_x_date(xlim=c(start,end),
ylim = c(range_60_tbl$min_low * 0.85,
range_60_tbl$max_high) * 1.05) +
theme_tq()

Cleaned_Data %>%
ggplot(aes(x = Date, y =`Bulk Agreement trading Volume`)) +
geom_segment(aes(xend = Date, yend = 0, color = `Bulk Agreement trading Volume` )) +
geom_smooth(method = "loess", se = FALSE) +
labs(title = "Volume Chart",
subtitle = "Charting Daily Volume",
y = "Volume", x = "") +
theme_tq() +
theme(legend.position = "none")
## `geom_smooth()` using formula 'y ~ x'

##And, we can zoom in on a specific region. Using scale_color_gradient we can quickly visualize the high and low points, and using geom_smooth we can see the trend.
start <- end - weeks(24)
Cleaned_Data %>%
filter(Date >= start - days(50)) %>%
ggplot(aes(x = Date, y = `Bulk Agreement trading Volume`)) +
geom_segment(aes(xend = Date, yend = 0, color = `Bulk Agreement trading Volume`)) +
geom_smooth(method = "loess", se = FALSE) +
labs(title = "Bar Chart",
subtitle = "Charting Daily Volume, Zooming In",
y = "Volume", x = "") +
coord_x_date(xlim = c(start, end)) +
scale_color_gradient(low = "red", high = "darkblue") +
theme_tq() +
theme(legend.position = "none")
## `geom_smooth()` using formula 'y ~ x'
