diff --git a/src/client/spark/view/Spark/PAndL/PAndLView.xaml b/src/client/spark/view/Spark/PAndL/PAndLView.xaml index 1dd62f5f3..8f3552ab6 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLView.xaml +++ b/src/client/spark/view/Spark/PAndL/PAndLView.xaml @@ -33,6 +33,8 @@ c:DataGridFilterProperties.FilterActive="{Binding DataContext.security_filter.active, Source={uc:DirectReference _grid}}" c:DataGridFilterProperties.FilterTemplate="{StaticResource RegexFinder}" /> + > sessions; + + // for object initializers + public MarketSchedule() { } + + public static MarketSchedule es_schedule { + get { + return new MarketSchedule { + product_complex = "MINI S&P FUTURES", + market_segment_id = 64, + security_group = "ES", + sessions = new Dictionary>() { + { DateTime.ParseExact("20180301","yyyyMMdd", null), new List<(trading_session_state session_state, DateTime session_start_time)>() { + (trading_session_state.ReadyToTrade, get_utctimestamp_as_localtime("20180301213000000000")), + } + }, // single day dict entry + { DateTime.ParseExact("20180302","yyyyMMdd", null), new List<(trading_session_state session_state, DateTime session_start_time)>() { + (trading_session_state.ReadyToTrade, get_utctimestamp_as_localtime("20180301230000000000")), + (trading_session_state.Close, get_utctimestamp_as_localtime("20180302220000000000")) + } + } // single day dict entry + } // all sessions for the week + }; // specific market schedule + } // get + } + + public static DateTime get_utctimestamp_as_localtime(string s) { + return DateTimeOffset.ParseExact(s, "yyyyMMddHHmmssffffff", null, System.Globalization.DateTimeStyles.AssumeUniversal).ToLocalTime().LocalDateTime; + } + + } + + + class PnLCalculator { + readonly List _data; + + public PnLCalculator(List data) { + _data = data; + } + + 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), + grouping.Sum(y => y.buy_quantity + y.sell_quantity), + grouping.Where(y => y.fill.Timestamp.ToDateTime() < DateTime.Today).Sum(y => 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.Sum(y => y.get_specific_pnl_type(pnl_request)),*/ + grouping.Select(i => i.fill) + ) + ); + } + + public static double get_driver(SecurityMD sec_md, pnl_calc_type pnl_request) { + ulong unused = 0; + sec_md.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); + sec_md.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(sec_md.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(sec_md.md.settlement_price)) { + // if bid and no ask and settle and bid > Settle + if (!Double.IsNaN(bid.price) && Double.IsNaN(ask.price) && !Double.IsNaN(sec_md.md.settlement_price) && bid.price > sec_md.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(sec_md.md.settlement_price) && ask.price < sec_md.md.settlement_price) { + return ask.price; + } + } + + // Settlement + // neither bid or ask are available + if (!Double.IsNaN(sec_md.md.settlement_price)) { + return sec_md.md.settlement_price; + } + + // Close + return sec_md.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(sec_md.security.ContractSpec, bid.price, ask.price); + if (pnl_request.HasFlag(pnl_calc_type.open)) return sec_md.md.open_price; + if (pnl_request.HasFlag(pnl_calc_type.close)) return sec_md.md.close_price; + if (pnl_request.HasFlag(pnl_calc_type.settle)) return sec_md.md.settlement_price; + return Double.NaN; + } + + public static double get_pnl(double bid_for_long, double ask_for_short, FillImmutable fill, double cpp) { + if (fill.Side == Side.Buy) { return _get_long_pnl(bid_for_long, fill.LastPrice, fill.LastShares, cpp); } + if (fill.Side == Side.Sell) { return _get_short_pnl(ask_for_short, fill.LastPrice, fill.LastShares, cpp); } + return Double.NaN; + } + + public static double get_pnl(double driver, FillImmutable fill, double cpp) { + if (!Double.IsNaN(driver)) { + if (fill.Side == Side.Buy) { return _get_long_pnl(driver, fill.LastPrice, fill.LastShares, cpp); } + if (fill.Side == Side.Sell) { return _get_short_pnl(driver, fill.LastPrice, fill.LastShares, cpp); } + } + return Double.NaN; + } + + private static double _get_long_pnl(double driver, double price, double quantity, double cpp) { + return (driver - price) * quantity * cpp; + } + + private static double _get_short_pnl(double driver, double price, double quantity, double cpp) { + return (price - driver) * quantity * cpp; + } + } + +} + diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs index a31e851f2..46efc275d 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -10,32 +10,10 @@ using System.Threading.Tasks; namespace mbts.spark { - class PnLCalculator { - readonly List _data; - public PnLCalculator(List data) { - _data = data; - } - - 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), - grouping.Sum(y => 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.Sum(y => y.get_specific_pnl_type(pnl_request)),*/ - grouping.Select( i => i.fill) - ) - ); - } - } - - class PnLSummaryRow : ViewModelBase, IEquatable { + internal class PnLSummaryRow : ViewModelBase, IEquatable { (string account, Venue? exchange, string symbol, SecurityImmutable? security, SecurityMD? sec_md) key; - double _buy_quantity, _sell_quantity, _net_position, _pnl, _weighted_average_buy, _weighted_average_sell; + double _buy_quantity, _sell_quantity, _net_position, _sod_position, _pnl, _weighted_average_buy, _weighted_average_sell; IEnumerable _fills; Dictionary _cpp_store_mockup = new Dictionary() { { "ES", 50.0 }, @@ -52,12 +30,17 @@ namespace mbts.spark { { "ZS", 50.0 } }; + //List _schedules = new List() { MarketSchedule.es_schedule }; + + + public PnLSummaryRow((string, Venue?, string, SecurityImmutable?, SecurityMD?) key, - double buy_quant, double sell_quant, double net_pos, double wavg_buy, double wavg_sell, /*double pnl_val,*/ IEnumerable fills) { + double buy_quant, double sell_quant, double net_pos, double sod_pos, double wavg_buy, double wavg_sell, /*double pnl_val,*/ IEnumerable fills) { this.key = key; buy_quantity = buy_quant; sell_quantity = sell_quant; - _net_position = net_pos; + net_position = net_pos; + sod_position = sod_pos; weighted_average_buy = wavg_buy; weighted_average_sell = wavg_sell; //pnl = pnl_val; @@ -77,16 +60,16 @@ namespace mbts.spark { double summary_pnl = 0.0; foreach (var fill in _fills) { - double _tick_pnl = _get_pnl(bid.price, ask.price, fill, cpp); + double _tick_pnl = PnLCalculator.get_pnl(bid.price, ask.price, fill, cpp); summary_pnl += _tick_pnl; } pnl = summary_pnl; } else { - double driver = _get_driver(key.sec_md.Value, pnl_request); + double driver = PnLCalculator.get_driver(key.sec_md.Value, pnl_request); double summary_pnl = 0.0; foreach (var fill in _fills) { - double _tick_pnl = _get_pnl(driver, fill, cpp); + double _tick_pnl = PnLCalculator.get_pnl(driver, fill, cpp); summary_pnl += _tick_pnl; } pnl = summary_pnl; @@ -96,110 +79,12 @@ namespace mbts.spark { } } - private double _get_driver(SecurityMD sec_md, pnl_calc_type pnl_request) { - ulong unused = 0; - sec_md.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); - sec_md.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(sec_md.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(sec_md.md.settlement_price)) { - // if bid and no ask and settle and bid > Settle - if ( !Double.IsNaN(bid.price) && Double.IsNaN(ask.price) && !Double.IsNaN(sec_md.md.settlement_price) && bid.price > sec_md.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(sec_md.md.settlement_price) && ask.price < sec_md.md.settlement_price) { - return ask.price; - } - } - - // Settlement - // neither bid or ask are available - if (!Double.IsNaN(sec_md.md.settlement_price)) { - return sec_md.md.settlement_price; - } - - // Close - return sec_md.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(sec_md.security.ContractSpec, bid.price, ask.price); - if (pnl_request.HasFlag(pnl_calc_type.open)) return sec_md.md.open_price; - if (pnl_request.HasFlag(pnl_calc_type.close)) return sec_md.md.close_price; - if (pnl_request.HasFlag(pnl_calc_type.settle)) return sec_md.md.settlement_price; - return Double.NaN; - } - - private double _get_pnl(double bid_for_long, double ask_for_short, FillImmutable fill, double cpp) { - if (fill.Side == Side.Buy) { return _get_long_pnl(bid_for_long, fill.LastPrice, fill.LastShares, cpp); } - if (fill.Side == Side.Sell) { return _get_short_pnl(ask_for_short, fill.LastPrice, fill.LastShares, cpp); } - return Double.NaN; - } - - private double _get_pnl(double driver, FillImmutable fill, double cpp) { - if (!Double.IsNaN(driver)) { - if (fill.Side == Side.Buy) { return _get_long_pnl(driver, fill.LastPrice, fill.LastShares, cpp); } - if (fill.Side == Side.Sell) { return _get_short_pnl(driver, fill.LastPrice, fill.LastShares, cpp); } - } - return Double.NaN; - } - - private double _get_long_pnl(double driver, double price, double quantity, double cpp) { - return (driver - price) * quantity * cpp; - } - - private double _get_short_pnl(double driver, double price, double quantity, double cpp) { - 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 && + return other != null && + key.account == other.account && key.exchange == other.exchange && key.security == other.security && key.symbol == other.symbol && @@ -255,6 +140,16 @@ namespace mbts.spark { } } + public double sod_position { + get => _sod_position; + set { + if (_sod_position != value) { + _sod_position = value; + RaisePropertyChanged(nameof(sod_position)); + } + } + } + public double net_position { get => _net_position; set {