Tải dữ liệu

setwd("d:/DATA2021/CandleStick.Chart")
dulieu <-read.csv("stock.grayscale.csv")
head(dulieu)
##        Date  Open  High   Low Close     Volume
## 1 1/22/2021 32.81 34.99 32.80 34.45 17,121,340
## 2 1/21/2021 32.59 33.01 30.75 31.94 28,642,420
## 3 1/20/2021 37.17 37.18 33.30 35.35 19,223,740
## 4 1/19/2021 40.85 40.98 37.85 38.06 14,874,590
## 5 1/15/2021 41.74 41.74 38.14 39.34 18,059,010
## 6 1/14/2021 43.90 44.50 43.45 43.53 13,053,650

Đồ thị Line

library(ggplot2)
library(tidyquant)
library(showtext)
font_add_google("Roboto Slab","Ro1")

ggplot(data=dulieu)+ theme_tq() +
  theme(text = element_text(family = "Ro1", size=12)) +
  geom_line(aes(x=Date, y=Close), size=3,color="red") + 
  labs(title = "GrayScale Line Chart", y = "Closing Price", x = "") 

Vẽ đồ thị nến

ggplot(data=dulieu, aes(x=Date,y=Close))+ theme_tq() +
  theme(text = element_text(family = "Ro1", size=12)) +
  geom_candlestick(aes(open = Open, high = High, low = Low, close = Close)) + 
  labs(title = "GrayScale CandlStick Chart", y = "Closing Price", x = "") 

Vẽ đồ thị lượng giao dịch

ggplot(data=dulieu, aes(x=Date, y=0)) + theme_tq() + 
  geom_segment(aes(xend = Date, yend = Volume, color = Volume), size=3) + 
    geom_smooth(aes(x=Date, y=Volume),method = "loess", se = FALSE) +
    labs(title = "AMZN Volume Chart", 
         subtitle = "Charting Daily Volume", 
         y = "Volume", x = "") +
     theme(legend.position = "none") 

Ước lượng xu hướng

n <-nrow(dulieu)
dulieu$STT <-seq(1, 20, 1)
head(dulieu)
##        Date  Open  High   Low Close     Volume STT
## 1 1/22/2021 32.81 34.99 32.80 34.45 17,121,340   1
## 2 1/21/2021 32.59 33.01 30.75 31.94 28,642,420   2
## 3 1/20/2021 37.17 37.18 33.30 35.35 19,223,740   3
## 4 1/19/2021 40.85 40.98 37.85 38.06 14,874,590   4
## 5 1/15/2021 41.74 41.74 38.14 39.34 18,059,010   5
## 6 1/14/2021 43.90 44.50 43.45 43.53 13,053,650   6
ggplot(data=dulieu, aes(x=STT, y=Close)) +
  geom_line() +  geom_smooth(method = "lm", color="red") 

  #geom_ma(aes(x=STT,y=Close),ma_fun = SMA, n = 3) +
  #geom_bbands(aes(x=STT, y=Close),ma_fun = SMA, sd = 2, n = 5)

Vẽ bằng gói Plotly

library(plotly)
#plot_ly(  data=dulieu, x= STT,
 #         type = "candlestick",
  #        open = Open,
   #       close = Close,
    #      high = High,
     #     low = Low)