PnL: Waterfall, you so awesome
This commit is contained in:
1 parent
0525f3ba0b
commit
5dedd45a35
4 files changed
+145
-30
No files matched your search
@@ -21,13 +21,15 @@ namespace mbts.spark
|
||||
var pnl = _model.on_pnl_data
|
||||
.ObserveOnDispatcher()
|
||||
.Subscribe(p => {
|
||||
_pnl_data.sync_with(p);
|
||||
PnLCalculator calc = new PnLCalculator(p);
|
||||
var results = calc.get_pnl();
|
||||
_pnl_data.sync_with(results);
|
||||
});
|
||||
|
||||
pnl_view = CollectionViewSource.GetDefaultView(_pnl_data);
|
||||
}
|
||||
|
||||
private ObservableCollection<PnLData> _pnl_data = new ObservableCollection<PnLData>();
|
||||
private ObservableCollection<PnLSummaryRow> _pnl_data = new ObservableCollection<PnLSummaryRow>();
|
||||
public ICollectionView pnl_view { get; }
|
||||
|
||||
|
||||
|
||||
@@ -22,10 +22,13 @@
|
||||
<c:DataGridEx Grid.Row="2" Grid.Column="1" x:Name="_grid" IsReadOnly="True" BorderThickness="0" GridLinesVisibility="None" CanUserSortColumns="True" SelectionUnit="FullRow"
|
||||
ItemsSource="{Binding pnl_view}" >
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="Account" Binding="{Binding account}" IsReadOnly="true" />
|
||||
<DataGridTextColumn Header="Exchange" Binding="{Binding exchange}" IsReadOnly="true" />
|
||||
<DataGridTextColumn Header="Symbol" Binding="{Binding symbol}" IsReadOnly="true" />
|
||||
<DataGridTextColumn Header="Security" Binding="{Binding security_text}" IsReadOnly="true" />
|
||||
<DataGridTextColumn Header="Buy Qty" Binding="{Binding buy_quantity}" IsReadOnly="true" />
|
||||
<DataGridTextColumn Header="Sell Qty" Binding="{Binding sell_quantity}" IsReadOnly="true" />
|
||||
<DataGridTextColumn Header="PnL" Binding="{Binding the_pnl}" IsReadOnly="true" />
|
||||
<DataGridTextColumn Header="PnL" Binding="{Binding pnl}" IsReadOnly="true" />
|
||||
</DataGrid.Columns>
|
||||
</c:DataGridEx>
|
||||
</Grid>
|
||||
|
||||
@@ -8,54 +8,74 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace mbts.spark {
|
||||
|
||||
[Flags]
|
||||
public enum pnl_calc_type {
|
||||
none = 0,
|
||||
last = 1,
|
||||
mid = 2,
|
||||
settlement = 4,
|
||||
closing = 8
|
||||
}
|
||||
|
||||
class PnLData {
|
||||
|
||||
public readonly FillImmutable fill;
|
||||
public readonly SecurityMD? sec_md;
|
||||
public readonly double mid;
|
||||
public readonly double pnl;
|
||||
private pnl_calc_type calc_type_available;
|
||||
private Dictionary<pnl_calc_type, double> _pnl_driver_dict = new Dictionary<pnl_calc_type, double>();
|
||||
|
||||
public PnLData(FillImmutable fill, SecurityMD? sec_md) {
|
||||
this.fill = fill;
|
||||
this.sec_md = sec_md;
|
||||
|
||||
ulong unused = 0;
|
||||
if (sec_md.HasValue) {
|
||||
sec_md.Value.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask);
|
||||
mid = TickUtils.mid_tick(sec_md.Value.security.ContractSpec, bid.price, ask.price);
|
||||
ulong unused = 0;
|
||||
sec_md.Value.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask);
|
||||
double mid = TickUtils.mid_tick(sec_md.Value.security.ContractSpec, bid.price, ask.price);
|
||||
sec_md.Value.md.get_last_trade(ref unused, out NativePriceSize last, out DateTime unused1, out ulong unused2);
|
||||
|
||||
_calc_and_set_type(last.price, pnl_calc_type.last);
|
||||
_calc_and_set_type(mid, pnl_calc_type.mid);
|
||||
_calc_and_set_type(sec_md.Value.md.settlement_price, pnl_calc_type.settlement);
|
||||
_calc_and_set_type(sec_md.Value.md.close_price, pnl_calc_type.closing);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string account => fill.Account;
|
||||
public Venue? exchange => sec_md?.security.Exchange;
|
||||
public string symbol => sec_md?.security.Symbol;
|
||||
public SecurityImmutable? security => sec_md?.security;
|
||||
public double buy_quantity => (fill.Side == Side.Buy) ? fill.LastShares : 0;
|
||||
public double sell_quantity => (fill.Side == Side.Sell) ? fill.LastShares : 0;
|
||||
|
||||
private void _calc_and_set_type(double driver, pnl_calc_type key) {
|
||||
if (!Double.IsNaN(driver)) {
|
||||
calc_type_available |= key;
|
||||
if (fill.Side == Side.Buy) {
|
||||
pnl = (mid - fill.LastPrice) * fill.LastShares;// * sec_md.Value.security.ContractSpec.CashPerPoint;
|
||||
_pnl_driver_dict[key] = (driver - fill.LastPrice) * fill.LastShares;// * sec_md.Value.security.ContractSpec.CashPerPoint;
|
||||
} else if (fill.Side == Side.Sell) {
|
||||
pnl = (fill.LastPrice - mid) * fill.LastShares;// * sec_md.Value.security.ContractSpec.CashPerPoint;
|
||||
_pnl_driver_dict[key] = (fill.LastPrice - driver) * fill.LastShares;// * sec_md.Value.security.ContractSpec.CashPerPoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Account => fill.Account;
|
||||
public Venue? Exchange => sec_md?.security.Exchange;
|
||||
public string Symbol => sec_md?.security.Symbol;
|
||||
public SecurityImmutable? Security => sec_md?.security;
|
||||
private double _waterfall_pnl() {
|
||||
if (calc_type_available.HasFlag(pnl_calc_type.last)) return _pnl_driver_dict[pnl_calc_type.last];
|
||||
if (calc_type_available.HasFlag(pnl_calc_type.mid)) return _pnl_driver_dict[pnl_calc_type.mid];
|
||||
if (calc_type_available.HasFlag(pnl_calc_type.settlement)) return _pnl_driver_dict[pnl_calc_type.settlement];
|
||||
if (calc_type_available.HasFlag(pnl_calc_type.closing)) return _pnl_driver_dict[pnl_calc_type.closing];
|
||||
return Double.NaN;
|
||||
|
||||
void ignore() {/*These setters are just for WPF databinding :(*/ throw new NotImplementedException(); }
|
||||
|
||||
public string security_text {
|
||||
get => sec_md.HasValue ? sec_md.Value.ToString() : "Unknown Security";
|
||||
set => ignore();
|
||||
}
|
||||
|
||||
public double buy_quantity {
|
||||
public double pnl {
|
||||
get {
|
||||
if (fill.Side == Side.Buy) return fill.LastShares;
|
||||
return 0;
|
||||
return _waterfall_pnl();
|
||||
}
|
||||
}
|
||||
|
||||
public double sell_quantity {
|
||||
get {
|
||||
if (fill.Side == Side.Sell) return fill.LastShares;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public double the_pnl => pnl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using mbts.co2;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace mbts.spark {
|
||||
|
||||
class PnLCalculator {
|
||||
readonly List<PnLData> _data;
|
||||
|
||||
public PnLCalculator(List<PnLData> data) {
|
||||
_data = data;
|
||||
}
|
||||
|
||||
public IEnumerable<PnLSummaryRow> get_pnl() {
|
||||
return (from pnl_item in _data
|
||||
group pnl_item by (pnl_item.account, pnl_item.exchange, pnl_item.symbol, pnl_item.security) into grouping
|
||||
select new PnLSummaryRow(grouping.Key,
|
||||
grouping.Sum(y => y.buy_quantity),
|
||||
grouping.Sum(y => y.sell_quantity),
|
||||
grouping.Sum(y => y.pnl)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class PnLSummaryRow {
|
||||
(string account, Venue? exchange, string symbol, SecurityImmutable? security) key;
|
||||
double _buy_quantity, _sell_quantity, _pnl;
|
||||
|
||||
public PnLSummaryRow((string, Venue?, string, SecurityImmutable?) key, double buy_quant, double sell_quant, double pnl) {
|
||||
this.key = key;
|
||||
_buy_quantity = buy_quant;
|
||||
_sell_quantity = sell_quant;
|
||||
_pnl = pnl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ignore() {/*These setters are just for WPF databinding :(*/ throw new NotImplementedException(); }
|
||||
|
||||
public string account {
|
||||
get => key.account;
|
||||
set => ignore();
|
||||
}
|
||||
|
||||
public Venue exchange {
|
||||
get {
|
||||
return key.exchange.HasValue ? key.exchange.Value : Venue.Unset;
|
||||
}
|
||||
set => ignore();
|
||||
}
|
||||
|
||||
public string symbol {
|
||||
get {
|
||||
return key.symbol;
|
||||
}
|
||||
set => ignore();
|
||||
}
|
||||
|
||||
public SecurityImmutable? security => key.security;
|
||||
|
||||
public string security_text {
|
||||
get => security.HasValue ? security.Value.ToString() : "Unknown Security";
|
||||
set => ignore();
|
||||
}
|
||||
|
||||
public double buy_quantity {
|
||||
get => _buy_quantity;
|
||||
set => ignore();
|
||||
}
|
||||
|
||||
public double sell_quantity {
|
||||
get => _sell_quantity;
|
||||
set => ignore();
|
||||
}
|
||||
|
||||
public double pnl {
|
||||
get => _pnl;
|
||||
set => ignore();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user