-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.py
233 lines (171 loc) · 7.63 KB
/
contact.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
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import uic
from kiwoom import *
form_class = uic.loadUiType("test02.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.kiwoom = Kiwoom()
self.kiwoom.comm_connect()
self.load_buy_sell_list()
self.trade_stocks_done = False
self.timer = QTimer(self)
self.timer.start(1000)
self.timer.timeout.connect(self.timeout)
self.timer2 = QTimer(self)
self.timer2.start(1000 * 10)
self.timer2.timeout.connect(self.timeout2)
self.lineEdit.textChanged.connect(self.code_changed)
self.pushButton.clicked.connect(self.send_order)
self.pushButton_2.clicked.connect(self.check_balance)
accouns_num = int(self.kiwoom.get_login_info("ACCOUNT_CNT"))
accounts = self.kiwoom.get_login_info("ACCNO")
accounts_list = accounts.split(';')[0:accouns_num]
self.comboBox.addItems(accounts_list)
#연결 타이머
def timeout(self):
market_start_time = QTime(9, 0, 0)
current_time = QTime.currentTime()
if current_time > market_start_time and self.trade_stocks_done is False:
self.trade_stocks()
self.trade_stocks_done = True
text_time = current_time.toString("hh:mm:ss")
time_msg = "현재시간: " + text_time
state = self.kiwoom.GetConnectState()
if state == 1:
state_msg = "서버 연결 중"
else:
state_msg = "서버 미 연결 중"
self.statusbar.showMessage(state_msg + " | " + time_msg)
#입력받은 종목코드를 종목명으로 변환해 lineEdit_2에 출력
def code_changed(self):
code = self.lineEdit.text()
name = self.kiwoom.get_master_code_name(code)
self.lineEdit_2.setText(name)
def send_order(self):
order_type_lookup = {'신규매수': 1, '신규매도': 2, '매수취소': 3, '매도취소': 4}
hoga_lookup = {'지정가': "00", '시장가': "03"}
account = self.comboBox.currentText()
order_type = self.comboBox_2.currentText()
code = self.lineEdit.text()
hoga = self.comboBox_3.currentText()
num = self.spinBox.value()
price = self.spinBox_2.value()
self.kiwoom.send_order("send_order_req", "0101", account, order_type_lookup[order_type], code, num, price,
hoga_lookup[hoga], "")
def check_balance(self):
self.kiwoom.reset_opw00018_output()
self.kiwoom.set_input_value("계좌번호", self.comboBox.currentText())
self.kiwoom.comm_rq_data("opw00018_req", "opw00018", 0, "2000")
while self.kiwoom.remained_data:
time.sleep(0.2)
self.kiwoom.set_input_value("계좌번호", self.comboBox.currentText())
self.kiwoom.comm_rq_data("opw00018_req", "opw00018", 2, "2000")
# opw00001
self.kiwoom.set_input_value("계좌번호", self.comboBox.currentText())
self.kiwoom.comm_rq_data("opw00001_req", "opw00001", 0, "2000")
# balance
item = QTableWidgetItem(self.kiwoom.d2_deposit)
item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
self.tableWidget.setItem(0, 0, item)
for i in range(1, 6):
item = QTableWidgetItem(self.kiwoom.opw00018_output['single'][i - 1])
item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
self.tableWidget.setItem(0, i, item)
self.tableWidget.resizeRowsToContents()
# Item list
item_count = len(self.kiwoom.opw00018_output['multi'])
self.tableWidget_2.setRowCount(item_count)
for j in range(item_count):
row = self.kiwoom.opw00018_output['multi'][j]
for i in range(len(row)):
item = QTableWidgetItem(row[i])
item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
self.tableWidget_2.setItem(j, i, item)
self.tableWidget_2.resizeRowsToContents()
#실시간 조회(타이머2 이용)
def timeout2(self):
if self.checkBox.isChecked():
self.check_balance()
def load_buy_sell_list(self):
f = open("buy_list.txt", 'rt')
buy_list = f.readlines()
f.close()
f = open("sell_list.txt", 'rt')
sell_list = f.readlines()
f.close()
row_count = len(buy_list) + len(sell_list)
self.tableWidget_4.setRowCount(row_count)
#매수 리스트
for j in range(len(buy_list)):
row_data = buy_list[j]
split_row_data = row_data.split(';')
split_row_data[1] = self.kiwoom.get_master_code_name(split_row_data[1].rsplit())
for i in range(len(split_row_data)):
item = QTableWidgetItem(split_row_data[i].rstrip())
item.setTextAlignment(Qt.AlignVCenter | Qt.AlignCenter)
self.tableWidget_4.setItem(j, i, item)
#매도 리스트
for j in range(len(sell_list)):
row_data = sell_list[j]
split_row_data = row_data.split(';')
split_row_data[1] = self.kiwoom.get_master_code_name(split_row_data[1].rstrip())
for i in range(len(split_row_data)):
item = QTableWidgetItem(split_row_data[i].rstrip())
item.setTextAlignment(Qt.AlignVCenter | Qt.AlignCenter)
self.tableWidget_4.setItem(len(buy_list) + j, i, item)
self.tableWidget_4.resizeRowsToContents()
def trade_stocks(self):
hoga_lookup = {'지정가': "00", '시장가': "03"}
f = open("buy_list.txt", 'rt')
buy_list = f.readlines()
f.close()
f = open("sell_list.txt", 'rt')
sell_list = f.readlines()
f.close()
# account
account = self.comboBox.currentText()
# buy list
for row_data in buy_list:
split_row_data = row_data.split(';')
hoga = split_row_data[2]
code = split_row_data[1]
num = split_row_data[3]
price = split_row_data[4]
if split_row_data[-1].rstrip() == '매수전':
self.kiwoom.send_order("send_order_req", "0101", account, 1, code, num, price,
hoga_lookup[hoga], "")
# sell list
for row_data in sell_list:
split_row_data = row_data.split(';')
hoga = split_row_data[2]
code = split_row_data[1]
num = split_row_data[3]
price = split_row_data[4]
if split_row_data[-1].rstrip() == '매도전':
self.kiwoom.send_order("send_order_req", "0101", account, 2, code, num, price,
hoga_lookup[hoga], "")
# buy list
for i, row_data in enumerate(buy_list):
buy_list[i] = buy_list[i].replace("매수전", "주문완료")
# file update
f = open("buy_list.txt", 'wt')
for row_data in buy_list:
f.write(row_data)
f.close()
# sell list
for i, row_data in enumerate(sell_list):
sell_list[i] = sell_list[i].replace("매도전", "주문완료")
# file update
f = open("sell_list.txt", 'wt')
for row_data in sell_list:
f.write(row_data)
f.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()