简体   繁体   中英

Best way to store ROE in C#

I want to save Rate of Exchange of all currencies corresponding different base currency. What is the best and efficient type or struct to save the same. Currently i am using

Dictionary<string, Dictionary<string, decimal>>();

Thanks in advance. Please suggest

Don't see big problem with your approach, if not

1) use some custom 3rd party in memory DB ( redis like) . But may reveal too combersome for your case.

2) derive from your type

public class MyCustomHolder : Dictionary<string, Dictionary<string, decimal>> {
}

So avoid that long and confusing definitions in the code, and bring more semantics to your code reader and yourself.

Typically I use a variation of the following class.

public ForexSpotContainer : IEnumerable<FxSpot>
{
     [DataMember] private readonly Dictionary<string, FxSpot> _fxSpots;

     public FxSpot this[string baseCurrency, string quoteCurrency]
     {
         get
         {
             var baseCurrencySpot = _fxSpots[baseCurrency];
             var quoteCurrencySpot = _fxSpots[quoteCurrency];

             return baseCurrencySpot.Invert()*quoteCurrencySpot;
         }
     }

    public IEnumerator<FxSpot> GetEnumerator()
    {
        return _fxSpots.Values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

}

I tend to then create a Money class and a FxSpot class then create +-*/ operators for the Money and FxSpot classes so that I can do financial calculations in a safe way.

EDIT: I my experience, when working in financial systems, I have always had issues with code like this

decimal sharePriceOfMsft = 40.30m;
decimal usdEur = 0.75m;
decimal msftInEur = sharePriceOfMsft * usdEur;

Since it always takes a few second for me to check if I should multiply the spot or divide.

The problem is compounded when I have to use Forex Crosses, such as JPYEUR or EURJPY etc, and hours were lost to subtitle bugs from close Forex Spots.

Also of consequence is the dimensional analysis of equations. When you multiple lots of numbers together and you expect a Price, are you sure you didn't mess up a multiple/divide. By creating a new class for each unit, you have a little more compile time error checking that can ultimately save you seconds for each line of code you read (which in a many thousand line library will add up to hours very quickly).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM