- 帖子
- 523
- 主題
- 56
- 精華
- 0
- 積分
- 601
- 點名
- 85
- 作業系統
- win 10
- 軟體版本
- []
- 閱讀權限
- 50
- 性別
- 男
- 註冊時間
- 2013-3-19
- 最後登錄
- 2025-4-11
           
|
4#
發表於 2020-4-12 17:42
| 只看該作者
回復 2# joey0415
試作如下,請 joey0415 大參考斧正,謝謝:
Option Base 1
'myArr() 的欄位: 日期、開、高、低、收、漲、漲%、張數
'鉅亨資料列 :日期、開、高、低、收、張數
Sub 鉅亨歷史K_Test1()
Dim stockno$, myTEXT, myText1
Dim sh As Worksheet
Dim T!, recCAT$, i%, j%, k%, n%, Trade%, BarCntReq%
Dim myXML As Object, URL$, myArr
Dim StartDate&, EndDate&
Const Dat% = 1, Op% = 2, Hi% = 3, Low% = 4, Klose% = 5, CHG% = 6, CHGpercent% = 7, Vol% = 8 'for myArr
StartDate = DateToUnixTime("2020/01/02")
EndDate = DateToUnixTime(Format(Date, "yyyy/mm/dd"))
Application.DisplayStatusBar = True
Application.StatusBar = stockno & " 連網中... "
Set myXML = CreateObject("WinHttp.WinHttpRequest.5.1")
recCAT = "D" '日線圖
URL = "https://ws.api.cnyes.com/charting/api/v1/history?resolution=" & recCAT & "&symbol=TWS:2330:STOCK&from=" & EndDate & "&to=" & StartDate
T = timer
With myXML
.Open "GET", URL, False
.send
Do While .Status <> 200
DoEvents
If timer - T > 3 Then Exit Do
Loop
myTEXT = .responsetext '文字串 .txt
End With
Set myXML = Nothing
If myTEXT = "" Then GoTo Exit_Sub
myTEXT = Split(myTEXT, ":")
myText1 = Split(Replace(Replace(myTEXT(5), "[", ""), "]", ""), ",")
n = UBound(myText1): ReDim myArr(n + 1, 8)
For i = 0 To UBound(myText1) - 1
myArr(i + 1, 1) = UnixTime2Date(myText1(i))
Next
k = 4
For j = 6 To 10 '開、高、低、收、張數
myText1 = Split(myTEXT(j), ",")
If j = 10 Then k = 2 '控制 myArr 的行數(column), 第8行是張數
For i = 0 To n - 1
myArr(i + 1, j - k) = myText1(i)
Next
myArr(1, j - k) = Replace(myArr(1, j - k), "[", ""): myArr(n, j - k) = Replace(myArr(n, j - k), "]", "")
Next j
For i = 1 To n - 1
myArr(i, 6) = myArr(i, 5) - myArr(i + 1, 5): myArr(i, 7) = Format(myArr(i, 6) / myArr(i + 1, 5) * 100, "#0.00") '漲跌、漲跌%
Next
Exit_Sub:
Set myXML = Nothing
Application.StatusBar = ""
Application.DisplayStatusBar = False
End Sub
Function DateToUnixTime(dstring) As Long
DateToUnixTime = (DateValue(dstring) - #1/1/1970 8:00:00 AM#) * 86400
End Function
Function UnixTime2Date(UnixT) As Date
UnixTime2Date = Format(UnixT / 86400 + #1/1/1970 8:00:00 AM#, "yyyy/mm/dd")
End Function |
|