안녕하세요.
오늘은 파이썬으로 주식시장 영업일과 휴장일을 구분하는 코드에 대해 공부해보겠습니다.
파이썬 패키지 exchange_calendars
해당 패키지에 대한 상세한 내용은 아래 사이트를 참조하시기 바랍니다.
- 사이트 바로가기
▣ 패키지 설치
pip install exchange_calendars
▣ 패키지 개요
○ 개장일/휴장일 여부 확인 가능 거래소 리스트
ecals.get_calendar_names(include_aliases=False)
## ['24/5', '24/7', 'AIXK', 'ASEX', 'BVMF', 'CMES', 'IEPA', 'XAMS', 'XASX', 'XBKK', 'XBOG', 'XBOM', 'XBRU',~~
반응형
○ 특정일 개장일 확인하기
import exchange_calendars as ecals
import pandas as pd
import datetime
XKRX = ecals.get_calendar("XKRX") # 한국 코드
NYSE = ecals.get_calendar("NYSE") # 뉴욕증시 코드
print(XKRX.is_session("2021-11-11")) # 2021-11-11 은 개장일인지 확인
## False
print(XKRX.is_session(datetime.date.today().strftime("%Y-%m-%d")))# 오늘은 개장일인지 확인
## False
○ 특정 날짜 개장일/휴장일 확인하기
dk= ecals.get_calendar("XKRX",
start= "2022-04-01",
end = "2022-04-30",
side = "left")
dk.schedule.loc["2022-04-07":"2022-04-14"]
################################################################################
market_open break_start break_end market_close
2022-04-07 00:00:00+00:00 2022-04-07 NaT NaT 2022-04-07 06:30:00
2022-04-08 00:00:00+00:00 2022-04-08 NaT NaT 2022-04-08 06:30:00
2022-04-11 00:00:00+00:00 2022-04-11 NaT NaT 2022-04-11 06:30:00
2022-04-12 00:00:00+00:00 2022-04-12 NaT NaT 2022-04-12 06:30:00
2022-04-13 00:00:00+00:00 2022-04-13 NaT NaT 2022-04-13 06:30:00
2022-04-14 00:00:00+00:00 2022-04-14 NaT NaT 2022-04-14 06:30:00
################################################################################
○ 특정기간 개장일 리스트 추출
k = ecals.get_calendar("XKRX")
df = pd.DataFrame(k.schedule.loc["2020-01-01":"2020-12-31"])
print(df)
date_list = []
for i in df['market_open']:
date_list.append(i.strftime("%Y-%m-%d"))
#테스트 print(i.strftime("%Y-%m-%d"))
print(date_list)
## ['2020-01-02', '2020-01-03', '2020-01-06', '2020-01-07', '2020-01-08', '2020-01-09', '2020-01-10', '2020-01-13', '2020-01-14', '2020-01-15', '2020-01-16', '2020-01-17', '2020-01-20', '2020-01-21', '2020-01-22', '2020-01-23', '2020-01-28', '2020-01-29', '2020-01-30', '2020-01-31', '2020-02-03', '2020-02-04', '2020-02-05', '2020-02-06', '2020-02-07', '2020-02-10', '2020-02-11', '2020-02-12', '2020-02-13', '2020-02-14', '2020-02-17', '2020-02-18', '2020-02-19', '2020-02-20', '2020-02-21', '2020-02-24', '2020-02-25', '2020-02-26', '2020-02-27', '2020-02-28', '2020-03-02', '2020-03-03', '2020-03-04', '2020-03-05', '2020-03-06', '2020-03-09', '2020-03-10', '2020-03-11', '2020-03-12', '2020-03-13', '2020-03-16', '2020-03-17', '2020-03-18', '2020-03-19', '2020-03-20', '2020-03-23', '2020-03-24', '2020-03-25', '2020-03-26', '2020-03-27', '2020-03-30', '2020-03-31', '2020-04-01', '2020-04-02', '2020-04-03', '2020-04-06', '2020-04-07', '2020-04-08', '2020-04-09', '2020-04-10', '2020-04-13', '2020-04-14', '2020-04-16', '2020-04-17', '2020-04-20', '2020-04-21', '2020-04-22', '2020-04-23', '2020-04-24', '2020-04-27', '2020-04-28', '2020-04-29', '2020-05-04', '2020-05-06', '2020-05-07', '2020-05-08', '2020-05-11', '2020-05-12', '2020-05-13', '2020-05-14', '2020-05-15', '2020-05-18', '2020-05-19', '2020-05-20', '2020-05-21', '2020-05-22', '2020-05-25', '2020-05-26', '2020-05-27', '2020-05-28', '2020-05-29', '2020-06-01', '2020-06-02', '2020-06-03', '2020-06-04', '2020-06-05', '2020-06-08', '2020-06-09', '2020-06-10', '2020-06-11', '2020-06-12', '2020-06-15', '2020-06-16', '2020-06-17', '2020-06-18', '2020-06-19', '2020-06-22', '2020-06-23', '2020-06-24', '2020-06-25', '2020-06-26', '2020-06-29', '2020-06-30', '2020-07-01', '2020-07-02', '2020-07-03', '2020-07-06', '2020-07-07', '2020-07-08', '2020-07-09', '2020-07-10', '2020-07-13', '2020-07-14', '2020-07-15', '2020-07-16', '2020-07-17', '2020-07-20', '2020-07-21', '2020-07-22', '2020-07-23', '2020-07-24', '2020-07-27', '2020-07-28', '2020-07-29', '2020-07-30', '2020-07-31', '2020-08-03', '2020-08-04', '2020-08-05', '2020-08-06', '2020-08-07', '2020-08-10', '2020-08-11', '2020-08-12', '2020-08-13', '2020-08-14', '2020-08-18', '2020-08-19', '2020-08-20', '2020-08-21', '2020-08-24', '2020-08-25', '2020-08-26', '2020-08-27', '2020-08-28', '2020-08-31', '2020-09-01', '2020-09-02', '2020-09-03', '2020-09-04', '2020-09-07', '2020-09-08', '2020-09-09', '2020-09-10', '2020-09-11', '2020-09-14', '2020-09-15', '2020-09-16', '2020-09-17', '2020-09-18', '2020-09-21', '2020-09-22', '2020-09-23', '2020-09-24', '2020-09-25', '2020-09-28', '2020-09-29', '2020-10-05', '2020-10-06', '2020-10-07', '2020-10-08', '2020-10-12', '2020-10-13', '2020-10-14', '2020-10-15', '2020-10-16', '2020-10-19', '2020-10-20', '2020-10-21', '2020-10-22', '2020-10-23', '2020-10-26', '2020-10-27', '2020-10-28', '2020-10-29', '2020-10-30', '2020-11-02', '2020-11-03', '2020-11-04', '2020-11-05', '2020-11-06', '2020-11-09', '2020-11-10', '2020-11-11', '2020-11-12', '2020-11-13', '2020-11-16', '2020-11-17', '2020-11-18', '2020-11-19', '2020-11-20', '2020-11-23', '2020-11-24', '2020-11-25', '2020-11-26', '2020-11-27', '2020-11-30', '2020-12-01', '2020-12-02', '2020-12-03', '2020-12-04', '2020-12-07', '2020-12-08', '2020-12-09', '2020-12-10', '2020-12-11', '2020-12-14', '2020-12-15', '2020-12-16', '2020-12-17', '2020-12-18', '2020-12-21', '2020-12-22', '2020-12-23', '2020-12-24', '2020-12-28', '2020-12-29', '2020-12-30']
앞서 소개해드린 파이썬 패키지는 원하는 기간을 마음대로 추출할 수 있다는 장점이 있으나 약간의 시간이 소요된다는 단점이 있습니다.
단순히 한 해의 휴장일을 확인하고 싶으시다면, 한국거래소 > 시장동향 > 휴장일에서 데이터 다운로드 받으시면 됩니다.
반응형
'Programming > Python' 카테고리의 다른 글
[Python] PyDictionary 영어사전, 번역기 (2) | 2022.07.24 |
---|---|
파이썬 네이버 종목토론방 크롤링 (3) | 2022.06.04 |
파이썬 브라우저 구글 코랩 소개 및 사용법 (0) | 2022.03.18 |
파이썬 리스트 중복 제거 (방법 및 예시) (0) | 2021.11.29 |
파이썬 haversine 위경도 거리 계산하기 (0) | 2021.11.19 |
댓글