在 Excel 中使用 YAHOO API 提取多家公司的历史股票数据
Pulling Historical stock data for multiple companies using YAHOO API in Excel
我正在 Excel 开展一个项目,该项目将为您提供投资组合的价值,并计算有关您持有的一些其他有用的统计数据。 (投资组合的 st.dev、您的投资组合的贝塔等)。
我已经使用 Yahoo Finance 提取盘中统计数据(公司名称、最后交易价格、开盘价、最高价、最低价等)。这部分相当简单,您只需在 URL 中添加符号和“+”,它就会提取每只股票的所有数据。
我想做的是提取投资组合中所有股票的历史收盘价(不幸的是,将股票代码和“+”添加到 URL 的相同逻辑不适用于此).
下面是我目前的代码。在 "Sheet1" 上是投资组合持有的位置(股票代码从单元格 A2 开始并向下)。
Sheet 2 将采用股票代码并将它们显示在第 2 行的顶部,并在第 1 行中其上方的每个相应股票代码显示 url。
此外,开始日期和结束日期分别位于 sheet 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
搞清楚了。
只是做了很多拆分和循环。
尽管如此,我确信可以更优雅地编写脚本。
干杯!
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
我正在 Excel 开展一个项目,该项目将为您提供投资组合的价值,并计算有关您持有的一些其他有用的统计数据。 (投资组合的 st.dev、您的投资组合的贝塔等)。
我已经使用 Yahoo Finance 提取盘中统计数据(公司名称、最后交易价格、开盘价、最高价、最低价等)。这部分相当简单,您只需在 URL 中添加符号和“+”,它就会提取每只股票的所有数据。
我想做的是提取投资组合中所有股票的历史收盘价(不幸的是,将股票代码和“+”添加到 URL 的相同逻辑不适用于此).
下面是我目前的代码。在 "Sheet1" 上是投资组合持有的位置(股票代码从单元格 A2 开始并向下)。 Sheet 2 将采用股票代码并将它们显示在第 2 行的顶部,并在第 1 行中其上方的每个相应股票代码显示 url。
此外,开始日期和结束日期分别位于 sheet 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
搞清楚了。
只是做了很多拆分和循环。
尽管如此,我确信可以更优雅地编写脚本。
干杯!
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