pnl refactoring - PnLModel didn't really do anything, so I remomed it
This commit is contained in:
1 parent
a3e00217a1
commit
e1229186de
6 files changed
+56
-214
No files matched your search
@@ -19,13 +19,11 @@ using static mbts.ui.controls.DIYGrid;
|
||||
namespace bts.shell {
|
||||
internal class PAndLWindowFactory : ShellWindowFactory<PAndLWindowFactory.PAndLStartupData> {
|
||||
readonly IBus _bus;
|
||||
readonly PAndLModel _model;
|
||||
readonly OrdersModel _orders_model;
|
||||
readonly UserSettingsHub _settings_hub;
|
||||
|
||||
public PAndLWindowFactory(IShellModule owner, IBus bus, UserSettingsHub settings_hub, PAndLModel model, OrdersModel orders_model) : base(owner, "SparkPAndL") {
|
||||
public PAndLWindowFactory(IShellModule owner, IBus bus, UserSettingsHub settings_hub, OrdersModel orders_model) : base(owner, "SparkPAndL") {
|
||||
_bus = bus;
|
||||
_model = model;
|
||||
_orders_model = orders_model;
|
||||
_settings_hub = settings_hub;
|
||||
_dispose = bus.subscribe(SparkTags.ShowPAndLTag).OnDispatcher().Subscribe(show);
|
||||
@@ -47,7 +45,7 @@ namespace bts.shell {
|
||||
state.grid,
|
||||
get: us => us.pnl_grid,
|
||||
set: (us, gs) => us.with(provided_pnl_gridsettings: gs));
|
||||
return new PAndLWindow(_model, _orders_model, this, _bus, state, SparkCommands.ShowPAndL.icon, settings_manager);
|
||||
return new PAndLWindow(_orders_model, this, _bus, state, SparkCommands.ShowPAndL.icon, settings_manager);
|
||||
}
|
||||
|
||||
[ProtoContract]
|
||||
|
||||
@@ -24,11 +24,11 @@ namespace bts.shell {
|
||||
|
||||
[ImportingConstructor]
|
||||
public TradingShellModule(IBus bus, IWindowManager wm, INotificationService notification_service, INotificationServiceDataCache notification_service_data_cache,
|
||||
UserSettingsHub settings_hub, OrdersModel orders_model, PAndLModel pandl_model, ErrorReporter error_reporter) {
|
||||
UserSettingsHub settings_hub, OrdersModel orders_model, ErrorReporter error_reporter) {
|
||||
_lwf = new LadderWindowFactory(this, bus, wm, settings_hub, orders_model);
|
||||
_obwf = new OrderBookWindowFactory(this, bus, notification_service, notification_service_data_cache, orders_model, settings_hub);
|
||||
_fwf = new FillsWindowFactory(this, bus, settings_hub, orders_model);
|
||||
_plwf = new PAndLWindowFactory(this, bus, settings_hub, pandl_model, orders_model);
|
||||
_plwf = new PAndLWindowFactory(this, bus, settings_hub, orders_model);
|
||||
_aswf = new AlgoStudioWindowFactory(this, bus, orders_model, error_reporter);
|
||||
_mfwf = new ManualFillsWindowFactory(this, bus, orders_model);
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
using bts;
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
using System.ComponentModel.Composition;
|
||||
|
||||
namespace mbts.spark {
|
||||
[Export]
|
||||
class PAndLModel : IDisposable {
|
||||
readonly IBus _bus;
|
||||
readonly OrdersModel _order_model;
|
||||
readonly MDLookup _mdlookup;
|
||||
IDisposable _dispose;
|
||||
|
||||
[ImportingConstructor]
|
||||
private PAndLModel(IBus bus, OrdersModel order_model) {
|
||||
_bus = bus;
|
||||
_order_model = order_model;
|
||||
_mdlookup = new MDLookup(bus.subscribe(SparkTags.SecuritiesTag));
|
||||
}
|
||||
|
||||
public IObservable<TableUpdate<PnLData, string>> on_pnl_data =>
|
||||
_order_model.on_fill_tuples.Select(ft => new PnLData(ft, _mdlookup.get_md(ft.security?.Id)), pd => pd.fill.FillKey);
|
||||
|
||||
public SecurityMD? get_security_md(SecurityImmutable? si) => _mdlookup.get_md(si?.Id);
|
||||
|
||||
public void Dispose() =>
|
||||
Disposable.dispose(ref _dispose);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
using mbts.spark.filters;
|
||||
using mbts.ui;
|
||||
using mbts.ui.shell;
|
||||
@@ -17,14 +18,15 @@ using System.Windows.Media;
|
||||
using System.Windows;
|
||||
using mbts.persist;
|
||||
using mbts.ui.settings;
|
||||
using bts;
|
||||
|
||||
namespace mbts.spark {
|
||||
class PAndLVM : DisposableViewModelBase {
|
||||
bool _use_position_service = true;
|
||||
|
||||
public PAndLVM(IBus bus, PAndLModel model, OrdersModel orders_model, PAndLStartupData startup_data, IWindowSettingsManager<GridSettings> settings_manager) {
|
||||
public PAndLVM(IBus bus, OrdersModel orders_model, PAndLStartupData startup_data, IWindowSettingsManager<GridSettings> settings_manager) {
|
||||
_bus = bus;
|
||||
_model = model;
|
||||
_mdlookup = new MDLookup(bus.subscribe(SparkTags.SecuritiesTag));
|
||||
_orders_model = orders_model;
|
||||
_settings_manager = settings_manager;
|
||||
_selected_pnl_calc_type = backing<pnl_calc_type>(nameof(selected_pnl_calc_type));
|
||||
@@ -47,18 +49,17 @@ namespace mbts.spark {
|
||||
// Once the PnL-via-position service is confirmed good, we can refactor much of how the underlying pnl data
|
||||
// is structured and calculated.
|
||||
if (!_use_position_service) {
|
||||
pnl = model.on_pnl_data
|
||||
pnl = _orders_model.on_fill_tuples.Select(ft => new PnLData(ft, _get_security_md(ft.security)), pd => pd.fill.FillKey)
|
||||
.Sample(_POSITION_UPDATE_PERIOD)
|
||||
.OnDispatcher()
|
||||
.Subscribe(p =>
|
||||
{
|
||||
.Subscribe(p => {
|
||||
if (p != null) {
|
||||
PnLCalculator calc = new PnLCalculator(_selected_pnl_calc_type.Value, p.state.Values);
|
||||
var summary_position_rows = calc.get_summary_position();
|
||||
// remove rows for which there is no net pos or no day pos
|
||||
summary_position_rows = summary_position_rows.Where(r =>
|
||||
!(r.start_net_position == 0 && r.day_long_quantity == 0 && r.day_short_quantity == 0)
|
||||
);
|
||||
// remove rows for which there is no net pos or no day pos
|
||||
summary_position_rows = summary_position_rows.Where(r =>
|
||||
!(r.start_net_position == 0 && r.day_long_quantity == 0 && r.day_short_quantity == 0)
|
||||
);
|
||||
_pnl_data.sync_with(summary_position_rows);
|
||||
_last_pnl_update_time = DateTime.Now;
|
||||
}
|
||||
@@ -67,10 +68,28 @@ namespace mbts.spark {
|
||||
pnl = _orders_model.on_positions
|
||||
.Sample(_POSITION_UPDATE_PERIOD)
|
||||
.OnDispatcher()
|
||||
.Subscribe(p => {
|
||||
if (p != null) {
|
||||
PnLCalculator calc = new PnLCalculator(model, _selected_pnl_calc_type.Value, p.state.Values);
|
||||
var summary_position_rows = calc.get_summary_position();
|
||||
.Subscribe(position_data => {
|
||||
if (position_data != null) {
|
||||
var summary_position_rows = position_data.state.Values.Select(p =>
|
||||
{
|
||||
var si = p.Item1;
|
||||
var pos = p.Item2;
|
||||
var sec_md = _get_security_md(si);
|
||||
var key = (p.Item2.Account, si?.Exchange, si?.Symbol, si, sec_md, si?.security_type());
|
||||
return new PnLSummaryRow(_selected_pnl_calc_type.Value,
|
||||
key,
|
||||
(uint)pos.DayLongPos,
|
||||
(uint)pos.DayShortPos,
|
||||
(int)pos.total_pos(),
|
||||
(int)pos.sod_pos(),
|
||||
pos.DayLongAvgPrice,
|
||||
pos.DayShortAvgPrice,
|
||||
null,
|
||||
pos
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
// remove rows for which there is no net pos or no day pos
|
||||
summary_position_rows = summary_position_rows.Where(r =>
|
||||
!(r.start_net_position == 0 && r.day_long_quantity == 0 && r.day_short_quantity == 0)
|
||||
@@ -217,6 +236,9 @@ namespace mbts.spark {
|
||||
}
|
||||
}
|
||||
|
||||
SecurityMD? _get_security_md(SecurityImmutable? si) => _mdlookup.get_md(si?.Id);
|
||||
|
||||
|
||||
#region Properties
|
||||
|
||||
public ICollectionView pnl_view { get; }
|
||||
@@ -313,8 +335,8 @@ namespace mbts.spark {
|
||||
#endregion
|
||||
|
||||
readonly IBus _bus;
|
||||
readonly PAndLModel _model;
|
||||
readonly OrdersModel _orders_model;
|
||||
readonly MDLookup _mdlookup;
|
||||
IWindowSettingsManager<GridSettings> _settings_manager;
|
||||
static readonly TimeSpan _UPDATE_PERIOD = TimeSpan.FromSeconds(1);
|
||||
static readonly TimeSpan _POSITION_UPDATE_PERIOD = TimeSpan.FromMilliseconds(333);
|
||||
|
||||
@@ -17,15 +17,13 @@ namespace mbts.spark {
|
||||
const string _window_type = "PAndL";
|
||||
readonly IWindowSettingsManager<GridSettings> _settings_manager;
|
||||
PAndLWindowFactory.PAndLStartupData _startup_data;
|
||||
PAndLModel _model;
|
||||
OrdersModel _orders_model;
|
||||
PAndLView _view;
|
||||
PAndLVM _vm;
|
||||
PAndLWindowFactory _factory;
|
||||
IReadOnlyList<SparkCommand> _commands;
|
||||
|
||||
public PAndLWindow(PAndLModel model, OrdersModel orders_model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData startup_data, ImageSource Icon, WindowSettingsManager<GridSettings> settings_manager) : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) {
|
||||
_model = model;
|
||||
public PAndLWindow(OrdersModel orders_model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData startup_data, ImageSource Icon, WindowSettingsManager<GridSettings> settings_manager) : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) {
|
||||
_orders_model = orders_model;
|
||||
_factory = factory;
|
||||
_startup_data = startup_data;
|
||||
@@ -54,7 +52,7 @@ namespace mbts.spark {
|
||||
public string thread_id { get; }
|
||||
|
||||
public void thread_init() {
|
||||
_vm = new PAndLVM(_bus, _model, _orders_model, _startup_data, _settings_manager);
|
||||
_vm = new PAndLVM(_bus, _orders_model, _startup_data, _settings_manager);
|
||||
_view = new PAndLView(_vm);
|
||||
_view.DataContext = _vm;
|
||||
_commands = new List<SparkCommand> {
|
||||
|
||||
@@ -83,175 +83,30 @@ namespace mbts.spark {
|
||||
|
||||
class PnLCalculator {
|
||||
readonly IEnumerable<PnLData> _data;
|
||||
readonly IEnumerable<(SecurityImmutable?, PositionUnitImmutable)> _position_data;
|
||||
readonly pnl_calc_type _calc_type;
|
||||
readonly PAndLModel _pnl_model;
|
||||
|
||||
public PnLCalculator(pnl_calc_type calc_type, IEnumerable<PnLData> data) {
|
||||
_data = data;
|
||||
_calc_type = calc_type;
|
||||
}
|
||||
|
||||
public PnLCalculator(PAndLModel model, pnl_calc_type calc_type, IEnumerable<(SecurityImmutable?, PositionUnitImmutable)> data) {
|
||||
_pnl_model = model;
|
||||
_position_data = data;
|
||||
_calc_type = calc_type;
|
||||
}
|
||||
|
||||
|
||||
public IEnumerable<PnLSummaryRow> get_summary_position() {
|
||||
if (_data != null) {
|
||||
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, pnl_item.security_type) into grouping
|
||||
/* let start_of_day = _get_start_of_day(grouping.Key.symbol) */
|
||||
select new PnLSummaryRow(_calc_type, grouping.Key,
|
||||
grouping.Where(y => y.fill.FillKind != Fill.Types.FillKind.Sod).Sum(y => (uint)y.buy_quantity),
|
||||
grouping.Where(y => y.fill.FillKind != Fill.Types.FillKind.Sod).Sum(y => (uint)y.sell_quantity),
|
||||
grouping.Sum(y => (int)(y.buy_quantity - y.sell_quantity)),
|
||||
grouping.Where(y => y.fill.FillKind == Fill.Types.FillKind.Sod).Sum(y => (int)(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.Select(i => i.fill),
|
||||
null
|
||||
)
|
||||
);
|
||||
} else {
|
||||
return _position_data.Select(p => {
|
||||
var si = p.Item1;
|
||||
var pos = p.Item2;
|
||||
var sec_md = _pnl_model.get_security_md(si);
|
||||
var key = (p.Item2.Account, si?.Exchange, si?.Symbol, si, sec_md, si?.security_type());
|
||||
return new PnLSummaryRow(_calc_type, key,
|
||||
(uint)pos.DayLongPos,
|
||||
(uint)pos.DayShortPos,
|
||||
(int)(pos.DayLongPos + pos.SodLongPos - pos.DayShortPos - pos.SodShortPos),
|
||||
(int)(pos.SodLongPos - pos.SodShortPos),
|
||||
pos.DayLongAvgPrice,
|
||||
pos.DayShortAvgPrice,
|
||||
null,
|
||||
pos
|
||||
);
|
||||
});
|
||||
}
|
||||
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, pnl_item.security_type) into grouping
|
||||
/* let start_of_day = _get_start_of_day(grouping.Key.symbol) */
|
||||
select new PnLSummaryRow(_calc_type, grouping.Key,
|
||||
grouping.Where(y => y.fill.FillKind != Fill.Types.FillKind.Sod).Sum(y => (uint)y.buy_quantity),
|
||||
grouping.Where(y => y.fill.FillKind != Fill.Types.FillKind.Sod).Sum(y => (uint)y.sell_quantity),
|
||||
grouping.Sum(y => (int)(y.buy_quantity - y.sell_quantity)),
|
||||
grouping.Where(y => y.fill.FillKind == Fill.Types.FillKind.Sod).Sum(y => (int)(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.Select(i => i.fill),
|
||||
null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static bool get_driver(SecurityMD sec_md, pnl_calc_type pnl_request, ref double driver_price) {
|
||||
// we always want to pass in 0 to the md query so that we don't get back NaN if the market
|
||||
// data hasn't changed between query periods
|
||||
ulong bbo_time = 0;
|
||||
ulong last_trade_time = 0;
|
||||
if (!sec_md.md.get_bbo(ref bbo_time, out NativePriceSize bid, out NativePriceSize ask)) return false;
|
||||
// last is only used in waterfall and last calculations, so if neither of those types are selected
|
||||
// we should not exit early if last is somehow not avaialble
|
||||
if (!sec_md.md.get_last_trade(ref last_trade_time, out NativePriceSize last, out DateTime unused1, out ulong unused2) && pnl_request.HasFlag(pnl_calc_type.waterfall | pnl_calc_type.last)) return false;
|
||||
|
||||
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) {
|
||||
driver_price = last.price;
|
||||
return true;
|
||||
}
|
||||
|
||||
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)) {
|
||||
driver_price = mid;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
driver_price = last.price;
|
||||
return true;
|
||||
} else {
|
||||
driver_price = bid.price;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
driver_price = last.price;
|
||||
return true;
|
||||
} else {
|
||||
driver_price = ask.price;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// last only
|
||||
if (!Double.IsNaN(last.price)) {
|
||||
driver_price = last.price;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
driver_price = bid.price;
|
||||
return true;
|
||||
}
|
||||
// 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) {
|
||||
driver_price = ask.price;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Settlement
|
||||
// neither bid or ask are available
|
||||
if (!Double.IsNaN(sec_md.md.settlement_price)) {
|
||||
driver_price = sec_md.md.settlement_price;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Close
|
||||
driver_price = sec_md.md.close_price;
|
||||
return true;
|
||||
|
||||
} // end waterfall
|
||||
|
||||
if (pnl_request.HasFlag(pnl_calc_type.last)) { driver_price = last.price; return true; }
|
||||
// note that bid_or_ask is not considered here since this method only
|
||||
// supports returning a single driver price; see update_pnl
|
||||
if (pnl_request.HasFlag(pnl_calc_type.midpoint)) { driver_price = (bid.price + ask.price) / 2.0; return true; }
|
||||
if (pnl_request.HasFlag(pnl_calc_type.open)) { driver_price = sec_md.md.open_price; return true; }
|
||||
if (pnl_request.HasFlag(pnl_calc_type.close)) { driver_price = sec_md.md.close_price; return true; }
|
||||
if (pnl_request.HasFlag(pnl_calc_type.settle)) { driver_price = sec_md.md.settlement_price; return true; }
|
||||
driver_price = Double.NaN;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static double get_pnl(double bid_for_long, double ask_for_short, FillImmutable fill, double cpp) {
|
||||
if (fill.Side == Side.Buy && !Double.IsNaN(bid_for_long)) { return get_long_pnl(bid_for_long, fill.LastPrice, fill.LastShares, cpp); }
|
||||
if (fill.Side == Side.Sell && !Double.IsNaN(ask_for_short)) { 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;
|
||||
}
|
||||
|
||||
public static double get_long_pnl(double driver, double price, double quantity, double cpp) =>
|
||||
quantity == 0 ? 0 : (driver - price) * quantity * cpp;
|
||||
|
||||
public static double get_short_pnl(double driver, double price, double quantity, double cpp) =>
|
||||
quantity == 0 ? 0 : (price - driver) * quantity * cpp;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user