# PyNetstockQuoteLib簡介<br>

一款臺灣金融市場的證券，期貨，期權多合一的輕量級報價API模塊，由臺灣Netstock公司開發，做到極致化的高效和輕量級。整包大小僅有不到200kb。<br>

**安裝方法：pip install PyNetstockQuoteLib**<br>
申請賬號請訪問公司網站：https://www.netstock.net/<br>
電話：+886-2-25775688<br>
地址：臺灣臺北市松山區105敦化南路一段21號2樓<br>

## 函數列表：<br>
**1，連接**<br>
`def Connect(Address: str, nPort: unsigned short)-> bool: ...`<br>
**2，斷開連接**<br>
`def Disconnect() -> None: ...`<br>
**3，登錄**<br>
`def Login(UserName: str, Password: str, Software: str, Version: str, Company: str, Branch: str) -> None: ...`<br>
**4，運行阻塞函數**<br>
`def Go() -> None: ...`<br>
**5，注冊報價函數**<br>
`def SubscribeToQuote(Symbol: str, Exc: int, sExc: int = -1, rlot:bool = False) -> int: ...`<br>
**6，關閉注冊報價**<br>
`def CloseSubscribe(Index: int) -> bool: ...`<br>
**7，注冊Tick函數**<br>
`def SubscribeToTick(Symbol: str, Exc: int, sExc: int = -1, rlot:bool = False) -> int: ...`<br>
**8，獲得報價**<br>
`def GetQuote(Index: int, Field: int) -> int | float: ...`<br>
**9，獲得Tick數據**<br>
`def GetTick(Index:int)-> List[Dict[str, Union[int, float]]]: ...`<br>
**10，獲得基本資料(9-16)**<br>
`def GetInfo(Exc:int, Symbol:str, Field:str, sExc:int)-> str: ...`<br>
**11，設置主動回報callback**<br>
`def SetNoticeCallback(callback: Callable[[int, int, object, int], None]) -> None: ...`<br>
**12，設置登錄回報函數**<br>
`def SetLoginCallback(callback: Callable[[int, str, int], None]) -> None: ...`<br>
**13，設置交易日資訊回報函數**<br>
`def SetExcInfoCallback(callback: Callable[[int, int], None]) -> None: ...`<br>
**14，設置基本資料更新回報函數**<br>
`def SetStkiCallback(callback: Callable[[int, int, int], None]) -> None: ...`<br>
**15，設置報價回報callback**<br>
`def SetQuoteCallback(callback: Callable[[int, int], None]) -> None: ...`<br>
**16，設置Tick回報函數**<br>
`def SetTickCallback(callback: Callable[[int], None]) -> None: ...`<br>

## 示例1：Quote.py<br>
```python
import PyNetstockQuoteLib as NsQuote

Ind = 0
Ind1 = 0
#登錄回報
def OnLogin(Type,pInfo,InfoSize):
    if(Type == NsQuote.SMSGL_LOGIN_OKAY):
        global Ind,Ind1
        print("登錄成功!")
        #登錄成功后對商品進行報價訂閱
        Ind = NsQuote.SubscribeToQuote("FITX",NsQuote.EXC_FITXN,NsQuote.EXC_FITX_T2)
        Ind1 = NsQuote.SubscribeToQuote("2330",NsQuote.EXC_STOCK)
    else:
        print("登錄失敗")        
#報價回報
def OnQuote(Index,Type):
    if(Type == 1):
        global Ind,Ind1
        if(Index == Ind):
            print("Sym:FITX,Name:" + NsQuote.GetInfo(NsQuote.EXC_FITXN,"FITX","name") +
                  "," + NsQuote.SYM_GLOBAL_DESC[NsQuote.SYM_LAST] +":" +
                  str(NsQuote.GetQuote(Index,NsQuote.SYM_LAST)) + 
                  "," + NsQuote.SYM_GLOBAL_DESC[NsQuote.SYM_VOLUME] +":" +
                  str(NsQuote.GetQuote(Index,NsQuote.SYM_VOLUME)))
        if(Index == Ind1):
            print("Sym:2330,Last:" + str(NsQuote.GetQuote(Index,NsQuote.SYM_LAST)) + 
                  ",Vol:" + str(NsQuote.GetQuote(Index,NsQuote.SYM_VOLUME)))

#啟動入口
if NsQuote.Connect():
    print("連接成功")
    NsQuote.SetLoginCallback(OnLogin)
    NsQuote.SetQuoteCallback(OnQuote)
    NsQuote.Login("xxxxx", "xxxxx")
    NsQuote.Go()
else:
    print("連接失敗")
print("Over")
```
## 示例2：Tick.py<br>
```python
import PyNetstockQuoteLib as NsQuote

indTick = 0
#登錄回報
def OnLogin(Type,pInfo,InfoSize):
    if(Type == NsQuote.SMSGL_LOGIN_OKAY):
        print("登錄成功!")
    else:
        print("登錄失敗")        
#Tick回報
def OnTick(Index):
    #獲得Tick數據,返回字典列表迭代器
    TickList = NsQuote.GetTick(Index)
    #獲得Tick總數
    Ticklen = len(TickList)
    count = 0
    #用基本資料獲取商品名稱
    print(NsQuote.GetInfo(NsQuote.EXC_FITXN,"FITX","name"))
    #顯示本次Tick信息
    print("Index:" + str(Index) + " TickCount:",str(Ticklen))
    #顯示10條數據用于測試
    for Item in TickList:
        print(Item)#Item是字典對象，實際使用時可以用Key取單元數據例如：Item["Last"]
        count = count + 1
        if (count >=10): break
#交易日資訊回報
def OnExcInfo(Type,Exc):
    if(Exc == NsQuote.EXC_FITXN):#收到對應市場交易日資訊后注冊（較為單純）
        global indTick
        indTick = NsQuote.SubscribeToTick("FITX",NsQuote.EXC_FITXN,NsQuote.EXC_FITX_T2)
#程序入口
if NsQuote.Connect():
    print("連接成功")
    NsQuote.SetLoginCallback(OnLogin)
    NsQuote.SetExcInfoCallback(OnExcInfo)
    NsQuote.SetTickCallback(OnTick)
    NsQuote.Login("xxxxx", "xxxxx")
    NsQuote.Go()
else:
    print("連接失敗")
print("Over")
```