Cache/index options for faster lookups in SystemContractData

This commit is contained in:
John Colvin authored and Adeel Khan committed 2024-10-03 17:46:51 +05:00
1 parent 0268bd476f
commit 4cb3dbc313
2 files changed
+25 -20

No files matched your search

@@ -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<SecurityMD> calls = sc_data.get_options(
null, symbol_theos.symbol, exp, strike, PutOrCall.Call);
IEnumerable<SecurityMD> 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));
}
}
}
+17 -12
View File
@@ -21,6 +21,7 @@ namespace mbts.spark {
strike_map = new Dictionary<SecurityExpiration, HashSet<SecurityStrike>>();
readonly Lazy<Dictionary<(Venue Venue, string Symbol, DateTime Expiration), SecurityMD>> futuresIndex;
readonly Lazy<Dictionary<(Venue Venue, string Symbol, DateTime Expiration, double Strike, PutOrCall CallPut), SecurityMD>> optionsIndex;
readonly Lazy<Dictionary<(Venue Venue, SecurityType Type, string Symbol), HashSet<DateTime>>> expirationsIndex;
public static readonly SystemContractData empty = new(new Dictionary<uint, SecurityMD>(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<SecurityMD> Futures =>
@@ -257,17 +271,8 @@ namespace mbts.spark {
return sec_md_map.Values.Where(sec_md => criteria(sec_md));
}
public IEnumerable<SecurityMD> 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;
}
}