Home » excel » excel – Adjsut the unix timestamp to be the same on Yahoo Finance

excel – Adjsut the unix timestamp to be the same on Yahoo Finance

Posted by: admin May 14, 2020 Leave a comment

Questions:

In Sheet1, I have ticker in B1 (say “AA”) and start date in B2 (say 21/4/2009) and the end date in B3 (say 23/4/2009)

When navigating manually to the desired url I got the link like that
https://finance.yahoo.com/quote/AA/history?period1=1240290000&period2=1240462800&interval=1d&filter=history&frequency=1d

But when using the code so as to construct the link I got little different UNIX timestamp like this
https://finance.yahoo.com/quote/AA/history?period1=1240272000&period2=1240444800&interval=1d&filter=history&frequency=1d

Notice period1 for example in both links
How can I adjust the code so as to be the same as the link at yahoo?

I tried something like that

period1 = ToUnix(.Range("B2").Value & " 05:00:00")

This solved the problem of those dates but doesn’t solve other and different dates so my logic is not correct

Here’s the code I tried

Sub Yahoo_Finance()
Dim ws          As Worksheet
Dim sURL        As String
Dim sTicker     As String
Dim period1     As Long
Dim period2     As Long
Dim r           As Long

Set ws = ThisWorkbook.Worksheets("Sheet1")
r = 6

With CreateObject("MSXML2.ServerXMLHTTP")
    With ws
        sTicker = .Range("B1").Value
        period1 = ToUnix(.Range("B2").Value & " 05:00:00")
        period2 = ToUnix(.Range("B3").Value & " 05:00:00")
    End With

    sURL = "https://finance.yahoo.com/quote/" & sTicker & "/history?period1=" & period1 & "&period2=" & period2 & "&interval=1d&filter=history&frequency=1d"

    Debug.Print sURL
End With
End Sub

Public Function ToUnix(dt) As Long
ToUnix = DateDiff("s", "1/1/1970", dt)
End Function
How to&Answers:

From what I observe it runs from 11pm the night before specified date for Start to 11pm night before date for End. So, DateAdd -1 day in code to remove 1 day from dates in sheets and ensure the hour is at 23:00:00. Then the urls match for me.

Public Sub Yahoo_Finance()
    Dim ws          As Worksheet
    Dim sURL        As String
    Dim sTicker     As String
    Dim period1     As Long
    Dim period2     As Long

    Set ws = ThisWorkbook.Worksheets("Sheet1")

    With CreateObject("MSXML2.ServerXMLHTTP")
        With ws
            sTicker = .Range("B1").Value
            period1 = ToUnix(DateAdd("d", -1, .Range("B2").Value) & "23:00:00")
            period2 = ToUnix(DateAdd("d", -1, .Range("B3").Value) & "23:00:00")
        End With

        sURL = "https://finance.yahoo.com/quote/" & sTicker & "/history?period1=" & period1 & "&period2=" & period2 & "&interval=1d&filter=history&frequency=1d"

        Debug.Print sURL
    End With
End Sub

For GMT local time conversions you could try the code by Rick Rothstein

Function Local2GMT(dtLocalDate As Date) As Date
    Local2GMT = DateAdd("s", -GetLocalToGMTDifference(), dtLocalDate)
End Function

Function GMT2Local(gmtTime As Date) As Date
    GMT2Local = DateAdd("s", GetLocalToGMTDifference(), gmtTime)
End Function

Function GetLocalToGMTDifference() As Long
    Const TIME_ZONE_ID_INVALID& = &HFFFFFFFF
    Const TIME_ZONE_ID_STANDARD& = 1
    Const TIME_ZONE_ID_UNKNOWN& = 0
    Const TIME_ZONE_ID_DAYLIGHT& = 2
    Dim TimeZoneInf As TIME_ZONE_INFORMATION
    Dim Ret As Long
    Dim Diff As Long
    Ret = GetTimeZoneInformation(TimeZoneInf)
    Diff = -TimeZoneInf.Bias * 60
    GetLocalToGMTDifference = Diff
    If Ret = TIME_ZONE_ID_DAYLIGHT& Then
        If TimeZoneInf.DaylightDate.wMonth <> 0 Then
            GetLocalToGMTDifference = Diff - TimeZoneInf.DaylightBias * 60
        End If
    End If
End Function

E.g. (per OP feedback.. no DateAdd adjustment required and )

period1 = ToUnix(Local2GMT(.Range("B2").Value))