0% found this document useful (0 votes)
151 views

Excel Yahoo Finance Vba

The document contains a series of functions for retrieving stock data from Yahoo Finance. The functions include StockOpen, StockHigh, StockLow, StockClose, StockVolume, and StockAdjustedClose which return the open, high, low, close, volume, and adjusted close price for a stock. Additional functions include YahooStockInfo which returns various stock data fields based on the data type parameter, GetYahooData which retrieves stock data from Yahoo Finance, IsLiveStockDataCell which checks if a cell contains a stock formula, and IsShortSale which checks if a stock transaction was a short sale.

Uploaded by

Samya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Excel Yahoo Finance Vba

The document contains a series of functions for retrieving stock data from Yahoo Finance. The functions include StockOpen, StockHigh, StockLow, StockClose, StockVolume, and StockAdjustedClose which return the open, high, low, close, volume, and adjusted close price for a stock. Additional functions include YahooStockInfo which returns various stock data fields based on the data type parameter, GetYahooData which retrieves stock data from Yahoo Finance, IsLiveStockDataCell which checks if a cell contains a stock formula, and IsShortSale which checks if a stock transaction was a short sale.

Uploaded by

Samya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Function StockOpen(strTicker As String, Optional dtDate As Variant)

Data = GetYahooData(strTicker, dtDate)


strColumns = Split(Data, ",") '
StockOpen = Val(strColumns(1))
End Function
Function StockHigh(strTicker As String, Optional dtDate As Variant)
Data = GetYahooData(strTicker, dtDate)
strColumns = Split(Data, ",") '
StockHigh = Val(strColumns(2))
End Function
Function StockLow(strTicker As String, Optional dtDate As Variant)
Data = GetYahooData(strTicker, dtDate)
strColumns = Split(Data, ",") '
StockLow = Val(strColumns(3))
End Function
Function StockClose(strTicker As String, Optional dtDate As Variant)
Data = GetYahooData(strTicker, dtDate)
strColumns = Split(Data, ",") '
StockClose = Val(strColumns(4))
End Function
Function StockVolume(strTicker As String, Optional dtDate As Variant)
Data = GetYahooData(strTicker, dtDate)
strColumns = Split(Data, ",") '
StockVolume = Val(strColumns(5))
End Function
Function StockAdjustedClose(strTicker As String, Optional dtDate As Variant)
Data = GetYahooData(strTicker, dtDate)
strColumns = Split(Data, ",") '
StockAdjustedClose = Val(strColumns(6))
End Function
Function YahooStockInfo(strTicker As String, Optional dtDataType As String)
On Error GoTo HandleErr
If IsMissing(dtDataType) Then
dtDataType = "None"
End If
Dim strURL As String, strCSV As String, strRows() As String, strColumns() As
String
Dim dbClose As Double
' Compile the request URL with start date and end date
strURL = "http://download.finance.yahoo.com/d/quotes.csv?s=" & strTicker & _
"&f=ohgl1pc1va2kjwnd2qr1yrs7xl9t5p4"
Debug.Print strURL
Set http = CreateObject("MSXML2.XMLHTTP")

http.Open "GET", strURL, False


http.Send
strCSV = http.responseText
'
'
'
'

Debug.Print strCSV
The most recent information is in Line 2.
Sample Data
Line 1: Date,Open,High,Low,Close,Volume,Adj Close
Line 2: 2015-07-03,59.86,60.30,59.22,60.30,72800,60.30

Data = Split(strCSV, ",")


Select Case LCase$(dtDataType)
Case "o", "open"
result = Val(Data(0))
Case "h", "high"
result = Val(Data(1))
Case "l", "low"
result = Val(Data(2))
Case "s", "sale", "lastsale"
result = Val(Data(3))
Case "pc", "prevclose"
result = Val(Data(4))
Case "ch", "change"
result = Val(Data(5))
Case "v", "volume"
result = Val(Data(6))
Case "av", "avgvol"
result = Val(Data(7))
Case "52h", "52high"
result = Val(Data(8))
Case "52l", "52low"
result = Val(Data(9))
Case "52r", "52range"
result = Data(10)
Case "n", "name"
result = Data(11)
Case "dt", "date"
result = Data(12)
' Trade Date
Case "dtx", "ex-div"
result = Data(13)
' Ex-Dividend Date
Case "dtp", "pay-div"
result = Data(14)
' Dividend Pay Date
Case "y", "yield"
result = Val(Data(13)) ' Dividend yield
Case "pe", "p/e"
result = Val(Data(14)) ' P/E Ratio
Case "sh", "short"
result = Val(Data(15)) ' Short Ratio
Case "x", "exchange"
result = Data(16)
' Exchange
Case Else
result = strCSV
End Select
YahooStockInfo = result
Set http = Nothing
' error handlers

' return the data

ExitHere:
Exit Function
HandleErr:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume ExitHere
Resume
End Function
Function GetYahooData(strTicker As String, Optional dtDate As Variant)
' Date is optional - if omitted, use today. If value is not a date, throw er
ror.
If IsMissing(dtDate) Then
dtDate = Date
Else
If Not (IsDate(dtDate)) Then
GetYahooData = CVErr(xlErrNum)
End If
End If
Dim dtPrevDate As Date
Dim strURL As String, strCSV As String, strRows() As String, strColumns() As
String
Dim dbClose As Double
dtPrevDate = dtDate - 7 'Get past 7 days to avoid error due to Market closed
days
' Debug.Print dtDate
' See https://code.google.com/p/yahoo-finance-managed/w/list, and
' See https://code.google.com/p/yahoo-finance-managed/wiki/csvHistQuotesDown
load
' Compile the request URL with start date and end date
strURL = "http://ichart.finance.yahoo.com/table.csv?s=" & strTicker & _
"&a=" & Month(dtPrevDate) - 1 & _
"&b=" & Day(dtPrevDate) & _
"&c=" & Year(dtPrevDate) & _
"&d=" & Month(dtDate) - 1 & _
"&e=" & Day(dtDate) & _
"&f=" & Year(dtDate) & _
"&g=d&ignore=.csv" ' G -> Frequency: Daily, Weekly, or Monthly (d|w|m). di
vidends: g=v
'Debug.Print strURL
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", strURL, False
http.Send
strCSV = http.responseText
'
'
'
'
'

Debug.Print strCSV
The most recent information is in Line 2.
Sample Data
Line 1: Date,Open,High,Low,Close,Volume,Adj Close
Line 2: 2015-07-03,59.86,60.30,59.22,60.30,72800,60.30
strRows() = Split(strCSV, Chr(10)) ' split the CSV into rows
GetYahooData = strRows(1)
' return the data for the day
Set http = Nothing

End Function
Function IsLiveStockDataCell(R As Range) As Boolean
'
'
'
'

Called from conditional formatting to determine


if cell contains a formula:
FormulaCell = rng.HasFormula
Siva: Modified to highlight LiveStockQuoteCell

bResult = False
strFormula = Trim(R.Formula)
If Left(strFormula, 1) = "=" Then
strFormula = Trim(Mid(strFormula, 2))
bResult = InStr(1, strFormula, "Stock", vbTextCompare) > 0
End If
LiveStockDataCell = bResult
End Function
Function IsShortSale(R As Range) As Boolean
bResult = False
If R.Cells.CountLarge = 1 Then
Row = R.Cells.Row
Col = R.Cells.Column
BuyDate = Cells(Row, 4)
'Buy Date is in column D
SellDate = Cells(Row, 11) 'Sell Date is in column K
If Not IsEmpty(SellDate) Then
If IsEmpty(BuyDate) Or (SellDate < BuyDate) Then
bResult = True
End If
End If
' Debug.Print "BuyDate: ", BuyDate, " Sell: ", SellDate, " [ ", bResult,
" ]"
IsShortSale = bResult
End If
End Function

You might also like