我自己不放心所以去申請了模擬平台帳號,在來試試看下單的功能,不過模擬帳號目前沒有提供夜盤的功能,就加減用吧!! 之前介紹模擬平台的文章,大家參考一下囉
模擬平台有練功跟競賽兩種帳號,我沒有研究,隨便拿一個來用
from comtypes.client import GetModule, CreateObject, GetEvents GetModule('C:\\skcom\\CapitalApi_2.13.17\\x64\\SKCOM.dll') import comtypes.gen.SKCOMLib as sk from datetime import datetime import getpass #建立物件,避免重複 createObject #登錄物件 if 'skC' not in globals(): skC=CreateObject(sk.SKCenterLib, interface=sk.ISKCenterLib) #下單物件 if 'skO' not in globals(): skO=CreateObject(sk.SKOrderLib , interface=sk.ISKOrderLib) #回報物件 if 'skR' not in globals(): skR=CreateObject(sk.SKReplyLib , interface=sk.ISKReplyLib) #輸入身分證與密碼 ID='' PW='' print(datetime.now(), 'ID & PW') #建立事件類別 class skO_events: def __init__(self): self.TFAcc=[] def OnAccount(self, bstrLogInID, bstrAccountData): strI=bstrAccountData.split(',') #找出期貨帳號 if len(strI) > 3 : if strI[0] == 'TF' : self.TFAcc = strI[1]+strI[3] class skR_events: def OnReplyMessage(self, bstrUserID, bstrMessage, sConfirmCode=0xFFFF): '''API 2.13.17 一定要返回 sConfirmCode=0xFFFF''' print('skR_OnReplyMessage', bstrMessage) return sConfirmCode def OnData(self, bstrUserID, bstrData):
#成交回報 self.bstrOnData.append(datetime.now().strftime('%H:%M:%S,') + bstrData) strI = bstrData.split(",") print("skR_OnData", strI) def OnNewData(self, bstrUserID , bstrData): print('skR_OnNewData', bstrData) #Event sink, 事件實體 EventO=skO_events() EventR=skR_events() #make connection to event sink ConnO = GetEvents(skO, EventO) ConnR = GetEvents(skR, EventR) print(datetime.now(), 'Making event objects') %matplotlib auto #登入模擬帳號 nCode=skC.SKCenterLib_ResetServer ("morder1.capital.com.tw") print('set server to morder1.capital.com.tw', skC.SKCenterLib_GetReturnCodeMessage(nCode)) #login nCode=skC.SKCenterLib_Login(ID,PW) print('Login', skC.SKCenterLib_GetReturnCodeMessage(nCode)) #初始 Order 物件 nCode=skO.SKOrderLib_Initialize() print("Initialize ", skC.SKCenterLib_GetReturnCodeMessage(nCode)) #初始 Cert by ID 物件,模擬平台不需要這個,會出現錯誤 nCode=skO.ReadCertByID(ID) print("ReadCertByID ", skC.SKCenterLib_GetReturnCodeMessage(nCode)) #Get User TF Account nCode=skO.GetUserAccount() print("GetUserAccount ", skC.SKCenterLib_GetReturnCodeMessage(nCode), EventO.TFAcc) #設定期貨單參數,請自行調整囉,注意參數的資料型別,例如 bstr開頭的是要輸入文字型態
#以下請再jupyter 裡用另外一個cell 跑
fo=sk.FUTUREORDER() fo.bstrFullAccount=EventO.TFAcc #TF 帳號 fo.bstrStockNo='MTX00' fo.sBuySell=0 #0 buy, 1 sell fo.bstrPrice='10700' #委託價格,「M」表示市價,{移動停損、MIT皆無須價格} fo.nQty=1 #交易口數 fo.sDayTrade=0 #當沖0:否 1:是,可當沖商品請參考交易所規定。 fo.sNewClose=2 #新平倉,0:新倉 1:平倉 2:自動{新期貨、選擇權使用} fo.sTradeType=1 #0:ROD 1:IOC 2:FOK fo.sReserved=0 #盤別,0:盤中(T盤及T+1盤);1:T盤預約{新期貨、停損單使用} #下單 nCode=skO.SendFutureOrder(ID, False, fo) print(nCode)