Fase 12 — Dati e matematica dei panieri #187
@@ -0,0 +1,79 @@
|
||||
namespace Anubis.Baskets;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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];
|
||||
|
||||
/// <summary>EUR, GBP, AUD, NZD, USD, CAD, CHF, JPY.</summary>
|
||||
public static IReadOnlyList<string> Order { get; } = Array.AsReadOnly(All);
|
||||
|
||||
/// <summary>Position of a currency in <see cref="Order"/>; -1 when it is not one of the eight.</summary>
|
||||
public static int IndexOf(string currency) => Array.IndexOf(All, currency);
|
||||
}
|
||||
|
||||
/// <summary>A currency pair: the price is how many units of <see cref="Quote"/> buy one unit of <see cref="Base"/>.</summary>
|
||||
public sealed record FxPair(string Base, string Quote)
|
||||
{
|
||||
/// <summary>The market name, base then quote: EURUSD.</summary>
|
||||
public string Name => Base + Quote;
|
||||
|
||||
/// <summary>The pip: 0.01 when the quote is the yen, 0.0001 otherwise (document, appendix C).</summary>
|
||||
public decimal Pip => Quote == Currencies.Jpy ? 0.01m : 0.0001m;
|
||||
|
||||
public override string ToString() => Name;
|
||||
}
|
||||
|
||||
/// <summary>The 28 pairs of the eight currencies, in the order of the document's appendix A.</summary>
|
||||
public static class FxPairs
|
||||
{
|
||||
/// <summary>Every pair whose base comes before its quote in <see cref="Currencies.Order"/>: 8 × 7 / 2 = 28.</summary>
|
||||
public static IReadOnlyList<FxPair> All { get; } = Build();
|
||||
|
||||
private static readonly Dictionary<string, FxPair> ByName = All.ToDictionary(p => p.Name, StringComparer.Ordinal);
|
||||
|
||||
/// <summary>The pair with this market name (EURUSD), or null when it is not one of the 28.</summary>
|
||||
public static FxPair? Find(string name) => ByName.GetValueOrDefault(name);
|
||||
|
||||
/// <summary>The pair with this market name; throws when it is not one of the 28.</summary>
|
||||
public static FxPair Get(string name) => Find(name) ?? throw new ArgumentException($"{name} non è una delle 28 coppie.", nameof(name));
|
||||
|
||||
/// <summary>The pair of two currencies, whichever order they are given in; null when they are the same or unknown.</summary>
|
||||
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<FxPair> Build()
|
||||
{
|
||||
var pairs = new List<FxPair>(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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user