if (!require(plotly))   install.packages("plotly")
##  要求されたパッケージ plotly をロード中です
##  要求されたパッケージ ggplot2 をロード中です
## 
##  次のパッケージを付け加えます: 'plotly'
##  以下のオブジェクトは 'package:ggplot2' からマスクされています:
## 
##     last_plot
##  以下のオブジェクトは 'package:stats' からマスクされています:
## 
##     filter
##  以下のオブジェクトは 'package:graphics' からマスクされています:
## 
##     layout
if (!require(quantmod)) install.packages("quantmod")
##  要求されたパッケージ quantmod をロード中です
##  要求されたパッケージ xts をロード中です
##  要求されたパッケージ zoo をロード中です
## 
##  次のパッケージを付け加えます: 'zoo'
##  以下のオブジェクトは 'package:base' からマスクされています:
## 
##     as.Date, as.Date.numeric
##  要求されたパッケージ TTR をロード中です
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
d0 <- read.csv("C:/Users/Kenny815/Downloads/pcr_tested_daily.csv")
library(DT)
datatable(d0)
plot_ly(x = d0$日付, y = d0$PCR.検査実施人数.単日., type = "scatter", mode = "markers+lines") |>
  layout(title = "PCR検査",
         xaxis = list(title = "日付"),
         yaxis = list(title = "人数"))
library(quantmod)
library(plotly)
x <- as.POSIXct(d0$日付, format = '%Y/%m/%d') 
y <- d0$PCR.検査実施人数.単日.

yhat <- rollmean(y, 31, na.pad = T)

plot_ly(type = "scatter", mode = "lines") |>
  add_trace(x = x, y = y,    name = "原系列") |>
  add_trace(x = x, y = yhat, name = "31日移動平均") |>
  config(locale = "ja") |> # x軸ラベルの月を日本語表示
  layout(title = "PCR検査実施人数(日別)",
         xaxis = list(title = "年"),
         yaxis = list(title = "実施人数"))
d1 <- read.csv("C:/Users/Kenny815/Downloads/zozo.csv")
library(DT)
datatable(d1)
library(plotly)
library(quantmod)
z <- getSymbols('3092', src  = 'yahooj',
                        from = '2022-01-01',
                        to   = '2022-12-31',
                        auto.assign = F)

# データの保存
write.zoo(z, file = 'zozo.csv', sep = ',', quote = F)

x <- as.POSIXct(index(z), format = '%Y-%m-%d')
y <- z[, 'YJ3092.Close']

# 1日分データをずらすことで当日の日付の株価が1日前のものになる。
ylag1 <- lag(y, k = 1)

# align = 'right'で右寄せ移動平均となる。
yhat5   <- rollmean(ylag1,  5, fill = NA, align = 'right')
yhat25  <- rollmean(ylag1, 25, fill = NA, align = 'right')
yhat25c <- rollmean(y, 25, fill = NA) # 参考(通常の移動平均:中心化移動平均)

matplot(x, y, type = 'l', col = 3,
main = 'ZOZO(3092)', xlab = '日', ylab = '円')
matlines(x, yhat5,   col = 2)
matlines(x, yhat25,  col = 4)
matlines(x, yhat25c, col = 1)
grid()
legend('bottomright', col = c(3, 2, 4, 1), lty = 1,
       legend = c('株価',
                  '5日移動平均',
                  '25日移動平均',
                  '25日中心化移動平均(参考)'))

chartSeries(z, TA = c(addBBands(), addMACD()), type='bar',
            subset='2022-06-01/2022-12-31')

plot_ly(type = "scatter", mode = "lines") |>
  add_trace(x = x, y = paste(y),       name = "株価") |>
  add_trace(x = x, y = paste(yhat5),   name = "5日移動平均") |>
  add_trace(x = x, y = paste(yhat25),  name = "25日移動平均") |>
  add_trace(x = x, y = paste(yhat25c), name = "25日中心化移動平均(参考)") |>
  config(locale = "ja") |> # x軸ラベルの月を日本語表示
  layout(title = "ZOZO(3092)",
         xaxis = list(title = "日"),
         yaxis = list(title = "円"))
## Warning: Can't display both discrete & non-discrete data on same axis