From 3ae413a983683fe4b36bcd16b04e82e15933cc8d Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Wed, 28 Feb 2018 11:50:55 -0600 Subject: [PATCH] PnL: Adding some grid filters --- .../spark/view/Spark/PAndL/PAndLModel.cs | 21 ++--------- src/client/spark/view/Spark/PAndL/PAndLVM.cs | 13 +++++++ .../spark/view/Spark/PAndL/PAndLView.xaml | 27 +++++++++++--- .../spark/view/Spark/PAndL/PnLSummaryRow.cs | 36 +++++++++---------- 4 files changed, 56 insertions(+), 41 deletions(-) diff --git a/src/client/spark/view/Spark/PAndL/PAndLModel.cs b/src/client/spark/view/Spark/PAndL/PAndLModel.cs index 25a1a6efc..606ba32b3 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLModel.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLModel.cs @@ -19,25 +19,10 @@ namespace mbts.spark private PAndLModel(IBus bus, OrdersModel order_model) { _bus = bus; _order_model = order_model; - - //var securities_sequence = _bus.subscribe(SparkTags.SecuritiesTag); - //var fills_with_md = _order_model.on_fill_tuples.CombineLatest(securities_sequence, - // (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)); - // } else { - // pnl_data.Add(new PnLData(ft.fill, null)); - // } - // } - // return pnl_data; - // }) - // .ObserveOnDispatcher() - // .Subscribe(); } public IObservable> on_pnl_data => + // combine w/ Securities to get SecurityMD _order_model.on_fill_tuples.CombineLatest(_bus.subscribe(SparkTags.SecuritiesTag), (fills, securities) => { List pnl_data = new List(fills.state.Count); foreach (var ft in fills.state.Values) { @@ -50,9 +35,7 @@ namespace mbts.spark return pnl_data; }); - - - + public void Dispose() => Disposable.dispose(ref _dispose); diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index dbe26e266..2802cf187 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -10,6 +10,7 @@ using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; using static bts.shell.PAndLWindowFactory; +using static mbts.spark.DataGridColumnFilters; namespace mbts.spark { @@ -29,7 +30,11 @@ namespace mbts.spark }); pnl_view = CollectionViewSource.GetDefaultView(_pnl_data); + #region Create grid filters and load state + #endregion + _dispose = Disposable.Create(pnl, + hook_filters(pnl_view, security_filter, pnl_filter, buy_quantity_filter, sell_quantity_filter), WorkLoop.Start(_calculate_pnl, TimeSpan.FromSeconds(1)) ); } @@ -43,6 +48,14 @@ namespace mbts.spark private ObservableCollection _pnl_data = new ObservableCollection(); public ICollectionView pnl_view { get; } + #region Filters + public IDataGridColumnPersistentFilter security_filter { get; } = new RegexFinder(r => r.security_text, nameof(PnLSummaryRow.security_text)); + public IDataGridColumnPersistentFilter pnl_filter { get; } = new DoubleMinMax(r => r.pnl, nameof(PnLSummaryRow.pnl)); + public IDataGridColumnPersistentFilter buy_quantity_filter { get; } = new DoubleMinMax(r => r.buy_quantity, nameof(PnLSummaryRow.buy_quantity)); + public IDataGridColumnPersistentFilter sell_quantity_filter { get; } = new DoubleMinMax(r => r.sell_quantity, nameof(PnLSummaryRow.sell_quantity)); + + #endregion + readonly IBus _bus; readonly PAndLModel _model; diff --git a/src/client/spark/view/Spark/PAndL/PAndLView.xaml b/src/client/spark/view/Spark/PAndL/PAndLView.xaml index 31177ec00..3ec6b968d 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLView.xaml +++ b/src/client/spark/view/Spark/PAndL/PAndLView.xaml @@ -11,6 +11,9 @@ TextElement.FontSize="11" mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="1080"> + + + @@ -25,10 +28,26 @@ - - - - + + + + diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs index cbcf09f88..cfd7f16f5 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -31,7 +31,7 @@ namespace mbts.spark { } class PnLSummaryRow : ViewModelBase, IEquatable { - (string account, Venue? exchange, string symbol, SecurityImmutable? security, SecurityMD? md) key; + (string account, Venue? exchange, string symbol, SecurityImmutable? security, SecurityMD? sec_md) key; double _buy_quantity, _sell_quantity, _pnl; IEnumerable _fills; @@ -44,10 +44,10 @@ namespace mbts.spark { } public void update_pnl(pnl_calc_type pnl_request) { - if (key.md.HasValue) { + if (key.sec_md.HasValue) { if (pnl_request.HasFlag(pnl_calc_type.bid_and_ask)) { ulong unused = 0; - key.md.Value.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); + key.sec_md.Value.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); double summary_pnl = 0.0; foreach (var fill in _fills) { @@ -56,7 +56,7 @@ namespace mbts.spark { } pnl = summary_pnl; } else { - double driver = _get_driver(pnl_request); + double driver = _get_driver(key.sec_md.Value, pnl_request); double summary_pnl = 0.0; foreach (var fill in _fills) { @@ -70,10 +70,10 @@ namespace mbts.spark { } } - private double _get_driver(pnl_calc_type pnl_request) { + private double _get_driver(SecurityMD sec_md, pnl_calc_type pnl_request) { ulong unused = 0; - key.md.Value.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); - key.md.Value.md.get_last_trade(ref unused, out NativePriceSize last, out DateTime unused1, out ulong unused2); + sec_md.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); + sec_md.md.get_last_trade(ref unused, out NativePriceSize last, out DateTime unused1, out ulong unused2); if (pnl_request.HasFlag(pnl_calc_type.waterfall)) { // last price is available and withing range of available bid and ask @@ -81,7 +81,7 @@ namespace mbts.spark { return last.price; } - double mid = TickUtils.mid_tick(key.md.Value.security.ContractSpec, bid.price, ask.price); + double mid = TickUtils.mid_tick(sec_md.security.ContractSpec, bid.price, ask.price); // last price is not valid, so try the mid if (!Double.IsNaN(mid)) { return mid; @@ -117,33 +117,33 @@ namespace mbts.spark { // indicative, no support yet // bid or ask available and a settle - if ( (!Double.IsNaN(bid.price) || !Double.IsNaN(ask.price)) && !Double.IsNaN(key.md.Value.md.settlement_price)) { + if ( (!Double.IsNaN(bid.price) || !Double.IsNaN(ask.price)) && !Double.IsNaN(sec_md.md.settlement_price)) { // if bid and no ask and settle and bid > Settle - if ( !Double.IsNaN(bid.price) && Double.IsNaN(ask.price) && !Double.IsNaN(key.md.Value.md.settlement_price) && bid.price > key.md.Value.md.settlement_price) { + if ( !Double.IsNaN(bid.price) && Double.IsNaN(ask.price) && !Double.IsNaN(sec_md.md.settlement_price) && bid.price > sec_md.md.settlement_price) { return bid.price; } // if ask and no bid and settle and ask < Settle - if (!Double.IsNaN(ask.price) && Double.IsNaN(bid.price) && !Double.IsNaN(key.md.Value.md.settlement_price) && ask.price < key.md.Value.md.settlement_price) { + if (!Double.IsNaN(ask.price) && Double.IsNaN(bid.price) && !Double.IsNaN(sec_md.md.settlement_price) && ask.price < sec_md.md.settlement_price) { return ask.price; } } // Settlement // neither bid or ask are available - if (!Double.IsNaN(key.md.Value.md.settlement_price)) { - return key.md.Value.md.settlement_price; + if (!Double.IsNaN(sec_md.md.settlement_price)) { + return sec_md.md.settlement_price; } // Close - return key.md.Value.md.close_price; + return sec_md.md.close_price; } // end waterfall if (pnl_request.HasFlag(pnl_calc_type.last)) return last.price; - if (pnl_request.HasFlag(pnl_calc_type.mid)) return TickUtils.mid_tick(key.md.Value.security.ContractSpec, bid.price, ask.price); - if (pnl_request.HasFlag(pnl_calc_type.open)) return key.md.Value.md.open_price; - if (pnl_request.HasFlag(pnl_calc_type.close)) return key.md.Value.md.close_price; - if (pnl_request.HasFlag(pnl_calc_type.settle)) return key.md.Value.md.settlement_price; + if (pnl_request.HasFlag(pnl_calc_type.mid)) return TickUtils.mid_tick(sec_md.security.ContractSpec, bid.price, ask.price); + if (pnl_request.HasFlag(pnl_calc_type.open)) return sec_md.md.open_price; + if (pnl_request.HasFlag(pnl_calc_type.close)) return sec_md.md.close_price; + if (pnl_request.HasFlag(pnl_calc_type.settle)) return sec_md.md.settlement_price; return Double.NaN; }