From 21ae8ab78fdf98cc41b40c78e280aed3f6bccea8 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Thu, 1 Mar 2018 10:24:30 -0600 Subject: [PATCH] PnL: adding weighted average --- .../core/GlobalExtensions/GlobalExtensions.cs | 7 ++++ .../spark/view/Spark/PAndL/PAndLView.xaml | 4 ++ .../spark/view/Spark/PAndL/PnLSummaryRow.cs | 38 +++++++++++++++---- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs index 8a331f8c7..3d08b83e0 100644 --- a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs @@ -307,6 +307,13 @@ namespace mbts } } + public static double weighted_average(this IEnumerable vals, Func value, Func weight) { + double weighted_numerator = vals.Sum(x => value(x) * weight(x)); + double weighted_denominator = vals.Sum(x => weight(x)); + if (weighted_denominator != 0) return weighted_numerator / weighted_denominator; + return double.NaN; + } + public struct TimeoutResult { public TimeoutResult(T result, TimeSpan ts) { diff --git a/src/client/spark/view/Spark/PAndL/PAndLView.xaml b/src/client/spark/view/Spark/PAndL/PAndLView.xaml index 3ec6b968d..34fc7ecd6 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLView.xaml +++ b/src/client/spark/view/Spark/PAndL/PAndLView.xaml @@ -48,6 +48,10 @@ c:DataGridFilterProperties.FilterActive="{Binding DataContext.pnl_filter.active, Source={uc:DirectReference _grid}}" c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}" /> + + diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs index f77b5133c..9320d5fdd 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -19,10 +19,12 @@ namespace mbts.spark { 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 - select new PnLSummaryRow(grouping.Key, - grouping.Sum(y => y.buy_quantity), - grouping.Sum(y => y.sell_quantity), + group pnl_item by (pnl_item.account, pnl_item.exchange, pnl_item.symbol, pnl_item.security, pnl_item.sec_md) into grouping + select new PnLSummaryRow(grouping.Key, + grouping.Sum(y => y.buy_quantity), + grouping.Sum(y => 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.Sum(y => y.get_specific_pnl_type(pnl_request)),*/ grouping.Select( i => i.fill) ) @@ -32,7 +34,7 @@ namespace mbts.spark { class PnLSummaryRow : ViewModelBase, IEquatable { (string account, Venue? exchange, string symbol, SecurityImmutable? security, SecurityMD? sec_md) key; - double _buy_quantity, _sell_quantity, _pnl; + double _buy_quantity, _sell_quantity, _pnl, _weighted_average_buy, _weighted_average_sell; IEnumerable _fills; Dictionary _cpp_store_mockup = new Dictionary() { { "ES", 50.0 }, @@ -49,10 +51,13 @@ namespace mbts.spark { { "ZS", 50.0 } }; - public PnLSummaryRow((string, Venue?, string, SecurityImmutable?, SecurityMD?) key, double buy_quant, double sell_quant, /*double pnl_val,*/ IEnumerable fills) { + public PnLSummaryRow((string, Venue?, string, SecurityImmutable?, SecurityMD?) key, + double buy_quant, double sell_quant, double wavg_buy, double wavg_sell, /*double pnl_val,*/ IEnumerable fills) { this.key = key; buy_quantity = buy_quant; sell_quantity = sell_quant; + weighted_average_buy = wavg_buy; + weighted_average_sell = wavg_sell; //pnl = pnl_val; _fills = fills; } @@ -256,7 +261,26 @@ namespace mbts.spark { RaisePropertyChanged(nameof(pnl)); } } - + } + + public double weighted_average_buy { + get => _weighted_average_buy; + set { + if (_weighted_average_buy != value) { + _weighted_average_buy = value; + RaisePropertyChanged(nameof(weighted_average_buy)); + } + } + } + + public double weighted_average_sell { + get => _weighted_average_sell; + set { + if (_weighted_average_sell != value) { + _weighted_average_sell = value; + RaisePropertyChanged(nameof(weighted_average_sell)); + } + } } }