# 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 # RSI 추가 df <- data.frame(Date=index(samsung),coredata(samsung)) df$rsi_week <- RSI(df$close,n=5) df$rsi_2weeks <- RSI(df$close,n=10) df$rsi_month <- RSI(df$close,n=20) df <- tail(df, 600) 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 = ~rsi_week , name = "RSI_일주일", line = list(color = 'green', width = 1), inherit = F, yaxis = "y2") fig <- fig %>% add_lines(x = ~Date, y = ~rsi_2weeks , name = "RSI_2주일", line = list(color = 'orange', width = 1), inherit = F, yaxis = "y2") fig <- fig %>% add_lines(x = ~Date, y = ~rsi_month , name = "RSI_한달", line = list(color = 'blue', width = 1), inherit = F, yaxis = "y2") 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, yaxis2 = list( tickfont = list(color = "green"), overlaying = "y", side = "right", title = "rsi" )) fig |
댓글 없음:
댓글 쓰기