綠茶妹 發表於 10-1-5 23:20

Access+VBA的台股選股程式

說明:
資料來源
http://www.wretch.cc/blog/eagle99/11079297

原作:黃魯克
他說程式可以散布。
我改了好幾版,有一陣子可以用,後來又變的怪怪的。
把檔案放上來,
有興趣可以研究看看。
它會自動抓證交所和OTC的檔案。
然後切一切放進資料庫,最後可用查詢來把你要的檔案找出來。

既然amibroker的選股能力這麼強,
我想這個程式的用途可能就給大家參考一下怎麼parse 證交所及OTC的資料吧。

==========================================
980113-股票程式-轉入上市櫃檔案V2
本程式將每日由證交所與櫃買中心下載的csv檔直接轉入Access資料庫,因為該csv檔並非固定欄位格式,所以必須由程式自動判斷處理。本程式為全自動,不需手動修改原檔案即可匯入。此部分程式可以獨立出來供各位網友使用。有興趣者請下載 stkbb.zip
該壓縮檔內附csv檔,打開mdb後,直接在[巨集]執行[匯入上市櫃盤後行情],記住先將csv檔COOY到程式下子目錄import。
本程式可一次處理多個csv檔,記住每個上櫃的csv檔都必須以RSTA開頭,每個上市的csv檔必須以A11開頭,用來區分上市與上櫃(兩者格式不同)
你必須先將csv檔copy 到程式下/import子目錄,處理完後檔案會移到/complete子目錄,你可以將上市與上櫃的檔案同時放入/import中同時處理。
本程式同時更新股票代碼檔。
「每日盤後」資訊可在證交所與櫃買中心免費取得。如下:
上市
證券交易所 http://www.twse.com.tw/
首頁 > 交易資訊 > 盤後資訊 >每日收盤行情
櫃檯
台灣櫃買中心 http://www.gretai.org.tw/ch/index.php
首頁 > 上櫃股票交易資訊 > 盤後資訊 > 上櫃股票行情


--------------------------------------------------------------------------------
Function importDaily()
    Call A11Import
    Call RSTAImport
End Function

--------------------------------------------------------------------------------
Function RSTAimp1(source)
' --- 匯入一個上櫃csv檔到主檔stk
Dim fs2 As New Scripting.FileSystemObject
Dim RsStk As Recordset
Dim RsStkId As Recordset
Dim inpFi As TextStream
Dim lines As Integer
Dim s1 As String
Dim yy As String, mm As String, dd As String
Dim sdate As String
Dim aa
Dim i As Integer
Dim s2 As String
Dim isdel As Boolean
Dim ch
Set inpFi = fs2.OpenTextFile(source)
Set RsStk = CurrentDb.OpenRecordset("select * from stk")
lines = 0
Do While Not inpFi.AtEndOfStream
    lines = lines + 1
    s1 = inpFi.ReadLine
    If lines = 1 Then
      yy = Mid(s1, 1, 2)
      mm = Mid(s1, InStr(s1, "年") + 1, 2)
      dd = Mid(s1, InStr(s1, "月") + 1, 2)
      sdate = (1911 + yy) & "/" & mm & "/" & dd
      Debug.Print sdate
    End If
    If InStr(s1, "上櫃家數") <> 0 Then
      Exit Do
    End If
    If lines >= 3 Then
            '**************************************
            isdeli = False
            s2 = ""
            For i = 1 To Len(s1)
                ch = Mid(s1, i, 1)
                If Mid(s1, i, 1) = """" Then
                  isdeli = Not isdeli
                Else
                  If isdeli Then
                        If ch <> "," Then
                            s2 = s2 & ch
                        End If
                  Else
                        s2 = s2 & ch
                  End If
                End If
            Next
            s2 = Replace(s2, "--", "00")
            'Debug.Print s2
            '*************************************
   
      aa = Split(s2, ",")
      If UBound(aa) < 9 Then
            Debug.Print s1
      ElseIf Len(aa(0)) = 4 Then
            '代號0,名稱1,收盤2 ,漲跌3,開盤4 ,最高5 ,最低6,均價7 ,成交股數8
            RsStk.AddNew
            RsStk("dte") = CDate(sdate)
            RsStk("stockid") = aa(0)
            RsStk("price") = aa(2)
            RsStk("p_open") = aa(4)
            RsStk("p_high") = aa(5)
            RsStk("p_low") = aa(6)
            s2 = Replace(aa(8), ",", "")
            s2 = Replace(s2, """", "")
            RsStk("vol") = s2
            RsStk.Update
                '--- 同時更新股票代號檔
                Set RsStkId = CurrentDb.OpenRecordset("select * from stkid where stockid='" & aa(0) & "'")
                If RsStkId.EOF Then
                  DoCmd.RunSQL ("insert into stkid values('" & aa(0) & "','" & aa(1) & "','2')")
                End If
            
      End If
    End If
Loop
RsStk.Close
Debug.Print source
End Function

--------------------------------------------------------------------------------
Function RSTAImport()
'--- 將dirImport內的所有 RSTA*.csv上櫃檔匯入到主檔 stk,同時將該檔移到 dircomplete
Dim fs As New Scripting.FileSystemObject
Dim outFi As TextStream, errFi As TextStream
Dim dirImport As String, dirComplete As String
Dim fi As File
Dim strRoot As String
Dim fo_root As Folder
Dim fo_Dir As Folder
Dim ext As String
Dim source As String, Target As String, stockId As String
strRoot = CurrentProject.path
dirImport = strRoot & "\import"
dirComplete = strRoot & "\complete"
Set outFi = fs.CreateTextFile(strRoot & "\" & "log.txt", True)
Set errFi = fs.CreateTextFile(strRoot & "\" & "errlog.txt", True)
Set fo_root = fs.GetFolder(strRoot)
If Not fs.FolderExists(dirImport) Then
    MsgBox "請將欲轉入之xls 放在" & dirImport
    Exit Function
End If
If Not fs.FolderExists(dirComplete) Then
    fs.CreateFolder (dirComplete)
End If
Set fo_Dir = fs.GetFolder(dirImport)
For Each fi In fo_Dir.Files
    ext = Mid(fi.Name, InStr(fi.Name, "."), 4)
    If Left(fi.Name, 4) = "RSTA" And LCase(ext) = ".csv" Then
      Debug.Print fi.Name
      source = fi.path
      Target = dirComplete & "\" & fi.Name
      fs.CopyFile source, Target, True
      Call RSTAimp1(source)
      fs.DeleteFile source
    End If
Next
outFi.Close
errFi.Close
Debug.Print "Done!RSTA*.csv Import" & vbCrLf & " 檔案已經搬移到" & dirComplete
End Function


--------------------------------------------------------------------------------
Function A11imp1(source)
' --- 匯入一個上櫃csv檔到主檔stk
Dim fs2 As New Scripting.FileSystemObject
Dim RsStk As Recordset
Dim RsStkId As Recordset
Dim inpFi As TextStream
Dim s1 As String
Dim yy As String, mm As String, dd As String
Dim sdate As String
Dim aa
Dim i As Integer
Dim s2 As String
Dim isBeginAs Boolean
Dim isdelim As Boolean
Dim lines, ch

Set inpFi = fs2.OpenTextFile(source)
Set RsStk = CurrentDb.OpenRecordset("select * from stk")
isBegin = False
lines = 0
Do While Not inpFi.AtEndOfStream
    s1 = inpFi.ReadLine
    If Not isBegin And InStr(s1, "每日收盤行情") <> 0 Then
      isBegin = True
      yy = Mid(s1, 1, 2)
      mm = Mid(s1, InStr(s1, "年") + 1, 2)
      dd = Mid(s1, InStr(s1, "月") + 1, 2)
      sdate = (1911 + yy) & "/" & mm & "/" & dd
      Debug.Print sdate
      inpFi.SkipLine      ' 標題欄
    ElseIf isBegin Then
         
      '--- 解決字串中的,號,如"200,450,000"==> 200450000
      If Mid(s1, 1, 1) > "0" Then
            lines = lines + 1
            'If lines > 5 Then
            '    Exit Do
            'End If
            '**************************************
            isdeli = False
            s2 = ""
            For i = 1 To Len(s1)
                ch = Mid(s1, i, 1)
                If Mid(s1, i, 1) = """" Then
                  isdeli = Not isdeli
                Else
                  If isdeli Then
                        If ch <> "," Then
                            s2 = s2 & ch
                        End If
                  Else
                        s2 = s2 & ch
                  End If
                End If
            Next
            s2 = Replace(s2, "--", "00")
            'Debug.Print s2
            '*************************************
            aa = Split(s2, ",")
            If UBound(aa) < 8 Or Len(aa(0)) > 4 Then
                'Debug.Print s2
            Else
            
            '證券代號0,證券名稱1,成交股數2,成交筆數3,成交金額4,開盤價5,最高價6,最低價7,收盤價8
                RsStk.AddNew
                RsStk("dte") = CDate(sdate)
                RsStk("stockid") = aa(0)
                RsStk("price") = "0" & aa(8)
                RsStk("p_open") = aa(5)
                RsStk("p_high") = aa(6)
                RsStk("p_low") = aa(7)
                s2 = Replace(aa(2), ",", "")
                s2 = Replace(s2, """", "")
                RsStk("vol") = s2
                RsStk.Update
                '--- 同時更新股票代號檔
                Set RsStkId = CurrentDb.OpenRecordset("select * from stkid where stockid='" & aa(0) & "'")
                If RsStkId.EOF Then
                  DoCmd.RunSQL ("insert into stkid values('" & aa(0) & "','" & aa(1) & "','1')")
                End If
            End If
      End If
    End If
Loop
RsStk.Close
Debug.Print source
End Function

--------------------------------------------------------------------------------
Function A11Import()
'--- 將dirImport內的所有 A11*.csv上市檔匯入到主檔 stk,同時將該檔移到 dircomplete
Dim fs As New Scripting.FileSystemObject
Dim outFi As TextStream, errFi As TextStream
Dim dirImport As String, dirComplete As String
Dim fi As File
Dim strRoot As String
Dim fo_root As Folder
Dim fo_Dir As Folder
Dim ext As String
Dim source As String, Target As String, stockId As String
strRoot = CurrentProject.path
dirImport = strRoot & "\import"
dirComplete = strRoot & "\complete"
Set outFi = fs.CreateTextFile(strRoot & "\" & "log.txt", True)
Set errFi = fs.CreateTextFile(strRoot & "\" & "errlog.txt", True)
Set fo_root = fs.GetFolder(strRoot)
If Not fs.FolderExists(dirImport) Then
    MsgBox "請將欲轉入之xls 放在" & dirImport
    Exit Function
End If
If Not fs.FolderExists(dirComplete) Then
    fs.CreateFolder (dirComplete)
End If
Set fo_Dir = fs.GetFolder(dirImport)
For Each fi In fo_Dir.Files
    ext = Mid(fi.Name, InStr(fi.Name, "."), 4)
    If Left(fi.Name, 3) = "A11" And LCase(ext) = ".csv" Then
      Debug.Print fi.Name
      source = fi.path
      Target = dirComplete & "\" & fi.Name
      fs.CopyFile source, Target, True
      Call A11imp1(source)
      fs.DeleteFile source
    End If
Next
outFi.Close
errFi.Close
Debug.Print "Done!A11*.csv Import" & vbCrLf & " 檔案已經搬移到" & dirComplete
End Function

End Function


sdnian 發表於 10-1-6 12:11

早上稍微試了一下, 應該可以用程式直接從證交所下載資料之後, 自動匯入 AB 裡面. 這樣就有 AB 就有台股的數據了. (盤後日K線, 非即時)

綠茶妹 發表於 10-1-6 12:16

本帖最後由 綠茶妹 於 10-1-6 12:27 PM 編輯

回復 2# sdnian


真的嗎?
太好了。
這樣子就可以轉進AB。

E大寫的這個程式,parse檔案的部份一切正常。
它還會自動更新股名表。
所以上市和上櫃的每日資料都是正確的。(但是我不會灌進AB)

但是主程式的部份(Access裡的查詢),我以前用的時候就有些問題。
例如我選出接近20日最高價的股票,正常。
但是,當我要篩選接近40日最高價的股票時,
選股結果出不來。

sdnian 發表於 10-1-6 13:29

回復 3# 綠茶妹


我沒有用上面這個程式, 因為我自己之前就有寫過一個程式可以從證交所取得盤後每日交易的數據. 我想如果數據能進到 AB, 那就可以做自己想做的事.. 選股, 回測.. 等等. 那既然能用 AB, 你提到 Access 的部分就不是問題了, AB 取代 Access 絕對沒問題. 我目前的想法是... 如果寫一個程式來自動匯入或更新 AB 裡每日的交易資料, 這樣應該很方便, 類似 AB 本身提供的 AmiQuote.

綠茶妹 發表於 10-1-6 13:44

嗯。那你要用什麼來寫呢?C#嗎?或VB?

sdnian 發表於 10-1-6 17:55

C#, VB 我都不會. 我愛用 Python.

我另外想到一個問題, 即使有當天的資料, 但更早的呢? 應該不適合一天一天慢慢從證交所下載吧!

綠茶妹 發表於 10-1-6 19:10

回復 6# sdnian


    是一天一天下載呀。(叫程式自動下載)
證交所有收盤的csv檔。

sdnian 發表於 10-1-7 11:57

嗯, 我再找時間來試看看.

綠茶妹 發表於 10-1-7 12:13

本帖最後由 綠茶妹 於 10-1-7 12:17 PM 編輯

ok 我今天才知道Python是什麼東西。

-------------------------------------------
Python 是什麼?
Python 是一種泛用性的動態物件導向程式語言。自 1990 年代初由 Guido van Rossum (又常被稱為 GvR 或 BDFL) 創造至今已歷十數年發展,應用於系統管理、網路管理、網路傳輸程式、網頁程式開發、數值分析程式、圖形介面應用程式等方面,均有優秀的表現。

/History 頁面簡述 Python 發展的歷史。

方便的 Python
Python 的標準程式庫豐富強大,「能量充沛」(batteries included)。

Python 的用途廣泛,使用者來自各個領域。在 Python Cheeseshop 裡,你可以找到符合各種需要的套件模組。

Python 不像 Java 有商業級的宣傳,但能寫出更加簡潔清晰的程式碼,發揮程式員的生產力,提昇軟體專案的成功率。在 Python & Java: A Side-by-Side Comparison 文章裡,研究顯示 Python 比 Java 普遍具備五倍以上的生產力。

/IDE 頁面簡述 PythonIDE 清單。

快速的 Python
Python 程式執行的速度,在常用的動態語言 (PHP, Perl, Ruby, etc.) 中是數一數二快的。

跨平台的 Python
Python 可以執行在 Windows、Mac OS X、Linux 等常見的作業系統平台和其它較少使用的作業系統上,也可以在 Java 和 .Net 環境中執行。

另外,除了最普及的 Windows CE PDA 之外,Nokia S60 系列手機上也可以執行 Python 語言喔。

高彈性的 Python
Python 以它的「膠著力」聞名,被稱作「膠水語言」 (Python as a glue),多年來都與 C/C++ 合作愉快。網路遊戲「星戰前夜 (EVE)」用它與 C++ 合作,打造成功商業範例。知名的戰略遊戲「火線交鋒 (Act of War)」也使用 Python 作為連線對戰介面。

此外,透過 Jython,Python 能與 Java 合作愉快;透過 Iron Python,Python 能與 .Net 合作愉快。Iron Python 的作者現任職於微軟,也正是 Jython 的原作者。

Python 的應用
Python 網頁開發
Python 有眾多網頁開發工具。從各式各樣的模板到框架級的 Django、TurboGears 與 Pylons,提供了優良的網頁開發支援。

如果你在網頁開發上的功能需求超越了框架的能力,請考慮 Zope: Z Object Publishing Environment,萬用的網頁應用程式伺服器。同時,你應該也會對 Python/Zope 下強大的網頁式內容管理系統 Plone 感興趣。

Python 支援各種資料庫。sqlite、MySQL、PostgresSQL、Oracle、MSSQL、FireBird 等等都沒問題。

Python 有極好的 SQL wrapper:SQLAlchemy,幫助我們用物件導向的方式存取資料庫。

以 Python 開發的知名軟體
愈來愈愛歡迎的原始碼管理程式 Trac 是用 Python所開發的。

常用的 wiki 引擎 MoinMoin、強大的應用程式伺服器 Zope,以及最常用的 mailing list 軟體 Mailman 也是用 Python 所開發出來的。

分散式版本控制系統 Mercurial 及 Bazaar 也使用Python開發。

Bittorrent (BT) 最早的主要版本 (mainline) 版本6.0以前都是用 Python 開發的。

以 Python 開發的知名服務
你知道嗎?

NASA 用 Python 計算衛星軌道。

大家常用的 YouTube 網站,大部分使用 Python 語言開發。

Google 用 Python 語言撰寫網路爬蟲 (crawler) 與許多其它服務;Guido van Rossum 在 2006 年加入 Google。

其它以 Python 開發的系統
Matplotlib:類似 Matlab 的自由工程計算/繪圖軟體。

SalStat Statistics:類似 SPSS 的自由統計軟體。

Bibus Bibliographic software:書目資料庫,像 Endnote 一樣是寫論文的好幫手。

Straw:一個好用的RSS閱讀器。

Cankiri:Linux 上的畫面錄製軟體。

PiTiVi:非線性影音剪輯。

Python 還可以作什麼?
NASA 使用 Python 計算衛星軌道,那麼離用來飛太空梭也不遠了?

讓我們來看看官方對 Python 使用領域的說明。

聽聽 Python 愛好者怎麼說
Thinker:我最愛用的語言!

timchen119:一種易學易用,鼓勵使用者開發易讀程式碼的優雅語言。

yungyuc:{OK}

marr:像初吻般的感動。

gasolin:學 Python 讓我寫其他語言程式時更清晰。

DrakeGuan:看到同事開始使用我用 wxPython 寫的程式,心情實在非常的 high。

sdnian 發表於 10-1-7 21:52

ok 我今天才知道Python是什麼東西。

綠茶妹 發表於 10-1-7 12:13 PM http://coco-in.net/images/common/back.gif


    綠茶妹會不會太閒了? 還去查這些資料.. 用什麼語言不重要, 能叫電腦替我們工作就好了啊.. 最終是要賺到錢, 而不是想學怎麼設計程式啊!! 這裡是 COCO研究院, 又不是程式設計俱樂部.

綠茶妹 發表於 10-1-7 22:18

回復 10# sdnian


    呵呵~~說的有道理。我查了一下,就順手放上來。
    我想做個自動抓檔的程式,自己去期交所抓資料,不然一個一個下載好麻煩。

sdnian 發表於 10-1-8 09:52

這幾天我應該會抽空寫一個.. 如果有人想用我再放上來. 先說好, 我的程式很爛, 因為我的重點不在寫程式, 我是想研究如何在股市賺錢, 所以我不會想把時間都浪費在程式上.

綠茶妹 發表於 10-1-8 09:54

回復 12# sdnian


    我們這裡只研究錢,呵呵。感謝。

sdnian 發表於 10-1-10 14:21

回復 13# 綠茶妹


    程式寫好了, 綠茶妹要幫忙測試功能是否正常嗎?

綠茶妹 發表於 10-1-10 17:08

回復 14# sdnian


強!
{:8_527:}

好啊,我來測一下。

只要有AmiBroker就可以測了嗎?
有搶先試用的感覺真好,
頁: [1] 2
查看完整版本: Access+VBA的台股選股程式