cybos com event processing

cybos com dll 의 이벤트를 처리하는 간단한 예제입니다.

Dependencies

  • win32com
  • pythoncom
  • win32event
  • PyQt (event loop 생성용, 직접 win32event를 이용하여 message pump code를 짜도 됨)

COM type library 의 python interface 생성

  • 이 예제를 돌리기 전에 "C:\Python26\Lib\site-packages\win32com\client\makepy.py" 를 이용하여 4개의 cybos type lib에 대한 python interface를 생성해야 합니다.
    • CpDib
    • CpSysDb
    • CpTrade
    • CpUtil
  • interface 파일은 "C:\Python26\Lib\site-packages\win32com\gen_py" 에 생성됩니다.

Test Code (선물 체결가)

# -*- coding: utf-8 -*-
 
from PyQt4.QtGui import QApplication
from weakref import proxy
from pythoncom import CoInitialize, PumpWaitingMessages
from win32com.client import gencache, DispatchWithEvents
from win32event import MsgWaitForMultipleObjects, QS_ALLEVENTS
 
class CpEvent(object):
    def OnReceived(self):
        self.parent.on_signal()
 
class Source(object):
    def __init__(self, code):
        gencache.EnsureModule("859343F1-08FD-11D4-8231-00105A7C4F8C", 0, 1, 0)
        gencache.EnsureModule("9C31B76A-7189-49A3-9781-3C6DD6ED5AD3", 0, 1, 0)
        gencache.EnsureModule("1F7D5E5A-05AB-4236-B6F3-3D383B09203A", 0, 1, 0)
        gencache.EnsureModule("2DA9C35C-FE59-4A32-A942-325EE8A6F659", 0, 1, 0)
 
        self.code = code
        self.event = DispatchWithEvents("dscbo1.FutureCurr", CpEvent)
        self.com = self.event._obj_
        self.com.SetInputValue(0, self.code)
        self.event.parent = proxy(self)
 
        self.com.SubscribeLatest()
        CoInitialize()
 
    def on_signal(self):
 
        price_trade = float(self.com.GetHeaderValue(1)) 
        print price_trade
 
if __name__ == "__main__":
 
    # use QApplication as a event loop        
    loop = QApplication([]) 
 
    s = Source('101F3')
 
    loop.exec_()