Skip to content

Commit

Permalink
index 등락률 API 추가
Browse files Browse the repository at this point in the history
get_index_price_change_by_name 함수 구현
입력된 날짜동안 등락률/거래량/거래대금 반환
  • Loading branch information
mr-yoo committed May 27, 2020
1 parent 3d928a3 commit 0b4accd
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
10 changes: 9 additions & 1 deletion pykrx/stock/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ def get_index_status_by_group(date, market):
return krx.get_index_status_by_group(date, market)


def get_index_price_change_by_name(fromdate, todate, market="KOSPI"):
if isinstance(fromdate, datetime.datetime):
fromdate = _datetime2string(fromdate)
if isinstance(todate, datetime.datetime):
todate = _datetime2string(todate)
return krx.get_index_price_change_by_name(fromdate, todate, market)

# -----------------------------------------------------------------------------
# 공매도(SHORTING) API
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -273,6 +280,7 @@ def get_etf_tracking_error(fromdate, todate, ticker):
# df = get_index_ohlcv_by_date("20190101", "20190228", "코스피")
# df = get_index_ohlcv_by_date("20190101", "20190228", "코스닥")
# df = get_index_ohlcv_by_date("20000101", "20180630", "코스피 200", "m")
df = get_index_price_change_by_name("20200520", "20200527", "KOSDAQ")
# df = get_index_portfolio_deposit_file("20190412", "코스피 소형주")
# df = krx.IndexTicker().get_id("코스피 200", "20000201")
# df = get_index_portfolio_deposit_file("20000201", "코스피 소형주")
Expand All @@ -281,7 +289,7 @@ def get_etf_tracking_error(fromdate, todate, ticker):
# df = get_shorting_volume_top50("20190401", "KOSPI")
# df = get_shorting_balance_by_ticker("20190401", "20190405", "005930")
# df = get_shorting_balance_top50("20190401", "KOSDAQ")
df = get_etf_ticker_list()
# df = get_etf_ticker_list()
# df = get_etf_isin("346000")
# df = get_etf_ohlcv_by_date("20200101", "20200401", "295820")
# df = get_etf_portfolio_deposit_file("252650", "20190329")
Expand Down
27 changes: 25 additions & 2 deletions pykrx/website/krx/market/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,29 @@ def read(self, date, index, market):
return DataFrame(result['output'])


class MKD80002(KrxWebIo):
@property
def bld(self):
return "MKD/13/1301/13010101/mkd13010101"

def read(self, fromdate, todate, market):
"""전체지수 등락률
:param fromdate : 조회 시작 일자 (YYMMDD)
:param todate : 조회 마지막 일자 (YYMMDD)
:param market: KRX (2) / KOSPI (3) / KOSDAQ (4)
:return : PDF DataFrame
group_code group_name kor_indx_ind_nm indx prv_dd_indx prv_dd_cmpr fluc_tp prv_dd_cmpr_chart updn_rate updn_flag tr_vl tr_amt
0 3 KRX KRX 300 1,207.80 1,236.27 28.47 1 28.47 2.36 3 1,439,933,029 55,545,303,395,341
1 3 KRX KRX 100 4,234.80 4,335.24 100.44 1 100.44 2.37 3 500,776,865 32,360,209,330,383
2 3 KRX KTOP 30 6,740.79 6,965.40 224.61 1 224.61 3.33 3 267,094,630 22,349,375,479,336
3 3 KRX KRX 자동차 1,128.89 1,156.61 27.72 1 27.72 2.46 3 68,136,810 2,085,570,910,980
4 3 KRX KRX 반도체 2,510.86 2,602.63 91.77 1 91.77 3.65 3 211,516,546 4,892,632,198,230
"""

result = self.post(marketTp=market, period_strt_dd=fromdate, period_end_dd=todate)
return DataFrame(result['block1'])


################################################################################
# Sorting
class ShortHttp(KrxWebIo):
Expand Down Expand Up @@ -387,12 +410,12 @@ def read(date, market=1):
# df = MKD80037().read("ALL", "20180501", "20180515")

# index
df = MKD20011_PDF().read("20190412", "001", 2)
# df = MKD20011_PDF().read("20190412", "001", 2)
# df = MKD20011().read("20190413", "03")
# df = MKD20011_SUB().read("20190408", "20190412", "001", 1)
# df = MKD20011_SUB().read("20190408", "20190412", "001", 2)
# print(MKD30009_1().read('20190322', '20190329', 'ALL', 'KR7005930003'))

df = MKD80002().read("20200520", "20200527", 2)
# shorting
# print(SRT02010100.read("KR7005930003", "20181205", "20181207"))
# print(SRT02020100.read("20190402", "20190402", market=1))
Expand Down
38 changes: 31 additions & 7 deletions pykrx/website/krx/market/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pykrx.website.krx.market.ticker import get_stock_ticker_isin
from pykrx.website.krx.market.core import (MKD30040, MKD80037, MKD30009_0,
MKD30009_1, MKD20011, MKD20011_SUB,
MKD20011_PDF, SRT02010100,
MKD20011_PDF, SRT02010100, MKD80002,
SRT02020100, SRT02020300,
SRT02020400, SRT02030100, SRT02030400
)
Expand Down Expand Up @@ -194,18 +194,41 @@ def get_index_status_by_group(date, market):
"""
market = {"KOSPI": "02", "KOSDAQ": "03"}.get(market, "02")
df = MKD20011().read(date, market)

df = df[['idx_nm', 'annc_tm', 'bas_tm', 'bas_idx', 'prsnt_prc',
'idx_mktcap']]
df.columns = ['지수명', '기준시점', '발표시점', '기준지수', '현재지수',
'시가총액']
df = df[['idx_nm', 'annc_tm', 'bas_tm', 'bas_idx', 'prsnt_prc', 'idx_mktcap']]
df.columns = ['지수명', '기준시점', '발표시점', '기준지수', '현재지수', '시가총액']
df = df.set_index('지수명')
df = df.replace(',', '', regex=True)
df = df.replace('', 0)
df = df.astype({"기준지수": float, "현재지수": float, "시가총액": int}, )
return df


@dataframe_empty_handler
def get_index_price_change_by_name(fromdate, todate, market):
"""전체지수 등락률
:param fromdate: 조회 시작 일자 (YYYYMMDD)
:param todate : 조회 종료 일자 (YYYYMMDD)
:param market : KRX / KOSPI / KOSDQA
:return : 전체지수 등락률 DataFrame
기준시점 발표시점 기준지수 현재지수 시가총액
코스피 1983.01.04 1980.01.04 100.0 2486.35 1617634318
코스피 벤치마크 2015.09.14 2010.01.04 1696.0 2506.92 1554948117
코스피 비중제한 8% 지수 2017.12.18 2015.01.02 1000.0 1272.93 1559869409
코스피 200 1994.06.15 1990.01.03 100.0 327.13 1407647304
코스피 100 2000.03.02 2000.01.04 1000.0 2489.34 1277592989
코스피 50 2000.03.02 2000.01.04 1000.0 2205.53 1102490712
"""
market = {"KRX": 2, "KOSPI": 3, "KOSDAQ": 4}.get(market, 3)
df = MKD80002().read(fromdate, todate, market)
df = df[['kor_indx_ind_nm', 'indx', 'prv_dd_indx', 'updn_rate', 'tr_vl', 'tr_amt']]
df.columns = ['지수명', '시가', '종가', '등락률', '거래량', '거래대금']
df = df.set_index('지수명')
df = df.replace(',', '', regex=True)
df = df.replace('', 0)
df = df.astype({"시가": float, "종가": float, "등락률": float, "거래량": np.int64, "거래대금": np.int64})
return df


@dataframe_empty_handler
def get_index_portfolio_deposit_file(date, id, market):
market = {"KOSPI": 1, "KOSDAQ": 2}.get(market, 1)
Expand Down Expand Up @@ -416,7 +439,8 @@ def get_shorting_balance_top50(date, market="KOSPI"):
# df = get_index_portfolio_deposit_file("20190410", "001", "KOSDAQ")
# df = get_index_portfolio_deposit_file("20190410", "028", "KOSPI")
# df = get_index_status_by_group("20190410", "KOSPI")
# print(df)
df = get_index_price_change_by_name("20200520", "20200527", "KOSDAQ")
print(df)

# shoring
# df = get_shorting_status_by_date("20190401", "20190405", "KR7005930003")
Expand Down
15 changes: 15 additions & 0 deletions readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,21 @@ print(df.head())
코스피 50 2000.03.02 2000.01.04 1000.0 1884.17 960728527
코스피 200 중소형주 2015.07.13 2010.01.04 1000.0 1011.56 118938839
```
#### 2.1.2.4 인덱스 등락률 조회
get_index_price_change_by_name() 함수는 지수의 등락률/거래량/거래대금을 반환합니다.
```python
df = stock.get_index_price_change_by_name("20200520", "20200527", "KOSDAQ")
print(df.head())
```
```text
시가 종가 등락률 거래량 거래대금
코스닥지수 696.36 724.59 4.05 10488319776 62986196230829
코스닥 150 1065.42 1102.57 3.49 729479528 18619100922088
제조 2266.76 2371.51 4.62 4855249693 27936884984652
음식료·담배 9253.67 9477.11 2.41 156868081 1171238893745
섬유·의류 141.03 147.24 4.40 107124162 449845448978
```


### 2.1.3 공매도 API
KRX는 (T+2)일 이후의 데이터를 제공합니다. 최근 영업일이 20190405라면 20190403일을 포함한 이전 데이터를 얻을 수 있습니다.
Expand Down

0 comments on commit 0b4accd

Please sign in to comment.