R 패키지를 처음 사용하려면 install.packages("tidyquant") 명령어를 한 번 실행해야 합니다. tidyquant 사용에 문제가 있을 경우 해당 패키지 설명서를 참조하십시오.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.3 ✔ tibble 3.2.1
✔ lubridate 1.9.2 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
Loading required package: PerformanceAnalytics
Loading required package: xts
Loading required package: zoo
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Date.numeric
######################### Warning from 'xts' package ##########################
# #
# The dplyr lag() function breaks how base R's lag() function is supposed to #
# work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
# source() into this session won't work correctly. #
# #
# Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
# conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
# dplyr from breaking base R's lag() function. #
# #
# Code in packages is not affected. It's protected by R's namespace mechanism #
# Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
# #
###############################################################################
Attaching package: 'xts'
The following objects are masked from 'package:dplyr':
first, last
Attaching package: 'PerformanceAnalytics'
The following object is masked from 'package:graphics':
legend
Loading required package: quantmod
Loading required package: TTR
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
먼저, 삼성전자 주식(005930.KS)과 같은 하나의 종목 심볼에 대한 일일 가격 데이터를 데이터 제공자 Yahoo!Finance에서 직접 다운로드하겠습니다. 데이터를 다운로드하기 위해 tq_get 명령어를 사용할 수 있습니다.
만약 tq_get 명령어 사용 방법을 모르는 경우, ?tq_get 명령어를 입력하여 도움말 파일을 확인하십시오. 특히 설명서의 예시 부분을 꼼꼼히 살펴보는 것을 추천합니다.
대한민국의 상장회사 주가 데이터를 받기 위하여 네이버https://www.naver.com/ 검색 사이트를 활용하는 것이 가장 편한 방법이 될 수도 있습니다.
원천 주가 데이터는 한국 거래소의 API를 활용하여 받아야 할 수 있으나 일반적으로 주가 데이터는 Raw data 형태를 그대로 사용하기에는 무리가 있습니다. 일례로 주식분할과 같은 사건이 발생하면 1개의 주식이 여러개로 나누어지면서 주가가 큰 폭으로 줄어들게 됩니다. 시가총액은 동일한데 주식 개당 가치가 어느 날 갑자기 감소하게 되면서 주식분할 일자 이전과 이후의 주식가격은 비교 가능성을 잃게 됩니다. 따라서 이전 주가에 주식분할 효과를 부여하여 수정한 수정 주가를 사용하여야 비교 분석이 가능합니다.
네이버 검색 사이트는 수정 주가 데이터를 제공하므로 이 데이터를 가져다가 가공하는 것이 대한민국 거래소에서 거래되는 주식 가격을 얻는 좋은 방법이 될 수 있습니다.
Rows: 953 Columns: 8
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (4): 종목코드, 종목명, 시장구분, 업종명
dbl (4): 종가, 대비, 등락률, 시가총액
ℹ 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.