Excel VBA for Yahoo Finance

sjedi

New Member
Joined
Dec 8, 2019
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
I realised that Yahoo Finance has removed the Download function.
I can replace the tickerURL line 'download' with 'chart' to extract data from Yahoo Finance. However, this is in JSON.
VBA Code:
tickerURL = "https://query1.finance.yahoo.com/v7/finance/download/"
How do I adjust my VBA code for JSON converter?

VBA Code:
Sub getYahooFinanceData(tickerSymbol As String, startDate As String, endDate As String, frequency As String, _
cookie As String, crumb As String, ByVal ticker As Long)
    Dim resultFromYahoo As String
    Dim objRequest
    Dim csv_rows() As String
    Dim resultArray As Variant
    Dim nColumns As Integer
    Dim iRows As Integer
    Dim CSV_Fields As Variant
    Dim iCols As Integer
    Dim tickerURL As String

    'Construct URL
    '***************************************************
    tickerURL = "https://query1.finance.yahoo.com/v7/finance/download/" & tickerSymbol & _
        "?period1=" & startDate & _
        "&period2=" & endDate & _
        "&interval=" & frequency & "&events=history" & "&crumb=" & crumb
    'Sheets("Parameters").Range("K" & ticker - 1) = tickerURL
    '***************************************************
               
    'Get data from Yahoo
    '***************************************************
    Set objRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    With objRequest
        .Open "GET", tickerURL, False
        '.setRequestHeader "Cookie", cookie
        .send
        .waitForResponse
        resultFromYahoo = .ResponseText
    End With
    '***************************************************
        
    'Parse returned string into an array
    '***************************************************
    nColumns = 6 'number of columns minus 1  (date, open, high, low, close, adj close, volume)
    csv_rows() = Split(resultFromYahoo, Chr(10))
    csv_rows = Filter(csv_rows, csv_rows(0), False)
    ReDim resultArray(0 To UBound(csv_rows), 0 To nColumns) As Variant
     
    For iRows = LBound(csv_rows) To UBound(csv_rows)
        CSV_Fields = Split(csv_rows(iRows), ",")
        If UBound(CSV_Fields) > nColumns Then
            nColumns = UBound(CSV_Fields)
            ReDim Preserve resultArray(0 To UBound(csv_rows), 0 To nColumns) As Variant
        End If
    
        For iCols = LBound(CSV_Fields) To UBound(CSV_Fields)
            If IsNumeric(CSV_Fields(iCols)) Then
                resultArray(iRows, iCols) = Val(CSV_Fields(iCols))
            ElseIf IsDate(CSV_Fields(iCols)) Then
                resultArray(iRows, iCols) = CDate(CSV_Fields(iCols))
            Else
                resultArray(iRows, iCols) = CStr(CSV_Fields(iCols))
            End If
        Next
    Next
 
    'Write results into worksheet for ticker
    Worksheets("Port").Range("$B$4").Offset(ticker - firstTickerRow, 0) _
    .Resize(UBound(resultArray, 1) + 1, UBound(resultArray, 2) + 1).Value = resultArray
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Did you bother to scroll down and read this section

Connect to international stocks​

Stock Connector supports stocks in 14 non-US exchanges across 13 countries (and 8 US exchanges). Non-US stocks update only at the end of each trading day, not in real time throughout the day. Here are the supported regions:

CountryNameCurrencySymbol suffixExample
BelgiumEuronext BrusselsEUR-BEKBCA-BE
BrazilSao Paolo Stock ExchangeBRL-BRMGLU-BR
CanadaToronto Stock ExchangeCAD-CATD-CA
ChinaHong Kong Stock ExchangeHKD-HK5-HK
FranceEuronext ParisEUR-FRMC-FR
GermanyXETRAEUR-DEVOW-DE
IndiaNational Stock Exchange of IndiaIST-INTATAMOTORS-IN
IsraelTel Aviv Stock ExchangeILS-ILTEVA-IL
JapanTokyo Stock ExchangeJPY.T-JP7203.T-JP
NetherlandsEuronext AmsterdamEUR-NLUNA-NL
TaiwanTaiwan Stock ExchangeTWD-TW2330-TW
United KingdomLondon Stock ExchangeGBP-GBLLOY-GB


In order to add the stock you want, take the symbol and append the suffix above. For example, to add Volkswagen on the German exchange, type VOW-GY. You can also use the Search function in Stock Connector to find international stocks.

Connect to FOREX currency exchange rates​

 
Upvote 0
Did you bother to scroll down and read this section

Connect to international stocks​

Stock Connector supports stocks in 14 non-US exchanges across 13 countries (and 8 US exchanges). Non-US stocks update only at the end of each trading day, not in real time throughout the day. Here are the supported regions:

CountryNameCurrencySymbol suffixExample
BelgiumEuronext BrusselsEUR-BEKBCA-BE
BrazilSao Paolo Stock ExchangeBRL-BRMGLU-BR
CanadaToronto Stock ExchangeCAD-CATD-CA
ChinaHong Kong Stock ExchangeHKD-HK5-HK
FranceEuronext ParisEUR-FRMC-FR
GermanyXETRAEUR-DEVOW-DE
IndiaNational Stock Exchange of IndiaIST-INTATAMOTORS-IN
IsraelTel Aviv Stock ExchangeILS-ILTEVA-IL
JapanTokyo Stock ExchangeJPY.T-JP7203.T-JP
NetherlandsEuronext AmsterdamEUR-NLUNA-NL
TaiwanTaiwan Stock ExchangeTWD-TW2330-TW
United KingdomLondon Stock ExchangeGBP-GBLLOY-GB


In order to add the stock you want, take the symbol and append the suffix above. For example, to add Volkswagen on the German exchange, type VOW-GY. You can also use the Search function in Stock Connector to find international stocks.

Connect to FOREX currency exchange rates​

Yes I did. I'm looking for Singapore stocks, which is not in this list...
 
Upvote 0
You may be able to search for a site that provides prices for stocks on that exchange and then import them into Excel with Power Query. This the best I can offer you now. Excel does not currently support pricing from that exchange.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,080
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top