diff --git a/src/client/spark/Modules/Trading/TradingShellModule.cs b/src/client/spark/Modules/Trading/TradingShellModule.cs index 7695c8caf..44aa7d7b6 100644 --- a/src/client/spark/Modules/Trading/TradingShellModule.cs +++ b/src/client/spark/Modules/Trading/TradingShellModule.cs @@ -36,7 +36,7 @@ namespace bts.shell public IEnumerable actions => new [] { SparkCommands.ShowOrders.complete(() => new OrderBookWindowFactory.OrderBookStartupData()), SparkCommands.ShowFills.complete(() => new FillsWindowFactory.StartupData()), - //SparkCommands.ShowPAndL.complete(() => new PAndLWindowFactory.PAndLStartupData()), + SparkCommands.ShowPAndL.complete(() => new PAndLWindowFactory.PAndLStartupData()), SparkCommands.PickLadder, SparkCommands.PickOrderTicket }; diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index 1ce3751e0..a5bf7ca07 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -18,15 +18,26 @@ namespace mbts.spark public PAndLVM(IBus bus, PAndLModel model, PAndLStartupData state) { _bus = bus; _model = model; - var pnl = _model.on_pnl_data + var pnl = model.on_pnl_data .ObserveOnDispatcher() .Subscribe(p => { - PnLCalculator calc = new PnLCalculator(p); - var results = calc.get_pnl(); - _pnl_data.sync_with(results); + if (p != null) { + PnLCalculator calc = new PnLCalculator(p); + var summary_position_rows = calc.get_summary_position(); + _pnl_data.sync_with(summary_position_rows); + } }); - pnl_view = CollectionViewSource.GetDefaultView(_pnl_data); + + _dispose = Disposable.Create(pnl, + WorkLoop.Start(_calculate_pnl, TimeSpan.FromSeconds(1)) + ); + } + + void _calculate_pnl() { + foreach (var r in _pnl_data) { + r.update_pnl(pnl_calc_type.open); + } } private ObservableCollection _pnl_data = new ObservableCollection(); diff --git a/src/client/spark/view/Spark/PAndL/PnLData.cs b/src/client/spark/view/Spark/PAndL/PnLData.cs index 117b8abb5..c031886b5 100644 --- a/src/client/spark/view/Spark/PAndL/PnLData.cs +++ b/src/client/spark/view/Spark/PAndL/PnLData.cs @@ -12,35 +12,40 @@ namespace mbts.spark { [Flags] public enum pnl_calc_type { none = 0, - last = 1, - mid = 2, - settlement = 4, - closing = 8 + waterfall = 1, + last = 2, + bid_and_ask = 4, + mid = 8, + open = 16, + close = 32, + settle = 64 } class PnLData { public readonly FillImmutable fill; public readonly SecurityMD? sec_md; - private pnl_calc_type calc_type_available; - private Dictionary _pnl_driver_dict = new Dictionary(); + //private pnl_calc_type calc_type_available; + //private Dictionary _pnl_calc_dict = new Dictionary(); + public PnLData(FillImmutable fill, SecurityMD? sec_md) { this.fill = fill; this.sec_md = sec_md; - if (sec_md.HasValue) { - 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); + //if (sec_md.HasValue) { + // 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); - - } + // _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); + // _calc_and_set_type(bid.price, ask.price, pnl_calc_type.direct); + // _calc_and_set_type(sec_md.Value.md.open_price, pnl_calc_type.open); + //} } @@ -51,30 +56,43 @@ namespace mbts.spark { 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_driver_dict[key] = (driver - fill.LastPrice) * fill.LastShares;// * sec_md.Value.security.ContractSpec.CashPerPoint; - } else if (fill.Side == Side.Sell) { - _pnl_driver_dict[key] = (fill.LastPrice - driver) * fill.LastShares;// * sec_md.Value.security.ContractSpec.CashPerPoint; - } - } - } + //public double get_specific_pnl_type(pnl_calc_type request) { + // if (request.HasFlag(pnl_calc_type.waterfall)) return _waterfall_pnl(); + // return Double.NaN; + //} - 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; + //private void _calc_and_set_type (double bid, double ask, pnl_calc_type key) { + // if (fill.Side == Side.Buy) { _calc_and_set_type(bid, key); } + // if (fill.Side == Side.Sell) { _calc_and_set_type(ask, key); } + //} + //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_calc_dict[key] = _get_long_pnl(driver); } + // if (fill.Side == Side.Sell) { _pnl_calc_dict[key] = _get_short_pnl(driver); } + // } + //} - } - public double pnl { - get { - return _waterfall_pnl(); - } - } + //private double _get_long_pnl(double driver) { + // return (driver - fill.LastPrice) * fill.LastShares; // * sec_md.Value.security.ContractSpec.CashPerPoint; + //} + + //private double _get_short_pnl(double driver) { + // return (fill.LastPrice - driver) * fill.LastShares;// * sec_md.Value.security.ContractSpec.CashPerPoint; + //} + + //private double _waterfall_pnl() { + // if (calc_type_available.HasFlag(pnl_calc_type.last)) return _pnl_calc_dict[pnl_calc_type.last]; + // if (calc_type_available.HasFlag(pnl_calc_type.mid)) return _pnl_calc_dict[pnl_calc_type.mid]; + // if (calc_type_available.HasFlag(pnl_calc_type.settlement)) return _pnl_calc_dict[pnl_calc_type.settlement]; + // if (calc_type_available.HasFlag(pnl_calc_type.closing)) return _pnl_calc_dict[pnl_calc_type.closing]; + // return Double.NaN; + //} + //public double pnl { + // get { + // return _waterfall_pnl(); + // } + //} } diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs index 5cdfef105..cbcf09f88 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -1,6 +1,7 @@ using bts.api; using bts.api.orderman; using mbts.co2; +using mbts.ui.shell; using System; using System.Collections.Generic; using System.Linq; @@ -16,33 +17,171 @@ namespace mbts.spark { _data = data; } - public IEnumerable get_pnl() { + 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) into grouping + 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.Sum(y => y.pnl) + /*grouping.Sum(y => y.get_specific_pnl_type(pnl_request)),*/ + grouping.Select( i => i.fill) ) ); } } - class PnLSummaryRow { - (string account, Venue? exchange, string symbol, SecurityImmutable? security) key; + class PnLSummaryRow : ViewModelBase, IEquatable { + (string account, Venue? exchange, string symbol, SecurityImmutable? security, SecurityMD? md) key; double _buy_quantity, _sell_quantity, _pnl; + IEnumerable _fills; - public PnLSummaryRow((string, Venue?, string, SecurityImmutable?) key, double buy_quant, double sell_quant, double pnl) { + public PnLSummaryRow((string, Venue?, string, SecurityImmutable?, SecurityMD?) key, double buy_quant, double sell_quant, /*double pnl_val,*/ IEnumerable fills) { this.key = key; - _buy_quantity = buy_quant; - _sell_quantity = sell_quant; - _pnl = pnl; + buy_quantity = buy_quant; + sell_quantity = sell_quant; + //pnl = pnl_val; + _fills = fills; } + public void update_pnl(pnl_calc_type pnl_request) { + if (key.md.HasValue) { + if (pnl_request.HasFlag(pnl_calc_type.bid_and_ask)) { + ulong unused = 0; + key.md.Value.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); + double summary_pnl = 0.0; + foreach (var fill in _fills) { + double _tick_pnl = _get_pnl(bid.price, ask.price, fill); + summary_pnl += _tick_pnl; + } + pnl = summary_pnl; + } else { + double driver = _get_driver(pnl_request); + double summary_pnl = 0.0; + foreach (var fill in _fills) { + double _tick_pnl = _get_pnl(driver, fill); + summary_pnl += _tick_pnl; + } + pnl = summary_pnl; + //Random r = new Random(); + //pnl = r.NextDouble() * 100; + } + } + } + + private double _get_driver(pnl_calc_type pnl_request) { + ulong unused = 0; + key.md.Value.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); + key.md.Value.md.get_last_trade(ref unused, out NativePriceSize last, out DateTime unused1, out ulong unused2); + + 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; + } + + double mid = TickUtils.mid_tick(key.md.Value.security.ContractSpec, bid.price, ask.price); + // last price is not valid, so try the mid + if (!Double.IsNaN(mid)) { + return mid; + } + + // last bid + // ltp and bid available, no ask + 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; + } else { + return bid.price; + } + } + + // last ask + // ltp and ask available, no bid + 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; + } else { + return ask.price; + } + } + + // last only + if (!Double.IsNaN(last.price)) { + return last.price; + } + + // indicative, no support yet + + // bid or ask available and a settle + if ( (!Double.IsNaN(bid.price) || !Double.IsNaN(ask.price)) && !Double.IsNaN(key.md.Value.md.settlement_price)) { + // if bid and no ask and settle and bid > Settle + if ( !Double.IsNaN(bid.price) && Double.IsNaN(ask.price) && !Double.IsNaN(key.md.Value.md.settlement_price) && bid.price > key.md.Value.md.settlement_price) { + return bid.price; + } + // if ask and no bid and settle and ask < Settle + if (!Double.IsNaN(ask.price) && Double.IsNaN(bid.price) && !Double.IsNaN(key.md.Value.md.settlement_price) && ask.price < key.md.Value.md.settlement_price) { + return ask.price; + } + } + + // Settlement + // neither bid or ask are available + if (!Double.IsNaN(key.md.Value.md.settlement_price)) { + return key.md.Value.md.settlement_price; + } + + // Close + return key.md.Value.md.close_price; + + } // 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(key.md.Value.security.ContractSpec, bid.price, ask.price); + if (pnl_request.HasFlag(pnl_calc_type.open)) return key.md.Value.md.open_price; + if (pnl_request.HasFlag(pnl_calc_type.close)) return key.md.Value.md.close_price; + if (pnl_request.HasFlag(pnl_calc_type.settle)) return key.md.Value.md.settlement_price; + return Double.NaN; + } + + private double _get_pnl(double bid_for_long, double ask_for_short, FillImmutable fill) { + if (fill.Side == Side.Buy) { return _get_long_pnl(bid_for_long, fill.LastPrice, fill.LastShares); } + if (fill.Side == Side.Sell) { return _get_short_pnl(ask_for_short, fill.LastPrice, fill.LastShares); } + return Double.NaN; + } + + private double _get_pnl(double driver, FillImmutable fill) { + if (!Double.IsNaN(driver)) { + if (fill.Side == Side.Buy) { return _get_long_pnl(driver, fill.LastPrice, fill.LastShares); } + if (fill.Side == Side.Sell) { return _get_short_pnl(driver, fill.LastPrice, fill.LastShares); } + } + return Double.NaN; + } + + private double _get_long_pnl(double driver, double price, double quantity, double cpp = 1.0) { + return (driver - price) * quantity * cpp; + } + + private double _get_short_pnl(double driver, double price, double quantity, double cpp = 1.0) { + return (price - driver) * quantity * cpp; + } + void ignore() {/*These setters are just for WPF databinding :(*/ throw new NotImplementedException(); } + // probably need to come up with a GetHashCode() while we're in here + public bool Equals(PnLSummaryRow other) { + return key.account == other.account && + 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; + } + public string account { get => key.account; set => ignore(); @@ -69,19 +208,36 @@ namespace mbts.spark { set => ignore(); } + public double buy_quantity { get => _buy_quantity; - set => ignore(); + set { + if (_buy_quantity != value) { + _buy_quantity = value; + RaisePropertyChanged(nameof(buy_quantity)); + } + } } public double sell_quantity { - get => _sell_quantity; - set => ignore(); + get => _sell_quantity; + set { + if (_sell_quantity != value) { + _sell_quantity = value; + RaisePropertyChanged(nameof(sell_quantity)); + } + } } public double pnl { get => _pnl; - set => ignore(); + set { + if (_pnl != value) { + _pnl = value; + RaisePropertyChanged(nameof(pnl)); + } + } + } }