호출을 만들었으니 응답이 도착했을 때 슬롯 함수에서 데이터를 확인할 수 있도록 하겠습니다. TR 응답은 모두 하나의 함수( _on_receive_tr_data)에서 처리하므로 기존 함수에 약간의 코드를 추가해 보겠습니다.
def _on_receive_tr_data(self, screen_no, rqname, trcode, record_name, next, unused1, unused2, unused3, unused4):
print("[Kiwoom] _on_receive_tr_data is called {} / {} / {}".format(screen_no, rqname, trcode))
tr_data_cnt = self.dynamicCall("GetRepeatCnt(QString, QString)", trcode, rqname)
if next == '2':
self.has_next_tr_data = True
else:
self.has_next_tr_data = False
if rqname == "opt10081_req":
ohlcv = {'date': [], 'open': [], 'high': [], 'low': [], 'close': [], 'volume': []}
for i in range(tr_data_cnt):
date = self.dynamicCall("GetCommData(QString, QString, int, QString", trcode, rqname, i, "일자")
open = self.dynamicCall("GetCommData(QString, QString, int, QString", trcode, rqname, i, "시가")
high = self.dynamicCall("GetCommData(QString, QString, int, QString", trcode, rqname, i, "고가")
low = self.dynamicCall("GetCommData(QString, QString, int, QString", trcode, rqname, i, "저가")
close = self.dynamicCall("GetCommData(QString, QString, int, QString", trcode, rqname, i, "현재가")
volume = self.dynamicCall("GetCommData(QString, QString, int, QString", trcode, rqname, i, "거래량")
ohlcv['date'].append(date.strip())
ohlcv['open'].append(int(open))
ohlcv['high'].append(int(high))
ohlcv['low'].append(int(low))
ohlcv['close'].append(int(close))
ohlcv['volume'].append(int(volume))
self.tr_data = ohlcv
elif rqname == "opw00001_req":
deposit = self.dynamicCall("GetCommData(QString, QString, int, QString", trcode, rqname, 0, "주문가능금액")
self.tr_data = int(deposit)
print(self.tr_data)
self.tr_event_loop.exit()
time.sleep(0.5)