群益API 批次呼叫”大量”報價

Steven Wang
Aug 9, 2017 · 4 min read

重點在於分配股票代號
把股票代號 分配至 不同的頁碼intPage之中 每個頁碼不能超過100個股票代號

Private Sub btnRequest_Click()
‘Call sk.skQ.SKQuoteLib_RequestStocks(1, “2330”)

Dim rng As Range
Dim intPage As Integer ‘頁碼 1~49
Dim strStock As String
Dim intN As Integer ‘頁碼其中的個數

intPage = 1 ‘群益規定Page=1開始
intN = 0 ‘就算不指定 excel預設也是0 但是寫0是好習慣

[b10:g1761].ClearContents


‘把股票代號 分配至 不同的頁碼之中 每個頁碼不能超過100個股票代號
For Each rng In [a10:a1761]
If intN = 0 Then
strStock = rng.Value
Else
strStock = strStock & “,” & rng.Value
End If
intN = intN + 1

If intN = 100 Then
Call sk.skQ.SKQuoteLib_RequestStocks(intPage, strStock)
intPage = intPage + 1
intN = 0
End If
Next

If intN > 0 Then ‘剩下不足100的代號處理呼叫報價
Call sk.skQ.SKQuoteLib_RequestStocks(intPage, strStock)

End If

End Sub

接下來的問題是

有好多代號 怎確定報價個股的位置

再去做該位置的報價資訊更新填入

方法一 Excel函式 MATCH

調整好儲存格格式 相同格式才能找得到(文字、數字)

isunmber(A10) 檢查A10儲存格是否為數字
TEXT(A10) 將A10儲存格轉為文字

效率差 讀出位置 計算 再寫入

Private Sub skQ_OnNotifyQuote(ByVal sMarketNo As Integer, ByVal sStockIdx As Integer)
‘[sample]通知有新報價OnNotifyQuote

Dim stockA As SKCOMLib.SKSTOCK

‘接收傳回SKQuoteLib_GetStockByIndex
Call sk.skQ.SKQuoteLib_GetStockByIndex(sMarketNo, sStockIdx, stockA)

‘有好多代號 怎確定報價個股的位置 在去做該位置的報價資訊更新填入

Dim intRow As Integer

‘[方法1 效率不好 寫出位置至a5 再繼續計算位置填入更新資料]
‘sheet1.[a5] = stockA.bstrStockNo
‘intRow = sheet1.[a6] + 10–1

‘[方法2]
intRow = WorksheetFunction.Match(stockA.bstrStockNo, sheet1.[a10:a1761], 0) + 10–1
‘worksheetfunction是命名空間 內有excel所有函式可以呼叫使用

sheet1.Cells(7, 1) = intRow

‘填入報價資訊
sheet1.Cells(intRow, 2).Value = stockA.bstrStockName
sheet1.Cells(intRow, 3).Value = stockA.nClose / 10 ^ stockA.sDecimal
sheet1.Cells(intRow, 4).Value = stockA.nBid
sheet1.Cells(intRow, 5).Value = stockA.nBc
sheet1.Cells(intRow, 6).Value = stockA.nAsk
sheet1.Cells(intRow, 7).Value = stockA.nAc

End Sub

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade