頁:
[1]
Excel VBA 撰寫 即時股票看盤系統 (二) 主程式
不好意思 , 時隔 3 個多月 , 下面 List 主程式 :Sub 個股即時股價_上市()
' stock_list = "tse_1101.tw|tse_2317.tw|tse_2448.tw|tse_3037.tw|otc_1264.tw"
stock_list = ""
For K = 12 To 39 Step 4
str33 = CStr(Cells(1, K))
If Len(str33) = 4 Then
If stock_list = "" Then
stock_list = "tse_" & str33 & ".tw"
Else
stock_list = stock_list & "|tse_" & str33 & ".tw"
End If
End If
Next K
str31 = "https://mis.twse.com.tw/stock/api/getStockInfo.jsp?json=1&delay=0&ex_ch=" & stock_list
Set IE = CreateObject("internetexplorer.application") ' 使用此方式可以免除 "設定引用項目"
With IE
.Visible = False ' True 為開啟 ie, False 為不開啟 ie
.Navigate str31
Do While .ReadyState <> 4 ' 等待網頁開啟
DoEvents
Loop
.ExecWB 17, 2 ' Select All
.ExecWB 12, 2 ' Copy selection
Range("A21").Select
ActiveSheet.PasteSpecial Format:="HTML", link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
End With
IE.Quit
FileD = ActiveWorkbook.Path & "\下載\Temp.d"
str22 = Trim(CStr(Cells(21, 1)))
Range("A21").ClearContents
h22 = Len(str22)
If h22 > 10 Then
Open FileD For Output As #1
h1 = 1
While h1 < h22
h2 = InStr(h1, str22, "}")
LW = Mid$(str22, h1, h2 - h1 + 1)
If Mid$(LW, 1, 1) = "{" Then Print #1, LW
h1 = h2 + 2
Wend
Close #1
Open FileD For Input As #1
For K = 12 To 39 Step 4
str33 = CStr(Cells(1, K))
If Len(str33) = 4 Then
Line Input #1, LW
cs5 = ""
cs7 = ""
cs10 = ""
cs11 = ""
cs12 = ""
cs13 = ""
cs14 = ""
cs15 = ""
cs16 = ""
h1 = 0
h2 = InStr(h1 + 1, LW, ",")
While h2 > 0
strH = Mid$(LW, h1 + 1, h2 - h1 - 1)
If Mid$(strH, 4, 1) = ":" Then
If Mid$(strH, 2, 1) = "d" Then ' 最近交易日期(YYYYMMDD)
cs3 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "w" Then ' 跌停價
cs4 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "v" Then ' 累積成交量
cs5 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "u" Then ' 漲停價
cs6 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "z" Then ' 當盤成交價
cs7 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "y" Then ' 昨收
cs8 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "t" Then ' 最近成交時刻(HH:MI:SS)
cs9 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "o" Then ' 開盤
cs10 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "h" Then ' 最高
cs11 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "l" Then ' 最低
cs12 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "b" Then ' 揭示買價(從高到低,以_分隔資料)
cs13 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "g" Then ' 揭示買量(配合b,以_分隔資料)
cs14 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "a" Then ' 揭示賣價(從低到高,以_分隔資料)
cs15 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "f" Then ' 揭示賣量(配合a,以_分隔資料)
cs16 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "c" Then ' 證券代號
cs17 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "n" Then ' 公司簡稱
cs18 = Mid$(strH, 6, Len(strH) - 6)
End If
End If
h1 = h2
h2 = InStr(h1 + 1, LW, ",")
Wend
strH = Mid$(LW, h1 + 1, Len(LW) - h1 - 1)
If Mid$(strH, 4, 1) = ":" Then
If Mid$(strH, 2, 1) = "d" Then ' 最近交易日期(YYYYMMDD)
cs3 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "w" Then ' 跌停價
cs4 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "v" Then ' 累積成交量
cs5 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "u" Then ' 漲停價
cs6 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "z" Then ' 當盤成交價
cs7 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "y" Then ' 昨收
cs8 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "t" Then ' 最近成交時刻(HH:MI:SS)
cs9 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "o" Then ' 開盤
cs10 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "h" Then ' 最高
cs11 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "l" Then ' 最低
cs12 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "b" Then ' 揭示買價(從高到低,以_分隔資料)
cs13 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "g" Then ' 揭示買量(配合b,以_分隔資料)
cs14 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "a" Then ' 揭示賣價(從低到高,以_分隔資料)
cs15 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "f" Then ' 揭示賣量(配合a,以_分隔資料)
cs16 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "c" Then ' 證券代號
cs17 = Mid$(strH, 6, Len(strH) - 6)
ElseIf Mid$(strH, 2, 1) = "n" Then ' 公司簡稱
cs18 = Mid$(strH, 6, Len(strH) - 6)
End If
End If
If Val(cs7) = 0 Then
W5 = ""
ElseIf Val(cs7) = Val(cs8) Then
W5 = "0"
ElseIf Val(cs7) = Val(cs6) Then
W5 = "▲" & Round(Val(cs7) - Val(cs8), 2)
ElseIf Val(cs7) = Val(cs4) Then
W5 = "▼" & Round(Val(cs8) - Val(cs7), 2)
ElseIf Val(cs7) > Val(cs8) Then
W5 = "△" & Round(Val(cs7) - Val(cs8), 2)
ElseIf Val(cs7) < Val(cs8) Then
W5 = "▽" & Round(Val(cs8) - Val(cs7), 2)
Else
W5 = ""
End If
' 成交 當盤 累積
' 代號 日期 時刻 漲停 昨收 跌停 開盤 最高 最低 價 量 漲跌 公司簡稱
' Write #2, cs17, cs3, cs9, cs6, cs8, cs4, cs10, cs11, cs12, cs7, cs5, W5, cs13, cs14, cs15, cs16, cs18
' W1 W2 W3 W4 W5 W6 W7 W8 W9 W10 W11 W12 W13 W14 W15 W16
Cells(2, K) = cs9
Cells(3, K + 1) = cs10
Cells(4, K + 1) = cs11
Cells(5, K + 1) = cs12
Cells(6, K + 1) = cs7
Cells(4, K + 3) = cs6
Cells(5, K + 3) = cs4
Cells(6, K + 3) = cs8
If cs13 <> "" Then
h1 = InStr(1, cs13, "_")
h2 = InStr(h1 + 1, cs13, "_")
h3 = InStr(h2 + 1, cs13, "_")
h4 = InStr(h3 + 1, cs13, "_")
h5 = InStr(h4 + 1, cs13, "_")
Cells(8, K) = Mid$(cs13, 1, h1 - 1)
Cells(9, K) = Mid$(cs13, h1 + 1, h2 - h1 - 1)
Cells(10, K) = Mid$(cs13, h2 + 1, h3 - h2 - 1)
Cells(11, K) = Mid$(cs13, h3 + 1, h4 - h3 - 1)
Cells(12, K) = Mid$(cs13, h4 + 1, h5 - h4 - 1)
End If
If cs14 <> "" Then
h1 = InStr(1, cs14, "_")
h2 = InStr(h1 + 1, cs14, "_")
h3 = InStr(h2 + 1, cs14, "_")
h4 = InStr(h3 + 1, cs14, "_")
h5 = InStr(h4 + 1, cs14, "_")
Cells(8, K + 1) = Mid$(cs14, 1, h1 - 1)
Cells(9, K + 1) = Mid$(cs14, h1 + 1, h2 - h1 - 1)
Cells(10, K + 1) = Mid$(cs14, h2 + 1, h3 - h2 - 1)
Cells(11, K + 1) = Mid$(cs14, h3 + 1, h4 - h3 - 1)
Cells(12, K + 1) = Mid$(cs14, h4 + 1, h5 - h4 - 1)
End If
If cs15 <> "" Then
h1 = InStr(1, cs15, "_")
h2 = InStr(h1 + 1, cs15, "_")
h3 = InStr(h2 + 1, cs15, "_")
h4 = InStr(h3 + 1, cs15, "_")
h5 = InStr(h4 + 1, cs15, "_")
Cells(8, K + 2) = Mid$(cs15, 1, h1 - 1)
Cells(9, K + 2) = Mid$(cs15, h1 + 1, h2 - h1 - 1)
Cells(10, K + 2) = Mid$(cs15, h2 + 1, h3 - h2 - 1)
Cells(11, K + 2) = Mid$(cs15, h3 + 1, h4 - h3 - 1)
Cells(12, K + 2) = Mid$(cs15, h4 + 1, h5 - h4 - 1)
End If
If cs16 <> "" Then
h1 = InStr(1, cs16, "_")
h2 = InStr(h1 + 1, cs16, "_")
h3 = InStr(h2 + 1, cs16, "_")
h4 = InStr(h3 + 1, cs16, "_")
h5 = InStr(h4 + 1, cs16, "_")
Cells(8, K + 3) = Mid$(cs16, 1, h1 - 1)
Cells(9, K + 3) = Mid$(cs16, h1 + 1, h2 - h1 - 1)
Cells(10, K + 3) = Mid$(cs16, h2 + 1, h3 - h2 - 1)
Cells(11, K + 3) = Mid$(cs16, h3 + 1, h4 - h3 - 1)
Cells(12, K + 3) = Mid$(cs16, h4 + 1, h5 - h4 - 1)
End If
If W5 <> "" Then
Cells(2, K + 3) = W5
If Mid$(W5, 1, 1) = "△" Or Mid$(W5, 1, 1) = "▲" Then
Cells(2, K + 3).Font.Color = -16776961
ElseIf Mid$(W5, 1, 1) = "▽" Or Mid$(W5, 1, 1) = "▼" Then
Cells(2, K + 3).Font.Color = -4165632
Else
Cells(2, K + 3).Font.ColorIndex = xlAutomatic
End If
End If
If cs9 <> "" Then
If TimeValue(cs9) >= TimeValue("9:00:00") Then
L = 25
FlagL = 0
Do While FlagL < 1
L = L + 1
If Cells(L, K) = "" Then
FlagL = 1
If L = 26 Then
Cells(L, K) = cs9
Cells(L, K + 1) = cs7
Cells(L, K + 2) = cs5
ElseIf TimeValue(cs9) > Cells(L - 1, K) Then
Cells(L, K) = cs9
Cells(L, K + 1) = cs7
Cells(L, K + 2) = Val(cs5) - Val(Cells(3, K + 3))
End If
End If
Loop
End If
Cells(1, K + 2) = Val(cs5) - Val(Cells(3, K + 3))
Cells(3, K + 3) = cs5
End If
End If
Next K
Close #1
If Dir(FileD) <> "" Then Kill (FileD)
End If
End Sub
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div> 重點啊 :
1. 找到 臺灣證券交易所 提供的個股即時股價資料
https://mis.twse.com.tw/stock/api/getStockInfo.jsp?json=1&delay=0&ex_ch=tse_1101
2. 用 VBA 將資料檔案下載在 Excel 上
Set IE = CreateObject("internetexplorer.application")
3. 讀取 分析 呈現 繪圖 , OK
缺點啊 :
1. 三不五時會被證交所視為不良企圖嫌疑行為 , 被中斷 ( 誰叫我們每秒下載人家網頁資料 )
2. Excel 跑程式時吃系統資源多 , 同時做其他事情容易掛掉 ( 功力不夠吧 )
3. 乖乖看 Yahoo 股市好...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div> 無法執行唷,變數無定義 缺點啊 :
1. 三不五時會被證交所視為不良企圖嫌疑行為 , 被中斷 ( 誰叫我們每秒下載人家網頁資料 )
2. Excel 跑程式時吃系統資源多 , 同時做其他事情容易掛掉 ( 功力不夠吧 )
3. 乖乖看 Yahoo 股市好
可以設計讓程式 休息再執行 唉 , 發表在自己的發文中 ,
在 eyny 中逛的時候要小心 ,
在某版塊某主題 , 一點進去就被扣 5 積分 ,
5 積分 , 沒有任何事前告知 ,
5 積分 , 狠 , 心痛<br><br><br><br><br><div></div> 用網頁抓資料.傳輸速度會不會比較慢阿?
還是用券商的DDE資料 應該會比較快吧.. 本帖最後由 tryit244178 於 2019-11-7 03:27 PM 編輯
MarryHenry 發表於 2019-11-6 04:57 PM static/image/common/back.gif
腿控{:47:}
害我超想看…
查了一下,好像超過9張圖就會自動扣1積分…
重新整理,再扣一次…
話說,你每秒連線一次改成每5秒連線一次。看還會不會被視為不良企圖嫌疑行為…
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div> slimb 發表於 2019-11-7 05:58 AM static/image/common/back.gif
用網頁抓資料.傳輸速度會不會比較慢阿?
還是用券商的DDE資料 應該會比較快吧.. ...
感謝 slimb 大 ,
只是對 Excel VBA 有興趣 ,
寫寫 VBA , 抓抓資料 , 練練功 ,
當然比不上專業的券商 Web , App
感謝回復 tryit244178 發表於 2019-11-7 02:05 PM static/image/common/back.gif
腿控
害我超想看…
查了一下,好像超過9張圖就會自動扣1積分…
感謝 tryit244178 大 ,
謝謝您的說明 ,
以後會小心逛 , 圖片主題就敬謝不敏了 ,
謝謝您的建議 ,
這是個好方法 ,
個人 VBA 程式功力還是很不足 ,
謝謝<br><br><br><br><br><div></div> river0127 發表於 2019-10-20 09:48 PM static/image/common/back.gif
無法執行唷,變數無定義
感謝 river0127 大 ,
謝謝您 ,
寫 VBA 有一個最大好處 ,
就是一般變數可以不用宣告 ,
當然這是一個不好的習慣 , 也是我的不良習慣 ,
當初不全然是因為懶 , 是有想增加他人看我程式的困難度 ,
就像程式中沒有多做註記說明 , 會增加程式閱讀維護困難 ,
謝謝您的回覆...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div> 本帖最後由 tryit244178 於 2019-11-9 06:14 PM 編輯
MarryHenry 發表於 2019-11-8 08:24 AM static/image/common/back.gif
感謝 river0127 大 ,
謝謝您 ,
寫 VBA 有一個最大好處 ,
未宣告的變數和未宣告資料類型的Object變數會被指派資料類型。 這可讓您快速撰寫程式, 但它可能會導致執行速度變慢。
詳細參考:
https://docs.microsoft.com/zh-tw ... fficient-data-types
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div> 好強大....初級學習中
多謝大大的分享' doda 發表於 2020-2-5 08:43 AM static/image/common/back.gif
好強大....初級學習中
多謝大大的分享'
感謝 doda 您的回覆
加油
vba 是個好工具 謝謝大大的分享..大大辛苦了<br><br><br><br><br><div></div>
頁:
[1]