# read from the .csv file
tbl_sh2017 <- as.tibble(read.csv("SH2017.csv"))
#summary(tbl_sh2017)
#head(tbl_sh2017)
#tail(tbl_sh2017)
typeof(tbl_sh2017$Close)
## [1] "double"
colnames(tbl_sh2017)
## [1] "Date" "Open" "High" "Low" "Close"
# reverse the tibble
#tbl_sh2017 <- tbl_sh2017[nrow(tbl_sh2017):1, ]
# I can also use the `arrange()` function here to get the same result
# Arrange tbl_sh2017 in *ascending* order by Date
tbl_sh2017 <- tbl_sh2017 %>% arrange(Date)
# self-defined Moving Average
#ma <- function(x,n = 20){filter(x,rep(1/n,n), sides=2)}
#ma1 <- function(x, n = 20){
# for index %in% 1:n
# y <- y + lg
#}
# calculate the 20-day moving average
tbl_sh2017$MA20 <- movavg(tbl_sh2017$Close,20, type = c("s"))
#tbl_sh2017$MA20 <- ma(tbl_sh2017$Close)
## calculate the 5-day moving average
#tbl_sh2017$MA5 <- ma(x = tbl_sh2017$Close,order= 5)
summary(tbl_sh2017)
## Date Open High Low
## 1990/12/19: 1 Min. : 96.05 Min. : 99.98 Min. : 95.79
## 1990/12/20: 1 1st Qu.:1133.54 1st Qu.:1144.81 1st Qu.:1119.45
## 1990/12/21: 1 Median :1648.22 Median :1663.41 Median :1633.72
## 1990/12/24: 1 Mean :1856.16 Mean :1874.74 Mean :1835.90
## 1990/12/25: 1 3rd Qu.:2506.53 3rd Qu.:2529.59 3rd Qu.:2478.18
## 1990/12/26: 1 Max. :6057.43 Max. :6124.04 Max. :6040.71
## (Other) :6512
## Close MA20
## Min. : 99.98 Min. : 99.98
## 1st Qu.:1133.72 1st Qu.:1130.51
## Median :1648.55 Median :1636.71
## Mean :1857.32 Mean :1852.74
## 3rd Qu.:2511.25 3rd Qu.:2518.67
## Max. :6092.06 Max. :5796.81
##
head(tbl_sh2017,21)
## # A tibble: 21 x 6
## Date Open High Low Close MA20
## <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1990/12/19 96.0 100.0 95.8 100.0 100.0
## 2 1990/12/20 104. 104. 100.0 104. 102.
## 3 1990/12/21 109. 109. 104. 109. 104.
## 4 1990/12/24 114. 115. 109. 115. 107.
## 5 1990/12/25 120. 120. 115. 120. 110.
## 6 1990/12/26 125. 125. 120. 125. 112.
## 7 1990/12/27 125. 125. 125. 125. 114.
## 8 1990/12/28 126. 126. 125. 126. 116.
## 9 1990/12/31 127. 128. 126. 128. 117.
## 10 1991/1/10 134. 134. 133. 134. 119.
## # ... with 11 more rows
tail(tbl_sh2017,21)
## # A tibble: 21 x 6
## Date Open High Low Close MA20
## <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2017/7/21 3237. 3248. 3232. 3238. 3185.
## 2 2017/7/24 3231. 3261. 3230. 3251. 3188.
## 3 2017/7/25 3249. 3262. 3233. 3244. 3191.
## 4 2017/7/26 3244. 3265. 3228. 3248. 3195.
## 5 2017/7/27 3244. 3252. 3221. 3250. 3198.
## 6 2017/7/28 3240. 3256. 3233. 3253. 3201.
## 7 2017/7/3 3192 3196. 3177. 3196. 3206.
## 8 2017/7/31 3253. 3277. 3251. 3273. 3215.
## 9 2017/7/4 3193. 3193. 3174. 3183. 3217.
## 10 2017/7/5 3179. 3207. 3175. 3207. 3220.
## # ... with 11 more rows
# find the golden cross, ie, previously below the average, and next day above the average.
#typeof(tbl_sh2017$Close)
tbl_sh2017$golden_crossing <- (stats::lag(tbl_sh2017$Close,2) <= stats::lag(tbl_sh2017$MA20,2)) & (tbl_sh2017$Close >= tbl_sh2017$MA20)
summary(tbl_sh2017$golden_crossing)
## Mode FALSE TRUE
## logical 6517 1
# the date when golden crossing occurs
summary(tbl_sh2017)
## Date Open High Low
## 1990/12/19: 1 Min. : 96.05 Min. : 99.98 Min. : 95.79
## 1990/12/20: 1 1st Qu.:1133.54 1st Qu.:1144.81 1st Qu.:1119.45
## 1990/12/21: 1 Median :1648.22 Median :1663.41 Median :1633.72
## 1990/12/24: 1 Mean :1856.16 Mean :1874.74 Mean :1835.90
## 1990/12/25: 1 3rd Qu.:2506.53 3rd Qu.:2529.59 3rd Qu.:2478.18
## 1990/12/26: 1 Max. :6057.43 Max. :6124.04 Max. :6040.71
## (Other) :6512
## Close MA20 golden_crossing
## Min. : 99.98 Min. : 99.98 Mode :logical
## 1st Qu.:1133.72 1st Qu.:1130.51 FALSE:6517
## Median :1648.55 Median :1636.71 TRUE :1
## Mean :1857.32 Mean :1852.74
## 3rd Qu.:2511.25 3rd Qu.:2518.67
## Max. :6092.06 Max. :5796.81
##
filter(tbl_sh2017,golden_crossing==TRUE)[,1]
## # A tibble: 1 x 1
## Date
## <fct>
## 1 1990/12/19
plot(tbl_sh2017$Close, main = "Close price and 20-day Moving Average")
lines(tbl_sh2017$MA20,col="red")
There is no golden cross in 2017. That it is, there is no up crossings. The stock market is bearish to some extent.