Skip to content

Instantly share code, notes, and snippets.

@korniichuk
Created June 4, 2019 20:05
Show Gist options
  • Save korniichuk/08407cd5722d840b4bb86dd26f772539 to your computer and use it in GitHub Desktop.
Save korniichuk/08407cd5722d840b4bb86dd26f772539 to your computer and use it in GitHub Desktop.
Returns the cross rates for selected currency
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Version: 0.1a1
# Introducing the API: https://swea.riksbank.se/sweaWS/docs/api/index.htm
# Series for web services: https://www.riksbank.se/en-gb/statistics/search-interest--exchange-rates/web-services/series-for-web-services/
import datetime
import zeep
def get_cross_rates(currency, start, end=None, delta=0, reverse=False):
result = {}
if end is None:
end = start
seriesid1 = 'SEK{}PMI'.format(currency.upper())
if isinstance(start, str):
start = datetime.datetime.strptime(start, '%Y-%m-%d').date()
start = start - datetime.timedelta(days=delta)
if isinstance(end, str):
end = datetime.datetime.strptime(end, '%Y-%m-%d').date()
end = end + datetime.timedelta(days=delta)
client = zeep.Client('http://swea.riksbank.se/sweaWS/wsdl/sweaWS.wsdl')
CrossRequestParameters = {'aggregateMethod': 'D',
'crossPair': {'seriesid1': seriesid1,
'seriesid2': 'SEK'},
'datefrom': start,
'dateto': end,
'languageid': 'en'}
groups = client.service.getCrossRates(CrossRequestParameters).groups
data = zeep.helpers.serialize_object(groups[0])
series = data['series']
rows = series[reverse]['resultrows']
for row in rows:
result[row['date']] = row['value']
return result
# Example: '1 SEK = ? USD' 4th of June 2019
#result = get_cross_rates('USD', '2019-06-04')
# Example: '1 SEK = ? USD' beetwen 4th and 5th of June 2019
#result = get_cross_rates('USD', '2019-06-04', '2019-06-05')
# Example: '1 USD = ? SEK' beetwen 4th and 5th of June 2019
#result = get_cross_rates('USD', '2019-06-04', '2019-06-05', reverse=True)
# Example: '1 SEK = ? USD' beetwen 3rd and 5th of June 2019
#result = get_cross_rates('USD', '2019-06-04', delta=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment