지금까지 코드를 정리해 보겠습니다.
Kiwoom.py
from PyQt5.QAxContainer import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Kiwoom(QAxWidget):
def __init__(self):
super().__init__()
self._make_kiwoom_instance()
self._set_signal_slots()
self._comm_connect()
self.account_number = self.get_account_number()
def _make_kiwoom_instance(self):
self.setControl("KHOPENAPI.KHOpenAPICtrl.1")
def _set_signal_slots(self):
self.OnEventConnect.connect(self._login_slot)
def _login_slot(self, err_code):
if err_code == 0:
print("connected")
else:
print("not connected")
self.login_event_loop.exit()
def _comm_connect(self):
self.dynamicCall("CommConnect()")
self.login_event_loop = QEventLoop()
self.login_event_loop.exec_()
def get_account_number(self, tag="ACCNO"):
account_list = self.dynamicCall("GetLoginInfo(QString)", tag)
account_number = account_list.split(';')[0]
print(account_number)
return account_number
def get_code_list_by_market(self, market_type): ------ 시장 내 종목 코드를 얻어 오는 함수
code_list = self.dynamicCall("GetCodeListByMarket(QString)", market_type)
code_list = code_list.split(';')[:-1]
return code_list
def get_master_code_name(self, code): ------ 종목 코드를 받아 종목명을 반환하는 함수
code_name = self.dynamicCall("GetMasterCodeName(QString)", code)
return code_name