diff --git a/src/client/spark/AW2/Bus/SingleOptionDriverPricePublisher.cs b/src/client/spark/AW2/Bus/SingleOptionDriverPricePublisher.cs index 0fbbb561e..9892488ae 100644 --- a/src/client/spark/AW2/Bus/SingleOptionDriverPricePublisher.cs +++ b/src/client/spark/AW2/Bus/SingleOptionDriverPricePublisher.cs @@ -177,15 +177,13 @@ namespace mbts.spark { public static Tuple find_neighbors(SystemContractData sc_data, SecurityTuple sec_tup, DateTime exp) { - var exps = sc_data.exp_map.Where( - kv => sec_tup.Equals(kv.Key.security_tuple)).Select(kv => kv.Key); + var exps = sc_data.GetExpirations(sec_tup.security_exchange, sec_tup.security_type, sec_tup.security_name); if (exps == null) { return null; } - SecurityExpiration low, high; VolSurfaceUtilityFunctions.find_neighbors(exps, exp, - (t, p) => t.expiration.Subtract(p).TotalMilliseconds, out low, out high); - return Tuple.Create(low, high); + (t, p) => t.Subtract(p).TotalMilliseconds, out var low, out var high); + return new(new(sec_tup, low), new(sec_tup, high)); } public static double interp_driver(SystemContractData sc_data, diff --git a/src/client/spark/AW2/LiveTheoPublisher.cs b/src/client/spark/AW2/LiveTheoPublisher.cs index 7adbe64f1..c429fb697 100644 --- a/src/client/spark/AW2/LiveTheoPublisher.cs +++ b/src/client/spark/AW2/LiveTheoPublisher.cs @@ -178,17 +178,10 @@ namespace mbts.spark { SystemContractData sc_data = _bb.read_state(SparkTags.SystemContractsTag); if (sc_data == null) return rv; - foreach (var kv in sc_data.exp_map) { - var id = kv.Value; - SecurityMD sec_md; - if (sc_data.sec_md_map.TryGetValue(id, out sec_md)) { - if (sec_md.security.HasFuture) { - SecurityExpiration exp = kv.Key; - double pr = sec_md.md.fixing_price; - rv.Add(sec_md.security.Id, - new GreeksTuple(0, pr, pr, 1, 0, 0, 0, 0, pr)); - } - } + foreach (var sec_md in sc_data.Futures) { + double pr = sec_md.md.fixing_price; + rv.Add(sec_md.security.Id, + new GreeksTuple(0, pr, pr, 1, 0, 0, 0, 0, pr)); } return rv; } @@ -202,16 +195,12 @@ namespace mbts.spark { SystemContractData sc_data = _bb.read_state(SparkTags.SystemContractsTag); if (sc_data == null) return; - foreach (var kv in sc_data.exp_map) { - var id = kv.Value; - SecurityMD sec_md; - if (sc_data.sec_md_map.TryGetValue(id, out sec_md)) { - if (sec_md.security.HasFuture) { - SecurityExpiration exp = kv.Key; - double pr = sec_md.md.fixing_price; - rv.Add(exp, new FutureTheoStructure(exp.expiration, pr, pr, 1, 0)); - } - } + foreach (var sec_md in sc_data.Futures) { + var sec = sec_md.security; + var exp = new SecurityExpiration( + sec.Exchange, sec.ContractCase, sec.Symbol, sec.Future.expiration_date()); + double pr = sec_md.md.fixing_price; + rv.Add(exp, new FutureTheoStructure(exp.expiration, pr, pr, 1, 0)); } _bb.post(SparkTags.TC2Tags.LiveFutureTheosTag, rv); } diff --git a/src/client/spark/AW2/Misc/AnalyticsExpirationParametersMap.cs b/src/client/spark/AW2/Misc/AnalyticsExpirationParametersMap.cs index 7378edc6a..719479a7b 100644 --- a/src/client/spark/AW2/Misc/AnalyticsExpirationParametersMap.cs +++ b/src/client/spark/AW2/Misc/AnalyticsExpirationParametersMap.cs @@ -6,6 +6,7 @@ using System.Linq; using bts.api; using mbts.analytics; +using mbts.utils; namespace mbts.spark { @@ -20,11 +21,16 @@ namespace mbts.spark { var empty = SecurityExpiration.empty; var temp = new Dictionary(exp_map_arg); - foreach (var kv in sc_data.exp_map) { - var exp = kv.Key; - if (!temp.ContainsKey(kv.Key)) { - temp.Add(kv.Key, - new AnalyticsExpirationParameters(exp, null, empty, empty, 0, 0, 0, 0, 0)); + foreach (var smd in sc_data.Futures) { + var sec = smd.security; + var exp = new SecurityExpiration(sec.Exchange, SecurityType.Future, sec.Symbol, sec.Future.expiration_date()); + if (!temp.ContainsKey(exp)) { + temp.Add(exp, new AnalyticsExpirationParameters(exp, null, empty, empty, 0, 0, 0, 0, 0)); + } + } + foreach (var sec_exp in sc_data.strike_map.Keys) { + if (!temp.ContainsKey(sec_exp)) { + temp.Add(sec_exp, new(sec_exp, null, empty, empty, 0, 0, 0, 0, 0)); } } exp_map = temp; @@ -32,14 +38,11 @@ namespace mbts.spark { public IEnumerable get_sorted_exps() { List sorted_exps = new List(exp_map.Keys); - sorted_exps.Sort((e1, e2) => { - int rv = e1.expiration.CompareTo(e2.expiration); - if (rv == 0) - rv = e1.symbol.CompareTo(e2.symbol); - if (rv == 0) - rv = e1.security_type.ToString().CompareTo(e2.security_type.ToString()); - return rv; - }); + sorted_exps.Sort(SecurityExpiration.get_comparer( + SecurityExpiration.Properties.Expiration, + SecurityExpiration.Properties.Symbol, + SecurityExpiration.Properties.Type, + SecurityExpiration.Properties.Venue)); return sorted_exps; } diff --git a/src/client/spark/AW2/Misc/SystemContractData.cs b/src/client/spark/AW2/Misc/SystemContractData.cs index f31996402..09ed974b7 100644 --- a/src/client/spark/AW2/Misc/SystemContractData.cs +++ b/src/client/spark/AW2/Misc/SystemContractData.cs @@ -3,7 +3,9 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; +using bts; using bts.api; +using bts.collections; using bts.utils; using mbts.analytics; @@ -15,98 +17,83 @@ namespace mbts.spark { public class SystemContractData { internal readonly IReadOnlyDictionary sec_md_map; public readonly HashSet sectup_map = new HashSet(); - // for expirations that are contracts themselves (ie non options) - public readonly IReadOnlyDictionary exp_map = - new Dictionary(); public readonly IReadOnlyDictionary> strike_map = new Dictionary>(); - private SystemContractData() { } - public static readonly SystemContractData empty = new SystemContractData(); + readonly Lazy> futuresIndex; + readonly Lazy>> expirationsIndex; - class CPKey { - public Nullable ckey = null, pkey = null; - } - - static void _add_strike( - ref Dictionary> exp_k_map, - SecurityImmutable sec) { - bts.Log.assert(sec.HasOption); - SecurityExpiration sec_exp = new SecurityExpiration(sec.Exchange, - SecurityType.Option, sec.Symbol, sec.Option.expiration_date()); - Dictionary k_map = null; - if (!exp_k_map.TryGetValue(sec_exp, out k_map)) { - k_map = new Dictionary(); - exp_k_map.Add(sec_exp, k_map); - } - CPKey cp_key = null; - if (!k_map.TryGetValue(sec.Option.Strike, out cp_key)) { - cp_key = new CPKey(); - k_map.Add(sec.Option.Strike, cp_key); - } - if (sec.Option.CallPut == PutOrCall.Put) { - cp_key.pkey = sec.Id; - } else { - cp_key.ckey = sec.Id; - } - } + public static readonly SystemContractData empty = new(new Dictionary(0)); internal SystemContractData(IReadOnlyDictionary sec_map_arg) { sec_md_map = sec_map_arg; HashSet sectup_map_aux = new HashSet(); - Dictionary exp_map_aux = - new Dictionary(); - Dictionary> strike_map_temp = - new Dictionary>(); - foreach (var kv in sec_md_map) { - SecurityImmutable sec = kv.Value.security; - SecurityTuple sec_tup = - new SecurityTuple(sec.security_type(), sec.Symbol, sec.Exchange); - sectup_map_aux.Add(sec_tup); - SecurityExpiration sec_exp = - new SecurityExpiration(sec_tup, sec.security_expiration() ?? default, sec.Id); - exp_map_aux[sec_exp] = sec.Id; - if (sec.HasOption) { - _add_strike(ref strike_map_temp, sec); + sectup_map = sec_md_map.Values + .Select(smd => new SecurityTuple(smd.security.security_type(), smd.security.Symbol, smd.security.Exchange)) + .ToHashSet(); + // Index options + strike_map = sec_md_map.Values + .Select(smd => smd.security) + .Where(s => s.security_type() == SecurityType.Option) + .GroupBy(s => new SecurityExpiration(s.Exchange, SecurityType.Option, s.Symbol, s.Option.expiration_date())) + .ToDictionary( + expGroup => expGroup.Key, + expGroup => expGroup.GroupBy(s => s.Option.Strike).Select(strikeGroup => { + // 'strikeGroup' should have 2 securities: the call and the put. + uint? callId = null; + uint? putId = null; + foreach (var s in strikeGroup) { + switch (s.Option.CallPut) { + case PutOrCall.Call: + Log.assert(!callId.HasValue); + callId = s.Id; + break; + case PutOrCall.Put: + Log.assert(!putId.HasValue); + putId = s.Id; + break; + default: + Log.assert(false, $"Option sec has invalid CallPut '{s.Option.CallPut}'"); + break; + } + } + return new SecurityStrike(expGroup.Key, strikeGroup.Key, new(callId, putId)); + }) + .ToHashSet()); + // Index exp dates (for futures and options) + expirationsIndex = new(() => { + var rv = new Dictionary<(Venue Venue, SecurityType Type, string Symbol), HashSet>(); + foreach (var sec in sec_md_map.Values.Select(s => s.security)) { + switch (sec.security_type()) { + case SecurityType.Future: + case SecurityType.Option: + rv.GetOrAdd((sec.Exchange, sec.security_type(), sec.Symbol), _ => new()) + .Add(sec.earliest_expiration()); + break; + } } - } - Dictionary> strike_map_aux = - new Dictionary>(); - foreach (var kv in strike_map_temp) { - SecurityExpiration exp = kv.Key; - HashSet k_map = new HashSet(); - foreach (var kv_i in kv.Value) { - double k = kv_i.Key; - CPKey cp_key = kv_i.Value; - SecurityStrike sec_k = - new SecurityStrike(exp, k, new Tuple(cp_key.ckey, cp_key.pkey)); - k_map.Add(sec_k); + return rv; + }, true); + // Index futures + futuresIndex = new(() => { + var rv = new Dictionary<(Venue, string, DateTime), SecurityMD>(); + foreach (var smd in sec_md_map.Values.Where(smd => smd.security.security_type() == SecurityType.Future)) { + var key = (smd.security.Exchange, smd.security.Symbol, smd.security.Future.expiration_date()); + if (rv.TryGetValue(key, out var existing)) { + continue; + } + rv.Add(key, smd); } - strike_map_aux.Add(exp, k_map); - } - sectup_map = sectup_map_aux; - exp_map = exp_map_aux; - strike_map = strike_map_aux; - _log_data(); + return rv; + }, + true); } - private void _log_data() { - bts.Log.Gen.info( - String.Format( - "SystemContractData constructed, sec_tups:{0} total_exps:{1} option_exps:{2}", - sectup_map.Count, exp_map.Count, strike_map.Count)); - sectup_map.ForEach(st => bts.Log.Gen.info( - String.Format("SystemContractData loaded sectup:{0}", st.ToString()))); - foreach (var kv in exp_map) { - var log_txt = String.Format("SystemContractData loaded exp:{0} id:{1}", - kv.Key.ToString(), kv.Value); - if (kv.Key.security_type == SecurityType.Option) { - strike_map.TryGetValue(kv.Key, out var k_map); - log_txt += " n_strikes:" + ((k_map == null) ? 0 : k_map.Count); - } - bts.Log.Gen.info(log_txt); - } - } + public IEnumerable Futures => + futuresIndex.Value.Values; + + public ISet GetExpirations(Venue venue, SecurityType type, string symbol) => + expirationsIndex.Value.GetValueOrDefault((venue, type, symbol)) ?? new(0); public SecurityImmutable? get_option_contract( Venue venue, string symbol, DateTime exp, double strike, PutOrCall cp) { @@ -167,18 +154,17 @@ namespace mbts.spark { new VolSurfaceMDUnit(bid, ask, sec_md.md.fixing_price)); } - public Tuple - get_exp_md(SecurityExpiration sec_exp) { - uint sec_id = 0; - if (!exp_map.TryGetValue(sec_exp, out sec_id)) { + public Tuple get_exp_md(SecurityExpiration sec_exp) { + Log.assert(sec_exp.security_type == SecurityType.Future); + if (!futuresIndex.Value.TryGetValue((sec_exp.exchange, sec_exp.symbol, sec_exp.expiration), out var smd)) { return null; } - return get_sec_md(sec_id); + ulong time = 0; + smd.md.get_bbo(ref time, out var bid, out var ask); + return Tuple.Create(smd.security, new VolSurfaceMDUnit(bid, ask, smd.md.fixing_price)); } - internal Tuple>> - get_option_bbo_vsexp(VolSurfaceExpiration exp) { + internal Tuple>> get_option_bbo_vsexp(VolSurfaceExpiration exp) { // this is a workaround for VSExp not having an exchange foreach (Venue v in System.Enum.GetValues(typeof(Venue))) { var sec_exp = @@ -222,18 +208,16 @@ namespace mbts.spark { .ToDictionary(kv => kv.Key, kv => kv.Value.Select(k => k.strike)); } - public Nullable get_exp_sec_id(SecurityExpiration sec_exp) { - UInt32 rv; - if (exp_map.TryGetValue(sec_exp, out rv)) { - return rv; + public IEnumerable get_option_expirations(string option_name) { + foreach (var expGroup in expirationsIndex.Value) { + var (venue, type, symbol) = expGroup.Key; + if (type != SecurityType.Option || symbol != option_name) { + continue; + } + foreach (var exp in expGroup.Value) { + yield return exp; + } } - return null; - } - - public IEnumerable get_option_expirations(string option_name, - bool is_display_expiration = false) { - return exp_map.Keys.Where(e => e.security_type == SecurityType.Option && - e.symbol == option_name).Select(e => e.expiration); } public IDictionary> get_option_expirations( diff --git a/src/client/spark/Daemons/FakeSlideDataDaemon.cs b/src/client/spark/Daemons/FakeSlideDataDaemon.cs index d5150b39b..136d80c2b 100644 --- a/src/client/spark/Daemons/FakeSlideDataDaemon.cs +++ b/src/client/spark/Daemons/FakeSlideDataDaemon.cs @@ -37,8 +37,23 @@ namespace bts.daemons { s => get_fake_trade(to_sec_exp(s.security), 4200)).ToList(); bus.post(SparkTags.TradeLogTag, fake_trades); var bbo_upd_refs = btc_secs.ToDictionary(s => s.security.Id, _ => 0UL); - var driver_sym_prices = btc_secs.ToImmutableDictionary( - s => to_sec_exp(s.security), get_driver_price); + var driver_sym_prices_bd = ImmutableDictionary.CreateBuilder(); + var sec_exps_index = new Dictionary>(); + foreach (var btc_sec in btc_secs) { + var sec_exp = to_sec_exp(btc_sec.security); + var existing_secs = sec_exps_index.GetOrAdd(sec_exp, _ => new(1)); + existing_secs.Add(btc_sec.security); + if (driver_sym_prices_bd.ContainsKey(sec_exp)) { + continue; + } + driver_sym_prices_bd.Add(sec_exp, get_driver_price(btc_sec)); + } + foreach (var kv in sec_exps_index) { + if (kv.Value.Count > 1) { + Log.Md.warn($"Duplicate BTC driver secs detected in fake md: {string.Join(", ", kv.Value)}"); + } + } + var driver_sym_prices = driver_sym_prices_bd.ToImmutable(); double get_driver_price(SecurityMD smd) { var upd_ref = bbo_upd_refs[smd.security.Id]; @@ -64,7 +79,7 @@ namespace bts.daemons { })); static SecurityExpiration to_sec_exp(SecurityImmutable sec) => - new(sec.Exchange, sec.ContractCase, sec.Symbol, sec.security_expiration() ?? DateTime.MinValue, sec.Id); + new(sec.Exchange, sec.ContractCase, sec.Symbol, sec.security_expiration() ?? DateTime.MinValue); ITradeLogRow get_fake_trade( SecurityExpiration sec_exp, diff --git a/src/client/spark/NMSheets/Outside/ExchangeSecurityMap.cs b/src/client/spark/NMSheets/Outside/ExchangeSecurityMap.cs index 2237c671d..c700e313c 100644 --- a/src/client/spark/NMSheets/Outside/ExchangeSecurityMap.cs +++ b/src/client/spark/NMSheets/Outside/ExchangeSecurityMap.cs @@ -48,8 +48,9 @@ namespace BCG option_map_aux[k] = new[] { get_sec(call_id.Value), get_sec(put_id.Value) }; } } - var fut_map_aux = - sc_data.exp_map.ToDictionary(kv => kv.Key, kv => key_sec_map_aux[kv.Value]); + var fut_map_aux = sc_data.Futures.Select(smd => smd.security).ToDictionary( + sec => new SecurityExpiration(sec.Exchange, SecurityType.Future, sec.Symbol, sec.Future.expiration_date()), + sec => sec.CloneToMutable()); _key_sec_map = key_sec_map_aux; _option_map = option_map_aux; _fut_map = fut_map_aux;