diff --git a/src/client/spark/AW2/Misc/SecurityNormalizer.cs b/src/client/spark/AW2/Misc/SecurityNormalizer.cs index 04ae36ca3..5abeabdbe 100644 --- a/src/client/spark/AW2/Misc/SecurityNormalizer.cs +++ b/src/client/spark/AW2/Misc/SecurityNormalizer.cs @@ -51,15 +51,15 @@ namespace mbts.spark { foreach (var k_kv in exp_kv.Value) { double strike = k_kv.Key; TheoPoint tp = k_kv.Value; - IEnumerable calls = sc_data.get_options( - null, symbol_theos.symbol, exp, strike, PutOrCall.Call); - IEnumerable puts = sc_data.get_options( - null, symbol_theos.symbol, exp, strike, PutOrCall.Put); - foreach (SecurityMD sec_md in calls) { - rv.Add(Tuple.Create(sec_md.security, tp)); + var call = sc_data.get_option( + symbol_theos.venue, symbol_theos.symbol, exp, strike, PutOrCall.Call); + var put = sc_data.get_option( + symbol_theos.venue, symbol_theos.symbol, exp, strike, PutOrCall.Put); + if (call.HasValue) { + rv.Add(Tuple.Create(call.Value.security, tp)); } - foreach (SecurityMD sec_md in puts) { - rv.Add(Tuple.Create(sec_md.security, tp)); + if (put.HasValue) { + rv.Add(Tuple.Create(put.Value.security, tp)); } } } diff --git a/src/client/spark/AW2/Misc/SystemContractData.cs b/src/client/spark/AW2/Misc/SystemContractData.cs index 1364f322a..b4fdb1a9e 100644 --- a/src/client/spark/AW2/Misc/SystemContractData.cs +++ b/src/client/spark/AW2/Misc/SystemContractData.cs @@ -21,6 +21,7 @@ namespace mbts.spark { strike_map = new Dictionary>(); readonly Lazy> futuresIndex; + readonly Lazy> optionsIndex; readonly Lazy>> expirationsIndex; public static readonly SystemContractData empty = new(new Dictionary(0)); @@ -87,6 +88,19 @@ namespace mbts.spark { return rv; }, true); + // Index options + optionsIndex = new(() => { + var rv = new Dictionary<(Venue, string, DateTime, double, PutOrCall), SecurityMD>(); + foreach (var smd in sec_md_map.Values.Where(smd => smd.security.security_type() == SecurityType.Option)) { + var sec = smd.security; + var key = (sec.Exchange, sec.Symbol, sec.Option.expiration_date(), sec.Option.Strike, sec.Option.CallPut); + if (rv.TryGetValue(key, out var existing)) { + continue; + } + rv.Add(key, smd); + } + return rv; + }); } public IEnumerable Futures => @@ -257,17 +271,8 @@ namespace mbts.spark { return sec_md_map.Values.Where(sec_md => criteria(sec_md)); } - public IEnumerable get_options( - Venue? exchange, string option_name, DateTime? exp, double? strike, PutOrCall? cp) { - return get_securities(sc => { - var s = sc.security; - return s.security_type() == SecurityType.Option && - (exchange == null || s.Exchange == exchange) && - (exp == null || s.earliest_expiration() == exp) && - (strike == null || s.strike() == strike) && - (cp == null || s.Option.CallPut == cp); - }); - } - + public SecurityMD? get_option( + Venue exchange, string symbol, DateTime exp, double strike, PutOrCall cp) => + optionsIndex.Value.TryGetValue((exchange, symbol, exp, strike, cp), out var smd) ? smd : null; } }