How best to calculate derived currency rate conversions using C#/LINQ?

Posted by chillitom on Stack Overflow See other posts from Stack Overflow or by chillitom
Published on 2010-03-11T19:22:50Z Indexed on 2010/03/11 19:39 UTC
Read the original article Hit count: 244

Filed under:
|
|
 class FxRate {
     string Base { get; set; }
     string Target  { get; set; }
     double Rate { get; set; }
 }

 private IList<FxRate> rates = new List<FxRate> {
          new FxRate {Base = "EUR", Target = "USD", Rate = 1.3668},
          new FxRate {Base = "GBP", Target = "USD", Rate = 1.5039},
          new FxRate {Base = "USD", Target = "CHF", Rate = 1.0694},
          new FxRate {Base = "CHF", Target = "SEK", Rate = 8.12}
          // ...
 };

Given a large yet incomplete list of exchange rates where all currencies appear at least once (either as a target or base currency): What algorithm would I use to be able to derive rates for exchanges that aren't directly listed?

I'm looking for a general purpose algorithm of the form:

 public double Rate(string baseCode, string targetCode, double currency)
 {
      return ...
 }

In the example above a derived rate would be GBP->CHF or EUR->SEK (which would require using the conversions for EUR->USD, USD->CHF, CHF->SEK)

Whilst I know how to do the conversions by hand I'm looking for a tidy way (perhaps using LINQ) to perform these derived conversions perhaps involving multiple currency hops, what's the nicest way to go about this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ