From 867bcf97d0f10dd2ddffa42b79bbcf9c6f9163e2 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Fri, 25 Nov 2022 16:36:26 -0500 Subject: [PATCH] Added InterpTheosServerPublisher, which allows to publish precomputed theos to the server --- docker/linux | 2 +- .../AW2/Bus/VolSurfaceBlackboardManager.cs | 4 +- .../spark/AW2/InterpTheosServerPublisher.cs | 91 +++++++++++++++++++ .../spark/AW2/Misc/OptionDriverPriceMap.cs | 7 ++ .../spark/AW2/Misc/SecurityNormalizer.cs | 66 ++++++++++++++ .../spark/AW2/Misc/SystemContractData.cs | 17 ++++ src/client/spark/Daemons/AnalyticsDaemon.cs | 7 +- 7 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 src/client/spark/AW2/InterpTheosServerPublisher.cs create mode 100644 src/client/spark/AW2/Misc/SecurityNormalizer.cs diff --git a/docker/linux b/docker/linux index 56c61231f..653781226 160000 --- a/docker/linux +++ b/docker/linux @@ -1 +1 @@ -Subproject commit 56c61231f38261f275e0b26baae70622c760dc33 +Subproject commit 65378122620c7a26561641cb49b477c822b315f2 diff --git a/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs b/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs index 8e1bc48a3..2bdc6d019 100644 --- a/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs +++ b/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs @@ -77,7 +77,8 @@ namespace mbts.spark { private HighPerformanceTimer _timer = new HighPerformanceTimer(); public VolSurfaceBusManager(bts.api.IBus bus, - bts.api.analytics.AnalyticsService analytics_service) { + bts.api.analytics.AnalyticsService analytics_service, + bts.api.orderman.OrderService order_service) { bts.Log.Ui.info("VSBusManager cons tid:{VolSurfaceTroubleshootingFunctions.tid()}"); _bus = bus; _bb = new ThreadedBlackboard("AW2", bus); @@ -111,6 +112,7 @@ namespace mbts.spark { _bb, SecurityType.Option, sym => SparkTags.TC2Tags.LiveTheosTag(sym), SparkTags.TC2Tags.AllLiveTheosTag)); _subs.Add(new LiveTheoPublisher(_bb)); + _subs.Add(new InterpTheosServerPublisher(_bb, order_service)); _subs.Add(new VolSurfaceInterpGreeksMultiplexer(_bb)); // usage var list_sub = _bus.subscribe(SparkTags.VolSurfaceListTag).OnDispatcher().Subscribe(t => { diff --git a/src/client/spark/AW2/InterpTheosServerPublisher.cs b/src/client/spark/AW2/InterpTheosServerPublisher.cs new file mode 100644 index 000000000..9d951600e --- /dev/null +++ b/src/client/spark/AW2/InterpTheosServerPublisher.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using mbts.analytics; +using bts.api; +using mbts.ui; +using MathNet.Numerics.Providers.FourierTransform; +using BCG; +using System.Windows.Automation; +using bts.collections; +using System.Linq; + +namespace mbts.spark { + // Listens to the interp theos, decorates the data + // and sends them over to the sparkd server + public class InterpTheosServerPublisher : IDisposable { + private readonly ThreadedBlackboard _bb; + private readonly bts.api.orderman.OrderService _order_service; + + public InterpTheosServerPublisher(ThreadedBlackboard bb, + bts.api.orderman.OrderService order_service) { + bts.Log.Ui.info( + $"InterpTheosServerPublisher cons tid:{VolSurfaceTroubleshootingFunctions.tid()}"); + _bb = bb; + _order_service = order_service; + _bb.subscribe(SparkTags.AllInterpGreeks); + _bb.subscribe(SparkTags.SystemContractsTag); + _bb.data_callback += _on_data; + } + + private void _on_data(object tag, object data) { + if (tag.Equals(SparkTags.SystemContractsTag) || + tag.Equals(SparkTags.AllInterpGreeks)) { + _publish_data(); + } + } + + // private UInt32 _get_driver_key( + // IReadOnlyDictionary driver_map) + + private IEnumerable _get_matching_options( + string option_symbol, DateTime exp, double strike, + PutOrCall cp, SystemContractData sc_data) { + return sc_data.get_securities( + (sec_md) => { + var s = sec_md.security; + return s.security_type() == SecurityType.Option && + s.Symbol == option_symbol && s.earliest_expiration() == exp && + s.Option.Strike == strike && s.Option.CallPut == cp; + }); + } + + private static IDictionary _get_drivers( + IReadOnlyDictionary driver_map, + SystemContractData sc_data) { + var rv = new Dictionary(); + foreach (var kv in driver_map) { + rv.AddRange( + SecurityNormalizer.normalize_drivers(kv.Value, sc_data).ToArray()); + } + return rv; + } + + private static IDictionary _get_theos( + IReadOnlyDictionary theo_data, + SystemContractData sc_data) { + var rv = new Dictionary(); + foreach (var kv in theo_data) { + rv.AddRange( + SecurityNormalizer.normalize_interp_theos(kv.Value, sc_data).ToArray()); + } + return rv; + } + + private void _publish_data() { + SystemContractData sc_data = _bb.read_state(SparkTags.SystemContractsTag, null); + IReadOnlyDictionary all_theos = + _bb.read_state(SparkTags.AllInterpGreeks, null); + IReadOnlyDictionary all_drivers = + _bb.read_state(SparkTags.AllOptionDriverBBOTag, null); + if (sc_data == null || all_theos == null || all_drivers == null) + return; + IDictionary driver_map = _get_drivers(all_drivers, sc_data); + IDictionary theo_map = _get_theos(all_theos, sc_data); + // assemble it together and send it to the server + } + + public void Dispose() { + _bb.data_callback -= _on_data; + } + } +} \ No newline at end of file diff --git a/src/client/spark/AW2/Misc/OptionDriverPriceMap.cs b/src/client/spark/AW2/Misc/OptionDriverPriceMap.cs index 989854515..cfe2a3e03 100644 --- a/src/client/spark/AW2/Misc/OptionDriverPriceMap.cs +++ b/src/client/spark/AW2/Misc/OptionDriverPriceMap.cs @@ -1,5 +1,7 @@ #nullable enable namespace mbts.spark { + using bts.api; + using mbts.analytics; using System; @@ -39,5 +41,10 @@ namespace mbts.spark { .SelectMany(kv => kv.Value.exp_price_map) .ToDictionaryCrushed(kv => kv.Key.to_vs_exp(), kv => kv.Value); } + + public IReadOnlyDictionary get_price_map() { + return new Dictionary(exp_price_map); + } + } } diff --git a/src/client/spark/AW2/Misc/SecurityNormalizer.cs b/src/client/spark/AW2/Misc/SecurityNormalizer.cs new file mode 100644 index 000000000..f66d6263c --- /dev/null +++ b/src/client/spark/AW2/Misc/SecurityNormalizer.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using mbts.analytics; +using bts.api; +using mbts.ui; +using MathNet.Numerics.Providers.FourierTransform; +using BCG; +using System.Windows.Automation; +using bts.collections; + +namespace mbts.spark { + + // A lot of options data (migrated from edge) is not normalized + // on the sec_id, but rather on a combination of symbol, exp, strike. + // Additionally the exchange is missing. This class serves as that + // one since point to resolve that. + // (Over time we will normalize all the data and this wont be needed) + public static class SecurityNormalizer { + // f(option_exp:driver_md) -> (sec_id:driver_md) + + public static IDictionary normalize_drivers( + OptionDriverPriceMap driver_map, SystemContractData sc_data) { + Dictionary rv = new Dictionary(); + foreach (var kv in driver_map.get_price_map()) { + var exp = kv.Key; + var dmd = kv.Value; + IEnumerable exp_secs = sc_data.get_securities( + (sec_md) => { + var s = sec_md.security; + return s.security_type() == SecurityType.Option && + s.Symbol == driver_map.option_symbol && s.earliest_expiration() == exp.expiration; + } + ); + foreach (var sec_md in exp_secs) { + rv.Add(sec_md.security.Id, kv.Value); + } + } + return rv; + } + public static IDictionary normalize_interp_theos( + OptionTheoInterpolationData symbol_theos, SystemContractData sc_data) { + Dictionary rv = new Dictionary(); + foreach (DateTime exp in symbol_theos.get_expirations()) { + Dictionary> exp_data = + symbol_theos.get_expiration_data(exp); + foreach (var exp_kv in exp_data) { + double strike = exp_kv.Key; + foreach (var k_kv in exp_kv.Value) { + 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(sec_md.security.Id, tp); + } + foreach (SecurityMD sec_md in puts) { + rv.Add(sec_md.security.Id, tp); + } + } + } + } + return rv; + } + } +} \ No newline at end of file diff --git a/src/client/spark/AW2/Misc/SystemContractData.cs b/src/client/spark/AW2/Misc/SystemContractData.cs index 1a08b773c..f31996402 100644 --- a/src/client/spark/AW2/Misc/SystemContractData.cs +++ b/src/client/spark/AW2/Misc/SystemContractData.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; @@ -265,5 +266,21 @@ namespace mbts.spark { } } + public IEnumerable get_securities(Func criteria) { + 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); + }); + } + } } diff --git a/src/client/spark/Daemons/AnalyticsDaemon.cs b/src/client/spark/Daemons/AnalyticsDaemon.cs index 98010a4f6..a1ec5ee39 100644 --- a/src/client/spark/Daemons/AnalyticsDaemon.cs +++ b/src/client/spark/Daemons/AnalyticsDaemon.cs @@ -23,6 +23,7 @@ namespace bts { class AnalyticsDaemon : IShellDaemon, IDisposable { private readonly IBus _bus; private readonly AnalyticsService _analytics_service; + private readonly bts.api.orderman.OrderService _order_service; private readonly VolSurfaceBusManager _vs_manager; private readonly SpreadManager _spread_manager; private readonly SpreadCalculationSynchronizer _spread_calc; @@ -30,11 +31,13 @@ namespace bts { IDisposable _dispose; [ImportingConstructor] - public AnalyticsDaemon(IBus bus, AnalyticsService analytics_service) { + public AnalyticsDaemon(IBus bus, AnalyticsService analytics_service, + bts.api.orderman.OrderService order_service) { bts.Log.Gen.info("AnalyticsDaemon started"); _bus = bus; _analytics_service = analytics_service; - _vs_manager = new VolSurfaceBusManager(_bus, _analytics_service); + _order_service = order_service; + _vs_manager = new VolSurfaceBusManager(_bus, _analytics_service, _order_service); _spread_manager = new SpreadManager(_vs_manager.bb(), "xxx"); _spread_calc = new SpreadCalculationSynchronizer(_vs_manager.bb()); }