From 5dedd45a359ca4e92520b71dd13d328c5b7e3ad8 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 26 Feb 2018 18:00:26 -0600 Subject: [PATCH] PnL: Waterfall, you so awesome --- src/client/spark/view/Spark/PAndL/PAndLVM.cs | 6 +- .../spark/view/Spark/PAndL/PAndLView.xaml | 5 +- src/client/spark/view/Spark/PAndL/PnLData.cs | 74 +++++++++------ .../spark/view/Spark/PAndL/PnLSummaryRow.cs | 90 +++++++++++++++++++ 4 files changed, 145 insertions(+), 30 deletions(-) create mode 100644 src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index 717a5de4d..1ce3751e0 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -21,13 +21,15 @@ namespace mbts.spark var pnl = _model.on_pnl_data .ObserveOnDispatcher() .Subscribe(p => { - _pnl_data.sync_with(p); + PnLCalculator calc = new PnLCalculator(p); + var results = calc.get_pnl(); + _pnl_data.sync_with(results); }); pnl_view = CollectionViewSource.GetDefaultView(_pnl_data); } - private ObservableCollection _pnl_data = new ObservableCollection(); + private ObservableCollection _pnl_data = new ObservableCollection(); public ICollectionView pnl_view { get; } diff --git a/src/client/spark/view/Spark/PAndL/PAndLView.xaml b/src/client/spark/view/Spark/PAndL/PAndLView.xaml index 856159199..31177ec00 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLView.xaml +++ b/src/client/spark/view/Spark/PAndL/PAndLView.xaml @@ -22,10 +22,13 @@ + + + - + diff --git a/src/client/spark/view/Spark/PAndL/PnLData.cs b/src/client/spark/view/Spark/PAndL/PnLData.cs index 1dbd9df67..117b8abb5 100644 --- a/src/client/spark/view/Spark/PAndL/PnLData.cs +++ b/src/client/spark/view/Spark/PAndL/PnLData.cs @@ -8,54 +8,74 @@ using System.Text; using System.Threading.Tasks; namespace mbts.spark { + + [Flags] + public enum pnl_calc_type { + none = 0, + last = 1, + mid = 2, + settlement = 4, + closing = 8 + } + class PnLData { + public readonly FillImmutable fill; public readonly SecurityMD? sec_md; - public readonly double mid; - public readonly double pnl; + private pnl_calc_type calc_type_available; + private Dictionary _pnl_driver_dict = new Dictionary(); public PnLData(FillImmutable fill, SecurityMD? sec_md) { this.fill = fill; this.sec_md = sec_md; - ulong unused = 0; if (sec_md.HasValue) { - sec_md.Value.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); - mid = TickUtils.mid_tick(sec_md.Value.security.ContractSpec, bid.price, ask.price); + ulong unused = 0; + sec_md.Value.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); + double mid = TickUtils.mid_tick(sec_md.Value.security.ContractSpec, bid.price, ask.price); + sec_md.Value.md.get_last_trade(ref unused, out NativePriceSize last, out DateTime unused1, out ulong unused2); + + _calc_and_set_type(last.price, pnl_calc_type.last); + _calc_and_set_type(mid, pnl_calc_type.mid); + _calc_and_set_type(sec_md.Value.md.settlement_price, pnl_calc_type.settlement); + _calc_and_set_type(sec_md.Value.md.close_price, pnl_calc_type.closing); + + } + } + + + public string account => fill.Account; + public Venue? exchange => sec_md?.security.Exchange; + public string symbol => sec_md?.security.Symbol; + public SecurityImmutable? security => sec_md?.security; + public double buy_quantity => (fill.Side == Side.Buy) ? fill.LastShares : 0; + public double sell_quantity => (fill.Side == Side.Sell) ? fill.LastShares : 0; + + private void _calc_and_set_type(double driver, pnl_calc_type key) { + if (!Double.IsNaN(driver)) { + calc_type_available |= key; if (fill.Side == Side.Buy) { - pnl = (mid - fill.LastPrice) * fill.LastShares;// * sec_md.Value.security.ContractSpec.CashPerPoint; + _pnl_driver_dict[key] = (driver - fill.LastPrice) * fill.LastShares;// * sec_md.Value.security.ContractSpec.CashPerPoint; } else if (fill.Side == Side.Sell) { - pnl = (fill.LastPrice - mid) * fill.LastShares;// * sec_md.Value.security.ContractSpec.CashPerPoint; + _pnl_driver_dict[key] = (fill.LastPrice - driver) * fill.LastShares;// * sec_md.Value.security.ContractSpec.CashPerPoint; } } } - public string Account => fill.Account; - public Venue? Exchange => sec_md?.security.Exchange; - public string Symbol => sec_md?.security.Symbol; - public SecurityImmutable? Security => sec_md?.security; + private double _waterfall_pnl() { + if (calc_type_available.HasFlag(pnl_calc_type.last)) return _pnl_driver_dict[pnl_calc_type.last]; + if (calc_type_available.HasFlag(pnl_calc_type.mid)) return _pnl_driver_dict[pnl_calc_type.mid]; + if (calc_type_available.HasFlag(pnl_calc_type.settlement)) return _pnl_driver_dict[pnl_calc_type.settlement]; + if (calc_type_available.HasFlag(pnl_calc_type.closing)) return _pnl_driver_dict[pnl_calc_type.closing]; + return Double.NaN; - void ignore() {/*These setters are just for WPF databinding :(*/ throw new NotImplementedException(); } - - public string security_text { - get => sec_md.HasValue ? sec_md.Value.ToString() : "Unknown Security"; - set => ignore(); } - - public double buy_quantity { + public double pnl { get { - if (fill.Side == Side.Buy) return fill.LastShares; - return 0; + return _waterfall_pnl(); } } - public double sell_quantity { - get { - if (fill.Side == Side.Sell) return fill.LastShares; - return 0; - } - } - public double the_pnl => pnl; } } diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs new file mode 100644 index 000000000..5cdfef105 --- /dev/null +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -0,0 +1,90 @@ +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 { + + class PnLCalculator { + readonly List _data; + + public PnLCalculator(List data) { + _data = data; + } + + public IEnumerable get_pnl() { + return (from pnl_item in _data + group pnl_item by (pnl_item.account, pnl_item.exchange, pnl_item.symbol, pnl_item.security) into grouping + select new PnLSummaryRow(grouping.Key, + grouping.Sum(y => y.buy_quantity), + grouping.Sum(y => y.sell_quantity), + grouping.Sum(y => y.pnl) + ) + ); + } + } + + class PnLSummaryRow { + (string account, Venue? exchange, string symbol, SecurityImmutable? security) key; + double _buy_quantity, _sell_quantity, _pnl; + + public PnLSummaryRow((string, Venue?, string, SecurityImmutable?) key, double buy_quant, double sell_quant, double pnl) { + this.key = key; + _buy_quantity = buy_quant; + _sell_quantity = sell_quant; + _pnl = pnl; + } + + + + void ignore() {/*These setters are just for WPF databinding :(*/ throw new NotImplementedException(); } + + public string account { + get => key.account; + set => ignore(); + } + + public Venue exchange { + get { + return key.exchange.HasValue ? key.exchange.Value : Venue.Unset; + } + set => ignore(); + } + + public string symbol { + get { + return key.symbol; + } + set => ignore(); + } + + public SecurityImmutable? security => key.security; + + public string security_text { + get => security.HasValue ? security.Value.ToString() : "Unknown Security"; + set => ignore(); + } + + public double buy_quantity { + get => _buy_quantity; + set => ignore(); + } + + public double sell_quantity { + get => _sell_quantity; + set => ignore(); + } + + public double pnl { + get => _pnl; + set => ignore(); + } + + } + + +}