From d168d8712e6d5d125c2caabb6c156bd7e1d8cf9b Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Thu, 25 Aug 2022 11:13:03 -0400 Subject: [PATCH] Added Keyed Theos that can be used to get theos from security ids --- .../AW2/Bus/VolSurfaceBlackboardManager.cs | 48 +++++++++++++++++++ .../spark/AW2/Bus/VolSurfaceTheoPoint.cs | 20 ++++++++ src/client/spark/core/Bus/SparkTags.cs | 5 ++ 3 files changed, 73 insertions(+) create mode 100644 src/client/spark/AW2/Bus/VolSurfaceTheoPoint.cs diff --git a/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs b/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs index 51b575596..6f5deda0a 100644 --- a/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs +++ b/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs @@ -145,6 +145,54 @@ namespace mbts.spark { _spin_vss(list_imm); }); _disps.Add(list_sub); + // f(contract keyed theos, securities) -> id keyed theos + // (so the newer UI controls can access theos vis sec keys) + var theo_sub = + _bus.subscribe(SparkTags.TC2Tags.AllLiveTheosTag) + .CombineLatest(bus.subscribe(SparkTags.SystemContractsTag), + (theos, sc_data) => (theos, sc_data)).Subscribe( + tup => { + _assemble_keyed_theos(tup.sc_data, tup.theos); + } + ); + _disps.Add(theo_sub); + } + + private void _assemble_keyed_theos( + SystemContractData sc_data, + IReadOnlyDictionary> theos) { + if (sc_data == null || theos == null) + return; + HighPerformanceTimer timer = new HighPerformanceTimer(); + timer.start(); + Dictionary rv = new Dictionary(); + foreach (var kv in theos) { + var symbol = kv.Key; + ExpStrikeKeyedData sym_theos = kv.Value; + if (sym_theos == null) + continue; + foreach (var s_kv in sym_theos.get_data_linearly()) { + DateTime exp = s_kv.first; + double strike = s_kv.second; + TheoPoint tp = s_kv.third; + // TODO: fix exchange later + Tuple keys = + sc_data.get_option_sec_id(Venue.Deribit, symbol, exp, strike); + if (keys != null) { + var c_key = keys.Item1; + var p_key = keys.Item2; + if (c_key.HasValue) { + rv[c_key.Value] = new VolSurfaceTheoPoint(tp, true); + } + if (p_key.HasValue) { + rv[p_key.Value] = new VolSurfaceTheoPoint(tp, false); + } + } + } + } + timer.stop(); + Log.Gen.info($"VolSurfaceBusManager keyed theos count: {rv.Count} " + + $"time: {timer.lastIntervalTime}"); } public ThreadedBlackboard bb() { diff --git a/src/client/spark/AW2/Bus/VolSurfaceTheoPoint.cs b/src/client/spark/AW2/Bus/VolSurfaceTheoPoint.cs new file mode 100644 index 000000000..3c3820436 --- /dev/null +++ b/src/client/spark/AW2/Bus/VolSurfaceTheoPoint.cs @@ -0,0 +1,20 @@ + + +namespace mbts.analytics { + using mbts.spark; + + // TheoPoint (from edge) has both calls and puts in the same structure. + // This shoudl dis-entangle that + public class VolSurfaceTheoPoint { + public readonly double theo = double.NaN; + public readonly double delta = double.NaN; + public readonly double driver = double.NaN; + public VolSurfaceTheoPoint(TheoPoint tp, bool is_call) { + if (tp != null) { + theo = is_call ? tp.fwd_normalized_call : tp.fwd_normalized_put; + delta = is_call ? tp.cdelta : tp.pdelta; + driver = tp.driver; + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/core/Bus/SparkTags.cs b/src/client/spark/core/Bus/SparkTags.cs index f14fa25d8..991aa1050 100644 --- a/src/client/spark/core/Bus/SparkTags.cs +++ b/src/client/spark/core/Bus/SparkTags.cs @@ -351,6 +351,11 @@ namespace mbts.spark { static KeyedSparkTag _mk_tag(TKey key, [CallerMemberName] string name = null) => new(key, name ?? throw new ArgumentNullException(nameof(name))); + // The bus theo data, because of legacy, is published without sec id's. This + // data (constructed out of the afore mentioned data) is keyed on the id's + public static ITag> KeyedTheos { get; } = + _mk_tag>(); + public class Nothing { Nothing() { } } public class TC2Tags {