diff --git a/src/Anubis/Baskets/Currencies.cs b/src/Anubis/Baskets/Currencies.cs new file mode 100644 index 0000000..b29590e --- /dev/null +++ b/src/Anubis/Baskets/Currencies.cs @@ -0,0 +1,79 @@ +namespace Anubis.Baskets; + +/// +/// The eight currencies of the research document (section 4.1) in the market naming order: in a pair the currency that comes +/// first is the base. The one home of the currency and pair lists: the basket maths and the ECB crosses both read them here. +/// +public static class Currencies +{ + public const string Eur = "EUR"; + public const string Gbp = "GBP"; + public const string Aud = "AUD"; + public const string Nzd = "NZD"; + public const string Usd = "USD"; + public const string Cad = "CAD"; + public const string Chf = "CHF"; + public const string Jpy = "JPY"; + + private static readonly string[] All = [Eur, Gbp, Aud, Nzd, Usd, Cad, Chf, Jpy]; + + /// EUR, GBP, AUD, NZD, USD, CAD, CHF, JPY. + public static IReadOnlyList Order { get; } = Array.AsReadOnly(All); + + /// Position of a currency in ; -1 when it is not one of the eight. + public static int IndexOf(string currency) => Array.IndexOf(All, currency); +} + +/// A currency pair: the price is how many units of buy one unit of . +public sealed record FxPair(string Base, string Quote) +{ + /// The market name, base then quote: EURUSD. + public string Name => Base + Quote; + + /// The pip: 0.01 when the quote is the yen, 0.0001 otherwise (document, appendix C). + public decimal Pip => Quote == Currencies.Jpy ? 0.01m : 0.0001m; + + public override string ToString() => Name; +} + +/// The 28 pairs of the eight currencies, in the order of the document's appendix A. +public static class FxPairs +{ + /// Every pair whose base comes before its quote in : 8 × 7 / 2 = 28. + public static IReadOnlyList All { get; } = Build(); + + private static readonly Dictionary ByName = All.ToDictionary(p => p.Name, StringComparer.Ordinal); + + /// The pair with this market name (EURUSD), or null when it is not one of the 28. + public static FxPair? Find(string name) => ByName.GetValueOrDefault(name); + + /// The pair with this market name; throws when it is not one of the 28. + public static FxPair Get(string name) => Find(name) ?? throw new ArgumentException($"{name} non è una delle 28 coppie.", nameof(name)); + + /// The pair of two currencies, whichever order they are given in; null when they are the same or unknown. + public static FxPair? Of(string one, string other) + { + var a = Currencies.IndexOf(one); + var b = Currencies.IndexOf(other); + if (a < 0 || b < 0 || a == b) + { + return null; + } + + return a < b ? Find(one + other) : Find(other + one); + } + + private static List Build() + { + var pairs = new List(28); + for (var i = 0; i < Currencies.Order.Count; i++) + { + for (var j = i + 1; j < Currencies.Order.Count; j++) + { + pairs.Add(new FxPair(Currencies.Order[i], Currencies.Order[j])); + } + } + + return pairs; + } +}