From ff2d150dc463f6c2b3151a169042616c678ef175 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Wed, 17 Apr 2019 17:33:46 -0500 Subject: [PATCH] restricting orders with piqs to just ladders for now --- src/client/spark/Daemons/OrderTicketDaemon.cs | 6 +- .../Notifications/INotificationService.cs | 2 +- .../INotificationServiceDataCache.cs | 2 +- .../spark/Orders/OrderActivitySummarizer.cs | 10 +-- src/client/spark/Orders/OrderSecurity.cs | 21 +++++-- src/client/spark/Orders/OrdersModel.cs | 61 +++++++++++++++---- .../spark/view/Spark/Ladder/LadderVM.cs | 2 +- 7 files changed, 75 insertions(+), 29 deletions(-) diff --git a/src/client/spark/Daemons/OrderTicketDaemon.cs b/src/client/spark/Daemons/OrderTicketDaemon.cs index 568a6b1ac..84e473c46 100644 --- a/src/client/spark/Daemons/OrderTicketDaemon.cs +++ b/src/client/spark/Daemons/OrderTicketDaemon.cs @@ -19,7 +19,7 @@ namespace bts.shell { readonly OrdersModel _model; readonly ErrorReporter _error_reporter; IReadOnlyDictionary _md_map; - TableUpdate _orders; + TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> _orders; IDisposable _dispose; [ImportingConstructor] @@ -60,13 +60,13 @@ namespace bts.shell { } void _show_modify_order(uint order_key) { - if (_orders.state.TryGetValue(order_key, out var ori) && ori.security.HasValue + if (_orders.state.TryGetValue(order_key, out var ori) && ori.sec.HasValue && (ori.order.HasDef && ori.order.Def.HasCustomDef && !(ori.order.Def.CustomDef.HasStop || ori.order.Def.CustomDef.HasStopLimit))) { var md = _md_map.TryGetValue(ori.order.security_key()); _show_order_window(new OrderModificationDialog { DataContext = new ExistingOrderTicketVM(_model, Observable.Return((md,ori.order)), - Observable.Return(_model.factory.default_ladder_settings(ori.security.Value).calculator_buttons) + Observable.Return(_model.factory.default_ladder_settings(ori.sec.Value).calculator_buttons) ) }); } diff --git a/src/client/spark/Notifications/INotificationService.cs b/src/client/spark/Notifications/INotificationService.cs index 820e2ed72..8e294a368 100644 --- a/src/client/spark/Notifications/INotificationService.cs +++ b/src/client/spark/Notifications/INotificationService.cs @@ -55,7 +55,7 @@ namespace bts.shell { void _on_activity(Activity activity) => _activity_notifications.process_item(activity); - void _on_orders(TableUpdate update) { + void _on_orders(TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> update) { // must update notification_service_data_cache first, some of the predicate/triggers (esp. ToastNotifications) will use the notification_service_data_cache to lookup order-history _data_cache.update(update.as_iincremental()); foreach (var o in update.added.Concat(update.changed)) diff --git a/src/client/spark/Notifications/INotificationServiceDataCache.cs b/src/client/spark/Notifications/INotificationServiceDataCache.cs index cf51f6e14..fac9deeed 100644 --- a/src/client/spark/Notifications/INotificationServiceDataCache.cs +++ b/src/client/spark/Notifications/INotificationServiceDataCache.cs @@ -50,7 +50,7 @@ namespace bts.shell { public DateTime last_order_reset_time { get; private set; } public bool is_first_order_update { get; private set; } - internal void update(IIncrementalUpdate data) { + internal void update(IIncrementalUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> data) { DateTime now = DateTime.Now; if (data.removed.Count == _order_cache.Count) { last_order_reset_time = now; diff --git a/src/client/spark/Orders/OrderActivitySummarizer.cs b/src/client/spark/Orders/OrderActivitySummarizer.cs index b085dddc9..b4b8e8b3d 100644 --- a/src/client/spark/Orders/OrderActivitySummarizer.cs +++ b/src/client/spark/Orders/OrderActivitySummarizer.cs @@ -27,7 +27,7 @@ namespace bts.spark { static readonly IEqualityComparer _id_comparer = bts.utils.EqualityComparer.Create(a => a.Key); - public OrderActivitySummarizer(IBus bus, IObservable> orders) { + public OrderActivitySummarizer(IBus bus, IObservable> orders) { _bus = bus; _dispose = Disposable.Create( bus.subscribe(SparkTags.SecuritiesTag).Subscribe(sec => _security = sec), @@ -124,12 +124,12 @@ namespace bts.spark { #endregion - void orders_updated(TableUpdate update) { + void orders_updated(TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> update) { foreach (var o in update.added) { if (!string.IsNullOrEmpty(o.order.State.LastMsg) && o.order.State.Created.ToDateTime().Date == DateTime.Today) { - if (o.security.HasValue) { - log_status(ActivityLogCategory.warn, o.security.Value, o.order.State.LastMsg); + if (o.sec.HasValue) { + log_status(ActivityLogCategory.warn, o.sec.Value, o.order.State.LastMsg); } } } @@ -137,7 +137,7 @@ namespace bts.spark { foreach (var order in update.changes) { var neworder = order.New.order; var oldorder = order.Old.order; - var security = order.New.security ?? order.Old.security; + var security = order.New.sec ?? order.Old.sec; // // description var mod_desc = modified_description(oldorder, neworder, security?.ContractSpec); diff --git a/src/client/spark/Orders/OrderSecurity.cs b/src/client/spark/Orders/OrderSecurity.cs index 71889e5a6..7045f2fe4 100644 --- a/src/client/spark/Orders/OrderSecurity.cs +++ b/src/client/spark/Orders/OrderSecurity.cs @@ -4,16 +4,27 @@ using System; namespace bts.api.orderman { public readonly struct OrderSecurity: IEquatable { + public OrderSecurity(SecurityImmutable? sec, OrderRecordImmutable order) { + this.security = sec; + this.order = order; + piq = null; + } + public OrderSecurity((SecurityImmutable? sec, OrderRecordImmutable order) t) { + security = t.sec; + order = t.order; + piq = null; + } public OrderSecurity(SecurityImmutable? sec, OrderRecordImmutable order, uint? piq) { this.security = sec; this.order = order; this.piq = piq; } - public OrderSecurity(OrderSecurity t) { - security = t.security; - order = t.order; - piq = t.piq; - } + //public OrderSecurity(OrderSecurity t) { + // security = t.security; + // order = t.order; + // piq = t.piq; + //} + public static implicit operator OrderSecurity(ValueTuple x) => new OrderSecurity(x); public SecurityImmutable? security { get; } public OrderRecordImmutable order { get; } public uint? piq { get; } diff --git a/src/client/spark/Orders/OrdersModel.cs b/src/client/spark/Orders/OrdersModel.cs index 694260926..7172a93cf 100644 --- a/src/client/spark/Orders/OrdersModel.cs +++ b/src/client/spark/Orders/OrdersModel.cs @@ -20,7 +20,8 @@ using System.Reactive.Concurrency; using System.Reactive.Linq; using System.Reactive.Subjects; using System.Threading.Tasks; -using OrderUpdate = bts.api.core.TableUpdate; +using OrderUpdate = bts.api.core.TableUpdate<(bts.api.SecurityImmutable? sec, bts.api.orderman.OrderRecordImmutable order), uint>; +using OrderUpdatePiq = bts.api.core.TableUpdate; namespace mbts.spark { [Export] @@ -54,7 +55,7 @@ namespace mbts.spark { this.algo_model = algo_model; var scheduler = ThreadPoolScheduler.Instance; var interval = TimeSpan.FromMilliseconds(100); - var pre_on_orders = + on_orders = _service .subscribei_orders() .Naggle(interval, scheduler) @@ -62,8 +63,9 @@ namespace mbts.spark { .join(sstore, order => order.security_key()) .Table() .Cache(); + if (_piq_enabled) { - on_orders = pre_on_orders + on_orders_with_piq = on_orders .LeftJoin< (SecurityImmutable?, OrderRecordImmutable), OrderPIQImmutable, @@ -75,9 +77,9 @@ namespace mbts.spark { (si_ort, r, p) => new OrderSecurity(si_ort.Item1, si_ort.Item2, p.Position), (si_ort, r) => new OrderSecurity(si_ort.Item1, si_ort.Item2, null), osp => osp.order.Key); - } else { - on_orders = pre_on_orders.Select((ft) => new OrderSecurity(ft.Item1, ft.Item2, null), os => os.order.Key) + on_orders_with_piq = on_orders + .Select((ft) => new OrderSecurity(ft.Item1, ft.Item2, null), os => os.order.Key) .Replay(1); } @@ -88,11 +90,18 @@ namespace mbts.spark { (workers, table) => _update_working_orders(workers, table)) .Replay(1).RefCount(); + working_orders_with_piq = + on_orders_with_piq.Scan( + ImmutableDictionary.Empty, + (workers, table) => _update_working_orders(workers, table)) + .Replay(1).RefCount(); + + on_order_securities = on_orders .as_iincremental() - .filter(t => t.security.HasValue) - .Select(t => t.security.Value); + .filter(t => t.sec.HasValue) + .Select(t => t.sec.Value); _activity = new OrderActivitySummarizer(bus, on_orders); _security_orders_demuxer = new TableUpdateDemuxer(o => o.Key, o => o.security_key()); @@ -109,8 +118,8 @@ namespace mbts.spark { t => t.order.Key, am => am.algo.key, t => new AlgoKey(new bts.persist.algo.AlgoPersistedKey(t.order.Def.AlgoTemplateKey)), - (os, okey, am, mkey) => new OrderSecurityAlgoMeta(os.order, os.security, am.algo, am.meta), - (os, okey, mkey) => new OrderSecurityAlgoMeta(os.order, os.security, null, null), + (os, okey, am, mkey) => new OrderSecurityAlgoMeta(os.order, os.sec, am.algo, am.meta), + (os, okey, mkey) => new OrderSecurityAlgoMeta(os.order, os.sec, null, null), ot => ot.order.Key); on_order_tuples = order_secs_algo_meta_join; var on_fills = @@ -129,9 +138,8 @@ namespace mbts.spark { f => f.fill.FillKey); on_fill_tuples = fill_order_tuples_join; _dispose = Disposable.Create( - // on_orders changed from an IObservable to an IConnectableObservable, so we call - // RefCount() here to start the connection - on_orders.RefCount().Select(t => t.order, o => o.Key).Subscribe(_security_orders_demuxer), + on_orders.Select(t => t.order, o => o.Key).Subscribe(_security_orders_demuxer), + on_orders_with_piq.RefCount().Select(t => t.order, o => o.Key).Subscribe(_security_orders_demuxer), on_positions.Subscribe(_security_positions_demuxer), order_secs_algo_meta_join.Connect(), algo_meta_join.Connect(), @@ -148,11 +156,13 @@ namespace mbts.spark { public OrderFactory factory => _factory; public SpreaderModel spreader => _spreader; public DynamicOrderHelper dynamic_helper => _dynamic_helper; - public IConnectableObservable on_orders { get; } + public IObservable on_orders { get; } + public IConnectableObservable on_orders_with_piq { get; } public IObservable> on_order_tuples { get; } public IObservable> on_order_securities { get; } public IObservable> on_fill_tuples { get; } public IObservable> working_orders { get; } + public IObservable> working_orders_with_piq { get; } public IObservable> on_positions { get; } public AdminModel admin_model { get; } @@ -249,8 +259,33 @@ namespace mbts.spark { } return clone.ToImmutableDictionary(); } + + static ImmutableDictionary + _update_working_orders(ImmutableDictionary target, OrderUpdatePiq table) { + var clone = target.ToBuilder(); + foreach (var o in table.removed) { + if (o.order.is_active()) { + clone.Remove(o.order.Key); + } + } + foreach (var o in table.changes) { + if (o.Old.order.is_active()) { + clone.Remove(o.Old.order.Key); + } + if (o.New.order.is_active()) { + clone.Add(o.New.order.Key, o.New); + } + } + foreach (var o in table.added) { + if (o.order.is_active()) { + clone.Add(o.order.Key, o); + } + } + return clone.ToImmutableDictionary(); + } } + // usinig new OrdersModel extensions to reduce the amount of code change needed to switch uis over to the new OrdersModel, // without changing any code in the actual new OrdersModel class. static class OrdersModelExtensions { diff --git a/src/client/spark/view/Spark/Ladder/LadderVM.cs b/src/client/spark/view/Spark/Ladder/LadderVM.cs index fadbf56b0..b32e25417 100644 --- a/src/client/spark/view/Spark/Ladder/LadderVM.cs +++ b/src/client/spark/view/Spark/Ladder/LadderVM.cs @@ -133,7 +133,7 @@ namespace mbts.spark { account_positions, size_calculator = new CalculatorLogic(0, _settings_manager.subscribe_current().Select(s => s.calculator_buttons)), bus.subscribe(SparkTags.LogActivity).OnDispatcher().Subscribe(status_update), - model.order_gateway.working_orders.OnDispatcher().Subscribe(working => { + model.order_gateway.working_orders_with_piq.OnDispatcher().Subscribe(working => { // Here we manage N orders to 1 price slot in the ladder Dictionary> orders_used_in_this_update = new Dictionary>(_equality);