CREON Plus

[파이썬] ETF NAV , ETN IIV 실시간 수신 예제

상품종류
주식
언어종류
파이썬
작성일
2018/12/26
조회수
14116

이번 예제는 ETF NAV 를 실시간으로 수신 또는 ETN 의 IIV 를 실시간으로 수신 하는 예제 코드입니다 

아래는 사용된 주요 플러스 서비스입니다. 

  • Dscbo1.Cpsvr7244 - ETF NAV 조회 서비스
  • Dscbo1.Cpsvr7718 - ETN IIV 조회 서비스
  • CpSysDib.CpSvrNew7244S - NAV/IIV 실시간 수신 서비스 
  • DsCbo1.StockCur - 현재가 실시간 수신 서비스 

이번 예제에서는 예제의 다양화를 위해여 조회 통신으로 BlockReqeust API 대신 Request ~ Received 이벤트 처리 방식으로 만들었습니다.

조회 종목은 ETF/ETN 각가 A069500, Q500032 종목으로 조회합니다.


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
import sys
from PyQt5.QtWidgets import *
import win32com.client
import ctypes
import time
 
################################################
# PLUS 공통 OBJECT
g_objCodeMgr = win32com.client.Dispatch('CpUtil.CpCodeMgr')
g_objCpStatus = win32com.client.Dispatch('CpUtil.CpCybos')
g_objCpTrade = win32com.client.Dispatch('CpTrade.CpTdUtil')
 
 
################################################
# PLUS 실행 기본 체크 함수
def InitPlusCheck():
    # 프로세스가 관리자 권한으로 실행 여부
    if ctypes.windll.shell32.IsUserAnAdmin():
        print('정상: 관리자권한으로 실행된 프로세스입니다.')
    else:
        print('오류: 일반권한으로 실행됨. 관리자 권한으로 실행해 주세요')
        return False
 
    # 연결 여부 체크
    if (g_objCpStatus.IsConnect == 0):
        print("PLUS가 정상적으로 연결되지 않음. ")
        return False
 
    # 주문 관련 초기화
    if (g_objCpTrade.TradeInit(0!= 0):
        print("주문 초기화 실패")
        return False
 
    return True
 
 
 
 
 
################################################
# CpEvent: 실시간 이벤트 수신 클래스
class CpEvent:
    def set_params(self, client, name, caller):
        self.cpObj = client  # CP 실시간 통신 object
        self.name = name  # 서비스가 다른 이벤트를 구분하기 위한 이름
        self.caller = caller  # callback 을 위해 보관
 
    def OnReceived(self):
        if self.name == 'nav':
            print('실시간 nav 수신')
            self.caller.OnPublish_NAV(self.cpObj)
            return
        elif self.name == 'stockcur':
            print('실시간 현재가 수신')
            self.caller.OnPublish_Cur(self.cpObj)
            return
        elif self.name == 'etfreply':
            print('reply')
            self.caller.OnReply(self.cpObj)
            return
 
 
 
 
################################################
# plus 실시간 수신 base 클래스
class CpPublish:
    def __init__(self, name, serviceID):
        self.name = name
        self.obj = win32com.client.Dispatch(serviceID)
        self.bIsSB = False
 
    def __del__(self):
        self.Unsubscribe()
 
    def Subscribe(self, var, caller):
        if self.bIsSB:
            self.Unsubscribe()
 
        if (len(var) > 0):
            self.obj.SetInputValue(0, var)
 
        handler = win32com.client.WithEvents(self.obj, CpEvent)
        handler.set_params(self.obj, self.name, caller)
        self.obj.Subscribe()
        self.bIsSB = True
 
    def Unsubscribe(self):
        if self.bIsSB:
            self.obj.Unsubscribe()
        self.bIsSB = False
 
 
# 실시간 nav/iiv 수신
class CP_PB_NAV(CpPublish):
    def __init__(self):
        super().__init__('nav''CpSysDib.CpSvrNew7244S')
 
# 실시간 현재가 수신
class CP_PB_CUR(CpPublish):
    def __init__(self):
        super().__init__('stockcur''DsCbo1.StockCur')
 
 
# Reply 이벤트 - 1회만 수신
class CpRpETF:
    def __init__(self):
        self.name = "etfreply"
 
    def SetEvent(self, cpobj, caller):
        handler = win32com.client.WithEvents(cpobj, CpEvent)
        handler.set_params(cpobj, self.name, caller)
 
 
class CP_ETF_NAV:
    def __init__(self):
        self.objRq = None
        self.objReply = None
        self.objname1 = 'Dscbo1.Cpsvr7244'
        self.objname2 = 'Dscbo1.Cpsvr7718'
        self.navlist = []
        self.code = ''
        self.caller = None
 
    def OnReply(self, objRq):
        cnt = objRq.GetHeaderValue(0)
        print('조회 개수', cnt)
 
        for i in range(cnt):
            item = {}
 
            item['시간'= objRq.GetDataValue(0, i)
            item['현재가'= objRq.GetDataValue(1, i)
            item['대비'= objRq.GetDataValue(3, i)
            item['거래량'= objRq.GetDataValue(5, i)
            if (self.code[0== 'A'):
                item['NAV대비'= objRq.GetDataValue(4, i)
                item['NAV'= objRq.GetDataValue(6, i)
            else:
                item['IIV대비'= objRq.GetDataValue(4, i)
                item['IIV'= objRq.GetDataValue(6, i)
            item['추적오차'= objRq.GetDataValue(8, i)
            item['괴리율'= objRq.GetDataValue(9, i)
            item['해당ETF지수'= objRq.GetDataValue(10, i)
            item['지수대비'= objRq.GetDataValue(11, i)
            print(item)
            self.navlist.append(item)
 
        self.caller.OnReply(self.navlist)
 
    def Request(self, code, caller):
        self.navlist = []
        self.code = code
        self.caller = caller
 
 
        if (self.objRq != None) :
            self.objRq = None
 
        if (code[0== 'A'):
            self.objRq = win32com.client.Dispatch(self.objname1)
        else:
            self.objRq = win32com.client.Dispatch(self.objname2)
 
        self.objReply = CpRpETF()
        self.objReply.SetEvent(self.objRq, self)
 
        self.objRq.SetInputValue(0, code)
        self.objRq.Request()
 
        return
 
 
 
################################################
# 테스트를 위한 메인 화면
class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.navlist = []
 
        # plus 상태 체크
        if InitPlusCheck() == False:
            exit()
        self.rqObj = None
        self.objPB7244 = None
        self.objPBcur = None
 
 
        #######################################
        # 윈도우 처리 
        self.setWindowTitle("ETF NAV TEST")
        self.setGeometry(300300300220)
 
        nH = 20
        btnETF = QPushButton('ETF NAV', self)
        btnETF.move(20, nH)
        btnETF.clicked.connect(self.btnETF_clicked)
        nH += 50
 
        btnETN = QPushButton('ETN NAV', self)
        btnETN.move(20, nH)
        btnETN.clicked.connect(self.btnETN_clicked)
        nH += 50
 
        btnPrint = QPushButton('print', self)
        btnPrint.move(20, nH)
        btnPrint.clicked.connect(self.btnPrint_clicked)
        nH += 50
 
        btnExit = QPushButton('종료', self)
        btnExit.move(20, nH)
        btnExit.clicked.connect(self.btnExit_clicked)
        nH += 50
 
    #######################################
    # NAV 실시간 수신      
    def OnPublish_NAV(self, obj):
        item = {}
        code = obj.GetHeaderValue(0)
        item['시간'= obj.GetHeaderValue(1)
        item['현재가'= obj.GetHeaderValue(2)
        item['대비'= obj.GetHeaderValue(4)
        item['거래량'= obj.GetHeaderValue(5)
        if (code[0== 'A'):
            item['NAV'= obj.GetHeaderValue(6)
            if item['NAV'!= 0:
                item['NAV'/= 100
            item['NAV대비'= item['현재가'- item['NAV']
 
        else:
            item['IIV'= obj.GetHeaderValue(6)
            if item['IIV'!= 0:
                item['IIV'/= 100
            item['IIV대비'= item['현재가'- item['IIV']
 
        item['추적오차'= obj.GetHeaderValue(10)
        if item['추적오차'> 0 :
            item['추적오차'/= 100
        flag = obj.GetHeaderValue(11)
        item['괴리율'= obj.GetHeaderValue(12)
        if item['괴리율'!= 0 :
            item['괴리율'/= 100
        if (flag == ord('-')):
            item['괴리율'*= -1
        item['해당ETF지수'= obj.GetHeaderValue(15)
        if item['해당ETF지수'> 0 :
            item['해당ETF지수'!= 100
        flag = obj.GetHeaderValue(13)
        item['지수대비'= obj.GetHeaderValue(14)
        if item['지수대비'!= 0 :
            item['지수대비'/= 100
        if (flag == ord('-')):
            item['지수대비'*= -1
        print(item)
 
        self.navlist.$인서트$(0, item)
 
    #######################################
    # 현재가 실시간 수신
    def OnPublish_Cur(self, obj):
        item = {}
        exflag = obj.GetHeaderValue(19)  # 예상체결 플래그
        if exflag != ord('2'):
            item['동시호가여부'= '예상'
        else:
            item['동시호가여부'= '정규'
        item['현재가']= obj.GetHeaderValue(13)  # 현재가
        item['대비'= obj.GetHeaderValue(2)  # 대비
        item['거래량'= obj.GetHeaderValue(9)  # 거래량
        print(item)
 
 
    # NAV/IIV 시간대별 데이터 조회 수신
    def OnReply(self, data):
        self.navlist = data
 
        #######################################
        # NAV/IIV 실시간 이벤트 요청
        if (self.objPB7244 != None):
            self.objPB7244 = None
 
        self.objPB7244 = CP_PB_NAV()
        self.objPB7244.Subscribe(self.code, self)
 
        #######################################
        # 현재가 실시간 요청
        if (self.objPBcur != None):
            self.objPBcur = None
        self.objPBcur = CP_PB_CUR()
        self.objPBcur.Subscribe(self.code, self)
 
    def Request(self, code):
        self.code = code
        self.navlist = []
 
        #######################################
        # NAV/IIV 시간대별 리스트 요청
        if (self.rqObj):
            self.rqObj = None
 
        self.rqObj = CP_ETF_NAV()
        self.rqObj.Request(code, self)
 
    # ETF 종목 요청
    def btnETF_clicked(self):
        code = 'A069500'
        self.Request(code)
 
    # ETN 종목 요청
    def btnETN_clicked(self):
        code = 'Q500032'
        self.Request(code)
 
    def btnPrint_clicked(self):
        for data in self.navlist:
            print(data)
        return
 
    def btnExit_clicked(self):
        self.objPB7244.Unsubscribe()
        self.objPBcur.Unsubscribe()
        exit()
        return
 
 
if __name__ == "__main__":
    app = QApplication($시스.$argv)
    myWindow = MyWindow()
    myWindow.show()
    app.$이엑스이씨$_()
 
cs



첨부파일
의견(1)
이수원 이거 어떻게 사용하는겁니까?? (2019.06.15)

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

PDF뷰어 다운로드

인쇄