From f9fbf9c45a828522efa614d4c62bcf8580259ba6 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 9 Jul 2018 15:40:58 -0500 Subject: [PATCH 1/9] SPARK-1435 --- src/client/spark/view/Spark/PAndL/PAndLVM.cs | 115 ++++++++----- .../spark/view/Spark/PAndL/PAndLView.xaml | 142 +++++++++------ .../spark/view/Spark/PAndL/PnLCalculator.cs | 66 ++++--- src/client/spark/view/Spark/PAndL/PnLData.cs | 12 +- .../spark/view/Spark/PAndL/PnLSummaryRow.cs | 162 ++++++++++++------ 5 files changed, 315 insertions(+), 182 deletions(-) diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index 757b927af..3635b04fa 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -2,6 +2,7 @@ using mbts.spark.filters; using mbts.ui; using mbts.ui.shell; +using mbts.ui.utility; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -13,17 +14,23 @@ using static bts.shell.PAndLWindowFactory; namespace mbts.spark { class PAndLVM : DisposableViewModelBase { + readonly Property _selected_pnl_calc_type; public PAndLVM(IBus bus, PAndLModel model, PAndLStartupData state) { _bus = bus; _model = model; + _selected_pnl_calc_type = backing(nameof(selected_pnl_calc_type)); + // get initial value from settings + // else assume waterfall + _selected_pnl_calc_type.Value = pnl_calc_type.midpoint; + _selected_pnl_calc_type.Changed += pnl_calc_type_changed; var pnl = model.on_pnl_data .Sample(_POSITION_UPDATE_PERIOD) .OnDispatcher() .Subscribe(p => { if (p != null) { - PnLCalculator calc = new PnLCalculator(p.state.Values); + PnLCalculator calc = new PnLCalculator(p.state.Values, _selected_pnl_calc_type.Value); var summary_position_rows = calc.get_summary_position(); _pnl_data.sync_with(summary_position_rows); _last_pnl_update_time = DateTime.Now; @@ -48,18 +55,31 @@ namespace mbts.spark { _dispose = Disposable.Create(pnl, DataGridFilterHelper.hook_filters(pnl_view, - security_filter, pnl_filter, - buy_quantity_filter, sell_quantity_filter, - net_position_filter, weighted_average_buy_filter, - weighted_average_sell_filter, pnl_price_filter, - sod_position_filter, account_filter, - exchange_string_filter, symbol_filter), + account_filter, + exchange_string_filter, + symbol_filter, + security_filter, + start_position_filter, + start_pnl_filter, + day_long_quantity_filter, + day_short_quantity_filter, + day_pnl_filter, + total_net_position_filter, + total_pnl_filter, + pnl_price_filter, + weighted_average_buy_filter, + weighted_average_sell_filter), WorkLoop.Start("PAndL", _UPDATE_PERIOD, _calculate_pnl) ); } + private void pnl_calc_type_changed(pnl_calc_type oldval, pnl_calc_type newval) { + _calculate_pnl(); + } + protected override void Dispose(bool disposing) { pnl_view.CollectionChanged -= Pnl_view_CollectionChanged; + _selected_pnl_calc_type.Changed -= pnl_calc_type_changed; } @@ -69,10 +89,10 @@ namespace mbts.spark { void _update_totals() { if (pnl_view != null) { - total_pnl = pnl_view.Cast().Where(r => !Double.IsNaN(r.pnl)).Sum(r => r.pnl); - total_net_pos = pnl_view.Cast().Sum(r => r.net_position); - total_buy_qty = pnl_view.Cast().Sum(r => r.buy_quantity); - total_sell_qty = pnl_view.Cast().Sum(r => r.sell_quantity); + total_aggregate_pnl = pnl_view.Cast().Where(r => !Double.IsNaN(r.total_pnl)).Sum(r => r.total_pnl); + total_aggregate_net_pos = pnl_view.Cast().Sum(r => r.total_net_position); + total_aggregate_buy_qty = pnl_view.Cast().Sum(r => r.day_long_quantity); + total_aggregate_sell_qty = pnl_view.Cast().Sum(r => r.day_short_quantity); } } @@ -81,8 +101,11 @@ namespace mbts.spark { // position update in a while if (DateTime.Now.Subtract(_last_pnl_update_time) > _UPDATE_PERIOD) { foreach (var r in _pnl_data) { - r.update_pnl(pnl_calc_type.waterfall); + if (_selected_pnl_calc_type.Value != pnl_calc_type.none) { + r.update_pnl(_selected_pnl_calc_type.Value); + } } + _update_totals(); } } @@ -90,57 +113,67 @@ namespace mbts.spark { private ObservableCollection _pnl_data = new ObservableCollection(); public ICollectionView pnl_view { get; } - private double _total_pnl; - public double total_pnl { + public List pnl_calc_types { get; } = EnumHelper.get_values_except(pnl_calc_type.none, pnl_calc_type.waterfall, pnl_calc_type.bid_or_ask, pnl_calc_type.open, pnl_calc_type.close).ToList(); + + public pnl_calc_type selected_pnl_calc_type { + get => _selected_pnl_calc_type.Value; + set => _selected_pnl_calc_type.Value = value; + } + + private double _total_aggregate_pnl; + public double total_aggregate_pnl { get { - return _total_pnl; + return _total_aggregate_pnl; } set { - _total_pnl = value; - RaisePropertyChanged(nameof(total_pnl)); + _total_aggregate_pnl = value; + RaisePropertyChanged(nameof(total_aggregate_pnl)); } } - private double _total_net_pos; - public double total_net_pos { - get { return _total_net_pos; } + private double _total_aggregate_net_pos; + public double total_aggregate_net_pos { + get { return _total_aggregate_net_pos; } set { - _total_net_pos = value; - RaisePropertyChanged(nameof(total_net_pos)); + _total_aggregate_net_pos = value; + RaisePropertyChanged(nameof(total_aggregate_net_pos)); } } - private double _total_buy_qty; - public double total_buy_qty { - get { return _total_buy_qty; } + private double _total_aggregate_buy_qty; + public double total_aggregate_buy_qty { + get { return _total_aggregate_buy_qty; } set { - _total_buy_qty = value; - RaisePropertyChanged(nameof(total_buy_qty)); + _total_aggregate_buy_qty = value; + RaisePropertyChanged(nameof(total_aggregate_buy_qty)); } } - private double _total_sell_qty; - public double total_sell_qty { - get { return _total_sell_qty; } + private double _total_aggregate_sell_qty; + public double total_aggregate_sell_qty { + get { return _total_aggregate_sell_qty; } set { - _total_sell_qty = value; - RaisePropertyChanged(nameof(total_sell_qty)); + _total_aggregate_sell_qty = value; + RaisePropertyChanged(nameof(total_aggregate_sell_qty)); } } #region Filters - public IDataGridColumnPersistentFilter security_filter { get; } = new RegexFilterVM(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)); - public IDataGridColumnPersistentFilter net_position_filter { get; } = new DoubleMinMax(r => r.net_position, nameof(PnLSummaryRow.net_position)); - public IDataGridColumnPersistentFilter weighted_average_buy_filter { get; } = new DoubleMinMax(r => r.weighted_average_buy, nameof(PnLSummaryRow.weighted_average_buy)); - public IDataGridColumnPersistentFilter weighted_average_sell_filter { get; } = new DoubleMinMax(r => r.weighted_average_sell, nameof(PnLSummaryRow.weighted_average_sell)); - public IDataGridColumnPersistentFilter pnl_price_filter { get; } = new DoubleMinMax(r => r.pnl_price, nameof(PnLSummaryRow.pnl_price)); - public IDataGridColumnPersistentFilter sod_position_filter { get; } = new DoubleMinMax(r => r.sod_position, nameof(PnLSummaryRow.sod_position)); + public IDataGridColumnPersistentFilter account_filter { get; } = new RegexFilterVM(r => r.account, nameof(PnLSummaryRow.account)); public IDataGridColumnPersistentFilter exchange_string_filter { get; } = new RegexFilterVM(r => r.exchange_string, nameof(PnLSummaryRow.exchange_string)); public IDataGridColumnPersistentFilter symbol_filter { get; } = new RegexFilterVM(r => r.symbol, nameof(PnLSummaryRow.symbol)); + public IDataGridColumnPersistentFilter security_filter { get; } = new RegexFilterVM(r => r.security_text, nameof(PnLSummaryRow.security_text)); + public IDataGridColumnPersistentFilter start_position_filter { get; } = new DoubleMinMax(r => r.start_net_position, nameof(PnLSummaryRow.start_net_position)); + public IDataGridColumnPersistentFilter start_pnl_filter { get; } = new DoubleMinMax(r => r.start_pnl, nameof(PnLSummaryRow.start_pnl)); + public IDataGridColumnPersistentFilter day_long_quantity_filter { get; } = new DoubleMinMax(r => r.day_long_quantity, nameof(PnLSummaryRow.day_long_quantity)); + public IDataGridColumnPersistentFilter day_short_quantity_filter { get; } = new DoubleMinMax(r => r.day_short_quantity, nameof(PnLSummaryRow.day_short_quantity)); + public IDataGridColumnPersistentFilter day_pnl_filter { get; } = new DoubleMinMax(r => r.day_pnl, nameof(PnLSummaryRow.day_pnl)); + public IDataGridColumnPersistentFilter total_net_position_filter { get; } = new DoubleMinMax(r => r.total_net_position, nameof(PnLSummaryRow.total_net_position)); + public IDataGridColumnPersistentFilter total_pnl_filter { get; } = new DoubleMinMax(r => r.total_pnl, nameof(PnLSummaryRow.total_pnl)); + public IDataGridColumnPersistentFilter pnl_price_filter { get; } = new DoubleMinMax(r => r.pnl_price, nameof(PnLSummaryRow.pnl_price)); + public IDataGridColumnPersistentFilter weighted_average_buy_filter { get; } = new DoubleMinMax(r => r.weighted_average_buy, nameof(PnLSummaryRow.weighted_average_buy)); + public IDataGridColumnPersistentFilter weighted_average_sell_filter { get; } = new DoubleMinMax(r => r.weighted_average_sell, nameof(PnLSummaryRow.weighted_average_sell)); #endregion diff --git a/src/client/spark/view/Spark/PAndL/PAndLView.xaml b/src/client/spark/view/Spark/PAndL/PAndLView.xaml index c43ff29c9..01564d81f 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLView.xaml +++ b/src/client/spark/view/Spark/PAndL/PAndLView.xaml @@ -25,13 +25,19 @@ + - + + - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -117,20 +159,13 @@ + - + --> - + - - + + @@ -169,8 +197,8 @@ - - + + @@ -182,8 +210,8 @@ - - + + @@ -195,8 +223,8 @@ - - + + diff --git a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs index 2526ba0b3..e8af91ba9 100644 --- a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs +++ b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs @@ -77,16 +77,18 @@ namespace mbts.spark { class PnLCalculator { readonly IEnumerable _data; + readonly pnl_calc_type _calc_type; - public PnLCalculator(IEnumerable data) { + public PnLCalculator(IEnumerable data, pnl_calc_type calc_type) { _data = data; + _calc_type = calc_type; } public IEnumerable get_summary_position() { return (from pnl_item in _data group pnl_item by (pnl_item.account, pnl_item.exchange, pnl_item.symbol, pnl_item.security, pnl_item.sec_md) into grouping let start_of_day = _get_start_of_day(grouping.Key.symbol) - select new PnLSummaryRow(grouping.Key, + select new PnLSummaryRow(_calc_type, start_of_day, grouping.Key, grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => y.buy_quantity), grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => y.sell_quantity), grouping.Sum(y => y.buy_quantity - y.sell_quantity), @@ -98,21 +100,23 @@ namespace mbts.spark { ); } - public static double get_driver(SecurityMD sec_md, pnl_calc_type pnl_request) { - ulong unused = 0; - 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); + public static bool get_driver(ref ulong bbo_time, ref ulong last_trade_time, SecurityMD sec_md, pnl_calc_type pnl_request, ref double driver_price) { + // check with eric as I'm not sure if i was every querying md correctly + if (!sec_md.md.get_bbo(ref bbo_time, out NativePriceSize bid, out NativePriceSize ask)) return false; + if (!sec_md.md.get_last_trade(ref last_trade_time, out NativePriceSize last, out DateTime unused1, out ulong unused2)) return false; if (pnl_request.HasFlag(pnl_calc_type.waterfall)) { // last price is available and withing range of available bid and ask if (!Double.IsNaN(last.price) && last.price >= bid.price && last.price <= ask.price) { - return last.price; + driver_price = last.price; + return true; } 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; + driver_price = mid; + return true; } // last bid @@ -120,9 +124,11 @@ namespace mbts.spark { if (!Double.IsNaN(last.price) && !Double.IsNaN(bid.price) && Double.IsNaN(ask.price)) { // if last >= bid then last else bid if (last.price >= bid.price) { - return last.price; + driver_price = last.price; + return true; } else { - return bid.price; + driver_price = bid.price; + return true; } } @@ -131,15 +137,18 @@ namespace mbts.spark { if (!Double.IsNaN(last.price) && !Double.IsNaN(ask.price) && Double.IsNaN(bid.price)) { // if last <= ask then last else ask if (last.price <= ask.price) { - return last.price; + driver_price = last.price; + return true; } else { - return ask.price; + driver_price = ask.price; + return true; } } // last only if (!Double.IsNaN(last.price)) { - return last.price; + driver_price = last.price; + return true; } // indicative, no support yet @@ -148,36 +157,43 @@ namespace mbts.spark { 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(sec_md.md.settlement_price) && bid.price > sec_md.md.settlement_price) { - return bid.price; + driver_price = bid.price; + return true; } // if ask and no bid and settle and ask < Settle 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; + driver_price = ask.price; + return true; } } // Settlement // neither bid or ask are available if (!Double.IsNaN(sec_md.md.settlement_price)) { - return sec_md.md.settlement_price; + driver_price = sec_md.md.settlement_price; + return true; } // Close - return sec_md.md.close_price; + driver_price = sec_md.md.close_price; + return true; } // 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(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; + if (pnl_request.HasFlag(pnl_calc_type.last)) { driver_price = last.price; return true; } + // note that bid_or_ask is not considered here since this method only + // supports returning a single driver price; see update_pnl + if (pnl_request.HasFlag(pnl_calc_type.midpoint)) { driver_price = TickUtils.mid_tick(sec_md.security.ContractSpec, bid.price, ask.price); return true; } + if (pnl_request.HasFlag(pnl_calc_type.open)) { driver_price = sec_md.md.open_price; return true; } + if (pnl_request.HasFlag(pnl_calc_type.close)) { driver_price = sec_md.md.close_price; return true; } + if (pnl_request.HasFlag(pnl_calc_type.settle)) { driver_price = sec_md.md.settlement_price; return true; } + driver_price = Double.NaN; + return false; } public static double get_pnl(double bid_for_long, double ask_for_short, FillImmutable fill, double cpp) { - if (fill.Side == Side.Buy) { return _get_long_pnl(bid_for_long, fill.LastPrice, fill.LastShares, cpp); } - if (fill.Side == Side.Sell) { return _get_short_pnl(ask_for_short, fill.LastPrice, fill.LastShares, cpp); } + if (fill.Side == Side.Buy && !Double.IsNaN(bid_for_long)) { return _get_long_pnl(bid_for_long, fill.LastPrice, fill.LastShares, cpp); } + if (fill.Side == Side.Sell && !Double.IsNaN(ask_for_short)) { return _get_short_pnl(ask_for_short, fill.LastPrice, fill.LastShares, cpp); } return Double.NaN; } diff --git a/src/client/spark/view/Spark/PAndL/PnLData.cs b/src/client/spark/view/Spark/PAndL/PnLData.cs index b102b92b8..760d4b3cc 100644 --- a/src/client/spark/view/Spark/PAndL/PnLData.cs +++ b/src/client/spark/view/Spark/PAndL/PnLData.cs @@ -1,18 +1,26 @@ using bts.api; using bts.api.orderman; using System; +using System.ComponentModel; namespace mbts.spark { [Flags] public enum pnl_calc_type { none = 0, + [Description("Last Tradeable Price Avalanche")] waterfall = 1, + [Description("Last")] last = 2, - bid_and_ask = 4, - mid = 8, + [Description("Bid/Ask")] + bid_or_ask = 4, + [Description("Midpoint")] + midpoint = 8, + [Description("Open")] open = 16, + [Description("Close")] close = 32, + [Description("Settle")] settle = 64 } diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs index 69ac5f485..32cd9dfa3 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -12,27 +12,33 @@ namespace mbts.spark { internal class PnLSummaryRow : ViewModelBase, IEquatable { + DateTime _start_of_day; (string account, Venue? exchange, string symbol, SecurityImmutable? security, SecurityMD? sec_md) key; - double _buy_quantity, _sell_quantity, _net_position, _sod_position, _pnl, _pnl_price, _weighted_average_buy, _weighted_average_sell; + double _day_long_quantity, _day_short_quantity, _total_net_position, _start_net_position, _total_pnl, _sod_pnl, _start_pnl, _pnl_price, _weighted_average_buy, _weighted_average_sell; IEnumerable _fills; + ulong _bbo_ref, _last_ref; + double _driver; //List _schedules = new List() { MarketSchedule.es_schedule }; - public PnLSummaryRow((string, Venue?, string, SecurityImmutable?, SecurityMD?) key, + public PnLSummaryRow(pnl_calc_type calc_type, + DateTime start_of_day, + (string, Venue?, string, SecurityImmutable?, SecurityMD?) key, double buy_quant, double sell_quant, double net_pos, double sod_pos, double wavg_buy, double wavg_sell, IEnumerable fills) { + this._start_of_day = start_of_day; this.key = key; - buy_quantity = buy_quant; - sell_quantity = sell_quant; - net_position = net_pos; - sod_position = sod_pos; + day_long_quantity = buy_quant; + day_short_quantity = sell_quant; + total_net_position = net_pos; + start_net_position = sod_pos; weighted_average_buy = wavg_buy; weighted_average_sell = wavg_sell; _fills = fills; // set the initial pnl and pnl_price when there has been a // position update i.e. this constructor - update_pnl(pnl_calc_type.waterfall); + update_pnl(calc_type); } private double _get_cpp_for_security(SecurityImmutable s) { @@ -56,26 +62,44 @@ namespace mbts.spark { public void update_pnl(pnl_calc_type pnl_request) { if (key.sec_md.HasValue) { double cpp = _get_cpp_for_security(key.security.Value); - if (pnl_request.HasFlag(pnl_calc_type.bid_and_ask)) { - ulong unused = 0; - key.sec_md.Value.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); + if (pnl_request.HasFlag(pnl_calc_type.bid_or_ask)) { + key.sec_md.Value.md.get_bbo(ref _bbo_ref, out NativePriceSize bid, out NativePriceSize ask); double summary_pnl = 0.0; + double start_pnl_total = 0.0; + double day_pnl_total = 0.0; foreach (var fill in _fills) { double _tick_pnl = PnLCalculator.get_pnl(bid.price, ask.price, fill, cpp); summary_pnl += _tick_pnl; + if (fill.Timestamp.ToDateTime() >= _start_of_day) { + day_pnl_total += _tick_pnl; + } else { + start_pnl_total += _tick_pnl; + } } - pnl = summary_pnl; + total_pnl = summary_pnl; + day_pnl = day_pnl_total; + start_pnl = start_pnl_total; + pnl_price = Double.NaN; } else { - double driver = PnLCalculator.get_driver(key.sec_md.Value, pnl_request); - - double summary_pnl = 0.0; - foreach (var fill in _fills) { - double _tick_pnl = PnLCalculator.get_pnl(driver, fill, cpp); - summary_pnl += _tick_pnl; + if (PnLCalculator.get_driver(ref _bbo_ref, ref _last_ref, key.sec_md.Value, pnl_request, ref _driver)) { + double summary_pnl = 0.0; + double start_pnl_total = 0.0; + double day_pnl_total = 0.0; + foreach (var fill in _fills) { + double _tick_pnl = PnLCalculator.get_pnl(_driver, fill, cpp); + summary_pnl += _tick_pnl; + if (fill.Timestamp.ToDateTime() >= _start_of_day) { + day_pnl_total += _tick_pnl; + } else { + start_pnl_total += _tick_pnl; + } + } + total_pnl = summary_pnl; + day_pnl = day_pnl_total; + start_pnl = start_pnl_total; + pnl_price = _driver; } - pnl = summary_pnl; - pnl_price = driver; } } } @@ -89,9 +113,9 @@ namespace mbts.spark { key.exchange == other.exchange && key.security == other.security && key.symbol == other.symbol && - buy_quantity == other.buy_quantity && - sell_quantity == other.sell_quantity && - pnl == other.pnl; + day_long_quantity == other.day_long_quantity && + day_short_quantity == other.day_short_quantity && + total_pnl == other.total_pnl; } public override int GetHashCode() { @@ -104,7 +128,7 @@ namespace mbts.spark { } public override string ToString() { - return $"account: {account} exchange: {exchange_string} symbol: {symbol} security: {security_text} buy_quantity: {buy_quantity} sell_quantity: {sell_quantity} pnl: {pnl}"; + return $"account: {account} exchange: {exchange_string} symbol: {symbol} security: {security_text} buy_quantity: {day_long_quantity} sell_quantity: {day_short_quantity} pnl: {total_pnl}"; } public string account { @@ -140,56 +164,77 @@ namespace mbts.spark { set => ignore(); } - - public double buy_quantity { - get => _buy_quantity; + + public double start_net_position { + get => _start_net_position; set { - if (_buy_quantity != value) { - _buy_quantity = value; - RaisePropertyChanged(nameof(buy_quantity)); + if (_start_net_position != value) { + _start_net_position = value; + RaisePropertyChanged(nameof(start_net_position)); + } + } + } + public double start_pnl { + get => _start_pnl; + set { + if (_start_pnl != value) { + _start_pnl = value; + RaisePropertyChanged(); + } + } + } + public double day_long_quantity { + get => _day_long_quantity; + set { + if (_day_long_quantity != value) { + _day_long_quantity = value; + RaisePropertyChanged(nameof(day_long_quantity)); + } + } + } + public double day_short_quantity { + get => _day_short_quantity; + set { + if (_day_short_quantity != value) { + _day_short_quantity = value; + RaisePropertyChanged(nameof(day_short_quantity)); + } + } + } + public double day_pnl { + get => _sod_pnl; + set { + if (_sod_pnl != value) { + _sod_pnl = value; + RaisePropertyChanged(); } } } - public double sell_quantity { - get => _sell_quantity; + + + public double total_net_position { + get => _total_net_position; set { - if (_sell_quantity != value) { - _sell_quantity = value; - RaisePropertyChanged(nameof(sell_quantity)); + if (_total_net_position != value) { + _total_net_position = value; + RaisePropertyChanged(nameof(total_net_position)); } } } - public double sod_position { - get => _sod_position; + public double total_pnl { + get => _total_pnl; set { - if (_sod_position != value) { - _sod_position = value; - RaisePropertyChanged(nameof(sod_position)); + if (_total_pnl != value) { + _total_pnl = value; + RaisePropertyChanged(nameof(total_pnl)); } } } - public double net_position { - get => _net_position; - set { - if (_net_position != value) { - _net_position = value; - RaisePropertyChanged(nameof(net_position)); - } - } - } - public double pnl { - get => _pnl; - set { - if (_pnl != value) { - _pnl = value; - RaisePropertyChanged(nameof(pnl)); - } - } - } + public double pnl_price { get => _pnl_price; @@ -221,6 +266,9 @@ namespace mbts.spark { } } + //public IEnumerable sod_fills => _fills.Where(f => f.Timestamp.ToDateTime() >= _start_of_day); + //public IEnumerable pos_fills => _fills.Where(f => f.Timestamp.ToDateTime() < _start_of_day); + } From edf5c784050cf1b2a24092ac6a622281fa9bda6d Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Tue, 10 Jul 2018 16:56:23 -0500 Subject: [PATCH 2/9] part of SPARK-1454; column filters and sort criteria --- .../Modules/Trading/PAndLWindowFactory.cs | 49 +++++++++++++- src/client/spark/view/Spark/PAndL/PAndLVM.cs | 65 ++++++++++++++----- .../spark/view/Spark/PAndL/PAndLWindow.cs | 5 +- .../spark/view/Spark/PAndL/PnLCalculator.cs | 8 +-- .../spark/view/Spark/PAndL/PnLSummaryRow.cs | 14 ++-- 5 files changed, 113 insertions(+), 28 deletions(-) diff --git a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs index b1f022725..f35adfb94 100644 --- a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs +++ b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs @@ -1,6 +1,7 @@ using bts.api; using bts.spark; using mbts.spark; +using mbts.spark.filters; using mbts.ui; using mbts.ui.spark; using ProtoBuf; @@ -20,7 +21,6 @@ namespace bts.shell { _dispose = bus.subscribe(SparkTags.ShowPAndLTag).OnDispatcher().Subscribe(show); } - public override Task load(PAndLStartupData state) { var w = new PAndLWindow(_model, this, _bus, state, SparkCommands.ShowPAndL.icon) as IShellWindow; return Task.FromResult(w); @@ -33,6 +33,53 @@ namespace bts.shell { [ProtoContract] public class PAndLStartupData { + + /// + /// Parameterless constructor for protobuf + /// + public PAndLStartupData() { } + + public PAndLStartupData(List sorting_criteria, FilterStates filters) { + this.sort_criteria = sorting_criteria; + this.filters = filters; + } + + [ProtoMember(5)] + public readonly List sort_criteria; + [ProtoMember(10)] + public FilterStates filters; + + + } + + [ProtoContract] + public class FilterStates { + [ProtoMember(1)] + public string account_regex; + [ProtoMember(2)] + public string exchange_regex; + [ProtoMember(3)] + public string symbol_regex; + [ProtoMember(4)] + public string security_regex; + [ProtoMember(5)] + public IntMinMaxState start_net_pos; + [ProtoMember(6)] + public DoubleMinMaxState start_pnl; + [ProtoMember(7)] + public UIntMinMaxState day_long_pos; + [ProtoMember(8)] + public UIntMinMaxState day_short_pos; + [ProtoMember(9)] + public DoubleMinMaxState day_pnl; + [ProtoMember(10)] + public IntMinMaxState total_net_pos; + [ProtoMember(11)] + public DoubleMinMaxState total_pnl; + [ProtoMember(12)] + public DoubleMinMaxState pnl_price; + + } } diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index 3635b04fa..b9669336f 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -37,14 +37,31 @@ namespace mbts.spark { } }); pnl_view = CollectionViewSource.GetDefaultView(_pnl_data); - pnl_view.SetSortingCriteria( - new List() { - new SortingCriteria("account", true) - , new SortingCriteria("exchange_string", true) - , new SortingCriteria("symbol", true) - , new SortingCriteria("security_text", true) - } - ); + + if (state?.filters is FilterStates fs) { + account_filter.load_state(fs.account_regex); + exchange_string_filter.load_state(fs.exchange_regex); + symbol_filter.load_state(fs.symbol_regex); + security_filter.load_state(fs.security_regex); + start_position_filter.load_state(fs.start_net_pos); + start_pnl_filter.load_state(fs.start_pnl); + day_long_quantity_filter.load_state(fs.day_long_pos); + day_short_quantity_filter.load_state(fs.day_short_pos); + day_pnl_filter.load_state(fs.day_pnl); + total_net_position_filter.load_state(fs.total_net_pos); + total_pnl_filter.load_state(fs.total_pnl); + pnl_price_filter.load_state(fs.pnl_price); + } + if (state != null && state.sort_criteria.Any()) { + pnl_view.SetSortingCriteria(state.sort_criteria); + } else { + pnl_view.SetSortingCriteria(new List() { + new SortingCriteria(nameof(PnLSummaryRow.account), true) + , new SortingCriteria(nameof(PnLSummaryRow.exchange_string), true) + , new SortingCriteria(nameof(PnLSummaryRow.symbol), true) + , new SortingCriteria(nameof(PnLSummaryRow.security_text), true) + }); + } pnl_view.CollectionChanged += Pnl_view_CollectionChanged; //Account[] accounts = @@ -127,7 +144,7 @@ namespace mbts.spark { } set { _total_aggregate_pnl = value; - RaisePropertyChanged(nameof(total_aggregate_pnl)); + RaisePropertyChanged(); } } @@ -136,7 +153,7 @@ namespace mbts.spark { get { return _total_aggregate_net_pos; } set { _total_aggregate_net_pos = value; - RaisePropertyChanged(nameof(total_aggregate_net_pos)); + RaisePropertyChanged(); } } @@ -145,7 +162,7 @@ namespace mbts.spark { get { return _total_aggregate_buy_qty; } set { _total_aggregate_buy_qty = value; - RaisePropertyChanged(nameof(total_aggregate_buy_qty)); + RaisePropertyChanged(); } } @@ -154,7 +171,7 @@ namespace mbts.spark { get { return _total_aggregate_sell_qty; } set { _total_aggregate_sell_qty = value; - RaisePropertyChanged(nameof(total_aggregate_sell_qty)); + RaisePropertyChanged(); } } @@ -164,19 +181,35 @@ namespace mbts.spark { public IDataGridColumnPersistentFilter exchange_string_filter { get; } = new RegexFilterVM(r => r.exchange_string, nameof(PnLSummaryRow.exchange_string)); public IDataGridColumnPersistentFilter symbol_filter { get; } = new RegexFilterVM(r => r.symbol, nameof(PnLSummaryRow.symbol)); public IDataGridColumnPersistentFilter security_filter { get; } = new RegexFilterVM(r => r.security_text, nameof(PnLSummaryRow.security_text)); - public IDataGridColumnPersistentFilter start_position_filter { get; } = new DoubleMinMax(r => r.start_net_position, nameof(PnLSummaryRow.start_net_position)); + public IDataGridColumnPersistentFilter start_position_filter { get; } = new IntMinMax(r => r.start_net_position, nameof(PnLSummaryRow.start_net_position)); public IDataGridColumnPersistentFilter start_pnl_filter { get; } = new DoubleMinMax(r => r.start_pnl, nameof(PnLSummaryRow.start_pnl)); - public IDataGridColumnPersistentFilter day_long_quantity_filter { get; } = new DoubleMinMax(r => r.day_long_quantity, nameof(PnLSummaryRow.day_long_quantity)); - public IDataGridColumnPersistentFilter day_short_quantity_filter { get; } = new DoubleMinMax(r => r.day_short_quantity, nameof(PnLSummaryRow.day_short_quantity)); + public IDataGridColumnPersistentFilter day_long_quantity_filter { get; } = new UIntMinMax(r => r.day_long_quantity, nameof(PnLSummaryRow.day_long_quantity)); + public IDataGridColumnPersistentFilter day_short_quantity_filter { get; } = new UIntMinMax(r => r.day_short_quantity, nameof(PnLSummaryRow.day_short_quantity)); public IDataGridColumnPersistentFilter day_pnl_filter { get; } = new DoubleMinMax(r => r.day_pnl, nameof(PnLSummaryRow.day_pnl)); - public IDataGridColumnPersistentFilter total_net_position_filter { get; } = new DoubleMinMax(r => r.total_net_position, nameof(PnLSummaryRow.total_net_position)); + public IDataGridColumnPersistentFilter total_net_position_filter { get; } = new IntMinMax(r => r.total_net_position, nameof(PnLSummaryRow.total_net_position)); public IDataGridColumnPersistentFilter total_pnl_filter { get; } = new DoubleMinMax(r => r.total_pnl, nameof(PnLSummaryRow.total_pnl)); public IDataGridColumnPersistentFilter pnl_price_filter { get; } = new DoubleMinMax(r => r.pnl_price, nameof(PnLSummaryRow.pnl_price)); public IDataGridColumnPersistentFilter weighted_average_buy_filter { get; } = new DoubleMinMax(r => r.weighted_average_buy, nameof(PnLSummaryRow.weighted_average_buy)); public IDataGridColumnPersistentFilter weighted_average_sell_filter { get; } = new DoubleMinMax(r => r.weighted_average_sell, nameof(PnLSummaryRow.weighted_average_sell)); + public FilterStates get_filters_states() => new FilterStates { + account_regex = account_filter.get_state(), + exchange_regex = exchange_string_filter.get_state(), + symbol_regex = exchange_string_filter.get_state(), + security_regex = security_filter.get_state(), + start_net_pos = start_position_filter.get_state(), + start_pnl = start_pnl_filter.get_state(), + day_long_pos = day_long_quantity_filter.get_state(), + day_short_pos = day_short_quantity_filter.get_state(), + day_pnl = day_pnl_filter.get_state(), + total_net_pos = total_net_position_filter.get_state(), + total_pnl = total_pnl_filter.get_state(), + pnl_price = pnl_price_filter.get_state() + }; + #endregion + public List get_sorting_states() => pnl_view.GetSortingCriteria(); readonly IBus _bus; readonly PAndLModel _model; diff --git a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs index a3bb149f1..1c3e34d4d 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs @@ -27,7 +27,10 @@ namespace mbts.spark { public override string window_type => _window_type; public override PAndLWindowFactory.PAndLStartupData SaveState => _state = - new PAndLWindowFactory.PAndLStartupData(); + new PAndLWindowFactory.PAndLStartupData( + _vm.get_sorting_states(), + _vm.get_filters_states() + ); public override UIElement Content => _view; public override double MinWidth => 894; public string thread_id => PNL_THREAD_ID; diff --git a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs index e8af91ba9..e409076c1 100644 --- a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs +++ b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs @@ -89,10 +89,10 @@ namespace mbts.spark { group pnl_item by (pnl_item.account, pnl_item.exchange, pnl_item.symbol, pnl_item.security, pnl_item.sec_md) into grouping let start_of_day = _get_start_of_day(grouping.Key.symbol) select new PnLSummaryRow(_calc_type, start_of_day, grouping.Key, - grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => y.buy_quantity), - grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => y.sell_quantity), - grouping.Sum(y => y.buy_quantity - y.sell_quantity), - grouping.Where(y => y.fill.Timestamp.ToDateTime() < start_of_day).Sum(y => y.buy_quantity - y.sell_quantity), + grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => (uint)y.buy_quantity), + grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => (uint)y.sell_quantity), + grouping.Sum(y => (int)(y.buy_quantity - y.sell_quantity)), + grouping.Where(y => y.fill.Timestamp.ToDateTime() < start_of_day).Sum(y => (int)(y.buy_quantity - y.sell_quantity)), grouping.weighted_average(y => y.fill.LastPrice, y => y.buy_quantity), grouping.weighted_average(y => y.fill.LastPrice, y => y.sell_quantity), grouping.Select(i => i.fill) diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs index 32cd9dfa3..52f2f8120 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -14,7 +14,9 @@ namespace mbts.spark { internal class PnLSummaryRow : ViewModelBase, IEquatable { DateTime _start_of_day; (string account, Venue? exchange, string symbol, SecurityImmutable? security, SecurityMD? sec_md) key; - double _day_long_quantity, _day_short_quantity, _total_net_position, _start_net_position, _total_pnl, _sod_pnl, _start_pnl, _pnl_price, _weighted_average_buy, _weighted_average_sell; + int _start_net_position, _total_net_position; + uint _day_long_quantity, _day_short_quantity; + double _total_pnl, _sod_pnl, _start_pnl, _pnl_price, _weighted_average_buy, _weighted_average_sell; IEnumerable _fills; ulong _bbo_ref, _last_ref; double _driver; @@ -25,7 +27,7 @@ namespace mbts.spark { public PnLSummaryRow(pnl_calc_type calc_type, DateTime start_of_day, (string, Venue?, string, SecurityImmutable?, SecurityMD?) key, - double buy_quant, double sell_quant, double net_pos, double sod_pos, double wavg_buy, double wavg_sell, IEnumerable fills) { + uint buy_quant, uint sell_quant, int net_pos, int sod_pos, double wavg_buy, double wavg_sell, IEnumerable fills) { this._start_of_day = start_of_day; this.key = key; day_long_quantity = buy_quant; @@ -165,7 +167,7 @@ namespace mbts.spark { } - public double start_net_position { + public int start_net_position { get => _start_net_position; set { if (_start_net_position != value) { @@ -183,7 +185,7 @@ namespace mbts.spark { } } } - public double day_long_quantity { + public uint day_long_quantity { get => _day_long_quantity; set { if (_day_long_quantity != value) { @@ -192,7 +194,7 @@ namespace mbts.spark { } } } - public double day_short_quantity { + public uint day_short_quantity { get => _day_short_quantity; set { if (_day_short_quantity != value) { @@ -213,7 +215,7 @@ namespace mbts.spark { - public double total_net_position { + public int total_net_position { get => _total_net_position; set { if (_total_net_position != value) { From 54676d5aa90d55439ad4ed4ad1cb8206c1f2c220 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Wed, 11 Jul 2018 09:12:06 -0500 Subject: [PATCH 3/9] Adding pnl_calc_type to state proro; tidying up VM --- .../Modules/Trading/PAndLWindowFactory.cs | 9 ++- src/client/spark/view/Spark/PAndL/PAndLVM.cs | 55 ++++++++++--------- .../spark/view/Spark/PAndL/PAndLWindow.cs | 13 +++-- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs index f35adfb94..783b8550a 100644 --- a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs +++ b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs @@ -39,16 +39,21 @@ namespace bts.shell { /// public PAndLStartupData() { } - public PAndLStartupData(List sorting_criteria, FilterStates filters) { + public PAndLStartupData(List sorting_criteria, FilterStates filters) : + this(sorting_criteria, filters, pnl_calc_type.midpoint) { } + + public PAndLStartupData(List sorting_criteria, FilterStates filters, pnl_calc_type calc_type) { this.sort_criteria = sorting_criteria; this.filters = filters; + this.calc_type = calc_type; } [ProtoMember(5)] public readonly List sort_criteria; [ProtoMember(10)] public FilterStates filters; - + [ProtoMember(15)] + public pnl_calc_type calc_type; } diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index b9669336f..493b376eb 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -14,16 +14,18 @@ using static bts.shell.PAndLWindowFactory; namespace mbts.spark { class PAndLVM : DisposableViewModelBase { - readonly Property _selected_pnl_calc_type; - - - public PAndLVM(IBus bus, PAndLModel model, PAndLStartupData state) { + + public PAndLVM(IBus bus, PAndLModel model, PAndLStartupData startup_data) { _bus = bus; _model = model; _selected_pnl_calc_type = backing(nameof(selected_pnl_calc_type)); // get initial value from settings - // else assume waterfall - _selected_pnl_calc_type.Value = pnl_calc_type.midpoint; + // else default to midpoint + // TODO: + // need to respond to settings change somehow as the + // combobox that selected_pnl_calc_type is bound to will be + // gone from this form. + _selected_pnl_calc_type.Value = (startup_data != null && startup_data.calc_type != pnl_calc_type.none) ? startup_data.calc_type : pnl_calc_type.midpoint; _selected_pnl_calc_type.Changed += pnl_calc_type_changed; var pnl = model.on_pnl_data .Sample(_POSITION_UPDATE_PERIOD) @@ -38,7 +40,8 @@ namespace mbts.spark { }); pnl_view = CollectionViewSource.GetDefaultView(_pnl_data); - if (state?.filters is FilterStates fs) { + // restore filter state + if (startup_data?.filters is FilterStates fs) { account_filter.load_state(fs.account_regex); exchange_string_filter.load_state(fs.exchange_regex); symbol_filter.load_state(fs.symbol_regex); @@ -52,8 +55,9 @@ namespace mbts.spark { total_pnl_filter.load_state(fs.total_pnl); pnl_price_filter.load_state(fs.pnl_price); } - if (state != null && state.sort_criteria.Any()) { - pnl_view.SetSortingCriteria(state.sort_criteria); + // restore sort state or supply default value + if (startup_data?.sort_criteria?.Any() == true) { + pnl_view.SetSortingCriteria(startup_data.sort_criteria); } else { pnl_view.SetSortingCriteria(new List() { new SortingCriteria(nameof(PnLSummaryRow.account), true) @@ -64,12 +68,8 @@ namespace mbts.spark { } pnl_view.CollectionChanged += Pnl_view_CollectionChanged; - //Account[] accounts = - - - #region Create grid filters and load state - #endregion - + // setup the main Disposable and + // hook up grid filters _dispose = Disposable.Create(pnl, DataGridFilterHelper.hook_filters(pnl_view, account_filter, @@ -90,6 +90,10 @@ namespace mbts.spark { ); } + private void Pnl_view_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { + _update_totals(); + } + private void pnl_calc_type_changed(pnl_calc_type oldval, pnl_calc_type newval) { _calculate_pnl(); } @@ -98,12 +102,7 @@ namespace mbts.spark { pnl_view.CollectionChanged -= Pnl_view_CollectionChanged; _selected_pnl_calc_type.Changed -= pnl_calc_type_changed; } - - - private void Pnl_view_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { - _update_totals(); - } - + void _update_totals() { if (pnl_view != null) { total_aggregate_pnl = pnl_view.Cast().Where(r => !Double.IsNaN(r.total_pnl)).Sum(r => r.total_pnl); @@ -126,8 +125,8 @@ namespace mbts.spark { } } - private DateTime _last_pnl_update_time = DateTime.MinValue; - private ObservableCollection _pnl_data = new ObservableCollection(); + #region Properties + public ICollectionView pnl_view { get; } public List pnl_calc_types { get; } = EnumHelper.get_values_except(pnl_calc_type.none, pnl_calc_type.waterfall, pnl_calc_type.bid_or_ask, pnl_calc_type.open, pnl_calc_type.close).ToList(); @@ -137,7 +136,6 @@ namespace mbts.spark { set => _selected_pnl_calc_type.Value = value; } - private double _total_aggregate_pnl; public double total_aggregate_pnl { get { return _total_aggregate_pnl; @@ -148,7 +146,6 @@ namespace mbts.spark { } } - private double _total_aggregate_net_pos; public double total_aggregate_net_pos { get { return _total_aggregate_net_pos; } set { @@ -157,7 +154,6 @@ namespace mbts.spark { } } - private double _total_aggregate_buy_qty; public double total_aggregate_buy_qty { get { return _total_aggregate_buy_qty; } set { @@ -166,7 +162,6 @@ namespace mbts.spark { } } - private double _total_aggregate_sell_qty; public double total_aggregate_sell_qty { get { return _total_aggregate_sell_qty; } set { @@ -211,11 +206,17 @@ namespace mbts.spark { public List get_sorting_states() => pnl_view.GetSortingCriteria(); + #endregion + readonly IBus _bus; readonly PAndLModel _model; static readonly TimeSpan _UPDATE_PERIOD = TimeSpan.FromSeconds(1); static readonly TimeSpan _POSITION_UPDATE_PERIOD = TimeSpan.FromMilliseconds(333); + readonly Property _selected_pnl_calc_type; + double _total_aggregate_pnl, _total_aggregate_net_pos, _total_aggregate_buy_qty, _total_aggregate_sell_qty; + DateTime _last_pnl_update_time = DateTime.MinValue; + ObservableCollection _pnl_data = new ObservableCollection(); } diff --git a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs index 1c3e34d4d..0fa58bd01 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs @@ -14,29 +14,30 @@ namespace mbts.spark { internal class PAndLWindow : SparkWindow, ISharedWindow { const string _window_type = "PAndL", PNL_THREAD_ID = "PnL"; - PAndLWindowFactory.PAndLStartupData _state; + PAndLWindowFactory.PAndLStartupData _startup_data; PAndLModel _model; PAndLView _view; PAndLVM _vm; PAndLWindowFactory _factory; - public PAndLWindow(PAndLModel model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData state, ImageSource Icon ) : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) { + public PAndLWindow(PAndLModel model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData startup_data, ImageSource Icon ) : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) { _model = model; _factory = factory; - _state = state; + _startup_data = startup_data; } public override string window_type => _window_type; - public override PAndLWindowFactory.PAndLStartupData SaveState => _state = + public override PAndLWindowFactory.PAndLStartupData SaveState => _startup_data = new PAndLWindowFactory.PAndLStartupData( _vm.get_sorting_states(), - _vm.get_filters_states() + _vm.get_filters_states(), + _vm.selected_pnl_calc_type ); public override UIElement Content => _view; public override double MinWidth => 894; public string thread_id => PNL_THREAD_ID; public void thread_init() { - _vm = new PAndLVM(_bus, _model, _state); + _vm = new PAndLVM(_bus, _model, _startup_data); _view = new PAndLView(_vm); _view.DataContext = _vm; } From a233451333eeaccd0c1b71d3bb7023cceff8406b Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Wed, 11 Jul 2018 12:55:15 -0500 Subject: [PATCH 4/9] add start of day to pnl settings --- .../Modules/Trading/PAndLWindowFactory.cs | 8 +++---- src/client/spark/view/Spark/PAndL/PAndLVM.cs | 4 +++- .../spark/view/Spark/PAndL/PAndLWindow.cs | 3 ++- .../spark/view/Spark/PAndL/PnLCalculator.cs | 21 ++++++++++--------- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs index 783b8550a..17344d270 100644 --- a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs +++ b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs @@ -39,13 +39,11 @@ namespace bts.shell { /// public PAndLStartupData() { } - public PAndLStartupData(List sorting_criteria, FilterStates filters) : - this(sorting_criteria, filters, pnl_calc_type.midpoint) { } - - public PAndLStartupData(List sorting_criteria, FilterStates filters, pnl_calc_type calc_type) { + public PAndLStartupData(List sorting_criteria, FilterStates filters, pnl_calc_type calc_type, TimeSpan start_of_day_time) { this.sort_criteria = sorting_criteria; this.filters = filters; this.calc_type = calc_type; + this.start_of_day_time = start_of_day_time; } [ProtoMember(5)] @@ -54,6 +52,8 @@ namespace bts.shell { public FilterStates filters; [ProtoMember(15)] public pnl_calc_type calc_type; + [ProtoMember(20)] + public TimeSpan? start_of_day_time; } diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index 493b376eb..453ade6d4 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -25,14 +25,16 @@ namespace mbts.spark { // need to respond to settings change somehow as the // combobox that selected_pnl_calc_type is bound to will be // gone from this form. + // ditto start of day changes _selected_pnl_calc_type.Value = (startup_data != null && startup_data.calc_type != pnl_calc_type.none) ? startup_data.calc_type : pnl_calc_type.midpoint; _selected_pnl_calc_type.Changed += pnl_calc_type_changed; + var start_of_day_ts = (startup_data != null && startup_data.start_of_day_time.HasValue) ? startup_data.start_of_day_time.Value : new TimeSpan(17, 0, 0); var pnl = model.on_pnl_data .Sample(_POSITION_UPDATE_PERIOD) .OnDispatcher() .Subscribe(p => { if (p != null) { - PnLCalculator calc = new PnLCalculator(p.state.Values, _selected_pnl_calc_type.Value); + PnLCalculator calc = new PnLCalculator(p.state.Values, _selected_pnl_calc_type.Value, start_of_day_ts); 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/PAndLWindow.cs b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs index 0fa58bd01..ee394c947 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs @@ -30,7 +30,8 @@ namespace mbts.spark { new PAndLWindowFactory.PAndLStartupData( _vm.get_sorting_states(), _vm.get_filters_states(), - _vm.selected_pnl_calc_type + _vm.selected_pnl_calc_type, + new TimeSpan(17,0,0) ); public override UIElement Content => _view; public override double MinWidth => 894; diff --git a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs index e409076c1..49280eed8 100644 --- a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs +++ b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs @@ -78,21 +78,26 @@ namespace mbts.spark { class PnLCalculator { readonly IEnumerable _data; readonly pnl_calc_type _calc_type; + readonly DateTime _start_of_trading_day; - public PnLCalculator(IEnumerable data, pnl_calc_type calc_type) { + public PnLCalculator(IEnumerable data, pnl_calc_type calc_type, TimeSpan start_of_day_ts) { _data = data; _calc_type = calc_type; + // Note that this isn't the acutal start of the trading day + // but it's close enough for pnl purposes. + // In reality, instruments have different schedules + _start_of_trading_day = DateTime.Now.Date.Add(start_of_day_ts).AddDays(-1); } public IEnumerable get_summary_position() { return (from pnl_item in _data group pnl_item by (pnl_item.account, pnl_item.exchange, pnl_item.symbol, pnl_item.security, pnl_item.sec_md) into grouping - let start_of_day = _get_start_of_day(grouping.Key.symbol) - select new PnLSummaryRow(_calc_type, start_of_day, grouping.Key, - grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => (uint)y.buy_quantity), - grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => (uint)y.sell_quantity), + /* let start_of_day = _get_start_of_day(grouping.Key.symbol) */ + select new PnLSummaryRow(_calc_type, _start_of_trading_day, grouping.Key, + grouping.Where(y => y.fill.Timestamp.ToDateTime() < _start_of_trading_day).Sum(y => (uint)y.buy_quantity), + grouping.Where(y => y.fill.Timestamp.ToDateTime() < _start_of_trading_day).Sum(y => (uint)y.sell_quantity), grouping.Sum(y => (int)(y.buy_quantity - y.sell_quantity)), - grouping.Where(y => y.fill.Timestamp.ToDateTime() < start_of_day).Sum(y => (int)(y.buy_quantity - y.sell_quantity)), + grouping.Where(y => y.fill.Timestamp.ToDateTime() >= _start_of_trading_day).Sum(y => (int)(y.buy_quantity - y.sell_quantity)), grouping.weighted_average(y => y.fill.LastPrice, y => y.buy_quantity), grouping.weighted_average(y => y.fill.LastPrice, y => y.sell_quantity), grouping.Select(i => i.fill) @@ -213,10 +218,6 @@ namespace mbts.spark { return (price - driver) * quantity * cpp; } - private static DateTime _get_start_of_day(string symbool) { - // I rekcon this will change according to the symbol's session - return DateTime.Today; - } } } From b5950ab8c07d959bf7760f7cb1a73038fa97474a Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Wed, 11 Jul 2018 13:04:34 -0500 Subject: [PATCH 5/9] actually save the start of day rather than overwriting it anew each time --- src/client/spark/view/Spark/PAndL/PAndLVM.cs | 12 +++++++++--- src/client/spark/view/Spark/PAndL/PAndLWindow.cs | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index 453ade6d4..ed7e1b948 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -25,16 +25,17 @@ namespace mbts.spark { // need to respond to settings change somehow as the // combobox that selected_pnl_calc_type is bound to will be // gone from this form. - // ditto start of day changes + // ditto start of day changes, but this will be a bit more involved + // since we'll need to create a new PnLCalculator when this changes. (infrequent, if ever) _selected_pnl_calc_type.Value = (startup_data != null && startup_data.calc_type != pnl_calc_type.none) ? startup_data.calc_type : pnl_calc_type.midpoint; _selected_pnl_calc_type.Changed += pnl_calc_type_changed; - var start_of_day_ts = (startup_data != null && startup_data.start_of_day_time.HasValue) ? startup_data.start_of_day_time.Value : new TimeSpan(17, 0, 0); + _start_of_day_ts = (startup_data != null && startup_data.start_of_day_time.HasValue) ? startup_data.start_of_day_time.Value : new TimeSpan(17, 0, 0); var pnl = model.on_pnl_data .Sample(_POSITION_UPDATE_PERIOD) .OnDispatcher() .Subscribe(p => { if (p != null) { - PnLCalculator calc = new PnLCalculator(p.state.Values, _selected_pnl_calc_type.Value, start_of_day_ts); + PnLCalculator calc = new PnLCalculator(p.state.Values, _selected_pnl_calc_type.Value, _start_of_day_ts); var summary_position_rows = calc.get_summary_position(); _pnl_data.sync_with(summary_position_rows); _last_pnl_update_time = DateTime.Now; @@ -172,6 +173,10 @@ namespace mbts.spark { } } + public TimeSpan start_of_day { + get { return _start_of_day_ts; } + } + #region Filters public IDataGridColumnPersistentFilter account_filter { get; } = new RegexFilterVM(r => r.account, nameof(PnLSummaryRow.account)); @@ -217,6 +222,7 @@ namespace mbts.spark { readonly Property _selected_pnl_calc_type; double _total_aggregate_pnl, _total_aggregate_net_pos, _total_aggregate_buy_qty, _total_aggregate_sell_qty; + TimeSpan _start_of_day_ts; DateTime _last_pnl_update_time = DateTime.MinValue; ObservableCollection _pnl_data = new ObservableCollection(); diff --git a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs index ee394c947..9b2d09dc0 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs @@ -31,7 +31,7 @@ namespace mbts.spark { _vm.get_sorting_states(), _vm.get_filters_states(), _vm.selected_pnl_calc_type, - new TimeSpan(17,0,0) + _vm.start_of_day ); public override UIElement Content => _view; public override double MinWidth => 894; From 77ad9ca011391e29ad4f3154cd287d52e52bb35d Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Thu, 12 Jul 2018 12:00:46 -0500 Subject: [PATCH 6/9] fixing no UTC to UTC time comparison bug; also, somehow swapped day vs start of day comparison --- src/client/spark/view/Spark/PAndL/PnLCalculator.cs | 12 ++++++------ src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs index 49280eed8..af042874e 100644 --- a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs +++ b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs @@ -78,7 +78,7 @@ namespace mbts.spark { class PnLCalculator { readonly IEnumerable _data; readonly pnl_calc_type _calc_type; - readonly DateTime _start_of_trading_day; + readonly DateTime _start_of_trading_day_utc; public PnLCalculator(IEnumerable data, pnl_calc_type calc_type, TimeSpan start_of_day_ts) { _data = data; @@ -86,18 +86,18 @@ namespace mbts.spark { // Note that this isn't the acutal start of the trading day // but it's close enough for pnl purposes. // In reality, instruments have different schedules - _start_of_trading_day = DateTime.Now.Date.Add(start_of_day_ts).AddDays(-1); + _start_of_trading_day_utc = DateTime.Now.Date.Add(start_of_day_ts).AddDays(-1).ToUniversalTime(); } public IEnumerable get_summary_position() { return (from pnl_item in _data group pnl_item by (pnl_item.account, pnl_item.exchange, pnl_item.symbol, pnl_item.security, pnl_item.sec_md) into grouping /* let start_of_day = _get_start_of_day(grouping.Key.symbol) */ - select new PnLSummaryRow(_calc_type, _start_of_trading_day, grouping.Key, - grouping.Where(y => y.fill.Timestamp.ToDateTime() < _start_of_trading_day).Sum(y => (uint)y.buy_quantity), - grouping.Where(y => y.fill.Timestamp.ToDateTime() < _start_of_trading_day).Sum(y => (uint)y.sell_quantity), + select new PnLSummaryRow(_calc_type, _start_of_trading_day_utc, grouping.Key, + grouping.Where(y => y.fill.Timestamp.ToDateTime() >= _start_of_trading_day_utc).Sum(y => (uint)y.buy_quantity), + grouping.Where(y => y.fill.Timestamp.ToDateTime() >= _start_of_trading_day_utc).Sum(y => (uint)y.sell_quantity), grouping.Sum(y => (int)(y.buy_quantity - y.sell_quantity)), - grouping.Where(y => y.fill.Timestamp.ToDateTime() >= _start_of_trading_day).Sum(y => (int)(y.buy_quantity - y.sell_quantity)), + grouping.Where(y => y.fill.Timestamp.ToDateTime() < _start_of_trading_day_utc).Sum(y => (int)(y.buy_quantity - y.sell_quantity)), grouping.weighted_average(y => y.fill.LastPrice, y => y.buy_quantity), grouping.weighted_average(y => y.fill.LastPrice, y => y.sell_quantity), grouping.Select(i => i.fill) diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs index 52f2f8120..2d4b3b05c 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -27,11 +27,11 @@ namespace mbts.spark { public PnLSummaryRow(pnl_calc_type calc_type, DateTime start_of_day, (string, Venue?, string, SecurityImmutable?, SecurityMD?) key, - uint buy_quant, uint sell_quant, int net_pos, int sod_pos, double wavg_buy, double wavg_sell, IEnumerable fills) { + uint day_buy_quant, uint day_sell_quant, int net_pos, int sod_pos, double wavg_buy, double wavg_sell, IEnumerable fills) { this._start_of_day = start_of_day; this.key = key; - day_long_quantity = buy_quant; - day_short_quantity = sell_quant; + day_long_quantity = day_buy_quant; + day_short_quantity = day_sell_quant; total_net_position = net_pos; start_net_position = sod_pos; weighted_average_buy = wavg_buy; From 6f39705d4fab2157449820fc072225d02fcc1b80 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Thu, 12 Jul 2018 13:26:47 -0500 Subject: [PATCH 7/9] PnL will now use cpp from config rather than security --- .../spark/view/Spark/PAndL/PnLSummaryRow.cs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs index 2d4b3b05c..a59c27111 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -20,8 +20,7 @@ namespace mbts.spark { IEnumerable _fills; ulong _bbo_ref, _last_ref; double _driver; - //List _schedules = new List() { MarketSchedule.es_schedule }; - + static readonly Dictionary cpp_dict = config.Configuration.get().cpp_entry; public PnLSummaryRow(pnl_calc_type calc_type, @@ -46,18 +45,29 @@ namespace mbts.spark { private double _get_cpp_for_security(SecurityImmutable s) { if (s.security_type() == SecurityType.Spread) { var distinct_cpps = s.Spread.Legs - .Select(l => l.Security.ContractSpec.CashPerPoint) + .Select(l => get_cpp_for_symbol(l.Security)) .Distinct(); bool all_legs_have_same_cpp = distinct_cpps.Count() < 2; if (all_legs_have_same_cpp) { - return s.Spread.Legs.First().Security.ContractSpec.CashPerPoint; + return distinct_cpps.First(); } else { - Logger.error(bts.log.Subsystem.Ui, + Logger.error(bts.log.Subsystem.Ui, $"Security {s} contains legs with the following cash multipliers: { string.Join(",", distinct_cpps.Select(d => d.ToString())) } "); return Double.NaN; } + } else { + return get_cpp_for_symbol(s); + } + + // local function for looking up cpp for a single SecurityImmutable + double get_cpp_for_symbol(SecurityImmutable si) { + if (cpp_dict.TryGetValue(si.Symbol, out double cpp_out)) { + return cpp_out; + } else { + Logger.error(bts.log.Subsystem.Ui, $"No cash multiplier found for Security {si}, Symbol {si.Symbol}"); + } + return double.NaN; } - return s.ContractSpec.CashPerPoint; } From edcb5a5a249a4eea4242b17c2fa140c3f8f33b8b Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Thu, 12 Jul 2018 13:29:21 -0500 Subject: [PATCH 8/9] Changing default day start time to 4:30 as a result of new info from dev call --- src/client/spark/view/Spark/PAndL/PAndLVM.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index ed7e1b948..f1ff1b131 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -29,7 +29,7 @@ namespace mbts.spark { // since we'll need to create a new PnLCalculator when this changes. (infrequent, if ever) _selected_pnl_calc_type.Value = (startup_data != null && startup_data.calc_type != pnl_calc_type.none) ? startup_data.calc_type : pnl_calc_type.midpoint; _selected_pnl_calc_type.Changed += pnl_calc_type_changed; - _start_of_day_ts = (startup_data != null && startup_data.start_of_day_time.HasValue) ? startup_data.start_of_day_time.Value : new TimeSpan(17, 0, 0); + _start_of_day_ts = (startup_data != null && startup_data.start_of_day_time.HasValue) ? startup_data.start_of_day_time.Value : new TimeSpan(16, 30, 0); var pnl = model.on_pnl_data .Sample(_POSITION_UPDATE_PERIOD) .OnDispatcher() From bd2e2674bf3a7805c899f5b9b64438fdddba29a6 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 16 Jul 2018 13:09:02 -0500 Subject: [PATCH 9/9] PR suggestion --- src/client/spark/view/Spark/PAndL/PAndLVM.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index f1ff1b131..8ba4b505c 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -44,7 +44,8 @@ namespace mbts.spark { pnl_view = CollectionViewSource.GetDefaultView(_pnl_data); // restore filter state - if (startup_data?.filters is FilterStates fs) { + var fs = startup_data?.filters; + if (startup_data != null) { account_filter.load_state(fs.account_regex); exchange_string_filter.load_state(fs.exchange_regex); symbol_filter.load_state(fs.symbol_regex);