2017-09-26 126 views
0

概述
我使用www.quandl.com免費的財務數據,試圖預測資產價格變動使用URLMON API函數從Quandl.com數據下載到訪問數據庫,用VBA

方法
我已經構建了一個函數來使用quandl API下載數據。我正在聲明位於urlmon.dll系統32文件夾中的Windows API函數。

代碼

Option Explicit 

#If VBA7 Then 
    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (_ 
     ByVal pCaller As LongPtr, _ 
     ByVal szURL As String, _ 
     ByVal szFileName As String, _ 
     ByVal dwReserved As LongPtr, _ 
     ByVal lpfnCB As LongPtr) As LongPtr 
#Else 
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownLoadToFileA" (_ 
     ByVal pCaller As Long, _ 
     ByVal szURL As String, _ 
     ByVal szFileName As String, _ 
     ByVal dwReserved As Long, _ 
     ByVal lpfnCB As LongPtr) As Long 
#End If 

Sub DownloadSingleFile() 

    Dim FileURL As String 
    Dim DestinationFile As String 

    FileURL = "https://www.quandl.com/api/v3/datasets/WIKI/FB/data.csv?" 
    DestinationFile = "C:\Users\hueve\Desktop\TheSystem\Fb.csv" 

    URLDownloadToFile 0, FileURL, DestinationFile, 0, 0 

End Sub 

問題
此代碼的工作,這將數據下載到正確的文件的目標,我想知道如果有一種方法,而不是直接下載到文件位置;直接運行它到Access數據庫表?我知道該函數明確指出它直接下載到一個文件,但是直接訪問Access DB將是一件好事。此外,我知道旁邊沒有這些API函數,所以請放輕鬆

+0

使用[DoCmd.TransferText]( https://msdn.microsoft.com/en-us/vba/access-vba/articles/docmd-transfertext-method-access)將csv導入Access表。完成後,刪除csv文件。 – Parfait

+0

我看過這樣做,那將是我採取的一切路線失敗的路線。我主要關心的是我計劃下載10,000個或更多數據集到多個dbs的時間,但我不確定Docmd.transfettext的速度如何? – ZacAttack

+0

這似乎是一個額外的步驟,創建一個文件,然後刪除它? – ZacAttack

回答

1

這裏是拉動一個數據點陣列(52周的表現)代碼:

' save 52 week performance for any scored quarter not saved yet 
Set rs = CurrentDb.OpenRecordset("Select * from qryQuarterlyStockDataMissing") 
If Not rs.EOF Then 
    ' some 52 week performance scores for scored companies are missing. 
    rs.MoveLast 
    rs.MoveFirst 
    intI = rs.RecordCount 
    Do While rs.EOF = False 
     StatBar_Msg "Updating 52 Week Performance Data for " & intI & " scored periods..." 
     strLink = GetQuandl52WeekPerformanceLink(rs![Ticker], rs![Active_Period]) 
     dbl52WeekPerformance = Nz(GetQuandl52WeekPerformance(strLink), "NULL") 
     strSQL = "INSERT INTO tblQuarterlyStockData (SDF_Details_ID, 52WeekPerformance, QuandlLink) " & _ 
       "VALUES(" & rs![SDF_Details_ID] & "," & CStr(dbl52WeekPerformance) & _ 
       ",'" & strLink & "')" 
     CurrentDb.Execute strSQL 
     rs.MoveNext 
     intI = intI - 1 
    Loop 
    rs.Close 
    Set rs = Nothing 
End If 

Public Function GetQuandl52WeekPerformanceLink(strTicker As String, dtDate As Date) 
Dim strLink As String 
Dim strStartDate As Date 
Dim strEndDate As Date 
Dim strResponse As String 
Dim objHttp As Object 
Dim LArray() As String 
Dim dtEndDate As Date 
Dim dtStartDate As Date 

' find nearest weekday date 
dtEndDate = GetNearestStockDay(dtDate) 
dtStartDate = dtEndDate - 367 ' make it slightly more than a year in case the previous year date falls on a Sunday 

GetQuandl52WeekPerformanceLink = "https://www.quandl.com/api/v3/datasets/WIKI/" & strTicker & _ 
      ".csv?column_index=4&start_date=" & Format(dtStartDate, "yyyy-mm-dd") & _ 
      "&end_date=" & Format(dtEndDate, "yyyy-mm-dd") & "&collapse=annual&transform=rdiff&api_key=ryCL1ih7fJ1eTH8y9U7E" 
End Function 



Public Function GetQuandl52WeekPerformance(strLink As String) 
Dim strResponse As String 
Dim objHttp As Object 
Dim LArray() As String 

Set objHttp = CreateObject("MSXML2.XMLHTTP") 

objHttp.Open "Get", strLink, False 
objHttp.send 
strResponse = objHttp.responseText 
Set objHttp = Nothing 

LArray = Split(strResponse, ",") 

GetQuandl52WeekPerformance = Null 
If LArray(0) = "code" Then 
    ' no data returned 
Else 
    If Len(strResponse) > 12 Then 
     GetQuandl52WeekPerformance = LArray(2) 
    Else 
     ' This stock doesn't have a full year's worth of data 
    End If 
End If 
End Function 

Public Function GetNearestStockDay(dtDate As Date) As Date 
If Weekday(dtDate) = 1 Then 
    GetNearestStockDay = dtDate - 2 
ElseIf Weekday(dtDate) = 7 Then 
    GetNearestStockDay = dtDate - 1 
Else 
    GetNearestStockDay = dtDate 
End If 
End Function