OAuth 인증
- REST
접근토큰발급
기본 정보
| POST |
| https://api.kiwoom.com |
| https://mockapi.kiwoom.com(KRX만 지원가능) |
| /oauth2/token |
| JSON |
| application/json;charset=UTF-8 |
다른 TR 확인하기
요청
| grant_type | grant_type | String | Y | client_credentials 입력 | |
| appkey | 앱키 | String | Y | ||
| secretkey | 시크릿키 | String | Y |
응답
| expires_dt | 만료일 | String | Y | ||
| token_type | 토큰타입 | String | Y | ||
| token | 접근토큰 | String | Y |
---------------------------------------------------------------------------------
import requests
import json
# 주식기본정보요청
def fn_ka10001(token, data, cont_yn='N', next_key=''):
# 1. 요청할 API URL
#host = 'https://mockapi.kiwoom.com' # 모의투자
host = 'https://api.kiwoom.com' # 실전투자
endpoint = '/api/dostk/stkinfo'
url = host + endpoint
# 2. header 데이터
headers = {
'Content-Type': 'application/json;charset=UTF-8', # 컨텐츠타입
'authorization': f'Bearer {token}', # 접근토큰
'cont-yn': cont_yn, # 연속조회여부
'next-key': next_key, # 연속조회키
'api-id': 'ka10001', # TR명
}
# 3. http POST 요청
response = requests.post(url, headers=headers, json=data)
# 4. 응답 상태 코드와 데이터 출력
print('Code:', response.status_code)
print('Header:', json.dumps({key: response.headers.get(key) for key in ['next-key', 'cont-yn', 'api-id']}, indent=4, ensure_ascii=False))
print('Body:', json.dumps(response.json(), indent=4, ensure_ascii=False)) # JSON 응답을 파싱하여 출력
# 실행 구간
if __name__ == '__main__':
# 1. 토큰 설정
MY_ACCESS_TOKEN = '사용자 AccessToken'# 접근토큰
# 2. 요청 데이터
params = {
'stk_cd': '005930', # 종목코드 거래소별 종목코드 (KRX:039490,NXT:039490_NX,SOR:039490_AL)
}
# 3. API 실행
fn_ka10001(token=MY_ACCESS_TOKEN, data=params)
# next-key, cont-yn 값이 있을 경우
# fn_ka10001(token=MY_ACCESS_TOKEN, data=params, cont_yn='Y', next_key='nextkey..')
REQUEST
{ "stk_cd" : "005930" }
------------------------------------------
TR명
투자자별일별매매종목요청
요청
Header| authorization | 접근토큰 | String | Y | 1000 | 토큰 지정시 토큰타입("Bearer") 붙혀서 호출 예) Bearer Egicyx... |
| cont-yn | 연속조회여부 | String | N | 1 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 cont-yn값 세팅 |
| next-key | 연속조회키 | String | N | 50 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 next-key값 세팅 |
| api-id | TR명 | String | Y | 10 |
| strt_dt | 시작일자 | String | Y | 8 | YYYYMMDD |
| end_dt | 종료일자 | String | Y | 8 | YYYYMMDD |
| trde_tp | 매매구분 | String | Y | 1 | 순매도:1, 순매수:2 |
| mrkt_tp | 시장구분 | String | Y | 3 | 001:코스피, 101:코스닥 |
| invsr_tp | 투자자구분 | String | Y | 4 | 8000:개인, 9000:외국인, 1000:금융투자, 3000:투신, 5000:기타금융, 4000:은행, 2000:보험, 6000:연기금, 7000:국가, 7100:기타법인, 9999:기관계 |
| stex_tp | 거래소구분 | String | Y | 1 | 1:KRX, 2:NXT 3.통합 |
응답
Header| cont-yn | 연속조회여부 | String | N | 1 | 다음 데이터가 있을시 Y값 전달 |
| next-key | 연속조회키 | String | N | 50 | 다음 데이터가 있을시 다음 키값 전달 |
| api-id | TR명 | String | Y | 10 |
| invsr_daly_trde_stk | 투자자별일별매매종목 | LIST | N | ||
| - stk_cd | 종목코드 | String | N | 20 | |
| - stk_nm | 종목명 | String | N | 20 | |
| - netslmt_qty | 순매도수량 | String | N | 20 | |
| - netslmt_amt | 순매도금액 | String | N | 20 | |
| - prsm_avg_pric | 추정평균가 | String | N | 20 | |
| - cur_prc | 현재가 | String | N | 20 | |
| - pre_sig | 대비기호 | String | N | 20 | |
| - pred_pre | 전일대비 | String | N | 20 | |
| - avg_pric_pre | 평균가대비 | String | N | 20 | |
| - pre_rt | 대비율 | String | N | 20 | |
| - dt_trde_qty | 기간거래량 | String | N | 20 |
import requests
import json
# 투자자별일별매매종목요청
def fn_ka10058(token, data, cont_yn='N', next_key=''):
# 1. 요청할 API URL
#host = 'https://mockapi.kiwoom.com' # 모의투자
host = 'https://api.kiwoom.com' # 실전투자
endpoint = '/api/dostk/stkinfo'
url = host + endpoint
# 2. header 데이터
headers = {
'Content-Type': 'application/json;charset=UTF-8', # 컨텐츠타입
'authorization': f'Bearer {token}', # 접근토큰
'cont-yn': cont_yn, # 연속조회여부
'next-key': next_key, # 연속조회키
'api-id': 'ka10058', # TR명
}
# 3. http POST 요청
response = requests.post(url, headers=headers, json=data)
# 4. 응답 상태 코드와 데이터 출력
print('Code:', response.status_code)
print('Header:', json.dumps({key: response.headers.get(key) for key in ['next-key', 'cont-yn', 'api-id']}, indent=4, ensure_ascii=False))
print('Body:', json.dumps(response.json(), indent=4, ensure_ascii=False)) # JSON 응답을 파싱하여 출력
# 실행 구간
if __name__ == '__main__':
# 1. 토큰 설정
MY_ACCESS_TOKEN = '사용자 AccessToken'# 접근토큰
# 2. 요청 데이터
params = {
'strt_dt': '20241106', # 시작일자 YYYYMMDD
'end_dt': '20241107', # 종료일자 YYYYMMDD
'trde_tp': '2', # 매매구분 순매도:1, 순매수:2
'mrkt_tp': '101', # 시장구분 001:코스피, 101:코스닥
'invsr_tp': '8000', # 투자자구분 8000:개인, 9000:외국인, 1000:금융투자, 3000:투신, 5000:기타금융, 4000:은행, 2000:보험, 6000:연기금, 7000:국가, 7100:기타법인, 9999:기관계
'stex_tp': '3', # 거래소구분 1:KRX, 2:NXT 3.통합
}
# 3. API 실행
fn_ka10058(token=MY_ACCESS_TOKEN, data=params)
# next-key, cont-yn 값이 있을 경우
# fn_ka10058(token=MY_ACCESS_TOKEN, data=params, cont_yn='Y', next_key='nextkey..')
--------------------------------
요청
Header| authorization | 접근토큰 | String | Y | 1000 | 토큰 지정시 토큰타입("Bearer") 붙혀서 호출 예) Bearer Egicyx... |
| cont-yn | 연속조회여부 | String | N | 1 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 cont-yn값 세팅 |
| next-key | 연속조회키 | String | N | 50 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 next-key값 세팅 |
| api-id | TR명 | String | Y | 10 |
| dt | 일자 | String | Y | 8 | YYYYMMDD |
| stk_cd | 종목코드 | String | Y | 20 | 거래소별 종목코드 (KRX:039490,NXT:039490_NX,SOR:039490_AL) |
| amt_qty_tp | 금액수량구분 | String | Y | 1 | 1:금액, 2:수량 |
| trde_tp | 매매구분 | String | Y | 1 | 0:순매수, 1:매수, 2:매도 |
| unit_tp | 단위구분 | String | Y | 4 | 1000:천주, 1:단주 |
응답
Header| cont-yn | 연속조회여부 | String | N | 1 | 다음 데이터가 있을시 Y값 전달 |
| next-key | 연속조회키 | String | N | 50 | 다음 데이터가 있을시 다음 키값 전달 |
| api-id | TR명 | String | Y | 10 |
| stk_invsr_orgn | 종목별투자자기관별 | LIST | N | ||
| - dt | 일자 | String | N | 20 | |
| - cur_prc | 현재가 | String | N | 20 | |
| - pre_sig | 대비기호 | String | N | 20 | |
| - pred_pre | 전일대비 | String | N | 20 | |
| - flu_rt | 등락율 | String | N | 20 | 우측 2자리 소수점자리수 |
| - acc_trde_qty | 누적거래량 | String | N | 20 | |
| - acc_trde_prica | 누적거래대금 | String | N | 20 | |
| - ind_invsr | 개인투자자 | String | N | 20 | |
| - frgnr_invsr | 외국인투자자 | String | N | 20 | |
| - orgn | 기관계 | String | N | 20 | |
| - fnnc_invt | 금융투자 | String | N | 20 | |
| - insrnc | 보험 | String | N | 20 | |
| - invtrt | 투신 | String | N | 20 | |
| - etc_fnnc | 기타금융 | String | N | 20 | |
| - bank | 은행 | String | N | 20 | |
| - penfnd_etc | 연기금등 | String | N | 20 | |
| - samo_fund | 사모펀드 | String | N | 20 | |
| - natn | 국가 | String | N | 20 | |
| - etc_corp | 기타법인 | String | N | 20 | |
| - natfor | 내외국인 | String | N | 20 |
import requests
import json
# 종목별투자자기관별요청
def fn_ka10059(token, data, cont_yn='N', next_key=''):
# 1. 요청할 API URL
#host = 'https://mockapi.kiwoom.com' # 모의투자
host = 'https://api.kiwoom.com' # 실전투자
endpoint = '/api/dostk/stkinfo'
url = host + endpoint
# 2. header 데이터
headers = {
'Content-Type': 'application/json;charset=UTF-8', # 컨텐츠타입
'authorization': f'Bearer {token}', # 접근토큰
'cont-yn': cont_yn, # 연속조회여부
'next-key': next_key, # 연속조회키
'api-id': 'ka10059', # TR명
}
# 3. http POST 요청
response = requests.post(url, headers=headers, json=data)
# 4. 응답 상태 코드와 데이터 출력
print('Code:', response.status_code)
print('Header:', json.dumps({key: response.headers.get(key) for key in ['next-key', 'cont-yn', 'api-id']}, indent=4, ensure_ascii=False))
print('Body:', json.dumps(response.json(), indent=4, ensure_ascii=False)) # JSON 응답을 파싱하여 출력
# 실행 구간
if __name__ == '__main__':
# 1. 토큰 설정
MY_ACCESS_TOKEN = '사용자 AccessToken'# 접근토큰
# 2. 요청 데이터
params = {
'dt': '20241107', # 일자 YYYYMMDD
'stk_cd': '005930', # 종목코드 거래소별 종목코드 (KRX:039490,NXT:039490_NX,SOR:039490_AL)
'amt_qty_tp': '1', # 금액수량구분 1:금액, 2:수량
'trde_tp': '0', # 매매구분 0:순매수, 1:매수, 2:매도
'unit_tp': '1000', # 단위구분 1000:천주, 1:단주
}
# 3. API 실행
fn_ka10059(token=MY_ACCESS_TOKEN, data=params)
# next-key, cont-yn 값이 있을 경우
# fn_ka10059(token=MY_ACCESS_TOKEN, data=params, cont_yn='Y', next_key='nextkey..')
---------------------------
요청
Header| authorization | 접근토큰 | String | Y | 1000 | 토큰 지정시 토큰타입("Bearer") 붙혀서 호출 예) Bearer Egicyx... |
| cont-yn | 연속조회여부 | String | N | 1 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 cont-yn값 세팅 |
| next-key | 연속조회키 | String | N | 50 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 next-key값 세팅 |
| api-id | TR명 | String | Y | 10 |
| stk_cd | 종목코드 | String | Y | 20 | 거래소별 종목코드 (KRX:039490,NXT:039490_NX,SOR:039490_AL) |
| strt_dt | 시작일자 | String | Y | 8 | YYYYMMDD |
| end_dt | 종료일자 | String | Y | 8 | YYYYMMDD |
| amt_qty_tp | 금액수량구분 | String | Y | 1 | 1:금액, 2:수량 |
| trde_tp | 매매구분 | String | Y | 1 | 0:순매수, 1:매수, 2:매도 |
| unit_tp | 단위구분 | String | Y | 4 | 1000:천주, 1:단주 |
응답
Header| cont-yn | 연속조회여부 | String | N | 1 | 다음 데이터가 있을시 Y값 전달 |
| next-key | 연속조회키 | String | N | 50 | 다음 데이터가 있을시 다음 키값 전달 |
| api-id | TR명 | String | Y | 10 |
| stk_invsr_orgn_tot | 종목별투자자기관별합계 | LIST | N | ||
| - ind_invsr | 개인투자자 | String | N | 20 | |
| - frgnr_invsr | 외국인투자자 | String | N | 20 | |
| - orgn | 기관계 | String | N | 20 | |
| - fnnc_invt | 금융투자 | String | N | 20 | |
| - insrnc | 보험 | String | N | 20 | |
| - invtrt | 투신 | String | N | 20 | |
| - etc_fnnc | 기타금융 | String | N | 20 | |
| - bank | 은행 | String | N | 20 | |
| - penfnd_etc | 연기금등 | String | N | 20 | |
| - samo_fund | 사모펀드 | String | N | 20 | |
| - natn | 국가 | String | N | 20 | |
| - etc_corp | 기타법인 | String | N | 20 | |
| - natfor | 내외국인 | String | N | 20 |
import requests
import json
# 종목별투자자기관별합계요청
def fn_ka10061(token, data, cont_yn='N', next_key=''):
# 1. 요청할 API URL
#host = 'https://mockapi.kiwoom.com' # 모의투자
host = 'https://api.kiwoom.com' # 실전투자
endpoint = '/api/dostk/stkinfo'
url = host + endpoint
# 2. header 데이터
headers = {
'Content-Type': 'application/json;charset=UTF-8', # 컨텐츠타입
'authorization': f'Bearer {token}', # 접근토큰
'cont-yn': cont_yn, # 연속조회여부
'next-key': next_key, # 연속조회키
'api-id': 'ka10061', # TR명
}
# 3. http POST 요청
response = requests.post(url, headers=headers, json=data)
# 4. 응답 상태 코드와 데이터 출력
print('Code:', response.status_code)
print('Header:', json.dumps({key: response.headers.get(key) for key in ['next-key', 'cont-yn', 'api-id']}, indent=4, ensure_ascii=False))
print('Body:', json.dumps(response.json(), indent=4, ensure_ascii=False)) # JSON 응답을 파싱하여 출력
# 실행 구간
if __name__ == '__main__':
# 1. 토큰 설정
MY_ACCESS_TOKEN = '사용자 AccessToken'# 접근토큰
# 2. 요청 데이터
params = {
'stk_cd': '005930', # 종목코드 거래소별 종목코드 (KRX:039490,NXT:039490_NX,SOR:039490_AL)
'strt_dt': '20241007', # 시작일자 YYYYMMDD
'end_dt': '20241107', # 종료일자 YYYYMMDD
'amt_qty_tp': '1', # 금액수량구분 1:금액, 2:수량
'trde_tp': '0', # 매매구분 0:순매수, 1:매수, 2:매도
'unit_tp': '1000', # 단위구분 1000:천주, 1:단주
}
# 3. API 실행
fn_ka10061(token=MY_ACCESS_TOKEN, data=params)
# next-key, cont-yn 값이 있을 경우
# fn_ka10061(token=MY_ACCESS_TOKEN, data=params, cont_yn='Y', next_key='nextkey..')
-----------------------------
요청
Header| authorization | 접근토큰 | String | Y | 1000 | 토큰 지정시 토큰타입("Bearer") 붙혀서 호출 예) Bearer Egicyx... |
| cont-yn | 연속조회여부 | String | N | 1 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 cont-yn값 세팅 |
| next-key | 연속조회키 | String | N | 50 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 next-key값 세팅 |
| api-id | TR명 | String | Y | 10 |
| mrkt_tp | 시장구분 | String | Y | 2 | 0:코스피,10:코스닥,3:ELW,8:ETF,30:K-OTC,50:코넥스,5:신주인수권,4:뮤추얼펀드,6:리츠,9:하이일드 |
응답
Header| cont-yn | 연속조회여부 | String | N | 1 | 다음 데이터가 있을시 Y값 전달 |
| next-key | 연속조회키 | String | N | 50 | 다음 데이터가 있을시 다음 키값 전달 |
| api-id | TR명 | String | Y | 10 |
| list | 종목리스트 | LIST | N | ||
| - code | 종목코드 | String | N | 20 | 단축코드 |
| - name | 종목명 | String | N | 20 | |
| - listCount | 상장주식수 | String | N | 20 | |
| - auditInfo | 감리구분 | String | N | 20 | |
| - regDay | 상장일 | String | N | 20 | |
| - lastPrice | 전일종가 | String | N | 20 | |
| - state | 종목상태 | String | N | 20 | |
| - marketCode | 시장구분코드 | String | N | 20 | |
| - marketName | 시장명 | String | N | 20 | |
| - upName | 업종명 | String | N | 20 | |
| - upSizeName | 회사크기분류 | String | N | 20 | |
| - companyClassName | 회사분류 | String | N | 20 | 코스닥만 존재함 |
| - orderWarning | 투자유의종목여부 | String | N | 20 | 0: 해당없음, 2: 정리매매, 3: 단기과열, 4: 투자위험, 5: 투자경과, 1: ETF투자주의요망(ETF인 경우만 전달 |
| - nxtEnable | NXT가능여부 | String | N | 20 | Y: 가능 |
import requests
import json
# 종목정보 리스트
def fn_ka10099(token, data, cont_yn='N', next_key=''):
# 1. 요청할 API URL
#host = 'https://mockapi.kiwoom.com' # 모의투자
host = 'https://api.kiwoom.com' # 실전투자
endpoint = '/api/dostk/stkinfo'
url = host + endpoint
# 2. header 데이터
headers = {
'Content-Type': 'application/json;charset=UTF-8', # 컨텐츠타입
'authorization': f'Bearer {token}', # 접근토큰
'cont-yn': cont_yn, # 연속조회여부
'next-key': next_key, # 연속조회키
'api-id': 'ka10099', # TR명
}
# 3. http POST 요청
response = requests.post(url, headers=headers, json=data)
# 4. 응답 상태 코드와 데이터 출력
print('Code:', response.status_code)
print('Header:', json.dumps({key: response.headers.get(key) for key in ['next-key', 'cont-yn', 'api-id']}, indent=4, ensure_ascii=False))
print('Body:', json.dumps(response.json(), indent=4, ensure_ascii=False)) # JSON 응답을 파싱하여 출력
# 실행 구간
if __name__ == '__main__':
# 1. 토큰 설정
MY_ACCESS_TOKEN = '사용자 AccessToken'# 접근토큰
# 2. 요청 데이터
params = {
'mrkt_tp': '0', # 시장구분 0:코스피,10:코스닥,3:ELW,8:ETF,30:K-OTC,50:코넥스,5:신주인수권,4:뮤추얼펀드,6:리츠,9:하이일드
}
# 3. API 실행
fn_ka10099(token=MY_ACCESS_TOKEN, data=params)
# next-key, cont-yn 값이 있을 경우
# fn_ka10099(token=MY_ACCESS_TOKEN, data=params, cont_yn='Y', next_key='nextkey..')
----------------------------------------
import requests
import json
# 종목정보 조회
def fn_ka10100(token, data, cont_yn='N', next_key=''):
# 1. 요청할 API URL
#host = 'https://mockapi.kiwoom.com' # 모의투자
host = 'https://api.kiwoom.com' # 실전투자
endpoint = '/api/dostk/stkinfo'
url = host + endpoint
# 2. header 데이터
headers = {
'Content-Type': 'application/json;charset=UTF-8', # 컨텐츠타입
'authorization': f'Bearer {token}', # 접근토큰
'cont-yn': cont_yn, # 연속조회여부
'next-key': next_key, # 연속조회키
'api-id': 'ka10100', # TR명
}
# 3. http POST 요청
response = requests.post(url, headers=headers, json=data)
# 4. 응답 상태 코드와 데이터 출력
print('Code:', response.status_code)
print('Header:', json.dumps({key: response.headers.get(key) for key in ['next-key', 'cont-yn', 'api-id']}, indent=4, ensure_ascii=False))
print('Body:', json.dumps(response.json(), indent=4, ensure_ascii=False)) # JSON 응답을 파싱하여 출력
# 실행 구간
if __name__ == '__main__':
# 1. 토큰 설정
MY_ACCESS_TOKEN = '사용자 AccessToken'# 접근토큰
# 2. 요청 데이터
params = {
'stk_cd': '005930', # 종목코드
}
# 3. API 실행
fn_ka10100(token=MY_ACCESS_TOKEN, data=params)
# next-key, cont-yn 값이 있을 경우
# fn_ka10100(token=MY_ACCESS_TOKEN, data=params, cont_yn='Y', next_key='nextkey..')
--------------------------------
import requests
import json
# 업종코드 리스트
def fn_ka10101(token, data, cont_yn='N', next_key=''):
# 1. 요청할 API URL
#host = 'https://mockapi.kiwoom.com' # 모의투자
host = 'https://api.kiwoom.com' # 실전투자
endpoint = '/api/dostk/stkinfo'
url = host + endpoint
# 2. header 데이터
headers = {
'Content-Type': 'application/json;charset=UTF-8', # 컨텐츠타입
'authorization': f'Bearer {token}', # 접근토큰
'cont-yn': cont_yn, # 연속조회여부
'next-key': next_key, # 연속조회키
'api-id': 'ka10101', # TR명
}
# 3. http POST 요청
response = requests.post(url, headers=headers, json=data)
# 4. 응답 상태 코드와 데이터 출력
print('Code:', response.status_code)
print('Header:', json.dumps({key: response.headers.get(key) for key in ['next-key', 'cont-yn', 'api-id']}, indent=4, ensure_ascii=False))
print('Body:', json.dumps(response.json(), indent=4, ensure_ascii=False)) # JSON 응답을 파싱하여 출력
# 실행 구간
if __name__ == '__main__':
# 1. 토큰 설정
MY_ACCESS_TOKEN = '사용자 AccessToken'# 접근토큰
# 2. 요청 데이터
params = {
'mrkt_tp': '0', # 시장구분 0:코스피(거래소),1:코스닥,2:KOSPI200,4:KOSPI100,7:KRX100(통합지수)
}
# 3. API 실행
fn_ka10101(token=MY_ACCESS_TOKEN, data=params)
# next-key, cont-yn 값이 있을 경우
# fn_ka10101(token=MY_ACCESS_TOKEN, data=params, cont_yn='Y', next_key='nextkey..')
--------------------------------
요청
Header| authorization | 접근토큰 | String | Y | 1000 | 토큰 지정시 토큰타입("Bearer") 붙혀서 호출 예) Bearer Egicyx... |
| cont-yn | 연속조회여부 | String | N | 1 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 cont-yn값 세팅 |
| next-key | 연속조회키 | String | N | 50 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 next-key값 세팅 |
| api-id | TR명 | String | Y | 10 |
| trde_upper_tp | 매매상위구분 | String | Y | 1 | 1:순매도상위, 2:순매수상위 |
| amt_qty_tp | 금액수량구분 | String | Y | 2 | 1:금액, 2:수량 |
| mrkt_tp | 시장구분 | String | Y | 10 | P00101:코스피, P10102:코스닥 |
| stex_tp | 거래소구분 | String | Y | 1 | 1:KRX, 2:NXT 3.통합 |
응답
Header| cont-yn | 연속조회여부 | String | N | 1 | 다음 데이터가 있을시 Y값 전달 |
| next-key | 연속조회키 | String | N | 50 | 다음 데이터가 있을시 다음 키값 전달 |
| api-id | TR명 | String | Y | 10 |
| prm_netprps_upper_50 | 프로그램순매수상위50 | LIST | N | ||
| - rank | 순위 | String | N | 20 | |
| - stk_cd | 종목코드 | String | N | 20 | |
| - stk_nm | 종목명 | String | N | 20 | |
| - cur_prc | 현재가 | String | N | 20 | |
| - flu_sig | 등락기호 | String | N | 20 | |
| - pred_pre | 전일대비 | String | N | 20 | |
| - flu_rt | 등락율 | String | N | 20 | |
| - acc_trde_qty | 누적거래량 | String | N | 20 | |
| - prm_sell_amt | 프로그램매도금액 | String | N | 20 | |
| - prm_buy_amt | 프로그램매수금액 | String | N | 20 | |
| - prm_netprps_amt | 프로그램순매수금액 | String | N | 20 |
import requests
import json
# 프로그램순매수상위50요청
def fn_ka90003(token, data, cont_yn='N', next_key=''):
# 1. 요청할 API URL
#host = 'https://mockapi.kiwoom.com' # 모의투자
host = 'https://api.kiwoom.com' # 실전투자
endpoint = '/api/dostk/stkinfo'
url = host + endpoint
# 2. header 데이터
headers = {
'Content-Type': 'application/json;charset=UTF-8', # 컨텐츠타입
'authorization': f'Bearer {token}', # 접근토큰
'cont-yn': cont_yn, # 연속조회여부
'next-key': next_key, # 연속조회키
'api-id': 'ka90003', # TR명
}
# 3. http POST 요청
response = requests.post(url, headers=headers, json=data)
# 4. 응답 상태 코드와 데이터 출력
print('Code:', response.status_code)
print('Header:', json.dumps({key: response.headers.get(key) for key in ['next-key', 'cont-yn', 'api-id']}, indent=4, ensure_ascii=False))
print('Body:', json.dumps(response.json(), indent=4, ensure_ascii=False)) # JSON 응답을 파싱하여 출력
# 실행 구간
if __name__ == '__main__':
# 1. 토큰 설정
MY_ACCESS_TOKEN = '사용자 AccessToken'# 접근토큰
# 2. 요청 데이터
params = {
'trde_upper_tp': '1', # 매매상위구분 1:순매도상위, 2:순매수상위
'amt_qty_tp': '1', # 금액수량구분 1:금액, 2:수량
'mrkt_tp': 'P00101', # 시장구분 P00101:코스피, P10102:코스닥
'stex_tp': '1', # 거래소구분 1:KRX, 2:NXT 3.통합
}
# 3. API 실행
fn_ka90003(token=MY_ACCESS_TOKEN, data=params)
# next-key, cont-yn 값이 있을 경우
# fn_ka90003(token=MY_ACCESS_TOKEN, data=params, cont_yn='Y', next_key='nextkey..')
--------------------------
요청
| authorization | 접근토큰 | String | Y | 1000 | 토큰 지정시 토큰타입("Bearer") 붙혀서 호출 예) Bearer Egicyx... |
| cont-yn | 연속조회여부 | String | N | 1 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 cont-yn값 세팅 |
| next-key | 연속조회키 | String | N | 50 | 응답 Header의 연속조회여부값이 Y일 경우 다음데이터 요청시 응답 Header의 next-key값 세팅 |
| api-id | TR명 | String | Y | 10 |
| dt | 일자 | String | Y | 8 | YYYYMMDD |
| mrkt_tp | 시장구분 | String | Y | 10 | P00101:코스피, P10102:코스닥 |
| stex_tp | 거래소구분 | String | Y | 1 | 1:KRX, 2:NXT 3.통합 |
응답
Header| cont-yn | 연속조회여부 | String | N | 1 | 다음 데이터가 있을시 Y값 전달 |
| next-key | 연속조회키 | String | N | 50 | 다음 데이터가 있을시 다음 키값 전달 |
| api-id | TR명 | String | Y | 10 |
| tot_1 | 매수체결수량합계 | String | N | 20 | |
| tot_2 | 매수체결금액합계 | String | N | 20 | |
| tot_3 | 매도체결수량합계 | String | N | 20 | |
| tot_4 | 매도체결금액합계 | String | N | 20 | |
| tot_5 | 순매수대금합계 | String | N | 20 | |
| tot_6 | 합계6 | String | N | 20 | |
| stk_prm_trde_prst | 종목별프로그램매매현황 | LIST | N | ||
| - stk_cd | 종목코드 | String | N | 20 | |
| - stk_nm | 종목명 | String | N | 20 | |
| - cur_prc | 현재가 | String | N | 20 | |
| - flu_sig | 등락기호 | String | N | 20 | |
| - pred_pre | 전일대비 | String | N | 20 | |
| - buy_cntr_qty | 매수체결수량 | String | N | 20 | |
| - buy_cntr_amt | 매수체결금액 | String | N | 20 | |
| - sel_cntr_qty | 매도체결수량 | String | N | 20 | |
| - sel_cntr_amt | 매도체결금액 | String | N | 20 | |
| - netprps_prica | 순매수대금 | String | N | 20 | |
| - all_trde_rt | 전체거래비율 | String | N | 20 |
import requests
import json
# 종목별프로그램매매현황요청
def fn_ka90004(token, data, cont_yn='N', next_key=''):
# 1. 요청할 API URL
#host = 'https://mockapi.kiwoom.com' # 모의투자
host = 'https://api.kiwoom.com' # 실전투자
endpoint = '/api/dostk/stkinfo'
url = host + endpoint
# 2. header 데이터
headers = {
'Content-Type': 'application/json;charset=UTF-8', # 컨텐츠타입
'authorization': f'Bearer {token}', # 접근토큰
'cont-yn': cont_yn, # 연속조회여부
'next-key': next_key, # 연속조회키
'api-id': 'ka90004', # TR명
}
# 3. http POST 요청
response = requests.post(url, headers=headers, json=data)
# 4. 응답 상태 코드와 데이터 출력
print('Code:', response.status_code)
print('Header:', json.dumps({key: response.headers.get(key) for key in ['next-key', 'cont-yn', 'api-id']}, indent=4, ensure_ascii=False))
print('Body:', json.dumps(response.json(), indent=4, ensure_ascii=False)) # JSON 응답을 파싱하여 출력
# 실행 구간
if __name__ == '__main__':
# 1. 토큰 설정
MY_ACCESS_TOKEN = '사용자 AccessToken'# 접근토큰
# 2. 요청 데이터
params = {
'dt': '20241125', # 일자 YYYYMMDD
'mrkt_tp': 'P00101', # 시장구분 P00101:코스피, P10102:코스닥
'stex_tp': '1', # 거래소구분 1:KRX, 2:NXT 3.통합
}
# 3. API 실행
fn_ka90004(token=MY_ACCESS_TOKEN, data=params)
# next-key, cont-yn 값이 있을 경우
# fn_ka90004(token=MY_ACCESS_TOKEN, data=params, cont_yn='Y', next_key='nextkey..')