using System; using System.Collections.Generic; using System.Linq; using static bts.api.Security; namespace bts.api { internal static class SecurityHelper { public static IEnumerable get_flat_future_legs(SecurityImmutable sec) { switch (sec.ContractCase) { case ContractOneofCase.Spread: return _get_flat_future_legs(sec.Spread.Legs, 1); case ContractOneofCase.Synthetic: return _get_flat_future_legs(sec.Synthetic.Legs, 1); default: return Enumerable.Empty(); } } public static IEnumerable get_flat_future_legs(IEnumerable legs) => _get_flat_future_legs(legs, 1); static IEnumerable _get_flat_future_legs(IEnumerable legs, int qty) { foreach (var l in legs) { switch (l.Security.ContractCase) { case ContractOneofCase.Future: yield return new FutureLeg(l.Security.Exchange, l.Security.Symbol, l.Security.security_desc(), l.Security.Future, qty * l.SignedQuantity()); break; case ContractOneofCase.Spread: foreach (var fl in _get_flat_future_legs(l.Security.Spread.Legs, qty * l.SignedQuantity())) yield return fl; break; case ContractOneofCase.Synthetic: foreach (var fl in _get_flat_future_legs(l.Security.Synthetic.Legs, qty * l.SignedQuantity())) yield return fl; break; } } } #region Extensions public static DateTime expiration_date(this FutureContract future) { return new DateTime((int)future.ExpirationYear, (int)future.ExpirationMonth, (int)future.ExpirationDay); } public static DateTime expiration_date(this FutureContractImmutable future) { return new DateTime((int)future.ExpirationYear, (int)future.ExpirationMonth, (int)future.ExpirationDay); } internal static DateTime EarliestExpiration(this SecurityImmutable security) { switch (security.ContractCase) { case ContractOneofCase.Synthetic: return security.Synthetic.Legs.First().Security.EarliestExpiration(); case ContractOneofCase.Spread: return security.Spread.Legs.First().Security.EarliestExpiration(); case ContractOneofCase.Future: return security.Future.expiration_date(); default: throw new NotImplementedException(); } } internal static int SignedQuantity(this ContractLegImmutable leg) => leg.Side == Side.Buy ? (int)leg.Quantity : -(int)leg.Quantity; internal static int SignedQuantity(this ContractLeg leg) => leg.Side == Side.Buy ? (int)leg.Quantity : -(int)leg.Quantity; internal static string ForceSymbol(this SecurityImmutable security) { if (security.ContractCase == ContractOneofCase.Synthetic) return ForceSymbol(security.Synthetic.Legs.First().Security); else return security.Symbol; } public static string DisplayExchange(this SecurityImmutable security) => security.ContractCase == ContractOneofCase.Synthetic ? $"[{string.Join(",", get_flat_future_legs(security).Select(flt => flt.exchange.ToString()).Distinct().OrderBy(e => e))}]" : security.Exchange.ToString(); public static string security_desc(this SecurityImmutable security) { if(security.HasGlbx) return security.Glbx.SecurityDesc; else if(security.HasIce) return security.Ice.SecurityDesc; else { Log.assert(false, "security_desc not implemented for exchange"); return null; } } #endregion public static class Comparer { public static readonly IComparer Instance = Comparer.Create(Compare); public static readonly IComparer InstanceImmutable = Comparer.Create(Compare); public static int Compare(Security x, Security y) { if (ReferenceEquals(x, y)) return 0; // x ==y if (x == null) return 1; // y > x if (y == null) return -1; // x > y return Compare(new SecurityImmutable(x), new SecurityImmutable(y)); } public static int Compare(SecurityImmutable x, SecurityImmutable y) { int c = non_deterministic_impl(); if (c == 0) { c = x.ToString().CompareTo(y.ToString()); if (c == 0) c = x.Id.CompareTo(y.Id); } return c; // this has been observed to be non-determinisitc with certain ICE securities // (it is determenistic with other exchanges..as far as we know). int non_deterministic_impl() { if (x.ContractCase == ContractOneofCase.Future) { return y.ContractCase == ContractOneofCase.Future ? Compare(x.Symbol, x.Future, y.Symbol, y.Future) : -1; } else if (y.ContractCase == ContractOneofCase.Future) { return 1; } var xlegs = get_flat_future_legs(x).ToList(); var ylegs = get_flat_future_legs(y).ToList(); int max = Math.Min(xlegs.Count, ylegs.Count); for (int i = 0; i < max; i++) { int rv = Compare(xlegs[i].symbol, xlegs[i].future, ylegs[i].symbol, ylegs[i].future); if (rv != 0) return rv; rv = xlegs[i].quantity.CompareTo(ylegs[i].quantity); if (rv != 0) return rv; rv = xlegs[i].exchange.CompareTo(ylegs[i].exchange); if (rv != 0) return rv; } return xlegs.Count.CompareTo(ylegs.Count); } } public static int Compare(string x_sym, FutureContract x, string y_sym, FutureContract y) { int sym_comp = x_sym.CompareTo(y_sym); return sym_comp == 0 ? x.expiration_date().CompareTo(y.expiration_date()) : sym_comp; } public static int Compare(string x_sym, FutureContractImmutable x, string y_sym, FutureContractImmutable y) { int sym_comp = x_sym.CompareTo(y_sym); return sym_comp == 0 ? x.expiration_date().CompareTo(y.expiration_date()) : sym_comp; } } } }