Edit (04-FEB-2014): new version of source code
I couldn’t keep away from programming during my long Chrismast Holiday. Decided to do some “just-code-something-useful” instead of doing something new. I remebered back to my previous employer and a quite simple and fun task of implementing a currency collector, and decided to rebuild this from scratch.
The national bank (Norges Bank) updates the curency rates every day at 15:15 (local time). This file is stored as a CSV file
This file is on the following format seen in the screenshot below. The first column contains the date on the format DD-MMM-YY. The following columns contains data for all the available currencies – one column for each currency. The column header contains both unit and currency code according to Norwegian Kroner (NOK). Each row contains data for all currencies for the specified date in column 1. The file contains all historical data back to 2001.
The main page for the currency rates are located here;
These data are the official data for currency rates according to NOK, but it might be more correct to collect data from different banks as well to add fees you have to pay when buying and selling currency.
The concept has a simple data model. One table for storing information on the available currencies, one table for the collected rates. I also have a table for sources for currency rates. For the time beeing, I have only implemented one – NB, Norges Bank.
I have developed the solution i Visual Studio 2012 as a console application. The database is created/published with the project type “SQL Server Database Project”. This project type has evolved since I used this a few years back and become more mature, in my opinion.
For reading CSV files I’m always using the super-fast CSV reader by Sebastien Lorion. This lovely library is now also available via Nuget. I’m using Log4Net as logging framework. And the database access is done by “LINQ to Classes” – simple and easy.
The program uses an input- and output directory. It will store all files retrieved from Norges Bank with a date in the output directory, and the main program will check if the file for current day is already stored locally and take no action if this file exists.
If the currency file for today doesn’t exist it will http download the latest currency rates from the URL described above. The program will read currency reates that is enabled in the database (Active = true), and read all new currency rates for all currencies from the downloaded file and write them to the database.
This code is probably not a school example, but it works. I will refactor the code later **IF** I decide to implement a new currency rate source, such as “DNB”.
Full source code is located at my dropbox.
- 04-FEB-2014), bugfix to fix hardcoding folder error