Function to Get FX Rates

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
938
I am using this function to get FX rates predominantly from USD to GBP. However, it is not always accurate to the specified date. Does anyone use something else which could be more accurate please?

Code:
Function GetFxRate(CurrencyIn As String, CurrencyOut As String, Dt As Date) As Double
Dim strURL As String
Dim StartAtPos As String
Dim objHTTP
Dim WebResponse
Dim d As Long
Dim DtTxt As String
Dim DtPos As String
Dim ValTxt As String

'https://api.exchangeratesapi.io/history?start_at=2017-12-25&end_at=2018-01-02&symbols=CAD&base=EUR

GetFxRate = 1
'Get 7 days of data as some days might not have data (new year, christmas, etc)
strURL = "https://api.exchangeratesapi.io/history?"
strURL = strURL & "start_at=" & Format(Dt - 7, "yyyy-mm-dd") & "&end_at=" & Format(Dt, "yyyy-mm-dd")
strURL = strURL & "&symbols=" & CurrencyIn & "&base=" & CurrencyOut

'Debug.Print strURL  'The Url that is being called

Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", strURL, False
objHTTP.send
If objHTTP.Status = "200" Then
    objHTTP.WaitForResponse
    WebResponse = objHTTP.responseText
    'e.g. "{"rates":{"2018-12-27":{"EUR":0.6451612903},"2018-12-31":{"EUR":0.6408202499},"2018-12-28":{"EUR":0.6409434688}},"start_at":"2018-12-25","base":"CAD","end_at":"2019-01-01"}"
    StartAtPos = InStr(WebResponse, "start_at")
    For d = Dt To Dt - 7 Step -1
        DtTxt = Format(d, "yyyy-mm-dd")
        DtPos = InStr(WebResponse, DtTxt)
        If DtPos > 0 And DtPos < StartAtPos Then
            ValTxt = Mid(WebResponse, 1 + InStr(DtPos, WebResponse, "{"), InStr(DtPos, WebResponse, "}") - InStr(DtPos, WebResponse, "{") - 1)
            GetFxRate = Val(Right(ValTxt, Len(ValTxt) - InStr(ValTxt, ":")))
            Exit For
        End If
    Next d
End If
Set objHTTP = Nothing

End Function

many thanks,
Andrew
 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Rijnsent

Well-known Member
Joined
Oct 17, 2005
Messages
1,300
Office Version
  1. 365
Platform
  1. Windows

Rijnsent

Well-known Member
Joined
Oct 17, 2005
Messages
1,300
Office Version
  1. 365
Platform
  1. Windows
Hi neodjandre,
one addition, you could also pull in the data from a long period back (1999) with e.g. PowerQuery. Go for Data->Get Data->PowerQuery editor->Advanced editor and copy-paste this code, it pulls in the data from a chart from ukfx.co.uk.
Cheers,
Koen

Code:
let
    Src = Json.Document(Web.Contents("https://api.ukfx.co.uk/pairs/gbp/usd/candle?t=10000")),
    #"ConvertToTable" = Table.FromList(Src, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"ExtractedValues" = Table.TransformColumns(#"ConvertToTable", {"Column1", each Combiner.CombineTextByDelimiter("=")(List.Transform(_, Text.From)), type text}),
    #"ColSplitBy" = Table.SplitColumn(#"ExtractedValues", "Column1", Splitter.SplitTextByDelimiter("=", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3", "Column1.4", "Column1.5"}),
    #"TypeChange" = Table.TransformColumnTypes(#"ColSplitBy",{{"Column1.1", Int64.Type}, {"Column1.2", type number}, {"Column1.3", type number}, {"Column1.4", type number}, {"Column1.5", type number}}),
    #"AddCalculatedColumn" = Table.AddColumn(TypeChange, "ExcelTime", each #datetime(1970, 1, 1, 0, 0, 0) + #duration(0, 0, 0, [Column1.1]/1000)),
    #"TypeChange2" = Table.TransformColumnTypes(#"AddCalculatedColumn",{{"ExcelTime", type datetime}})
in
    #"TypeChange2"
 

Watch MrExcel Video

Forum statistics

Threads
1,127,299
Messages
5,623,846
Members
415,995
Latest member
SergioCM92

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
Top