diff --git a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs index b1f022725..17344d270 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,58 @@ namespace bts.shell { [ProtoContract] public class PAndLStartupData { + + /// + /// Parameterless constructor for protobuf + /// + public PAndLStartupData() { } + + 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)] + public readonly List sort_criteria; + [ProtoMember(10)] + public FilterStates filters; + [ProtoMember(15)] + public pnl_calc_type calc_type; + [ProtoMember(20)] + public TimeSpan? start_of_day_time; + + } + + [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 757b927af..8ba4b505c 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,66 +14,105 @@ using static bts.shell.PAndLWindowFactory; namespace mbts.spark { class PAndLVM : DisposableViewModelBase { - - - 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 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. + // 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; + _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() .Subscribe(p => { if (p != null) { - PnLCalculator calc = new PnLCalculator(p.state.Values); + 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; } }); 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) - } - ); + + // restore filter state + 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); + 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); + } + // 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) + , 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 = - - - #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, - 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) ); } - protected override void Dispose(bool disposing) { - pnl_view.CollectionChanged -= Pnl_view_CollectionChanged; - } - - 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(); + } + + protected override void Dispose(bool disposing) { + pnl_view.CollectionChanged -= Pnl_view_CollectionChanged; + _selected_pnl_calc_type.Changed -= pnl_calc_type_changed; + } + 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,75 +121,111 @@ 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(); } } - private DateTime _last_pnl_update_time = DateTime.MinValue; - private ObservableCollection _pnl_data = new ObservableCollection(); + #region Properties + 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; + } + + 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(); } } - private double _total_net_pos; - public double total_net_pos { - get { return _total_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(); } } - private double _total_buy_qty; - public double total_buy_qty { - get { return _total_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(); } } - private double _total_sell_qty; - public double total_sell_qty { - get { return _total_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(); } } + public TimeSpan start_of_day { + get { return _start_of_day_ts; } + } + #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 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 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 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(); + + #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; + 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/PAndLView.xaml b/src/client/spark/view/Spark/PAndL/PAndLView.xaml index ac770efc4..46ca9cba7 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/PAndLWindow.cs b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs index a3bb149f1..9b2d09dc0 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs @@ -14,26 +14,31 @@ 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 = - new PAndLWindowFactory.PAndLStartupData(); + public override PAndLWindowFactory.PAndLStartupData SaveState => _startup_data = + new PAndLWindowFactory.PAndLStartupData( + _vm.get_sorting_states(), + _vm.get_filters_states(), + _vm.selected_pnl_calc_type, + _vm.start_of_day + ); 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; } diff --git a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs index 2526ba0b3..af042874e 100644 --- a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs +++ b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs @@ -77,20 +77,27 @@ namespace mbts.spark { class PnLCalculator { readonly IEnumerable _data; + readonly pnl_calc_type _calc_type; + readonly DateTime _start_of_trading_day_utc; - public PnLCalculator(IEnumerable data) { + 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_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(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), + /* let start_of_day = _get_start_of_day(grouping.Key.symbol) */ + 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_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) @@ -98,21 +105,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 +129,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 +142,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 +162,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; } @@ -197,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; - } } } 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..a59c27111 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -12,70 +12,106 @@ 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; + 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; - //List _schedules = new List() { MarketSchedule.es_schedule }; + ulong _bbo_ref, _last_ref; + double _driver; + static readonly Dictionary cpp_dict = config.Configuration.get().cpp_entry; - - public PnLSummaryRow((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) { + public PnLSummaryRow(pnl_calc_type calc_type, + DateTime start_of_day, + (string, Venue?, string, SecurityImmutable?, SecurityMD?) key, + 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; - buy_quantity = buy_quant; - sell_quantity = sell_quant; - net_position = net_pos; - sod_position = sod_pos; + 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; 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) { 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; } 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 +125,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 +140,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 +176,77 @@ namespace mbts.spark { set => ignore(); } - - public double buy_quantity { - get => _buy_quantity; + + public int 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 uint day_long_quantity { + get => _day_long_quantity; + set { + if (_day_long_quantity != value) { + _day_long_quantity = value; + RaisePropertyChanged(nameof(day_long_quantity)); + } + } + } + public uint 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 int 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 +278,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); + }