CREON Plus

[파이썬] 선물 현재가/시간대별/일자별/주문/잔고/미체결 예제

상품종류
선물옵션
언어종류
파이썬
작성일
2018/06/01
조회수
13115
이번 예제는 PLUS API 를 이용하여 선물 시세 및 계좌, 주문 관련 예제 코드 코드입니다.

주요 제공 예제:
  •    선물 현재가
  •    선물 시간대별
  •    선물 일자별
  •    선물 매수 - 현재가 조회 후 현재가로 1주 매수
  •    선물 매도 - 현재가 조회 후 현재가로 1주 매도 
  •    선물 잔고 조회
  •    선물 미체결 조회

※ 주의사항: 본 예제는 PLUS 활용을 위한 참고용으로만 제공 됩니다.
특히 주문의 경우 현재가로 직접 주문이 발생함으로 모의투자에서 관련 내용을 충분히 확인 하시기 바랍니다.



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
import sys
from PyQt5.QtWidgets import *
import win32com.client
import ctypes
 
g_objCodeMgr = win32com.client.Dispatch('CpUtil.CpCodeMgr')
g_objCpStatus = win32com.client.Dispatch('CpUtil.CpCybos')
g_objCpTrade = win32com.client.Dispatch('CpTrade.CpTdUtil')
g_objFutureMgr = win32com.client.Dispatch("CpUtil.CpFutureCode")
 
def InitPlusCheck():
    # 프로세스가 관리자 권한으로 실행 여부
    if ctypes.windll.shell32.IsUserAnAdmin():
        print('정상: 관리자권한으로 실행된 프로세스입니다.')
    else:
        print('오류: 일반권한으로 실행됨. 관리자 권한으로 실행해 주세요')
        return False
 
    # 연결 여부 체크
    if (g_objCpStatus.IsConnect == 0):
        print("PLUS가 정상적으로 연결되지 않음. ")
        return False
 
    # 주문 관련 초기화
    ret = g_objCpTrade.TradeInit(0)
    if (ret != 0):
        print("주문 초기화 실패, 오류번호 ", ret)
        return False
 
    return True
 
# CpFutureMst: 선물 현재가
class CpFutureMst:
    def __init__(self):
        self.objRq = win32com.client.Dispatch("Dscbo1.FutureMst")
 
    def request(self, code, retItem):
        self.objRq.SetInputValue(0, code)
        self.objRq.BlockRequest()
 
        rqStatus = self.objRq.GetDibStatus()
        rqRet = self.objRq.GetDibMsg1()
        print("통신상태", rqStatus, rqRet)
        if rqStatus != 0:
            return False
 
        retItem['한글종목명'= self.objRq.GetHeaderValue(2)
        retItem['잔존일'= self.objRq.GetHeaderValue(8)
        retItem['최종거래일'= self.objRq.GetHeaderValue(9)
        retItem['현재가'= self.objRq.GetHeaderValue(71)
        retItem['시가'= self.objRq.GetHeaderValue(72)
        retItem['고가'= self.objRq.GetHeaderValue(73)
        retItem['저가'= self.objRq.GetHeaderValue(74)
 
        retItem['매수1호가'= self.objRq.GetHeaderValue(54)
        retItem['매수1호가수량'= self.objRq.GetHeaderValue(59)
        retItem['매도1호가'= self.objRq.GetHeaderValue(37)
        retItem['매도1호가수량'= self.objRq.GetHeaderValue(42)
 
        retItem['K200지수'= self.objRq.GetHeaderValue(89)
        retItem['BASIS'= self.objRq.GetHeaderValue(90)
 
        for key, value in retItem.items():
            if (type(value) == float) :
                print('%s:%.2f' %(key, value))
            else:
                print(key, ':', value)
        return True
 
# CpFutureBid : 선물 시간대별 리스트 조회
class CpFutureBid:
    def __init__(self):
        self.objRq = win32com.client.Dispatch("Dscbo1.FutureBid1")
 
    def request(self, code, retList):
        self.objRq.SetInputValue(0, code)
        self.objRq.SetInputValue(175# 요청개수
 
        datacnt= 0
        while True:
            self.objRq.BlockRequest()
 
            rqStatus = self.objRq.GetDibStatus()
            rqRet = self.objRq.GetDibMsg1()
            if rqStatus != 0:
                print("통신상태", rqStatus, rqRet)
                return False
 
            cnt = self.objRq.GetHeaderValue(2)
 
            for i in range(cnt):
                item = {}
                item['시각'= self.objRq.GetDataValue(11, i)
                item['매도호가'= self.objRq.GetDataValue(1, i)
                item['매수호가'= self.objRq.GetDataValue(2, i)
                item['현재가'= self.objRq.GetDataValue(3, i)
                item['전일대비'= self.objRq.GetDataValue(4, i)
                item['누적거래량'= self.objRq.GetDataValue(6, i)
                item['미체결약정'= self.objRq.GetDataValue(8, i)
                item['체결거래량'= self.objRq.GetDataValue(9, i)
 
                retList.append(item)
            # end of for
 
            datacnt += cnt
            if self.objRq.Continue == False :
                break
            if datacnt > 500:
                break
 
        # end of while
 
 
        for item in  retList:
            data = ''
            for key, value in item.items():
                if (type(value) == float):
                    data += '%s:%.2f' % (key, value)
                elif (type(value) == str):
                    data += '%s:%s' % (key, value)
                elif (type(value) == int):
                    data += '%s:%d' % (key, value)
 
                data += ' '
            print(data)
        return True
 
# CpFutureWeek: 선물 일자별
class CpFutureWeek:
    def __init__(self):
        self.objRq = win32com.client.Dispatch("Dscbo1.FutureWeek1")
 
    def request(self, code, retList):
        self.objRq.SetInputValue(0, code)
 
        datacnt= 0
        while True:
            self.objRq.BlockRequest()
 
            rqStatus = self.objRq.GetDibStatus()
            rqRet = self.objRq.GetDibMsg1()
            if rqStatus != 0:
                print("통신상태", rqStatus, rqRet)
                return False
 
            cnt = self.objRq.GetHeaderValue(0)
 
            for i in range(cnt):
                item = {}
                item['일자'= self.objRq.GetDataValue(0, i)
                item['시가'= self.objRq.GetDataValue(1, i)
                item['고가'= self.objRq.GetDataValue(2, i)
                item['저가'= self.objRq.GetDataValue(3, i)
                item['종가'= self.objRq.GetDataValue(4, i)
                item['전일대비'= self.objRq.GetDataValue(5, i)
                item['누적거래량'= self.objRq.GetDataValue(6, i)
                item['거래대금'= self.objRq.GetDataValue(7, i)
                item['미결제약정'= self.objRq.GetDataValue(8, i)
 
                retList.append(item)
            # end of for
 
            datacnt += cnt
            if self.objRq.Continue == False :
                break
        # end of while
 
 
        for item in  retList:
            data = ''
            for key, value in item.items():
                if (type(value) == float):
                    data += '%s:%.2f' % (key, value)
                elif (type(value) == str):
                    data += '%s:%s' % (key, value)
                elif (type(value) == int):
                    data += '%s:%d' % (key, value)
 
                data += ' '
            print(data)
        return True
 
# CpFutureOrder : 선물 주문
class CpFutureOrder:
    def __init__(self):
        self.acc = g_objCpTrade.AccountNumber[0]  # 계좌번호
        self.accFlag = g_objCpTrade.GoodsList(self.acc, 2)  # 선물/옵션 계좌구분
        print(self.acc, self.accFlag[0])
        self.objOrder = win32com.client.Dispatch("CpTrade.CpTd6831")
 
    def Order(self, buysell, code, price, amount, retData):
        self.objOrder.SetInputValue(1, self.acc)
        self.objOrder.SetInputValue(2, code)
        self.objOrder.SetInputValue(3, amount)
        self.objOrder.SetInputValue(4, price)
        self.objOrder.SetInputValue(5, buysell) # '1' 매도 '2' 매수
        self.objOrder.SetInputValue(6'1'# 주문유형 : '1' 지정가
        self.objOrder.SetInputValue(7'0')  # '주문 조건 구분 '0' : 없음
 
        ret = self.objOrder.BlockRequest()
        if ret == 4:
            remainTime = g_objCpStatus.LimitRequestRemainTime
            print('연속조회 제한 오류, 남은 시간', remainTime)
            return False
 
 
        rqStatus = self.objOrder.GetDibStatus()
        rqRet = self.objOrder.GetDibMsg1()
        print("통신상태", rqStatus, rqRet)
        if rqStatus != 0:
            return False
 
        retData['종목'= code
        retData['주문수량'= self.objOrder.GetHeaderValue(3)
        retData['주문가격'= self.objOrder.GetHeaderValue(4)
        retData['주문번호'= self.objOrder.GetHeaderValue(8)
 
 
    def buyOrder(self, code, price, amount, retData):
        return self.Order('2', code, price, amount, retData)
 
 
 
    def sellOrder(self, code, price, amount, retData):
        return self.Order('1', code, price, amount, retData)
 
# CpFutureBalance: 선물 잔고
class CpFutureBalance:
    def __init__(self):
        self.objRq = win32com.client.Dispatch("CpTrade.CpTd0723")
        self.acc = g_objCpTrade.AccountNumber[0]  # 계좌번호
        self.accFlag = g_objCpTrade.GoodsList(self.acc, 2)  # 선물/옵션 계좌구분
        print(self.acc, self.accFlag[0])
 
    def request(self,  retList):
        self.objRq.SetInputValue(0, self.acc)
        self.objRq.SetInputValue(1, self.accFlag[0])
        self.objRq.SetInputValue(450)
 
 
        while True:
            self.objRq.BlockRequest()
 
            rqStatus = self.objRq.GetDibStatus()
            rqRet = self.objRq.GetDibMsg1()
 
            if rqStatus != 0:
                print("통신상태", rqStatus, rqRet)
                return False
 
            cnt = self.objRq.GetHeaderValue(2)
 
            for i in range(cnt):
                item = {}
                item['코드'= self.objRq.GetDataValue(0, i)
                item['종목명'= self.objRq.GetDataValue(1, i)
                flag = self.objRq.GetDataValue(2, i)
                if flag == '1':
                    item['잔고구분'= '매도'
                elif flag == '2':
                    item['잔고구분'= '매수'
 
                item['잔고수량'= self.objRq.GetDataValue(3, i)
                item['평균단가'= self.objRq.GetDataValue(5, i)
                item['청산가능수량'= self.objRq.GetDataValue(9, i)
 
                retList.append(item)
            # end of for
 
            if self.objRq.Continue == False :
                break
        # end of while
 
 
        for item in  retList:
            data = ''
            for key, value in item.items():
                if (type(value) == float):
                    data += '%s:%.2f' % (key, value)
                elif (type(value) == str):
                    data += '%s:%s' % (key, value)
                elif (type(value) == int):
                    data += '%s:%d' % (key, value)
 
                data += ' '
            print(data)
        return True
 
 
# CpFutureNContract: 선물 미체결 조회
class CpFutureNContract:
    def __init__(self):
        self.objRq = win32com.client.Dispatch("CpTrade.CpTd5371")
        self.acc = g_objCpTrade.AccountNumber[0]  # 계좌번호
        self.accFlag = g_objCpTrade.GoodsList(self.acc, 2)  # 선물/옵션 계좌구분
        print(self.acc, self.accFlag[0])
 
    def request(self,  retList):
        self.objRq.SetInputValue(0, self.acc)
        self.objRq.SetInputValue(1, self.accFlag[0])
        self.objRq.SetInputValue(6'3'# '3' : 미체결
 
 
        while True:
            self.objRq.BlockRequest()
 
            rqStatus = self.objRq.GetDibStatus()
            rqRet = self.objRq.GetDibMsg1()
            if rqStatus != 0:
                print("통신상태", rqStatus, rqRet)
                return False
 
            cnt = self.objRq.GetHeaderValue(6)
 
            for i in range(cnt):
                item = {}
                item['주문번호'= self.objRq.GetDataValue(2, i)
                item['코드'= self.objRq.GetDataValue(4, i)
                item['종목명'= self.objRq.GetDataValue(5, i)
                item['주문가격'= self.objRq.GetDataValue(8, i)
                item['잔량'= self.objRq.GetDataValue(9, i)
                item['거래구분']= self.objRq.GetDataValue(6, i)
 
                retList.append(item)
            # end of for
 
            if self.objRq.Continue == False :
                break
        # end of while
 
 
        for item in  retList:
            data = ''
            for key, value in item.items():
                if (type(value) == float):
                    data += '%s:%.2f' % (key, value)
                elif (type(value) == str):
                    data += '%s:%s' % (key, value)
                elif (type(value) == int):
                    data += '%s:%d' % (key, value)
 
                data += ' '
            print(data)
        return True
 
 
 
class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
 
 
        self.setWindowTitle("선물 옵션 예제")
        self.setGeometry(300300500180)
        nH = 20
 
 
        self.fcodelist = []
 
        for i in range(g_objFutureMgr.GetCount()):
            code = g_objFutureMgr.GetData(0, i)
            name = g_objFutureMgr.GetData(1, i)
            if (code[0== '4') :   # spread skip
                continue
            if (code[0== '10100') : #연결선물 skip
                continue
            self.fcodelist.append((code, name))
 
 
        self.comboCodeList = QComboBox(self)
        for code, name in self.fcodelist :
            self.comboCodeList.addItem(code)
 
        self.comboCodeList.currentIndexChanged.connect(self.OnComboChanged)
        self.comboCodeList.move(20, nH)
        self.comboCodeList.resize(20030)
        nH += 50
 
 
        nLeft = 20
        btnCPrice = QPushButton('현재가', self)
        btnCPrice.move(nLeft, nH)
        btnCPrice.clicked.connect(self.btnCPrice_clicked)
 
        nLeft += 100
        btnBid = QPushButton('시간대별', self)
        btnBid.move(nLeft, nH)
        btnBid.clicked.connect(self.btnBid_clicked)
 
        nLeft += 100
        btnWeek = QPushButton('일자별', self)
        btnWeek.move(nLeft, nH)
        btnWeek.clicked.connect(self.btnWeek_clicked)
        nLeft += 100
        nH += 50
 
 
        nLeft = 20
        btnBuy = QPushButton('매수', self)
        btnBuy.move(nLeft, nH)
        btnBuy.clicked.connect(self.btnBuy_clicked)
 
        nLeft += 100
        btnSell = QPushButton('매도', self)
        btnSell.move(nLeft, nH)
        btnSell.clicked.connect(self.btnSell_clicked)
 
        nH += 50
 
        nLeft = 20
        btnBalance = QPushButton('잔고', self)
        btnBalance.move(nLeft, nH)
        btnBalance.clicked.connect(self.btnBalance_clicked)
 
 
        nLeft += 100
        btnNotContract = QPushButton('미체결', self)
        btnNotContract.move(nLeft, nH)
        btnNotContract.clicked.connect(self.btnNotContract_clicked)
 
 
        nH += 50
        btnExit = QPushButton('종료', self)
        btnExit.move(20, nH)
        btnExit.clicked.connect(self.btnExit_clicked)
 
        nH += 50
 
 
        self.setGeometry(300300340, nH)
 
    def OnComboChanged(self):
        pass
 
    def btnCPrice_clicked(self):
        code = self.comboCodeList.currentText()
        objFutureMst = CpFutureMst()
        retItem = {}
        objFutureMst.request(code, retItem)
 
    def btnBid_clicked(self):
        code = self.comboCodeList.currentText()
        obj = CpFutureBid()
        retList = []
        obj.request(code, retList)
 
    def btnWeek_clicked(self):
        code = self.comboCodeList.currentText()
        obj = CpFutureWeek()
        retList = []
        obj.request(code, retList)
 
    def btnBuy_clicked(self):
        # 현재가 조회 > 매수 1주 주문
        code = self.comboCodeList.currentText()
        objFutureMst = CpFutureMst()
        retItem = {}
        objFutureMst.request(code, retItem)
 
        objOrder = CpFutureOrder()
        price = retItem['현재가']
        retOrder = {}
        objOrder.buyOrder(code, price, 1, retOrder)
 
        print(retOrder)
 
    def btnSell_clicked(self):
        # 현재가 조회 > 매도 1주 주문
        code = self.comboCodeList.currentText()
        objFutureMst = CpFutureMst()
        retItem = {}
        objFutureMst.request(code, retItem)
 
        objOrder = CpFutureOrder()
        price = retItem['현재가']
        retOrder = {}
        objOrder.sellOrder(code, price, 1, retOrder)
 
        print(retOrder)
 
    def btnBalance_clicked(self):
        objRq = CpFutureBalance()
        retList = []
        objRq.request(retList)
 
    def btnNotContract_clicked(self):
        objRq = CpFutureNContract()
        retList = []
        objRq.request(retList)
 
    def btnExit_clicked(self):
        exit()
        return
 
 
if __name__ == "__main__":
    if False == InitPlusCheck() :
        exit()
 
    app = QApplication($시스.$argv)
    myWindow = MyWindow()
    myWindow.show()
    app.$이엑스이씨$_()
 
 
cs
첨부파일
의견(0)

첨부파일을 PDF뷰어로 확인 하실수 있으며, PDF뷰어 미설치 고객께서는 우측 다운로드를 통해 설치 후 이용 가능 합니다.

PDF뷰어 다운로드

인쇄