-
Notifications
You must be signed in to change notification settings - Fork 0
/
kiwoom.py
540 lines (412 loc) · 21.3 KB
/
kiwoom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import sys
from PyQt5.QtWidgets import *
from PyQt5.QAxContainer import *
from PyQt5.QtCore import *
import time
import pandas as pd
import sqlite3
from pytrader import *
import datetime
TR_REQ_TIME_INTERVAL = 0.2
class Kiwoom(QAxWidget):
def __init__(self, ui):
super().__init__()
self._create_kiwoom_instance()
self._set_signal_slots()
self.price = 0
self.time = ""
self.data = 0
self.first_data = ""
self.ui = ui
self.account = ""
self.code = ""
self.state = "초기상태"
self.sell_time = ""
self.present_time = ""
self.trade_count = 0
self.ticker = ""
self.liquidation = ""
self.trade_dic = {}
#COM오브젝트 생성
def _create_kiwoom_instance(self):
self.setControl("KHOPENAPI.KHOpenAPICtrl.1") #고유 식별자 가져옴
#이벤트 처리
def _set_signal_slots(self):
self.OnEventConnect.connect(self._event_connect) # 로그인 관련 이벤트 (.connect()는 이벤트와 슬롯을 연결하는 역할)
self.OnReceiveTrData.connect(self._receive_tr_data) # 트랜잭션 요청 관련 이벤트
self.OnReceiveChejanData.connect(self._receive_chejan_data) #체결잔고 요청 이벤트
self.OnReceiveRealData.connect(self._handler_real_data) #실시간 데이터 처리
#로그인
def comm_connect(self):
self.dynamicCall("CommConnect()") # CommConnect() 시그널 함수 호출(.dynamicCall()는 서버에 데이터를 송수신해주는 기능)
self.login_event_loop = QEventLoop() # 로그인 담당 이벤트 루프(프로그램이 종료되지 않게하는 큰 틀의 루프)
self.login_event_loop.exec_() #exec_()를 통해 이벤트 루프 실행 (다른데이터 간섭 막기)
#이벤트 연결 여부
def _event_connect(self, err_code):
if err_code == 0:
print("connected")
else:
print("disconnected")
self.login_event_loop.exit()
#종목리스트 반환
def get_code_list_by_market(self, market):
code_list = self.dynamicCall("GetCodeListByMarket(QString)", market) #종목리스트 호출
code_list = code_list.split(';')
return code_list[:-1]
#종목명 반환
def get_master_code_name(self, code):
code_name = self.dynamicCall("GetMasterCodeName(QString)", code) #종목명 호출
return code_name
#통신접속상태 반환
def get_connect_state(self):
ret = self.dynamicCall("GetConnectState()") #통신접속상태 호출
return ret
#로그인정보 반환
def get_login_info(self, tag):
ret = self.dynamicCall("GetLoginInfo(QString)", tag) #로그인정보 호출
return ret
#TR별 할당값 지정하기
def set_input_value(self, id, value):
self.dynamicCall("SetInputValue(QString, QString)", id, value) #SetInputValue() 밸류값으로 원하는값지정 ex) SetInputValue("비밀번호" , "")
#통신데이터 수신(tr)
def comm_rq_data(self, rqname, trcode, next, screen_no):
self.dynamicCall("CommRqData(QString, QString, int, QString)", rqname, trcode, next, screen_no)
self.tr_event_loop = QEventLoop()
self.tr_event_loop.exec_()
#실제 데이터 가져오기
def _comm_get_data(self, code, real_type, field_name, index, item_name):
ret = self.dynamicCall("CommGetData(QString, QString, QString, int, QString)", code, #더이상 지원 안함??
real_type, field_name, index, item_name)
return ret.strip()
#수신받은 데이터 반복횟수
def _get_repeat_cnt(self, trcode, rqname):
ret = self.dynamicCall("GetRepeatCnt(QString, QString)", trcode, rqname)
return ret
#주문 (주식)
def send_order(self, rqname, screen_no, acc_no, order_type, code, quantity, price, hoga, order_no):
self.dynamicCall("SendOrder(QString, QString, QString, int, QString, int, int, QString, QString)",
[rqname, screen_no, acc_no, order_type, code, quantity, price, hoga, order_no])
#주문 (선물)
def send_order_fo(self, rqname, screen_no, acc_no, code, order_type, slbytp, hoga, quantity, price, order_no):
self.dynamicCall("SendOrderFO(QString, QString, QString, QString, int, QString, QString, int, QString, QString)",
[rqname, screen_no, acc_no, code, order_type, slbytp, hoga, quantity, price, order_no])
####
#실시간 조회관련 핸들
def _handler_real_data(self, trcode, ret):
#ui에서 계좌랑 종목코드 가져오기
self.account = self.ui.comboBox.currentText()
self.code = self.ui.lineEdit.text()
#체결시간
self.time = self.get_comm_real_data(trcode, 20)
#현재시간
now = datetime.datetime.now()
date = now.strftime("%Y-%m-%d ")
hour = now.strftime("%H")
self.present_time = now.strftime("%H : %M")
#티커와 강제청산 기준
self.ticker = float(self.ui.lineEdit_4.text())
self.liquidation = float(self.ui.lineEdit_5.text())
#강제청산할 시간 ui에서 가져오기
self.sell_time = int(self.ui.comboBox_7.currentText())
if self.time != "":
self.time = datetime.datetime.strptime(date + self.time, "%Y-%m-%d %H%M%S")
if self.first_data == "":
self.first_data = self.get_comm_real_data(trcode, 10)
self.first_data = float(self.first_data[1:])
print("기준가격:" , self.first_data, end=" ")
print("상태: ", self.state)
print("")
#기준 변경할 시간 입력받기
standard_time = float(self.ui.lineEdit_6.text())
if int(hour) < standard_time:
# 현재가
self.price = self.get_comm_real_data(trcode, 10)
self.price = self.price[1:]
print("-----------------------------")
print("|기준가격: " , self.first_data)
print("|상태: ", self.state)
print("|거래량: ", self.trade_count)
print("|티커: ", self.ticker*2)
print("|강천: ", self.liquidation)
print(self.trade_dic)
print("-----------------------------")
print("")
if self.price !="":
self.price = float(self.price)
print(self.time)
print("현재가: ", self.price)
print("")
self.strategy(self.price, self.ticker*2, self.liquidation)
self.ui.present_price()
elif self.sell_time == 0 or int(hour) < self.sell_time and int(hour) >= standard_time:
# 현재가
self.price = self.get_comm_real_data(trcode, 10)
self.price = self.price[1:]
print("-----------------------------")
print("|기준가격: " , self.first_data)
print("|상태: ", self.state)
print("|거래량: ", self.trade_count)
print("|티커: ", self.ticker)
print(self.trade_dic)
print("-----------------------------")
print("")
if self.price !="":
self.price = float(self.price)
print(self.time)
print("현재가: ", self.price)
print("")
self.strategy(self.price, self.ticker, self.liquidation)
self.ui.present_price()
else:
if self.state == "숏포지션":
#청산(매수)
self.send_order_fo("send_order_fo_req", "0101", self.account, self.code, 1, "2", "3", 1, "0", "")
print("청산_매수")
self.state = "초기상태"
elif self.state == "롱포지션":
#청산(매도)
self.send_order_fo("send_order_fo_req", "0101", self.account, self.code, 1, "1", "3", 1, "0", "")
print("청산_매도")
self.state = "초기상태"
#실시간 데이터 가져오기
def get_comm_real_data(self, trcode, fid):
ret = self.dynamicCall("GetCommRealData(QString, int)", trcode, fid)
return ret
#체결정보
def get_chejan_data(self, fid):
ret = self.dynamicCall("GetChejanData(int)", fid)
return ret
def get_server_gubun(self):
ret = self.dynamicCall("KOA_Functions(QString, QString)", "GetServerGubun", "")
return ret
def _receive_chejan_data(self, gubun, item_cnt, fid_list):
print(gubun)
print(self.get_chejan_data(9203))
print(self.get_chejan_data(302))
print(self.get_chejan_data(900))
print(self.get_chejan_data(901))
#받은 tr데이터가 무엇인지, 연속조회 할수있는지
def _receive_tr_data(self, screen_no, rqname, trcode, record_name, next, unused1, unused2, unused3, unused4):
if next == '2':
self.remained_data = True
else:
self.remained_data = False
#받은 tr에따라 각각의 함수 호출
if rqname == "opt10081_req":
self._opt10081(rqname, trcode)
elif rqname == "opw00001_req":
self._opw00001(rqname, trcode)
elif rqname == "opw00018_req":
self._opw00018(rqname, trcode)
elif rqname == "opw20006_req":
self._opw20006(rqname, trcode)
elif rqname == "opt50001_req":
self._opt50001(rqname, trcode)
try:
self.tr_event_loop.exit()
except AttributeError:
pass
@staticmethod
#입력받은데이터 정제
def change_format(data):
strip_data = data.lstrip('-0')
if strip_data == '' or strip_data == '.00':
strip_data = '0'
try:
format_data = format(int(strip_data), ',d')
except:
format_data = format(float(strip_data))
if data.startswith('-'):
format_data = '-' + format_data
return format_data
#입력받은데이터(수익률) 정제
@staticmethod
def change_format2(data):
strip_data = data.lstrip('-0')
if strip_data == '':
strip_data = '0'
if strip_data.startswith('.'):
strip_data = '0' + strip_data
if data.startswith('-'):
strip_data = '-' + strip_data
return strip_data
def _opw00001(self, rqname, trcode):
d2_deposit = self._comm_get_data(trcode, "", rqname, 0, "d+2추정예수금")
self.d2_deposit = Kiwoom.change_format(d2_deposit)
def _opt10081(self, rqname, trcode):
data_cnt = self._get_repeat_cnt(trcode, rqname) #데이터 갯수 확인
for i in range(data_cnt): #시고저종 거래량 가져오기
date = self._comm_get_data(trcode, "", rqname, i, "일자")
open = self._comm_get_data(trcode, "", rqname, i, "시가")
high = self._comm_get_data(trcode, "", rqname, i, "고가")
low = self._comm_get_data(trcode, "", rqname, i, "저가")
close = self._comm_get_data(trcode, "", rqname, i, "현재가")
volume = self._comm_get_data(trcode, "", rqname, i, "거래량")
self.ohlcv['date'].append(date)
self.ohlcv['open'].append(int(open))
self.ohlcv['high'].append(int(high))
self.ohlcv['low'].append(int(low))
self.ohlcv['close'].append(int(close))
self.ohlcv['volume'].append(int(volume))
#opw박스 초기화 (주식)
def reset_opw00018_output(self):
self.opw00018_output = {'single': [], 'multi': []}
#여러 정보들 저장 (주식)
def _opw00018(self, rqname, trcode):
# single data
total_purchase_price = self._comm_get_data(trcode, "", rqname, 0, "총매입금액")
total_eval_price = self._comm_get_data(trcode, "", rqname, 0, "총평가금액")
total_eval_profit_loss_price = self._comm_get_data(trcode, "", rqname, 0, "총평가손익금액")
total_earning_rate = self._comm_get_data(trcode, "", rqname, 0, "총수익률(%)")
estimated_deposit = self._comm_get_data(trcode, "", rqname, 0, "추정예탁자산")
self.opw00018_output['single'].append(Kiwoom.change_format(total_purchase_price))
self.opw00018_output['single'].append(Kiwoom.change_format(total_eval_price))
self.opw00018_output['single'].append(Kiwoom.change_format(total_eval_profit_loss_price))
total_earning_rate = Kiwoom.change_format(total_earning_rate)
if self.get_server_gubun():
total_earning_rate = float(total_earning_rate) / 100
total_earning_rate = str(total_earning_rate)
self.opw00018_output['single'].append(total_earning_rate)
self.opw00018_output['single'].append(Kiwoom.change_format(estimated_deposit))
# multi data
rows = self._get_repeat_cnt(trcode, rqname)
for i in range(rows):
name = self._comm_get_data(trcode, "", rqname, i, "종목명")
quantity = self._comm_get_data(trcode, "", rqname, i, "보유수량")
purchase_price = self._comm_get_data(trcode, "", rqname, i, "매입가")
current_price = self._comm_get_data(trcode, "", rqname, i, "현재가")
eval_profit_loss_price = self._comm_get_data(trcode, "", rqname, i, "평가손익")
earning_rate = self._comm_get_data(trcode, "", rqname, i, "수익률(%)")
quantity = Kiwoom.change_format(quantity)
purchase_price = Kiwoom.change_format(purchase_price)
current_price = Kiwoom.change_format(current_price)
eval_profit_loss_price = Kiwoom.change_format(eval_profit_loss_price)
earning_rate = Kiwoom.change_format2(earning_rate)
self.opw00018_output['multi'].append([name, quantity, purchase_price, current_price, eval_profit_loss_price,
earning_rate])
#opw박스 초기화(선물)
def reset_opw20006_output(self):
self.opw20006_output = {'single': [], 'multi': []}
#여러 정보들 저장 (선물)
def _opw20006(self, rqname, trcode):
# single data
total_purchase_price = self._comm_get_data(trcode, "", rqname, 0, "총매입금액")
total_eval_price = self._comm_get_data(trcode, "", rqname, 0, "총평가금액")
total_eval_profit_loss_price = self._comm_get_data(trcode, "", rqname, 0, "총평가손익금액")
total_earning_rate = self._comm_get_data(trcode, "", rqname, 0, "총수익률(%)")
estimated_deposit = self._comm_get_data(trcode, "", rqname, 0, "추정예탁자산")
self.opw20006_output['single'].append(Kiwoom.change_format(total_purchase_price))
self.opw20006_output['single'].append(Kiwoom.change_format(total_eval_price))
self.opw20006_output['single'].append(Kiwoom.change_format(total_eval_profit_loss_price))
total_earning_rate = Kiwoom.change_format(total_earning_rate)
if self.get_server_gubun():
total_earning_rate = float(total_earning_rate) / 100
total_earning_rate = str(total_earning_rate)
self.opw20006_output['single'].append(total_earning_rate)
self.opw20006_output['single'].append(Kiwoom.change_format(estimated_deposit))
# multi data
rows = self._get_repeat_cnt(trcode, rqname)
for i in range(rows):
name = self._comm_get_data(trcode, "", rqname, i, "종목명")
quantity = self._comm_get_data(trcode, "", rqname, i, "잔고수량")
purchase_price = self._comm_get_data(trcode, "", rqname, i, "매입단가")
current_price = self._comm_get_data(trcode, "", rqname, i, "현재가")
eval_profit_loss_price = self._comm_get_data(trcode, "", rqname, i, "평가손익")
earning_rate = self._comm_get_data(trcode, "", rqname, i, "손익율")
quantity = Kiwoom.change_format(quantity)
purchase_price = Kiwoom.change_format(purchase_price)
current_price = Kiwoom.change_format(current_price)
eval_profit_loss_price = Kiwoom.change_format(eval_profit_loss_price)
earning_rate = Kiwoom.change_format2(earning_rate)
self.opw20006_output['multi'].append([name, quantity, purchase_price, current_price, eval_profit_loss_price,
earning_rate])
###
def first_price(self):
self.set_input_value("종목코드", self.code)
self.comm_rq_data("opt50003_req", "opt50003", 0, "1000")
##
def _opt50001(self, rqname, trcode):
print("connect")
#전략
def strategy(self, present_price, ticker, liquidation):
data = present_price
#초기 상태
if self.state == "초기상태":
#매수
if data >= self.first_data + ticker:
self.send_order_fo("send_order_fo_req", "0101", self.account, self.code, 1, "2", "3", 1, "0", "")
print(type(data), type(self.first_data + ticker))
print("매수", data )
print("상태 : 롱포지션 진입")
print("")
self.first_data = data
self.state = "롱포지션"
self.trade_dic[self.present_time] = "롱진입"
#매도
elif data <= self.first_data - ticker:
self.send_order_fo("send_order_fo_req", "0101", self.account, self.code, 1, "1", "3", 1, "0", "")
print(type(data), type(self.first_data + ticker))
print("매도", data)
print("상태 : 숏포지션 진입")
print("")
self.first_data = data
self.state = "숏포지션"
self.trade_dic[self.present_time] = "숏진입"
#매수 포지션
elif self.state == "롱포지션":
#매도
if data <= self.first_data - liquidation:
self.send_order_fo("send_order_fo_req", "0101", self.account, self.code, 1, "1", "3", 1, "0", "")
print("매도", data)
print("상태 : 롱포지션 청산- /초기상태 진입")
print("")
self.first_data = data
self.trade_count += 1
self.state = "초기상태"
self.trade_dic[self.present_time] = "롱청산"
#윗단계로 기준 바꾸고 홀딩
elif data >= self.first_data + ticker:
self.first_data = data
print("한단계위 진입", data)
print("")
#매도 포지션
elif self.state == "숏포지션":
if data >= self.first_data + liquidation:
self.send_order_fo("send_order_fo_req", "0101", self.account, self.code, 1, "2", "3", 1, "0", "")
print("매수", data )
print("상태 : 숏포지션 청산- /초기상태 진입")
print("")
self.first_data = data
self.trade_count += 1
self.state = "초기상태"
self.trade_dic[self.present_time] = "숏청산"
#아랫단계로 기준 바꾸고 홀딩
elif data <= self.first_data - ticker:
self.first_data = data
print("한단계 아랫단계 진입", date)
print("")
if __name__ == "__main__":
app = QApplication(sys.argv)
kiwoom = Kiwoom()
kiwoom.comm_connect() #연결
# kiwoom.reset_opw00018_output()
# kiwoom.reset_opw20006_output()
# account_number = kiwoom.get_login_info("ACCNO")
# account_number = account_number.split(';')[0]
# kiwoom.set_input_value("계좌번호", account_number)
# kiwoom.comm_rq_data("opw20006_req", "opw20006", 0, "2000")
# #kiwoom.comm_rq_data("opw00018_req", "opw00018", 0, "2000")
# print(kiwoom.opw20006_output['single'])
# print(kiwoom.opw20006_output['multi'])
# print(kiwoom.GetCommRealData("000660", 10))
# kiwoom.send_order("send_order_req", "0101", "8004269811", 1, "000660", 2, "0","03","")
# kiwoom.send_order_fo("send_order_fo_req", "0101", "7001076831", "101RR000", 1, "2", "3", 1, "0", "")
# def send_order_fo(self, rqname, screen_no, acc_no, code, order_type, slbytp, hoga, quantity, price, order_no):
# self.dynamicCall("SendOrderFO(QString, QString, QString, QString, int, int, QString, int, int, QString)",
# [rqname, screen_no, acc_no, code, order_type, slbytp, hoga, quantity, price, order_no]
# kiwoom.set_input_value("종목코드", "105R9000")
# kiwoom.CommRqData("opt50001_req", "opt50001", 0, "2000")
# print(kiwoom.get_comm_real_data("105R9000",10))
# kiwoom.set_input_value("종목코드", "101R9000")
# kiwoom.comm_rq_data("opt50003_req", "opt50003", 0, "1000")
# print(kiwoom._comm_get_data(self, "101R9000", "", "opt50003_req", 0, "현재가"))