# plotly 챠트 library(plotly) library(quantmod) samsung <- getSymbols('005930.KS', auto.assign = FALSE) colnames(samsung) <- c('open','high','low','close','volume','adjusted') #samsung$rtn <- ROC(Cl(samsung)) #일일 수익률 samsung <- na.omit(samsung) # NA 제거 # basic example of ohlc charts df <- data.frame(Date=index(samsung),coredata(samsung)) df <- tail(df, 600) # cutom colors i <- list(line = list(color = 'red')) d <- list(line = list(color = 'blue')) fig <- df %>% plot_ly(x = ~Date, type="candlestick", open = ~open, close = ~close, high = ~high, low = ~low, increasing = i, decreasing = d) fig <- fig %>% add_lines(x = ~Date, y = ~close, line = list(color = 'black', width = 2), inherit = F) fig <- fig %>% layout(title = "Basic Candlestick Chart", xaxis = list(rangeslider = list(visible = F)), showlegend = FALSE) fig # 이동평균 추가 df <- data.frame(Date=index(samsung),coredata(samsung)) # 단순이동평균 SMA : calculates the arithmetic mean of the series over the past n observations. df$MA5 <- SMA(df$close,n=5) df$MA20 <- SMA(df$close,n=20) # 지수평활법 EMA : calculates an exponentially-weighted mean, giving more weight to recent observations. See Warning section below. df$EMA5 <- EMA(df$close,n=5) df$EMA20 <- EMA(df$close,n=20) # 선형가중이동평균 WMA is similar to an EMA, but with linear weighting if the length of wts is equal to n. If the length of wts is equal to the length of x, the WMA will use the values of wts as weights. df$WMA5 <- WMA(df$close,n=5) df$WMA20 <- WMA(df$close,n=20) # 거래량가중이동평균 VWAP calculate the volume-weighted moving average price. df$VWAP5 <- VWAP(df$close,df$volume,n=5) df$VWAP20 <- VWAP(df$close,df$volume,n=20) df <- tail(df, 300) i <- list(line = list(color = 'red')) d <- list(line = list(color = 'blue')) fig <- df %>% plot_ly(x = ~Date, type="candlestick", name = "일봉", open = ~open, close = ~close, high = ~high, low = ~low, increasing = i, decreasing = d) fig <- fig %>% add_lines(x = ~Date, y = ~MA5 , name = "단순이동평균_5일", line = list( width = 1), legendgroup = "SMA",inherit = F) fig <- fig %>% add_lines(x = ~Date, y = ~MA20 , name = "단순이동평균_20일", line = list( width = 1), legendgroup = "SMA",inherit = F) fig <- fig %>% add_lines(x = ~Date, y = ~EMA5 , name = "지수평활법_5일", line = list( width = 1), legendgroup = "EMA",inherit = F) fig <- fig %>% add_lines(x = ~Date, y = ~EMA20 , name = "지수평활법_20일", line = list( width = 1), legendgroup = "EMA",inherit = F) fig <- fig %>% add_lines(x = ~Date, y = ~WMA5 , name = "선형가중이동평균_5일", line = list( width = 1), legendgroup = "WMA",inherit = F) fig <- fig %>% add_lines(x = ~Date, y = ~WMA20 , name = "선형가중이동평균_20일", line = list( width = 1), legendgroup = "WMA",inherit = F) fig <- fig %>% add_lines(x = ~Date, y = ~VWAP5 , name = "거래량가중이동평균_5일", line = list( width = 1), legendgroup = "VWAP",inherit = F) fig <- fig %>% add_lines(x = ~Date, y = ~VWAP20 , name = "거래량가중이동평균_20일", line = list( width = 1), legendgroup = "VWAP",inherit = F) fig <- fig %>% add_lines(x = ~Date, y = ~close, name='종가', line = list(color = 'black', width = 2), inherit = F) fig <- fig %>% layout(title = "Basic Candlestick Chart", xaxis = list(rangeslider = list(visible = F)), showlegend = TRUE) fig |
댓글 없음:
댓글 쓰기