From a233451333eeaccd0c1b71d3bb7023cceff8406b Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Wed, 11 Jul 2018 12:55:15 -0500 Subject: [PATCH] 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; - } } }