feat: 한국투자증권 KIS openapi 연동 추가
This commit is contained in:
parent
0872cb8f7b
commit
fee7c18e76
90
example/17-KIS-openapi.py
Normal file
90
example/17-KIS-openapi.py
Normal file
@ -0,0 +1,90 @@
|
||||
import os
|
||||
|
||||
import requests, json, keyring
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# 모의 계좌
|
||||
user_name = os.getenv('MOCK_USER')
|
||||
keyring.set_password('mock_app_key', user_name, os.getenv('MOCK_APP_KEY'))
|
||||
keyring.set_password('mock_app_secret', user_name, os.getenv('MOCK_APP_SECRET'))
|
||||
|
||||
# key
|
||||
app_key = keyring.get_password('mock_app_key', user_name)
|
||||
app_secret = keyring.get_password('mock_app_secret', user_name)
|
||||
|
||||
# base url
|
||||
url_base = "https://openapivts.koreainvestment.com:29443" # 모의투자
|
||||
|
||||
def get_token():
|
||||
path = "oauth2/tokenP"
|
||||
url = f"{url_base}/{path}"
|
||||
headers = {"content-type": "application/json"}
|
||||
body = {
|
||||
"grant_type": "client_credentials",
|
||||
"appkey": app_key,
|
||||
"appsecret": app_secret
|
||||
}
|
||||
res = requests.post(url, headers=headers, data=json.dumps(body))
|
||||
return res.json()['access_token']
|
||||
|
||||
|
||||
def get_hash_key(datas):
|
||||
path = "uapi/hashkey"
|
||||
url = f"{url_base}/{path}"
|
||||
headers = {
|
||||
'content-Type': 'application/json',
|
||||
'appkey': app_key,
|
||||
'appsecret': app_secret,
|
||||
}
|
||||
res = requests.post(url, headers=headers, data=json.dumps(datas))
|
||||
return res.json()["HASH"]
|
||||
|
||||
|
||||
def get_current_stock_price(access_token):
|
||||
path = "uapi/domestic-stock/v1/quotations/inquire-price"
|
||||
url = f"{url_base}/{path}"
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"authorization": f"Bearer {access_token}",
|
||||
"appKey": app_key,
|
||||
"appSecret": app_secret,
|
||||
"tr_id": "FHKST01010100"
|
||||
}
|
||||
|
||||
params = {"fid_cond_mrkt_div_code": "J", "fid_input_iscd": "005930"}
|
||||
|
||||
res = requests.get(url, headers=headers, params=params)
|
||||
return res.json()['output']['stck_prpr']
|
||||
|
||||
|
||||
token = get_token()
|
||||
print(get_current_stock_price(token))
|
||||
|
||||
|
||||
def order_stock(access_token):
|
||||
path = "/uapi/domestic-stock/v1/trading/order-cash"
|
||||
url = f"{url_base}/{path}"
|
||||
|
||||
data = {
|
||||
"CANO": "50068923", # 계좌번호 앞 8지리
|
||||
"ACNT_PRDT_CD": "01", # 계좌번호 뒤 2자리
|
||||
"PDNO": "005930", # 종목코드
|
||||
"ORD_DVSN": "01", # 주문 방법
|
||||
"ORD_QTY": "10", # 주문 수량
|
||||
"ORD_UNPR": "0", # 주문 단가 (시장가의 경우 0)
|
||||
}
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"authorization": f"Bearer {access_token}",
|
||||
"appkey": app_key,
|
||||
"appsecret": app_secret,
|
||||
"tr_id": "VTTC0802U",
|
||||
"custtype": "P",
|
||||
"hashkey": get_hash_key(data)
|
||||
}
|
||||
|
||||
res = requests.post(url, headers=headers, data=json.dumps(data))
|
||||
res.json()
|
||||
Loading…
x
Reference in New Issue
Block a user