quantmod

library(readr)
AAPL_4_  <- read_csv("~/Downloads/AAPL (4).csv")
## Rows: 252 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl  (6): Open, High, Low, Close, Adj Close, Volume
## date (1): Date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
getSymbols(Symbols = "AAPL",
           src = "yahoo",
           periodicity = "daily",# "weekly", "month"
           from = "2022-06-01",  # Year-month-day
           to = "2023-02-28")
## [1] "AAPL"

Download multiple stocks

getSymbols(Symbols = c("MSFT","META","PTT.BK"),
           src = "yahoo",
           periodicity = "week",# "weekly", "month"
           from = "2020-01-01",  # Year-month-day
           to = "2022-12-31")    # Year-month-day
## [1] "MSFT"   "META"   "PTT.BK"
str(AAPL)
## An xts object on 2022-06-01 / 2023-02-27 containing: 
##   Data:    double [186, 6]
##   Columns: AAPL.Open, AAPL.High, AAPL.Low, AAPL.Close, AAPL.Volume ... with 1 more column
##   Index:   Date [186] (TZ: "UTC")
##   xts Attributes:
##     $ src    : chr "yahoo"
##     $ updated: POSIXct[1:1], format: "2023-04-26 15:35:44"
# open, high, low, and close price
AAPL <- AAPL[,1:4]
head(AAPL)
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close
## 2022-06-01    149.90    151.74   147.68     148.71
## 2022-06-02    147.83    151.27   146.86     151.21
## 2022-06-03    146.90    147.97   144.46     145.38
## 2022-06-06    147.03    148.57   144.90     146.14
## 2022-06-07    144.35    149.00   144.10     148.71
## 2022-06-08    148.58    149.87   147.46     147.96

Select by year

AAPL["2023"]
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close
## 2023-01-03    130.28    130.90   124.17     125.07
## 2023-01-04    126.89    128.66   125.08     126.36
## 2023-01-05    127.13    127.77   124.76     125.02
## 2023-01-06    126.01    130.29   124.89     129.62
## 2023-01-09    130.47    133.41   129.89     130.15
## 2023-01-10    130.26    131.26   128.12     130.73
## 2023-01-11    131.25    133.51   130.46     133.49
## 2023-01-12    133.88    134.26   131.44     133.41
## 2023-01-13    132.03    134.92   131.66     134.76
## 2023-01-17    134.83    137.29   134.13     135.94
## 2023-01-18    136.82    138.61   135.03     135.21
## 2023-01-19    134.08    136.25   133.77     135.27
## 2023-01-20    135.28    138.02   134.22     137.87
## 2023-01-23    138.12    143.32   137.90     141.11
## 2023-01-24    140.31    143.16   140.30     142.53
## 2023-01-25    140.89    142.43   138.81     141.86
## 2023-01-26    143.17    144.25   141.90     143.96
## 2023-01-27    143.16    147.23   143.08     145.93
## 2023-01-30    144.96    145.55   142.85     143.00
## 2023-01-31    142.70    144.34   142.28     144.29
## 2023-02-01    143.97    146.61   141.32     145.43
## 2023-02-02    148.90    151.18   148.17     150.82
## 2023-02-03    148.03    157.38   147.83     154.50
## 2023-02-06    152.57    153.10   150.78     151.73
## 2023-02-07    150.64    155.23   150.64     154.65
## 2023-02-08    153.88    154.58   151.17     151.92
## 2023-02-09    153.78    154.33   150.42     150.87
## 2023-02-10    149.46    151.34   149.22     151.01
## 2023-02-13    150.95    154.26   150.92     153.85
## 2023-02-14    152.12    153.77   150.86     153.20
## 2023-02-15    153.11    155.50   152.88     155.33
## 2023-02-16    153.51    156.33   153.35     153.71
## 2023-02-17    152.35    153.00   150.85     152.55
## 2023-02-21    150.20    151.30   148.41     148.48
## 2023-02-22    148.87    149.95   147.16     148.91
## 2023-02-23    150.09    150.34   147.24     149.40
## 2023-02-24    147.11    147.19   145.72     146.71
## 2023-02-27    147.71    149.17   147.45     147.92

Select year and month

# 2022-12/2023-01
AAPL["2022-12/2023-01"]
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close
## 2022-12-01    148.21    149.13   146.61     148.31
## 2022-12-02    145.96    148.00   145.65     147.81
## 2022-12-05    147.77    150.92   145.77     146.63
## 2022-12-06    147.07    147.30   141.92     142.91
## 2022-12-07    142.19    143.37   140.00     140.94
## 2022-12-08    142.36    143.52   141.10     142.65
## 2022-12-09    142.34    145.57   140.90     142.16
## 2022-12-12    142.70    144.50   141.06     144.49
## 2022-12-13    149.50    149.97   144.24     145.47
## 2022-12-14    145.35    146.66   141.16     143.21
## 2022-12-15    141.11    141.80   136.03     136.50
## 2022-12-16    136.69    137.65   133.73     134.51
## 2022-12-19    135.11    135.20   131.32     132.37
## 2022-12-20    131.39    133.25   129.89     132.30
## 2022-12-21    132.98    136.81   132.75     135.45
## 2022-12-22    134.35    134.56   130.30     132.23
## 2022-12-23    130.92    132.42   129.64     131.86
## 2022-12-27    131.38    131.41   128.72     130.03
## 2022-12-28    129.67    131.03   125.87     126.04
## 2022-12-29    127.99    130.48   127.73     129.61
## 2022-12-30    128.41    129.95   127.43     129.93
## 2023-01-03    130.28    130.90   124.17     125.07
## 2023-01-04    126.89    128.66   125.08     126.36
## 2023-01-05    127.13    127.77   124.76     125.02
## 2023-01-06    126.01    130.29   124.89     129.62
## 2023-01-09    130.47    133.41   129.89     130.15
## 2023-01-10    130.26    131.26   128.12     130.73
## 2023-01-11    131.25    133.51   130.46     133.49
## 2023-01-12    133.88    134.26   131.44     133.41
## 2023-01-13    132.03    134.92   131.66     134.76
## 2023-01-17    134.83    137.29   134.13     135.94
## 2023-01-18    136.82    138.61   135.03     135.21
## 2023-01-19    134.08    136.25   133.77     135.27
## 2023-01-20    135.28    138.02   134.22     137.87
## 2023-01-23    138.12    143.32   137.90     141.11
## 2023-01-24    140.31    143.16   140.30     142.53
## 2023-01-25    140.89    142.43   138.81     141.86
## 2023-01-26    143.17    144.25   141.90     143.96
## 2023-01-27    143.16    147.23   143.08     145.93
## 2023-01-30    144.96    145.55   142.85     143.00
## 2023-01-31    142.70    144.34   142.28     144.29

Selection from starting date to any date

# start to 2022-06-05
AAPL["/2022-06-05"]
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close
## 2022-06-01    149.90    151.74   147.68     148.71
## 2022-06-02    147.83    151.27   146.86     151.21
## 2022-06-03    146.90    147.97   144.46     145.38

Selection from any date to last date

 # 2023-02  to last date
AAPL["2023-02/"]
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close
## 2023-02-01    143.97    146.61   141.32     145.43
## 2023-02-02    148.90    151.18   148.17     150.82
## 2023-02-03    148.03    157.38   147.83     154.50
## 2023-02-06    152.57    153.10   150.78     151.73
## 2023-02-07    150.64    155.23   150.64     154.65
## 2023-02-08    153.88    154.58   151.17     151.92
## 2023-02-09    153.78    154.33   150.42     150.87
## 2023-02-10    149.46    151.34   149.22     151.01
## 2023-02-13    150.95    154.26   150.92     153.85
## 2023-02-14    152.12    153.77   150.86     153.20
## 2023-02-15    153.11    155.50   152.88     155.33
## 2023-02-16    153.51    156.33   153.35     153.71
## 2023-02-17    152.35    153.00   150.85     152.55
## 2023-02-21    150.20    151.30   148.41     148.48
## 2023-02-22    148.87    149.95   147.16     148.91
## 2023-02-23    150.09    150.34   147.24     149.40
## 2023-02-24    147.11    147.19   145.72     146.71
## 2023-02-27    147.71    149.17   147.45     147.92
Data <- AAPL["2023-02/"]

Select the first n units

# day, week, month,  quarter, year
first(AAPL,"1 year")
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close
## 2022-06-01    149.90    151.74   147.68     148.71
## 2022-06-02    147.83    151.27   146.86     151.21
## 2022-06-03    146.90    147.97   144.46     145.38
## 2022-06-06    147.03    148.57   144.90     146.14
## 2022-06-07    144.35    149.00   144.10     148.71
## 2022-06-08    148.58    149.87   147.46     147.96
## 2022-06-09    147.08    147.95   142.53     142.64
## 2022-06-10    140.28    140.76   137.06     137.13
## 2022-06-13    132.87    135.20   131.44     131.88
## 2022-06-14    133.13    133.89   131.48     132.76
##        ...                                        
## 2022-12-16    136.69    137.65   133.73     134.51
## 2022-12-19    135.11    135.20   131.32     132.37
## 2022-12-20    131.39    133.25   129.89     132.30
## 2022-12-21    132.98    136.81   132.75     135.45
## 2022-12-22    134.35    134.56   130.30     132.23
## 2022-12-23    130.92    132.42   129.64     131.86
## 2022-12-27    131.38    131.41   128.72     130.03
## 2022-12-28    129.67    131.03   125.87     126.04
## 2022-12-29    127.99    130.48   127.73     129.61
## 2022-12-30    128.41    129.95   127.43     129.93

Select the last n units

# day, week, month,  quarter, year
last(AAPL,"1 week")
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close
## 2023-02-27    147.71    149.17   147.45     147.92

Daily price to weekly price

to.weekly(AAPL)
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close
## 2022-06-03    149.90    151.74   144.46     145.38
## 2022-06-10    147.03    149.87   137.06     137.13
## 2022-06-17    132.87    137.34   129.04     131.56
## 2022-06-24    133.42    141.91   133.32     141.66
## 2022-07-01    142.70    143.49   133.77     138.93
## 2022-07-08    137.77    147.55   136.93     147.04
## 2022-07-15    145.67    150.86   142.12     150.17
## 2022-07-22    150.74    156.28   146.70     154.09
## 2022-07-29    154.01    163.63   150.80     162.51
## 2022-08-05    161.01    167.19   159.63     165.35
## 2022-08-12    166.37    172.17   163.25     172.10
## 2022-08-19    171.52    176.15   171.31     171.52
## 2022-08-26    169.69    171.05   163.56     163.62
## 2022-09-02    161.15    162.90   154.67     155.81
## 2022-09-09    156.47    157.82   152.68     157.37
## 2022-09-16    159.59    164.26   148.37     150.70
## 2022-09-23    149.31    158.74   148.56     150.43
## 2022-09-30    149.66    154.72   138.00     138.20
## 2022-10-07    138.21    147.54   137.69     140.09
## 2022-10-14    140.42    144.52   134.37     138.38
## 2022-10-21    141.07    147.85   140.27     147.27
## 2022-10-28    147.19    157.50   144.13     155.74
## 2022-11-04    153.16    155.45   134.38     138.38
## 2022-11-11    137.11    150.01   134.59     149.70
## 2022-11-18    148.97    153.59   146.15     151.29
## 2022-11-25    150.16    151.83   146.93     148.11
## 2022-12-02    145.14    149.13   140.35     147.81
## 2022-12-09    147.77    150.92   140.00     142.16
## 2022-12-16    142.70    149.97   133.73     134.51
## 2022-12-23    135.11    136.81   129.64     131.86
## 2022-12-30    131.38    131.41   125.87     129.93
## 2023-01-06    130.28    130.90   124.17     129.62
## 2023-01-13    130.47    134.92   128.12     134.76
## 2023-01-20    134.83    138.61   133.77     137.87
## 2023-01-27    138.12    147.23   137.90     145.93
## 2023-02-03    144.96    157.38   141.32     154.50
## 2023-02-10    152.57    155.23   149.22     151.01
## 2023-02-17    150.95    156.33   150.85     152.55
## 2023-02-24    150.20    151.30   145.72     146.71
## 2023-02-27    147.71    149.17   147.45     147.92
to.monthly(AAPL)
##          AAPL.Open AAPL.High AAPL.Low AAPL.Close
## Jun 2022    149.90    151.74   129.04     136.72
## Jul 2022    136.04    163.63   135.66     162.51
## Aug 2022    161.01    176.15   157.14     157.22
## Sep 2022    156.64    164.26   138.00     138.20
## Oct 2022    138.21    157.50   134.37     153.34
## Nov 2022    155.08    155.45   134.38     148.03
## Dec 2022    148.21    150.92   125.87     129.93
## Jan 2023    130.28    147.23   124.17     144.29
## Feb 2023    143.97    157.38   141.32     147.92

Line plot

plot(AAPL$AAPL.Close,
     col = "#5263BF", 
     main = "AAPL daily close price")

# Subset plot

plot(AAPL$AAPL.Close,
     col = "#5263BF", 
     main = "AAPL daily close price",
     subset = "2022-05/2022-06")

Financial chart

chartSeries(AAPL,subset = "2023")

chartSeries(PTT.BK)
addSMA(n=5,col = "red")
addSMA(n=30, col ="#458B74")
addSMA(n =100, col = "white")

AAPL.close <- Cl(AAPL)
head(AAPL.close)
##            AAPL.Close
## 2022-06-01     148.71
## 2022-06-02     151.21
## 2022-06-03     145.38
## 2022-06-06     146.14
## 2022-06-07     148.71
## 2022-06-08     147.96
plot(AAPL.close)

diff(AAPL.close, lag = 1)
##            AAPL.Close
## 2022-06-01         NA
## 2022-06-02  2.5000000
## 2022-06-03 -5.8300018
## 2022-06-06  0.7599945
## 2022-06-07  2.5700073
## 2022-06-08 -0.7500000
## 2022-06-09 -5.3200073
## 2022-06-10 -5.5099945
## 2022-06-13 -5.2500000
## 2022-06-14  0.8799896
##        ...           
## 2023-02-13  2.8400116
## 2023-02-14 -0.6500092
## 2023-02-15  2.1300049
## 2023-02-16 -1.6199951
## 2023-02-17 -1.1600037
## 2023-02-21 -4.0700073
## 2023-02-22  0.4300079
## 2023-02-23  0.4899902
## 2023-02-24 -2.6899872
## 2023-02-27  1.2099915
acf(AAPL.close)

pacf(AAPL.close)

plot(periodReturn(AAPL, period = "daily"),
     col= "red",
     main = "AAPL daily return")

library(forecast)
library(ggplot2)
P1 <- autoplot(AAPL$AAPL.Open, color = I("blue"))+
ggtitle("AAPL close price")+
xlab("Date")+
ylab("Price")+
  theme_linedraw()
P1

P2 <- ggAcf(AAPL.close)+ggtitle("acf")
P2

P3 <- ggPacf(AAPL.close)+ggtitle("pacf")
P3

pacf(AAPL.close)

library(patchwork)
P1+P2

P1/P2

P1/(P2+P3)

(P2+P3)/P1

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
ggplotly(P1)
subplot(P1,P2,P3, nrows = 2)

csv to xts object

AAPL_csv <- read_csv("AAPL (4).csv")
## Rows: 252 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl  (6): Open, High, Low, Close, Adj Close, Volume
## date (1): Date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
str(AAPL_csv)
## spc_tbl_ [252 × 7] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ Date     : Date[1:252], format: "2022-04-25" "2022-04-26" ...
##  $ Open     : num [1:252] 161 162 156 159 162 ...
##  $ High     : num [1:252] 163 162 160 165 166 ...
##  $ Low      : num [1:252] 158 157 155 159 157 ...
##  $ Close    : num [1:252] 163 157 157 164 158 ...
##  $ Adj Close: num [1:252] 162 156 156 163 157 ...
##  $ Volume   : num [1:252] 9.60e+07 9.56e+07 8.81e+07 1.30e+08 1.32e+08 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   Date = col_date(format = ""),
##   ..   Open = col_double(),
##   ..   High = col_double(),
##   ..   Low = col_double(),
##   ..   Close = col_double(),
##   ..   `Adj Close` = col_double(),
##   ..   Volume = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
ggplot(AAPL_csv) +
  aes(x = Date, y = Close) +
  geom_line(colour = "#AF668A") +
  labs(
    y = "price",
    title = "AAPL",
    subtitle = "Daily close price",
    caption = "design by Somsak Chanaim"
  ) +
  theme_gray()

data frame to xts

AAPL.xts <- xts( x = AAPL_csv[,2:7],
                 order.by = AAPL_csv$Date)
AAPL.xts
##              Open   High    Low  Close Adj.Close    Volume
## 2022-04-25 161.12 163.17 158.46 162.88  161.8993  96046400
## 2022-04-26 162.25 162.34 156.72 156.80  155.8559  95623200
## 2022-04-27 155.91 159.79 155.38 156.57  155.6273  88063200
## 2022-04-28 159.25 164.52 158.93 163.64  162.6547 130216800
## 2022-04-29 161.84 166.20 157.25 157.65  156.7007 131747600
## 2022-05-02 156.71 158.23 153.27 157.96  157.0089 123055300
## 2022-05-03 158.15 160.71 156.32 159.48  158.5197  88966500
## 2022-05-04 159.67 166.48 159.26 166.02  165.0204 108256500
## 2022-05-05 163.85 164.08 154.95 156.77  155.8261 130525300
## 2022-05-06 156.01 159.44 154.18 157.28  156.5627 116124600
##        ...                                                
## 2023-04-12 161.22 162.06 159.78 160.10  160.1000  50133100
## 2023-04-13 161.63 165.80 161.42 165.56  165.5600  68445600
## 2023-04-14 164.59 166.32 163.82 165.21  165.2100  49337200
## 2023-04-17 165.09 165.39 164.03 165.23  165.2300  41516200
## 2023-04-18 166.10 167.41 165.65 166.47  166.4700  49923000
## 2023-04-19 165.80 168.16 165.54 167.63  167.6300  47720200
## 2023-04-20 166.09 167.87 165.56 166.65  166.6500  52456400
## 2023-04-21 165.05 166.45 164.49 165.02  165.0200  58311900
## 2023-04-24 165.00 165.60 163.89 165.33  165.3300  41949600
## 2023-04-25 165.19 166.31 163.73 163.77  163.7700  48534900
str(AAPL.xts)
## An xts object on 2022-04-25 / 2023-04-25 containing: 
##   Data:    double [252, 6]
##   Columns: Open, High, Low, Close, Adj Close ... with 1 more column
##   Index:   Date [252] (TZ: "UTC")
plot(AAPL.xts$Close)