diff --git a/src/client/spark/Modules/Trading/Fills/FillsVM.cs b/src/client/spark/Modules/Trading/Fills/FillsVM.cs index fe296df5c..44814be1c 100644 --- a/src/client/spark/Modules/Trading/Fills/FillsVM.cs +++ b/src/client/spark/Modules/Trading/Fills/FillsVM.cs @@ -30,7 +30,10 @@ namespace mbts.spark { account_filter = DiscreteValuesFilterVM.Create(_model.factory.subscribe_accounts(), r => r.account, nameof(Row.account)); user_filter = DiscreteValuesFilterVM.Create(_model.factory.subscribe_users(), r => r.user, nameof(Row.user)); security_filter = DiscreteValuesFilterVM.Create( - fills_source.with_respect_to(ft => ft.security ?? SecurityDataHelper.Defaults.SecurityImmutable, s => s.Id, ft => ft.key).Select(t => (t.added.Values, t.removed.Values)), + fills_source.with_respect_to( + ft => ft.security_md.HasValue ? ft.security_md.Value.security : SecurityDataHelper.Defaults.SecurityImmutable, + s => s.Id, ft => ft.key + ).Select(t => (t.added.Values, t.removed.Values)), r => r.security, SecurityCriteria.from_securities, SecurityCriteria.get_to_securities(bus), @@ -138,7 +141,7 @@ namespace mbts.spark { readonly FillImmutable _fill; readonly OrderRecordImmutable _order; - public Row(FillTuple fill_tuple, OrdersModel model) : base(fill_tuple.security ?? SecurityDataHelper.Defaults.Security) { + public Row(FillTuple fill_tuple, OrdersModel model) : base(fill_tuple.security_md.HasValue ? fill_tuple.security_md.Value.security : SecurityDataHelper.Defaults.Security) { _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 1eb57a16b..7a2ca5b44 100644 --- a/src/client/spark/Orders/FillTuple.cs +++ b/src/client/spark/Orders/FillTuple.cs @@ -3,26 +3,23 @@ using bts.api.orderman; using mbts.utils; using System; -namespace mbts.spark -{ +namespace mbts.spark { class FillTuple : IEquatable { public readonly FillImmutable fill; public readonly OrderRecordImmutable? order; - public readonly SecurityImmutable? security; + public readonly SecurityMD? security_md; public readonly FillTupleKey key; - public FillTuple(FillImmutable fill, OrderRecordImmutable? order, SecurityImmutable? security) { + public FillTuple(FillImmutable fill, OrderRecordImmutable? order, SecurityMD? security_md) { this.fill = fill; this.order = order; - this.security = security; - // a FillTuple that DOES NOT HAVE associated order/security needs to be distinguishable - // from a FillTuple (of the same underlying FillImmutable) that DOES HAVE associated order/security - key = new FillTupleKey(fill.FillKey, order.HasValue ? order.Value.Key : 0u, security.HasValue ? security.Value.Id : 0u); + 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); } public bool Equals(FillTuple other) => other != null && other.order.HasValue == order.HasValue - && other.security.HasValue == security.HasValue + && other.security_md.HasValue == security_md.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 c047701a3..d7bbcec4c 100644 --- a/src/client/spark/Orders/FillTupleSource.cs +++ b/src/client/spark/Orders/FillTupleSource.cs @@ -1,5 +1,4 @@ -using bts.api; -using bts.api.core; +using bts.api.core; using bts.api.orderman; using mbts.collections; using mbts.utils; @@ -39,9 +38,9 @@ namespace mbts.spark { readonly Dictionary _state = new Dictionary(); readonly SetMap _order_fills = new SetMap(); readonly SetMap _security_fills = new SetMap(); - IReadOnlyDictionary _fills = new Dictionary(0); - IReadOnlyDictionary _orders = new Dictionary(0); - IReadOnlyDictionary _securities = new Dictionary(0); + IReadOnlyDictionary _fills_cache = new Dictionary(0); + IReadOnlyDictionary _orders_cache = new Dictionary(0); + IReadOnlyDictionary _security_md_cache = new Dictionary(0); // _n_write_ops should never be greater than 1, bc all 3 input observables fire 'OnDispatcher' int _n_read_ops, _n_write_ops; IDisposable _dispose; @@ -49,13 +48,12 @@ namespace mbts.spark { FillTupleSource( IObservable> securities, IObservable> orders, - IObservable> fills) { + IObservable> fills) => // _dispose = Disposable.Create( securities.OnDispatcher().Subscribe(_update_securities), orders.OnDispatcher().Subscribe(_update_orders), fills.OnDispatcher().Subscribe(_update_fills)); - } public IDisposable Subscribe(IObserver> observer) { using (_read()) { @@ -70,18 +68,18 @@ namespace mbts.spark { using (_write()) { Dictionary removed = new Dictionary(), added = new Dictionary(); foreach (FillImmutable fill in fills.removed) { - FillTuple old_ft = new FillTuple(fill, _get_order(fill.OrderKey), _get_security(fill.SecurityKey)); + FillTuple old_ft = new FillTuple(fill, _get_order(fill.OrderKey), _get_security_md(fill.SecurityKey)); 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(fill.SecurityKey)); + FillTuple new_ft = new FillTuple(fill, _get_order(fill.OrderKey), _get_security_md(fill.SecurityKey)); added.Add(new_ft.key, new_ft); _security_fills.add(fill.SecurityKey, fill.FillKey); _order_fills.add(fill.OrderKey, fill.FillKey); } - _fills = fills.state; + _fills_cache = fills.state; _apply_changes(added, removed); } } @@ -92,10 +90,10 @@ namespace mbts.spark { HashSet changed_keys = new HashSet(); foreach (var o in orders.removed) { if (_order_fills.try_lookup(o.Key, out ISet fill_keys)) { - IEnumerable fills = fill_keys.Select(fk => _fills[fk]); - SecurityImmutable? sec = _get_security(o.security_key()); + IEnumerable fills = fill_keys.Select(fk => _fills_cache[fk]); + SecurityMD? sec_md = _get_security_md(o.security_key()); foreach (FillImmutable fill in fills) { - FillTuple old_ft = new FillTuple(fill, o, sec); + FillTuple old_ft = new FillTuple(fill, o, sec_md); removed.Add(old_ft.key, old_ft); } OrderRecordImmutable? curr_order = null; @@ -104,24 +102,24 @@ namespace mbts.spark { changed_keys.Add(o.Key); } foreach (FillImmutable fill in fills) { - FillTuple new_ft = new FillTuple(fill, curr_order, sec); + FillTuple new_ft = new FillTuple(fill, curr_order, sec_md); added.Add(new_ft.key, new_ft); } } } 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[fk]); - SecurityImmutable? sec = _get_security(o.security_key()); + IEnumerable fills = fill_keys.Select(fk => _fills_cache[fk]); + SecurityMD? sec_md = _get_security_md(o.security_key()); foreach (FillImmutable fill in fills) { - FillTuple old_ft = new FillTuple(fill, null, sec); + FillTuple old_ft = new FillTuple(fill, null, sec_md); removed.Add(old_ft.key, old_ft); - FillTuple new_ft = new FillTuple(fill, o, sec); + FillTuple new_ft = new FillTuple(fill, o, sec_md); added.Add(new_ft.key, new_ft); } } } - _orders = orders.state; + _orders_cache = orders.state; _apply_changes(added, removed); } } @@ -129,14 +127,14 @@ namespace mbts.spark { void _update_securities(IReadOnlyDictionary securities) { using (_write()) { Dictionary added = new Dictionary(), removed = new Dictionary(); - _securities.Keys.difference(securities.Keys, out IEnumerable added_keys, out IEnumerable removed_keys); + _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[fk]); - SecurityImmutable old_security = _securities[k].security; + 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_security); + 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); @@ -145,27 +143,27 @@ namespace mbts.spark { } foreach (var k in added_keys) { if (_security_fills.try_lookup(k, out ISet fill_keys)) { - IEnumerable fills = fill_keys.Select(fk => _fills[fk]); - SecurityImmutable new_security = securities[k].security; + 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_security); + FillTuple new_ft = new FillTuple(fill, order, new_sec_md); added.Add(new_ft.key, new_ft); } } } - _securities = securities; + _security_md_cache = securities; _apply_changes(added, removed); } } - SecurityImmutable? _get_security(uint key) => - _securities.TryGetValue(key, out SecurityMD s) ? s.security : new SecurityImmutable?(); + SecurityMD? _get_security_md(uint key) => + _security_md_cache.TryGetValue(key, out SecurityMD smd) ? smd : new SecurityMD?(); OrderRecordImmutable? _get_order(uint key) => - _orders.TryGetValue(key, out OrderRecordImmutable o) ? o : new OrderRecordImmutable?(); + _orders_cache.TryGetValue(key, out OrderRecordImmutable o) ? o : new OrderRecordImmutable?(); void _apply_changes(Dictionary added, Dictionary removed) { foreach (var kv in removed) diff --git a/src/client/spark/core/Utility/TableUpdateTransform.cs b/src/client/spark/core/Utility/TableUpdateTransform.cs index 307db553d..bf4fe4e6e 100644 --- a/src/client/spark/core/Utility/TableUpdateTransform.cs +++ b/src/client/spark/core/Utility/TableUpdateTransform.cs @@ -88,6 +88,15 @@ namespace mbts.utils { .Select(u => new FilteredTableUpdateTransform(u, predicate)) .Where(u => u.added.Count > 0 || u.removed.Count > 0); + /// + /// Makes a 1:1 conversion to an IncrementalUpdate sequence of TResult (TKey remains the same). + /// + public static IObservable> convert(this IObservable> source, Func selector) + where TKey : IEquatable => + source.Select(update => new IncrementalUpdate( + update.added.ToDictionary(kv => kv.Key, kv => selector(kv.Value)), + update.removed.ToDictionary(kv => kv.Key, kv => selector(kv.Value)))); + /// /// Converts sequence to show incremental changes with respect to the [TResult] value returned by result_selector. /// diff --git a/src/client/spark/view/Spark/PAndL/PAndLModel.cs b/src/client/spark/view/Spark/PAndL/PAndLModel.cs index d7cfb0a40..558f2305a 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLModel.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLModel.cs @@ -1,15 +1,9 @@ using bts.api; using mbts.utils; using System; -using System.Collections.Generic; -using System.Collections.Specialized; using System.ComponentModel.Composition; -using System.Linq; -using System.Reactive.Linq; -using System.Threading.Tasks; -namespace mbts.spark -{ +namespace mbts.spark { [Export] class PAndLModel : IDisposable { readonly IBus _bus; @@ -24,19 +18,8 @@ namespace mbts.spark //_account_service = account_service; } - public IObservable> on_pnl_data => - // combine w/ Securities to get SecurityMD - _order_model.on_fill_tuples.table(ft => ft.key).CombineLatest(_bus.subscribe(SparkTags.SecuritiesTag), (fills, securities) => { - List pnl_data = new List(fills.state.Count); - foreach (var ft in fills.state.Values) { - if (ft.security.HasValue && securities.TryGetValue(ft.security.Value.Id, out SecurityMD sec_md)) { - pnl_data.Add(new PnLData(ft.fill, sec_md, ft.order)); - } else { - pnl_data.Add(new PnLData(ft.fill, null, ft.order)); - } - } - return pnl_data; - }); + public IObservable> on_pnl_data => + _order_model.on_fill_tuples.convert(ft => new PnLData(ft)).table(pd => pd.key); //public async Task get_accounts() { // var request = await _account_service.get_accounts_async(new GetAccountsRequest()); @@ -46,12 +29,8 @@ namespace mbts.spark // } // return new Account[] { }; //} - public void Dispose() => Disposable.dispose(ref _dispose); - } - - -} +} \ No newline at end of file diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index f9e74e817..757b927af 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -23,7 +23,7 @@ namespace mbts.spark { .OnDispatcher() .Subscribe(p => { if (p != null) { - PnLCalculator calc = new PnLCalculator(p); + PnLCalculator calc = new PnLCalculator(p.state.Values); var summary_position_rows = calc.get_summary_position(); _pnl_data.sync_with(summary_position_rows); _last_pnl_update_time = DateTime.Now; diff --git a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs index cfbad1a67..2526ba0b3 100644 --- a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs +++ b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs @@ -76,9 +76,9 @@ namespace mbts.spark { class PnLCalculator { - readonly List _data; + readonly IEnumerable _data; - public PnLCalculator(List data) { + public PnLCalculator(IEnumerable data) { _data = data; } diff --git a/src/client/spark/view/Spark/PAndL/PnLData.cs b/src/client/spark/view/Spark/PAndL/PnLData.cs index 0481dfdfc..b102b92b8 100644 --- a/src/client/spark/view/Spark/PAndL/PnLData.cs +++ b/src/client/spark/view/Spark/PAndL/PnLData.cs @@ -1,11 +1,6 @@ using bts.api; using bts.api.orderman; -using mbts.co2; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace mbts.spark { @@ -23,16 +18,16 @@ namespace mbts.spark { class PnLData { - public readonly FillImmutable fill; - public readonly SecurityMD? sec_md; - public readonly OrderRecordImmutable? order; + readonly FillTuple _fill_tuple; - public PnLData(FillImmutable fill, SecurityMD? sec_md, OrderRecordImmutable? order) { - this.fill = fill; - this.sec_md = sec_md; - this.order = order; - } + public PnLData(FillTuple fill_tuple) => + _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 OrderRecordImmutable? order => _fill_tuple.order; + // public string account => order?.Def.Account; public Venue? exchange => sec_md?.security.Exchange; public string symbol => sec_md?.security.Symbol; @@ -43,7 +38,5 @@ namespace mbts.spark { public override string ToString() { return $"Fill: ( {account} {exchange} {security.ToString()} buy: {buy_quantity} sell: {sell_quantity} ) Security: {security.ToString()}"; } - - } -} +} \ No newline at end of file