PnL: adding weighted average

This commit is contained in:
Jake Norbut committed 2018-03-01 10:24:45 -06:00
1 parent 60ea25e7f9
commit 21ae8ab78f
3 files changed
+42 -7

No files matched your search

@@ -307,6 +307,13 @@ namespace mbts
}
}
public static double weighted_average<T>(this IEnumerable<T> vals, Func<T, double> value, Func<T, double> 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<T>
{
public TimeoutResult(T result, TimeSpan ts) {
@@ -48,6 +48,10 @@
c:DataGridFilterProperties.FilterActive="{Binding DataContext.pnl_filter.active, Source={uc:DirectReference _grid}}"
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}"
/>
<DataGridTextColumn Header="AvgBuy" Binding="{Binding weighted_average_buy}" IsReadOnly="true"
/>
<DataGridTextColumn Header="AvgSell" Binding="{Binding weighted_average_sell}" IsReadOnly="true"
/>
</DataGrid.Columns>
</c:DataGridEx>
</Grid>
@@ -19,10 +19,12 @@ namespace mbts.spark {
public IEnumerable<PnLSummaryRow> 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<PnLSummaryRow> {
(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<FillImmutable> _fills;
Dictionary<string, double> _cpp_store_mockup = new Dictionary<string, double>() {
{ "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<FillImmutable> 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<FillImmutable> 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));
}
}
}
}