【问题标题】:Pulling Historical stock data for multiple companies using YAHOO API in Excel在 Excel 中使用 YAHOO API 为多家公司提取历史股票数据
【发布时间】:2017-01-31 23:40:07
【问题描述】:

我正在使用 Excel 进行一个项目,该项目将为您提供投资组合的价值,并计算有关您的资产的其他一些有用的统计数据。 (投资组合的 st.dev、你的投资组合的 beta 等)。

我已经使用 Yahoo Finance 提取了盘中统计数据(公司名称、最后交易价格、开盘价、最高价、最低价等)。这部分非常简单,您只需在 URL 中添加符号和“+”,它就会提取每只股票的所有数据。

我想要做的是提取投资组合中所有股票的历史收盘价(不幸的是,在 URL 中添加股票代码和“+”的相同逻辑不适用于此)。

下面是我到目前为止的代码。在“Sheet1”上是投资组合持有的位置(股票代码从单元格 A2 开始并向下)。 工作表 2 将采用股票代码并将它们显示在第 2 行的顶部,并在第 1 行的上方显示每个相应股票代码的 url。

开始日期和结束日期也分别位于工作表 2 单元格 C 4 和 5 中。

目标是尝试让excel获取每个股票代码的.CSV,并在对应的列中记录收盘价。

也许我做错了,有一种更简单的方法可以获取这些数据,但我们将不胜感激。

提前谢谢你!

Private Sub btnHistoricalData_Click()

Dim W As Worksheet: Set W = ActiveSheet
Dim DataW As Worksheet: Set DataW = ActiveWorkbook.Sheets("Sheet1") ' This is where you enter the stocks in your portfolio
Dim Last As Integer: Last = W.Range("c2").End(xlToRight).Column
Dim dataLast As Integer: dataLast = DataW.Range("A2").End(xlDown).Row

'*************************************************************************************
If Last <> dataLast Then
    W.Rows(2).Clear ' clears row if values are different so correct data can be enterred into this row
End If
'*************************************************************************************
Dim i As Integer
For i = 1 To dataLast
    W.Cells(2, 3 + i).Value = DataW.Cells(1 + i, 1).Value
Next i


Dim strtDate As Date: strtDate = W.Range("B4").Value 'Starting Date
Dim endDate As Date: endDate = W.Range("B5").Value 'End Date
 '-------------------breaks down starting month, day and year to be entered into the URL -------------------
        Dim strtMonth As String: strtMonth = Month(strtDate)
        Dim strtDay As String: strtDay = Day(strtDate)
        Dim strtYear As String: strtYear = Year(strtDate)
        Dim endMonth As String: endMonth = Month(endDate)
        Dim endDay As String: endDay = Day(endDate)
        Dim endYear As String: endYear = Year(endDate)
'-------------------------------------------------------------------------------------------------------------------------------------

Dim urlStartRange As String: urlStartRange = "&a=" & strtMonth & "&b=" & strtDay & "&c=" & strtYear ' This goes into URL for start date
Dim urlEndRange As String: urlEndRange = "&d=" & endMonth & "e=" & endDay & "&f=" & endYear & "&g=d&ignore=.csv" 'this goes into the URL as end date
'-------------------------------------------------------------------------------------------------------------------------------------

'creates a string of all symbols separated by "+"
Dim urlSymbols As String
For i = 0 To dataLast
    urlSymbols = urlSymbols & W.Cells(2, 4 + i).Value & "+"
Next i
urlSymbols = Left(urlSymbols, Len(urlSymbols) - 3) 'gets rid of extra "+" values

 Dim splitUrlSymbols As Variant: splitUrlSymbols = Split(urlSymbols, Chr(43))
 For i = 0 To dataLast - 2
       W.Cells(1, 4 + i).Value = "http://ichart.finance.yahoo.com/table.csv?s=" & splitUrlSymbols(i) & urlStartRange & urlEndRange
 Next i
   'Pulls data from YAHOO Finance --------------
Dim getHttp As New WinHttpRequest
'For i = 0 To lastdata - 2 **(eventually I need to loop this request through each column for each stock enterred)**
    getHttp.Open "GET", W.Cells(1, 5).Value, False ' *********just selected 1 cell for now****************
    getHttp.Send
    Dim httpResp As String: httpResp = getHttp.ResponseText
    Dim dataLines As Variant: dataLines = Split(httpResp, vbTab)
    Dim splitDataLines As String
    Dim dataValues As Variant
    Dim x As Integer
        For x = 0 To UBound(dataLines)
            splitDataLines = dataLines(x)

           dataValues = Split(splitDataLines, ",")

        Next x
'----------------------------------------------
   ' Next i
    MsgBox (httpResp)


End Sub

【问题讨论】:

  • 上面的代码有效吗?如果是这样,您是否在问是否有更有效的方法来获取数据?如果有错误,或者没有按预期工作,出了什么问题,在哪里?
  • 上面的代码有效。我在问是否有办法获得被拉出的.csv(收盘价)文件的第三列,然后转到投资组合中的下一只股票并拉出数据(第 3 列每个 .csv 文件)
  • 当代码运行时,它会拉取一个链接(例如,股票代码 ABT 看起来像这样...ichart.finance.yahoo.com/…

标签: vba excel api stock


【解决方案1】:

想通了。

只是进行了很多拆分和循环。

不过,我确信它可以编写得更优雅。

干杯!

Dim W As Worksheet: Set W = ActiveSheet
Dim DataW As Worksheet: Set DataW = ActiveWorkbook.Sheets("Sheet1") ' This is where you enter the stocks in your portfolio
Dim Last As Integer: Last = W.Range("d2").End(xlToRight).Column
Dim dataLast As Integer: dataLast = DataW.Range("A2").End(xlDown).Row


'*************************************************************************************
If Last <> dataLast + 2 Then
    W.Rows(2).Clear ' clears row if values are different so correct data can be enterred into this row
        Dim i As Integer
For i = 1 To dataLast
    W.Cells(2, 3 + i).Value = DataW.Cells(1 + i, 1).Value
Next i
End If
'*************************************************************************************



Dim strtDate As Date: strtDate = W.Range("B4").Value 'Starting Date
Dim endDate As Date: endDate = W.Range("B5").Value 'End Date
 '-------------------breaks down starting month, day and year to be entered into the URL -------------------
        Dim strtMonth As String: strtMonth = Month(strtDate) - 1
        Dim strtDay As String: strtDay = Day(strtDate)
        Dim strtYear As String: strtYear = Year(strtDate)
        Dim endMonth As String: endMonth = Month(endDate) - 1
        Dim endDay As String: endDay = Day(endDate)
        Dim endYear As String: endYear = Year(endDate)
'-------------------------------------------------------------------------------------------------------------------------------------

Dim urlStartRange As String: urlStartRange = "&a=" & strtMonth & "&b=" & strtDay & "&c=" & strtYear ' This goes into URL for start date
Dim urlEndRange As String: urlEndRange = "&d=" & endMonth & "&e=" & endDay & "&f=" & endYear & "&g=d&ignore=.csv" 'this goes into the URL as end date
'-------------------------------------------------------------------------------------------------------------------------------------

'creates a string of all symbols separated by "+"
Dim urlSymbols As String
For i = 0 To dataLast
    urlSymbols = urlSymbols & W.Cells(2, 4 + i).Value & "+"
Next i
urlSymbols = Left(urlSymbols, Len(urlSymbols) - 3) 'gets rid of extra "+" values

 Dim splitUrlSymbols As Variant: splitUrlSymbols = Split(urlSymbols, Chr(43))
 For i = 0 To dataLast - 2
       W.Cells(1, 4 + i).Value = "http://ichart.finance.yahoo.com/table.csv?s=" & splitUrlSymbols(i) & urlStartRange & urlEndRange
 Next i
   'Pulls data from YAHOO Finance --------------
For Z = 0 To dataLast - 2
Dim getHttp As New WinHttpRequest

    getHttp.Open "GET", W.Cells(1, Z + 4).Value, False ' *********just selected 1 cell for now****************
    getHttp.Send
    Dim httpResp As String: httpResp = getHttp.ResponseText
    Dim dataLines As Variant: dataLines = Split(httpResp, vbLf)
    Dim closeValue As String
    Dim x As Integer
        For x = 1 To UBound(dataLines) - 4: Debug.Print dataLines(2)
            closeValue = dataLines(x)
            Dim adjClose As Variant: adjClose = Split(closeValue, ",")
                 If InStr(closeValue, ",") > 0 Then
            W.Cells(2 + x, Z + 4).Value = adjClose(6)
                End If
        Next x
    Dim y As Integer
    'Dim adjClose As Variant: adjClose = Split(closeValue, ",")



Next Z

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-21
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多