現在沒有模擬單帳號可以測試了,因此我用群益下單軟體,下了個很遠不會成交的單子當作委託測試單。
另外最近 jupyter lab 開發不知道為什麼當我使用 %matplotlib auto 的 magic function時,會使整個notebook 當機,因此用 asyncio 自製了一個簡易的 event loop 來定時 pump event
jupyter跑 asyncio 的用法
範例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from comtypes.client import CreateObject, GetEvents | |
import comtypes.gen.SKCOMLib as sk | |
import pythoncom | |
import asyncio | |
# 建立物件, 登錄物件, 回報物件 | |
skC = CreateObject(sk.SKCenterLib, interface=sk.ISKCenterLib) | |
skR = CreateObject(sk.SKReplyLib , interface=sk.ISKReplyLib) | |
# 輸入身分證與密碼 | |
ID = '' | |
PW = '' | |
# working functions, async coruntime to pump events | |
async def pump_task(): | |
while True: | |
pythoncom.PumpWaitingMessages() | |
await asyncio.sleep(0.1) | |
# 建立事件類別 | |
class skR_events: | |
def OnReplyMessage(self, bstrUserID, bstrMessage): | |
'''API 2.13.17 以上一定要返回 sConfirmCode=-1''' | |
sConfirmCode=-1 | |
print('skR_OnReplyMessage', bstrMessage) | |
return sConfirmCode | |
def OnNewData(self, bstrUserID , bstrData): | |
print('skR_OnNewData', bstrData) | |
# Event sink, 事件實體 | |
EventR = skR_events() | |
# make connection to event sink | |
ConnR = GetEvents(skR, EventR) | |
print('Making event handeller') | |
# get an event loop | |
loop = asyncio.get_event_loop() | |
pumping_loop = loop.create_task(pump_task()) | |
print('Event pumping!') | |
# login | |
nCode=skC.SKCenterLib_Login(ID,PW) | |
print('Login', skC.SKCenterLib_GetReturnCodeMessage(nCode)) | |
# 連接回報伺服器 | |
skR.SKReplyLib_ConnectByID(ID) |
結果