From e05fc076e88748e087b0e083cef58d35dbc88ebc Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Thu, 7 Feb 2019 10:32:13 -0500 Subject: [PATCH] Update fills to use SecurityStore.join --- .../spark/Modules/Trading/Fills/FillsVM.cs | 5 +- src/client/spark/Orders/FillTuple.cs | 10 +- src/client/spark/Orders/FillTupleSource.cs | 99 ++++++------------- src/client/spark/Orders/OrdersModel.cs | 6 +- src/client/spark/Security/MDLookup.cs | 30 ++++++ src/client/spark/Security/SecurityStore.cs | 4 +- .../spark/view/Spark/PAndL/PAndLModel.cs | 9 +- src/client/spark/view/Spark/PAndL/PnLData.cs | 14 ++- 8 files changed, 85 insertions(+), 92 deletions(-) create mode 100644 src/client/spark/Security/MDLookup.cs diff --git a/src/client/spark/Modules/Trading/Fills/FillsVM.cs b/src/client/spark/Modules/Trading/Fills/FillsVM.cs index 595108167..f3586e228 100644 --- a/src/client/spark/Modules/Trading/Fills/FillsVM.cs +++ b/src/client/spark/Modules/Trading/Fills/FillsVM.cs @@ -39,7 +39,7 @@ namespace mbts.spark { user_filter = DiscreteValuesFilterVM.Create(_model.factory.subscribe_users().Select(d => d.Values), r => r.user, nameof(Row.user)); security_filter = DiscreteValuesFilterVM.Create( fills_source.with_respect_to( - ft => ft.security_md.HasValue ? ft.security_md.Value.security : SecurityDataHelper.Defaults.SecurityImmutable, + ft => ft.security ?? SecurityDataHelper.Defaults.SecurityImmutable, s => s.Id, ft => ft.key ).Select(t => (t.added.Values, t.removed.Values)), r => r.security, @@ -186,7 +186,8 @@ namespace mbts.spark { readonly FillImmutable _fill; readonly OrderRecordImmutable _order; - public Row(FillTuple fill_tuple, OrdersModel model) : base(fill_tuple.security_md.HasValue ? fill_tuple.security_md.Value.security : SecurityDataHelper.Defaults.SecurityImmutable) { + public Row(FillTuple fill_tuple, OrdersModel model) : + base(fill_tuple.security ?? SecurityDataHelper.Defaults.SecurityImmutable) { _fill = fill_tuple.fill; _order = fill_tuple.order ?? OrderDataHelper.Defaults.OrderRecordImmutable; _model = model; diff --git a/src/client/spark/Orders/FillTuple.cs b/src/client/spark/Orders/FillTuple.cs index 4481d2ffa..80f7eb946 100644 --- a/src/client/spark/Orders/FillTuple.cs +++ b/src/client/spark/Orders/FillTuple.cs @@ -10,14 +10,14 @@ namespace mbts.spark { class FillTuple : IEquatable { public readonly FillImmutable fill; public readonly OrderRecordImmutable? order; - public readonly SecurityMD? security_md; + public readonly SecurityImmutable? security; public readonly FillTupleKey key; - public FillTuple(FillImmutable fill, OrderRecordImmutable? order, SecurityMD? security_md) { + public FillTuple(FillImmutable fill, OrderRecordImmutable? order, SecurityImmutable? security) { this.fill = fill; this.order = order; - this.security_md = security_md; - key = new FillTupleKey(fill.FillKey, order.HasValue ? order.Value.Key : 0u, security_md.HasValue ? security_md.Value.security.Id : 0u); + this.security = security; + key = new FillTupleKey(fill.FillKey, order.HasValue ? order.Value.Key : 0u, security?.Id ?? 0); } public string account { @@ -31,7 +31,7 @@ namespace mbts.spark { public bool Equals(FillTuple other) => other != null && other.order.HasValue == order.HasValue - && other.security_md.HasValue == security_md.HasValue + && other.security.HasValue == security.HasValue && other.fill.Equals(fill); public override bool Equals(object obj) => Equals(obj as FillTuple); diff --git a/src/client/spark/Orders/FillTupleSource.cs b/src/client/spark/Orders/FillTupleSource.cs index 18629764e..dc8962b70 100644 --- a/src/client/spark/Orders/FillTupleSource.cs +++ b/src/client/spark/Orders/FillTupleSource.cs @@ -1,4 +1,5 @@ -using bts.api.core; +using bts.api; +using bts.api.core; using bts.api.orderman; using mbts.collections; using mbts.utils; @@ -14,15 +15,14 @@ namespace mbts.spark { // returns a ref-count Observable.Create-wrapper that initializes/destroys the [hot-observable] FillTupleSource. // (initializes when the first subscription is made, and destroys when the last subscription is disposed). public static IObservable> create_observable( - IObservable> securities, - IObservable> orders, - IObservable> fills) { + IObservable> orders, + IObservable> fills) { FillTupleSource source = null; int ref_count = 0; return Observable.Create>(observer => { if (Interlocked.Increment(ref ref_count) == 1) { SparkUtils.lazy_assert(source == null, () => fmt_assert_msg("source should be null, there must be a leak.")); - source = new FillTupleSource(securities, orders, fills); + source = new FillTupleSource(orders, fills); } IDisposable sub = source.Subscribe(observer); return Disposable.Create(() => { @@ -38,19 +38,16 @@ namespace mbts.spark { readonly Dictionary _state = new Dictionary(); readonly SetMap _order_fills = new SetMap(); readonly SetMap _security_fills = new SetMap(); - IReadOnlyDictionary _fills_cache = new Dictionary(0); - IReadOnlyDictionary _orders_cache = new Dictionary(0); - IReadOnlyDictionary _security_md_cache = new Dictionary(0); + IReadOnlyDictionary _fills_cache = new Dictionary(); + IReadOnlyDictionary _orders_cache = new Dictionary(); IDisposable _dispose; object _lock = new object(); FillTupleSource( - IObservable> securities, - IObservable> orders, - IObservable> fills) => + IObservable> orders, + IObservable> fills) => // _dispose = Disposable.Create( - securities.Subscribe(_update_securities), orders.Subscribe(_update_orders), fills.AddRemoveOnly().Subscribe(_update_fills)); @@ -63,19 +60,19 @@ namespace mbts.spark { public void Dispose() => Disposable.dispose(ref _dispose); - void _update_fills(LegacyTableUpdate fills) { + void _update_fills(LegacyTableUpdate<(SecurityImmutable?,FillImmutable), string> fills) { lock (_lock) { var removed = new Dictionary(); var added = new Dictionary(); - foreach (FillImmutable fill in fills.removed) { - FillTuple old_ft = new FillTuple(fill, _get_order(fill.OrderKey), _get_security_md(fill.SecurityKey)); + foreach (var (security,fill) in fills.removed) { + FillTuple old_ft = new FillTuple(fill, _get_order(fill.OrderKey), security); removed.Add(old_ft.key, old_ft); _security_fills.remove(fill.SecurityKey, fill.FillKey); _order_fills.remove(fill.OrderKey, fill.FillKey); } - foreach (FillImmutable fill in fills.added) { - FillTuple new_ft = new FillTuple(fill, _get_order(fill.OrderKey), _get_security_md(fill.SecurityKey)); + foreach (var (security,fill) in fills.added) { + FillTuple new_ft = new FillTuple(fill, _get_order(fill.OrderKey), security); added.Add(new_ft.key, new_ft); _security_fills.add(fill.SecurityKey, fill.FillKey); _order_fills.add(fill.OrderKey, fill.FillKey); @@ -85,35 +82,33 @@ namespace mbts.spark { } } - void _update_orders(TableUpdate orders) { + void _update_orders(TableUpdate<(SecurityImmutable?,OrderRecordImmutable), uint> orders) { lock (_lock) { Dictionary added = new Dictionary(), removed = new Dictionary(); HashSet changed_keys = new HashSet(); - foreach (var o in orders.removed) { + foreach (var (_,o) /*winking owl*/ in orders.removed) { if (_order_fills.try_lookup(o.Key, out ISet fill_keys)) { OrderRecordImmutable? curr_order = null; - if (orders.try_get(o.Key, out OrderRecordImmutable newval)) { - curr_order = newval; + if (orders.try_get(o.Key, out var newval)) { + curr_order = newval.Item2; changed_keys.Add(o.Key); } - IEnumerable fills = fill_keys.Select(fk => _fills_cache[fk]); - foreach (FillImmutable fill in fills) { - SecurityMD? sec_md = _get_security_md(fill.SecurityKey); - FillTuple old_ft = new FillTuple(fill, o, sec_md); + var fills = fill_keys.Select(fk => _fills_cache[fk]); + foreach (var (security, fill) in fills) { + FillTuple old_ft = new FillTuple(fill, o, security); removed.Add(old_ft.key, old_ft); - FillTuple new_ft = new FillTuple(fill, curr_order, sec_md); + FillTuple new_ft = new FillTuple(fill, curr_order, security); added.Add(new_ft.key, new_ft); } } } - foreach (var o in orders.added) { + foreach (var (_,o) in orders.added) { if (!changed_keys.Contains(o.Key) && _order_fills.try_lookup(o.Key, out ISet fill_keys)) { - IEnumerable fills = fill_keys.Select(fk => _fills_cache[fk]); - foreach (FillImmutable fill in fills) { - SecurityMD? sec_md = _get_security_md(fill.SecurityKey); - FillTuple old_ft = new FillTuple(fill, null, sec_md); + var fills = fill_keys.Select(fk => _fills_cache[fk]); + foreach (var (security,fill) in fills) { + FillTuple old_ft = new FillTuple(fill, null, security); removed.Add(old_ft.key, old_ft); - FillTuple new_ft = new FillTuple(fill, o, sec_md); + FillTuple new_ft = new FillTuple(fill, o, security); added.Add(new_ft.key, new_ft); } } @@ -123,46 +118,8 @@ namespace mbts.spark { } } - void _update_securities(IReadOnlyDictionary securities) { - lock (_lock) { - Dictionary added = new Dictionary(), removed = new Dictionary(); - _security_md_cache.Keys.difference(securities.Keys, out IEnumerable added_keys, out IEnumerable removed_keys); - foreach (var k in removed_keys) { - if (_security_fills.try_lookup(k, out ISet fill_keys)) { - IEnumerable fills = fill_keys.Select(fk => _fills_cache[fk]); - SecurityMD old_sec_md = _security_md_cache[k]; - foreach (FillImmutable fill in fills) { - OrderRecordImmutable? order = _get_order(fill.OrderKey); - FillTuple old_ft = new FillTuple(fill, order, old_sec_md); - removed.Add(old_ft.key, old_ft); - FillTuple new_ft = new FillTuple(fill, order, null); - added.Add(new_ft.key, new_ft); - } - } - } - foreach (var k in added_keys) { - if (_security_fills.try_lookup(k, out ISet fill_keys)) { - IEnumerable fills = fill_keys.Select(fk => _fills_cache[fk]); - SecurityMD new_sec_md = securities[k]; - foreach (FillImmutable fill in fills) { - OrderRecordImmutable? order = _get_order(fill.OrderKey); - FillTuple old_ft = new FillTuple(fill, order, null); - removed.Add(old_ft.key, old_ft); - FillTuple new_ft = new FillTuple(fill, order, new_sec_md); - added.Add(new_ft.key, new_ft); - } - } - } - _security_md_cache = securities; - _apply_changes(added, removed); - } - } - - SecurityMD? _get_security_md(uint key) => - _security_md_cache.TryGetValue(key, out SecurityMD smd) ? smd : new SecurityMD?(); - OrderRecordImmutable? _get_order(uint key) => - _orders_cache.TryGetValue(key, out OrderRecordImmutable o) ? o : new OrderRecordImmutable?(); + _orders_cache.TryGetValue(key, out var o) ? o.Item2 : new OrderRecordImmutable?(); void _apply_changes(Dictionary added, Dictionary removed) { foreach (var kv in removed) diff --git a/src/client/spark/Orders/OrdersModel.cs b/src/client/spark/Orders/OrdersModel.cs index c5892f4a4..0503e569e 100644 --- a/src/client/spark/Orders/OrdersModel.cs +++ b/src/client/spark/Orders/OrdersModel.cs @@ -71,10 +71,10 @@ namespace mbts.spark { _service.subscribei_fills() .Naggle(interval, scheduler) .PermissionFilter(admin_model.on_admin_data) - //.join(sstore, f => ) + .join(sstore, f => f.SecurityKey) .Table().Cache(); _activity = new OrderActivitySummarizer(bus, on_orders); - on_fill_tuples = FillTupleSource.create_observable(bus.subscribe(SparkTags.SecuritiesTag), on_orders.Select(i => i.order, order => order.Key), on_fills); + on_fill_tuples = FillTupleSource.create_observable(on_orders, on_fills); _security_orders_demuxer = new TableUpdateDemuxer(o => o.Key, o => o.security_key()); _security_positions_demuxer = new TableUpdateDemuxer(o => o.RecordId, p => p.SecurityId); _dispose = Disposable.Create( @@ -94,7 +94,7 @@ namespace mbts.spark { public DynamicOrderHelper dynamic_helper => _dynamic_helper; public IObservable on_orders { get; } public IObservable> on_order_securities { get; } - public IObservable> on_fills { get; } + public IObservable> on_fills { get; } public IObservable> on_fill_tuples { get; } public IObservable> working_orders { get; } public IObservable> on_positions { get; } diff --git a/src/client/spark/Security/MDLookup.cs b/src/client/spark/Security/MDLookup.cs new file mode 100644 index 000000000..e7949dda7 --- /dev/null +++ b/src/client/spark/Security/MDLookup.cs @@ -0,0 +1,30 @@ +using mbts; +using System; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.Reactive.Linq; + +namespace bts { + class MDLookup : IDisposable { + IReadOnlyDictionary _state; + IDisposable _dispose; + + [ImportingConstructor] + public MDLookup(IObservable> src) { + _dispose = src.Subscribe(s => _state = s); + } + + public void Dispose() { Disposable.dispose(ref _dispose); } + + public SecurityMD? get_md(uint key) { + if (_state?.TryGetValue(key, out var md) == true) { + return md; + } + return null; + } + public SecurityMD? get_md(uint? key) { + if (key.HasValue) return get_md(key.Value); + return null; + } + } +} diff --git a/src/client/spark/Security/SecurityStore.cs b/src/client/spark/Security/SecurityStore.cs index 731ca169f..cf2dd306d 100644 --- a/src/client/spark/Security/SecurityStore.cs +++ b/src/client/spark/Security/SecurityStore.cs @@ -139,11 +139,11 @@ namespace bts { /////////////// #region public - // Guarunteed to return a security if it has ever been seen by the system + // Guaranteed to return a security if it has ever been seen by the system public Task get_security(security_key key) { return get_security(key, _live_securities); } - // Guarunteed to return a security if it has ever been seen by the system + // Guaranteed to return a security if it has ever been seen by the system public Task> get_securities(IEnumerable keys) { return get_securities(keys, _live_securities); } diff --git a/src/client/spark/view/Spark/PAndL/PAndLModel.cs b/src/client/spark/view/Spark/PAndL/PAndLModel.cs index 558f2305a..9ffa2d95b 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLModel.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLModel.cs @@ -1,4 +1,5 @@ -using bts.api; +using bts; +using bts.api; using mbts.utils; using System; using System.ComponentModel.Composition; @@ -8,18 +9,18 @@ namespace mbts.spark { class PAndLModel : IDisposable { readonly IBus _bus; readonly OrdersModel _order_model; - //readonly AccountService _account_service; + readonly MDLookup _mdlookup; IDisposable _dispose; [ImportingConstructor] private PAndLModel(IBus bus, OrdersModel order_model/*, AccountService account_service*/) { _bus = bus; _order_model = order_model; - //_account_service = account_service; + _mdlookup = new MDLookup(bus.subscribe(SparkTags.SecuritiesTag)); } public IObservable> on_pnl_data => - _order_model.on_fill_tuples.convert(ft => new PnLData(ft)).table(pd => pd.key); + _order_model.on_fill_tuples.Select(ft => new PnLData(ft, _mdlookup.get_md(ft.security?.Id))).table(pd => pd.key); //public async Task get_accounts() { // var request = await _account_service.get_accounts_async(new GetAccountsRequest()); diff --git a/src/client/spark/view/Spark/PAndL/PnLData.cs b/src/client/spark/view/Spark/PAndL/PnLData.cs index 99c441c16..5b44015a5 100644 --- a/src/client/spark/view/Spark/PAndL/PnLData.cs +++ b/src/client/spark/view/Spark/PAndL/PnLData.cs @@ -28,19 +28,23 @@ namespace mbts.spark { class PnLData { readonly FillTuple _fill_tuple; + readonly SecurityMD? _md; - public PnLData(FillTuple fill_tuple) => + public PnLData(FillTuple fill_tuple, SecurityMD? md) { + // It is entirely possible for the FillTuple to have a security but for no SecrityMD to be available. For example: expired security + _md = md; _fill_tuple = fill_tuple; + } public FillTupleKey key => _fill_tuple.key; public FillImmutable fill => _fill_tuple.fill; - public SecurityMD? sec_md => _fill_tuple.security_md; + public SecurityMD? sec_md => _md; + public SecurityImmutable? security => _fill_tuple.security; public OrderRecordImmutable? order => _fill_tuple.order; public string account => _fill_tuple.account; - public Venue? exchange => sec_md?.security.Exchange; - public string symbol => sec_md?.security.Symbol; - public SecurityImmutable? security => sec_md?.security; + public Venue? exchange => security?.Exchange; + public string symbol => security?.Symbol; public double buy_quantity => (fill.Side == Side.Buy) ? fill.LastShares : 0; public double sell_quantity => (fill.Side == Side.Sell) ? fill.LastShares : 0; public SecurityType? security_type => security?.security_type();