2017年8月19日 星期六

群益api, 使用 python 取得上市櫃股票/海期代號

最近想把策略回測試試看每檔個股的表現,所以整理了一下目前上市櫃的股票代號,在證交所抓的,用excel整理了一下,在此備份一下

CSV檔案連結


剛EnterMonitor完,要等連線回報完 3003,才可以跑 RequestStockList(),不然會報 3024的錯誤代碼,本範例在 OnConnection 回報3003 後才 RequestStockList()


一併附上海期取得商品代碼

#取得商品名稱範例

import comtypes.client as cc
cc.GetModule('D:\\l679\\programfile\\SKCOM\\CapitalApi_2.13.14\\x86\\SKCOM.dll')
import comtypes.gen.SKCOMLib as sk
#建立物件
#登錄物件
if 'skC' not in globals():
    #避免重複 createObject
    skC=cc.CreateObject(sk.SKCenterLib, interface=sk.ISKCenterLib)
#報價物件
if 'skQ' not in globals():
    #避免重複 createObject
    skQ=cc.CreateObject(sk.SKQuoteLib , interface=sk.ISKQuoteLib)
#海期報價物件
if 'skOSQ' not in globals():
    #避免重複 createObject
    skOSQ=cc.CreateObject(sk.SKOSQuoteLib , interface=sk.ISKOSQuoteLib)
    
#Configuration
ID=''
PW=''
#建立事件類別
class skQ_events:      
    def __init__(self):
        self.stockList=[]    
    def OnConnection(self, nKind, nCode):
        if nCode == 0 :
            if nKind == 3001 :
                print("skQ連線中, nkind= ", nKind)        
            elif nCode == 0 & (nKind == 3003):
                print("skQ連線成功, nkind= ", nKind)
                #確認連線完成後,取得商品名單
                self.stockList=[]
                nCode=skQ.SKQuoteLib_RequestStockList(0)
                print('RequestStockList', skC.SKCenterLib_GetReturnCodeMessage(nCode))
            else:
                print('nCode=0, nKind=', nKind)
        else:
            print('Error, please check nKind, nCode for erro msg, ', nKind, nCode)

    def OnNotifyStockList(self,sMarketNo, bstrStockData):
        self.stockList.append(bstrStockData)
        #print(bstrStockData)
        
class skOSQ_events:      
    def __init__(self):
        self.OverseaProducts=[]    
    def OnConnect(self, nKind, nCode):
        if nCode == 0 :
            if nKind == 3001 :
                print("skOSQ 連線中, nkind= ", nKind)
                nCode=skOSQ.SKOSQuoteLib_RequestOverseaProducts()
                print('RequestOverseaProducts', skC.SKCenterLib_GetReturnCodeMessage(nCode))
            else:
                print('nCode=0, nKind=', nKind)
        else:
            print('Error, please check nKind, nCode for erro msg, ', nKind, nCode)

    def OnOverseaProducts(self, bstrValue):
        self.OverseaProducts.append(bstrValue)
        #print(bstrValue)
          
#Event sink, 事件實體
EventQ=skQ_events()
EventOSQ=skOSQ_events()
#make connection to event sink
ConnQ = cc.GetEvents(skQ, EventQ) 
ConnOSQ = cc.GetEvents(skOSQ, EventOSQ) 

#使用jupyter notebook, 這句可以讓 Event Pump 出來,用其他IDE的請不要加這句
#使用 %matplotlib auto 來推 event,放 %matplotlib auto 的位置蠻有關係的,
#放太前面有時候不會有反應,本範例我放 import 下方,就沒反應
#請使用者自己試試看放在不同位置試試看
%matplotlib auto

#main()
print('Login', skC.SKCenterLib_GetReturnCodeMessage(skC.SKCenterLib_Login(ID,PW)))
#Enter quote server
nCode=skQ.SKQuoteLib_EnterMonitor()
print('SKQuoteLib_EnterMonitor()', skC.SKCenterLib_GetReturnCodeMessage(nCode))

#Enter oversea quote server
nCoce=skOSQ.SKOSQuoteLib_EnterMonitor()
print('SKOSQuoteLib_EnterMonitor()', skC.SKCenterLib_GetReturnCodeMessage(nCode))

#等連線成功後,確定有requestc,後再取出商品
EventOSQ.OverseaProducts[0:5], EventQ.stockList[0:5]

#市場別代號: 市 0、櫃 1、期 2、選 3、興 4

#取得其他上商品代號,改動參數即可
skQ.SKQuoteLib_RequestStockList(0)

#商品代號都存在 EventQ.stockList 裡囉

3 則留言:

  1. 您好,感謝您的分享。
    我遇到的問題「python 已經停止運作」,我是X64的系統,請問您知道怎麼解嗎?

    回覆刪除
  2. 跑了哪些code呀? 有具體一點的描述嗎? 如果用 x64系統,那python跟skcom都要用64 bit的,這樣應該比較沒問題

    回覆刪除
  3. File "D:/python/qunYi/PythonExample/Quote.py", line 14, in
    import comtypes.gen.SKCOMLib as sk

    ModuleNotFoundError: No module named 'comtypes.gen.SKCOMLib'

    回覆刪除