Merged pnl_nits2 into master
This commit is contained in:
commit
e40dc48fa8
7 files changed
+475
-229
No files matched your search
@@ -1,6 +1,7 @@
|
||||
using bts.api;
|
||||
using bts.spark;
|
||||
using mbts.spark;
|
||||
using mbts.spark.filters;
|
||||
using mbts.ui;
|
||||
using mbts.ui.spark;
|
||||
using ProtoBuf;
|
||||
@@ -20,7 +21,6 @@ namespace bts.shell {
|
||||
_dispose = bus.subscribe(SparkTags.ShowPAndLTag).OnDispatcher().Subscribe(show);
|
||||
}
|
||||
|
||||
|
||||
public override Task<IShellWindow> load(PAndLStartupData state) {
|
||||
var w = new PAndLWindow(_model, this, _bus, state, SparkCommands.ShowPAndL.icon) as IShellWindow;
|
||||
return Task.FromResult(w);
|
||||
@@ -33,6 +33,58 @@ namespace bts.shell {
|
||||
|
||||
[ProtoContract]
|
||||
public class PAndLStartupData {
|
||||
|
||||
/// <summary>
|
||||
/// Parameterless constructor for protobuf
|
||||
/// </summary>
|
||||
public PAndLStartupData() { }
|
||||
|
||||
public PAndLStartupData(List<SortingCriteria> sorting_criteria, FilterStates filters, pnl_calc_type calc_type, TimeSpan start_of_day_time) {
|
||||
this.sort_criteria = sorting_criteria;
|
||||
this.filters = filters;
|
||||
this.calc_type = calc_type;
|
||||
this.start_of_day_time = start_of_day_time;
|
||||
}
|
||||
|
||||
[ProtoMember(5)]
|
||||
public readonly List<SortingCriteria> sort_criteria;
|
||||
[ProtoMember(10)]
|
||||
public FilterStates filters;
|
||||
[ProtoMember(15)]
|
||||
public pnl_calc_type calc_type;
|
||||
[ProtoMember(20)]
|
||||
public TimeSpan? start_of_day_time;
|
||||
|
||||
}
|
||||
|
||||
[ProtoContract]
|
||||
public class FilterStates {
|
||||
[ProtoMember(1)]
|
||||
public string account_regex;
|
||||
[ProtoMember(2)]
|
||||
public string exchange_regex;
|
||||
[ProtoMember(3)]
|
||||
public string symbol_regex;
|
||||
[ProtoMember(4)]
|
||||
public string security_regex;
|
||||
[ProtoMember(5)]
|
||||
public IntMinMaxState start_net_pos;
|
||||
[ProtoMember(6)]
|
||||
public DoubleMinMaxState start_pnl;
|
||||
[ProtoMember(7)]
|
||||
public UIntMinMaxState day_long_pos;
|
||||
[ProtoMember(8)]
|
||||
public UIntMinMaxState day_short_pos;
|
||||
[ProtoMember(9)]
|
||||
public DoubleMinMaxState day_pnl;
|
||||
[ProtoMember(10)]
|
||||
public IntMinMaxState total_net_pos;
|
||||
[ProtoMember(11)]
|
||||
public DoubleMinMaxState total_pnl;
|
||||
[ProtoMember(12)]
|
||||
public DoubleMinMaxState pnl_price;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using mbts.spark.filters;
|
||||
using mbts.ui;
|
||||
using mbts.ui.shell;
|
||||
using mbts.ui.utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
@@ -13,66 +14,105 @@ using static bts.shell.PAndLWindowFactory;
|
||||
|
||||
namespace mbts.spark {
|
||||
class PAndLVM : DisposableViewModelBase {
|
||||
|
||||
|
||||
public PAndLVM(IBus bus, PAndLModel model, PAndLStartupData state) {
|
||||
|
||||
public PAndLVM(IBus bus, PAndLModel model, PAndLStartupData startup_data) {
|
||||
_bus = bus;
|
||||
_model = model;
|
||||
_selected_pnl_calc_type = backing<pnl_calc_type>(nameof(selected_pnl_calc_type));
|
||||
// get initial value from settings
|
||||
// else default to midpoint
|
||||
// TODO:
|
||||
// need to respond to settings change somehow as the
|
||||
// combobox that selected_pnl_calc_type is bound to will be
|
||||
// gone from this form.
|
||||
// ditto start of day changes, but this will be a bit more involved
|
||||
// since we'll need to create a new PnLCalculator when this changes. (infrequent, if ever)
|
||||
_selected_pnl_calc_type.Value = (startup_data != null && startup_data.calc_type != pnl_calc_type.none) ? startup_data.calc_type : pnl_calc_type.midpoint;
|
||||
_selected_pnl_calc_type.Changed += pnl_calc_type_changed;
|
||||
_start_of_day_ts = (startup_data != null && startup_data.start_of_day_time.HasValue) ? startup_data.start_of_day_time.Value : new TimeSpan(16, 30, 0);
|
||||
var pnl = model.on_pnl_data
|
||||
.Sample(_POSITION_UPDATE_PERIOD)
|
||||
.OnDispatcher()
|
||||
.Subscribe(p => {
|
||||
if (p != null) {
|
||||
PnLCalculator calc = new PnLCalculator(p.state.Values);
|
||||
PnLCalculator calc = new PnLCalculator(p.state.Values, _selected_pnl_calc_type.Value, _start_of_day_ts);
|
||||
var summary_position_rows = calc.get_summary_position();
|
||||
_pnl_data.sync_with(summary_position_rows);
|
||||
_last_pnl_update_time = DateTime.Now;
|
||||
}
|
||||
});
|
||||
pnl_view = CollectionViewSource.GetDefaultView(_pnl_data);
|
||||
pnl_view.SetSortingCriteria(
|
||||
new List<SortingCriteria>() {
|
||||
new SortingCriteria("account", true)
|
||||
, new SortingCriteria("exchange_string", true)
|
||||
, new SortingCriteria("symbol", true)
|
||||
, new SortingCriteria("security_text", true)
|
||||
}
|
||||
);
|
||||
|
||||
// restore filter state
|
||||
var fs = startup_data?.filters;
|
||||
if (startup_data != null) {
|
||||
account_filter.load_state(fs.account_regex);
|
||||
exchange_string_filter.load_state(fs.exchange_regex);
|
||||
symbol_filter.load_state(fs.symbol_regex);
|
||||
security_filter.load_state(fs.security_regex);
|
||||
start_position_filter.load_state(fs.start_net_pos);
|
||||
start_pnl_filter.load_state(fs.start_pnl);
|
||||
day_long_quantity_filter.load_state(fs.day_long_pos);
|
||||
day_short_quantity_filter.load_state(fs.day_short_pos);
|
||||
day_pnl_filter.load_state(fs.day_pnl);
|
||||
total_net_position_filter.load_state(fs.total_net_pos);
|
||||
total_pnl_filter.load_state(fs.total_pnl);
|
||||
pnl_price_filter.load_state(fs.pnl_price);
|
||||
}
|
||||
// restore sort state or supply default value
|
||||
if (startup_data?.sort_criteria?.Any() == true) {
|
||||
pnl_view.SetSortingCriteria(startup_data.sort_criteria);
|
||||
} else {
|
||||
pnl_view.SetSortingCriteria(new List<SortingCriteria>() {
|
||||
new SortingCriteria(nameof(PnLSummaryRow.account), true)
|
||||
, new SortingCriteria(nameof(PnLSummaryRow.exchange_string), true)
|
||||
, new SortingCriteria(nameof(PnLSummaryRow.symbol), true)
|
||||
, new SortingCriteria(nameof(PnLSummaryRow.security_text), true)
|
||||
});
|
||||
}
|
||||
pnl_view.CollectionChanged += Pnl_view_CollectionChanged;
|
||||
|
||||
//Account[] accounts =
|
||||
|
||||
|
||||
#region Create grid filters and load state
|
||||
#endregion
|
||||
|
||||
// setup the main Disposable and
|
||||
// hook up grid filters
|
||||
_dispose = Disposable.Create(pnl,
|
||||
DataGridFilterHelper.hook_filters(pnl_view,
|
||||
security_filter, pnl_filter,
|
||||
buy_quantity_filter, sell_quantity_filter,
|
||||
net_position_filter, weighted_average_buy_filter,
|
||||
weighted_average_sell_filter, pnl_price_filter,
|
||||
sod_position_filter, account_filter,
|
||||
exchange_string_filter, symbol_filter),
|
||||
account_filter,
|
||||
exchange_string_filter,
|
||||
symbol_filter,
|
||||
security_filter,
|
||||
start_position_filter,
|
||||
start_pnl_filter,
|
||||
day_long_quantity_filter,
|
||||
day_short_quantity_filter,
|
||||
day_pnl_filter,
|
||||
total_net_position_filter,
|
||||
total_pnl_filter,
|
||||
pnl_price_filter,
|
||||
weighted_average_buy_filter,
|
||||
weighted_average_sell_filter),
|
||||
WorkLoop.Start("PAndL", _UPDATE_PERIOD, _calculate_pnl)
|
||||
);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing) {
|
||||
pnl_view.CollectionChanged -= Pnl_view_CollectionChanged;
|
||||
}
|
||||
|
||||
|
||||
private void Pnl_view_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
|
||||
_update_totals();
|
||||
}
|
||||
|
||||
private void pnl_calc_type_changed(pnl_calc_type oldval, pnl_calc_type newval) {
|
||||
_calculate_pnl();
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing) {
|
||||
pnl_view.CollectionChanged -= Pnl_view_CollectionChanged;
|
||||
_selected_pnl_calc_type.Changed -= pnl_calc_type_changed;
|
||||
}
|
||||
|
||||
void _update_totals() {
|
||||
if (pnl_view != null) {
|
||||
total_pnl = pnl_view.Cast<PnLSummaryRow>().Where(r => !Double.IsNaN(r.pnl)).Sum(r => r.pnl);
|
||||
total_net_pos = pnl_view.Cast<PnLSummaryRow>().Sum(r => r.net_position);
|
||||
total_buy_qty = pnl_view.Cast<PnLSummaryRow>().Sum(r => r.buy_quantity);
|
||||
total_sell_qty = pnl_view.Cast<PnLSummaryRow>().Sum(r => r.sell_quantity);
|
||||
total_aggregate_pnl = pnl_view.Cast<PnLSummaryRow>().Where(r => !Double.IsNaN(r.total_pnl)).Sum(r => r.total_pnl);
|
||||
total_aggregate_net_pos = pnl_view.Cast<PnLSummaryRow>().Sum(r => r.total_net_position);
|
||||
total_aggregate_buy_qty = pnl_view.Cast<PnLSummaryRow>().Sum(r => r.day_long_quantity);
|
||||
total_aggregate_sell_qty = pnl_view.Cast<PnLSummaryRow>().Sum(r => r.day_short_quantity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,75 +121,111 @@ namespace mbts.spark {
|
||||
// position update in a while
|
||||
if (DateTime.Now.Subtract(_last_pnl_update_time) > _UPDATE_PERIOD) {
|
||||
foreach (var r in _pnl_data) {
|
||||
r.update_pnl(pnl_calc_type.waterfall);
|
||||
if (_selected_pnl_calc_type.Value != pnl_calc_type.none) {
|
||||
r.update_pnl(_selected_pnl_calc_type.Value);
|
||||
}
|
||||
}
|
||||
_update_totals();
|
||||
}
|
||||
}
|
||||
|
||||
private DateTime _last_pnl_update_time = DateTime.MinValue;
|
||||
private ObservableCollection<PnLSummaryRow> _pnl_data = new ObservableCollection<PnLSummaryRow>();
|
||||
#region Properties
|
||||
|
||||
public ICollectionView pnl_view { get; }
|
||||
|
||||
private double _total_pnl;
|
||||
public double total_pnl {
|
||||
public List<pnl_calc_type> pnl_calc_types { get; } = EnumHelper.get_values_except(pnl_calc_type.none, pnl_calc_type.waterfall, pnl_calc_type.bid_or_ask, pnl_calc_type.open, pnl_calc_type.close).ToList();
|
||||
|
||||
public pnl_calc_type selected_pnl_calc_type {
|
||||
get => _selected_pnl_calc_type.Value;
|
||||
set => _selected_pnl_calc_type.Value = value;
|
||||
}
|
||||
|
||||
public double total_aggregate_pnl {
|
||||
get {
|
||||
return _total_pnl;
|
||||
return _total_aggregate_pnl;
|
||||
}
|
||||
set {
|
||||
_total_pnl = value;
|
||||
RaisePropertyChanged(nameof(total_pnl));
|
||||
_total_aggregate_pnl = value;
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private double _total_net_pos;
|
||||
public double total_net_pos {
|
||||
get { return _total_net_pos; }
|
||||
public double total_aggregate_net_pos {
|
||||
get { return _total_aggregate_net_pos; }
|
||||
set {
|
||||
_total_net_pos = value;
|
||||
RaisePropertyChanged(nameof(total_net_pos));
|
||||
_total_aggregate_net_pos = value;
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private double _total_buy_qty;
|
||||
public double total_buy_qty {
|
||||
get { return _total_buy_qty; }
|
||||
public double total_aggregate_buy_qty {
|
||||
get { return _total_aggregate_buy_qty; }
|
||||
set {
|
||||
_total_buy_qty = value;
|
||||
RaisePropertyChanged(nameof(total_buy_qty));
|
||||
_total_aggregate_buy_qty = value;
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private double _total_sell_qty;
|
||||
public double total_sell_qty {
|
||||
get { return _total_sell_qty; }
|
||||
public double total_aggregate_sell_qty {
|
||||
get { return _total_aggregate_sell_qty; }
|
||||
set {
|
||||
_total_sell_qty = value;
|
||||
RaisePropertyChanged(nameof(total_sell_qty));
|
||||
_total_aggregate_sell_qty = value;
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public TimeSpan start_of_day {
|
||||
get { return _start_of_day_ts; }
|
||||
}
|
||||
|
||||
#region Filters
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, string> security_filter { get; } = new RegexFilterVM<PnLSummaryRow>(r => r.security_text, nameof(PnLSummaryRow.security_text));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> pnl_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.pnl, nameof(PnLSummaryRow.pnl));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> buy_quantity_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.buy_quantity, nameof(PnLSummaryRow.buy_quantity));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> sell_quantity_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.sell_quantity, nameof(PnLSummaryRow.sell_quantity));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> net_position_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.net_position, nameof(PnLSummaryRow.net_position));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> weighted_average_buy_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.weighted_average_buy, nameof(PnLSummaryRow.weighted_average_buy));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> weighted_average_sell_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.weighted_average_sell, nameof(PnLSummaryRow.weighted_average_sell));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> pnl_price_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.pnl_price, nameof(PnLSummaryRow.pnl_price));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> sod_position_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.sod_position, nameof(PnLSummaryRow.sod_position));
|
||||
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, string> account_filter { get; } = new RegexFilterVM<PnLSummaryRow>(r => r.account, nameof(PnLSummaryRow.account));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, string> exchange_string_filter { get; } = new RegexFilterVM<PnLSummaryRow>(r => r.exchange_string, nameof(PnLSummaryRow.exchange_string));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, string> symbol_filter { get; } = new RegexFilterVM<PnLSummaryRow>(r => r.symbol, nameof(PnLSummaryRow.symbol));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, string> security_filter { get; } = new RegexFilterVM<PnLSummaryRow>(r => r.security_text, nameof(PnLSummaryRow.security_text));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, IntMinMaxState> start_position_filter { get; } = new IntMinMax<PnLSummaryRow>(r => r.start_net_position, nameof(PnLSummaryRow.start_net_position));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> start_pnl_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.start_pnl, nameof(PnLSummaryRow.start_pnl));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, UIntMinMaxState> day_long_quantity_filter { get; } = new UIntMinMax<PnLSummaryRow>(r => r.day_long_quantity, nameof(PnLSummaryRow.day_long_quantity));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, UIntMinMaxState> day_short_quantity_filter { get; } = new UIntMinMax<PnLSummaryRow>(r => r.day_short_quantity, nameof(PnLSummaryRow.day_short_quantity));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> day_pnl_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.day_pnl, nameof(PnLSummaryRow.day_pnl));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, IntMinMaxState> total_net_position_filter { get; } = new IntMinMax<PnLSummaryRow>(r => r.total_net_position, nameof(PnLSummaryRow.total_net_position));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> total_pnl_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.total_pnl, nameof(PnLSummaryRow.total_pnl));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> pnl_price_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.pnl_price, nameof(PnLSummaryRow.pnl_price));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> weighted_average_buy_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.weighted_average_buy, nameof(PnLSummaryRow.weighted_average_buy));
|
||||
public IDataGridColumnPersistentFilter<PnLSummaryRow, DoubleMinMaxState> weighted_average_sell_filter { get; } = new DoubleMinMax<PnLSummaryRow>(r => r.weighted_average_sell, nameof(PnLSummaryRow.weighted_average_sell));
|
||||
|
||||
public FilterStates get_filters_states() => new FilterStates {
|
||||
account_regex = account_filter.get_state(),
|
||||
exchange_regex = exchange_string_filter.get_state(),
|
||||
symbol_regex = exchange_string_filter.get_state(),
|
||||
security_regex = security_filter.get_state(),
|
||||
start_net_pos = start_position_filter.get_state(),
|
||||
start_pnl = start_pnl_filter.get_state(),
|
||||
day_long_pos = day_long_quantity_filter.get_state(),
|
||||
day_short_pos = day_short_quantity_filter.get_state(),
|
||||
day_pnl = day_pnl_filter.get_state(),
|
||||
total_net_pos = total_net_position_filter.get_state(),
|
||||
total_pnl = total_pnl_filter.get_state(),
|
||||
pnl_price = pnl_price_filter.get_state()
|
||||
};
|
||||
|
||||
#endregion
|
||||
|
||||
public List<SortingCriteria> get_sorting_states() => pnl_view.GetSortingCriteria();
|
||||
|
||||
#endregion
|
||||
|
||||
readonly IBus _bus;
|
||||
readonly PAndLModel _model;
|
||||
static readonly TimeSpan _UPDATE_PERIOD = TimeSpan.FromSeconds(1);
|
||||
static readonly TimeSpan _POSITION_UPDATE_PERIOD = TimeSpan.FromMilliseconds(333);
|
||||
readonly Property<pnl_calc_type> _selected_pnl_calc_type;
|
||||
|
||||
double _total_aggregate_pnl, _total_aggregate_net_pos, _total_aggregate_buy_qty, _total_aggregate_sell_qty;
|
||||
TimeSpan _start_of_day_ts;
|
||||
DateTime _last_pnl_update_time = DateTime.MinValue;
|
||||
ObservableCollection<PnLSummaryRow> _pnl_data = new ObservableCollection<PnLSummaryRow>();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -25,13 +25,19 @@
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<!-- Table -->
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition />
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
<c:DataGridEx Grid.Row="0" Grid.Column="0" x:Name="_grid" IsReadOnly="True" BorderThickness="0" GridLinesVisibility="None" CanUserSortColumns="True" SelectionUnit="FullRow"
|
||||
<ComboBox Grid.Row="0"
|
||||
ItemsSource="{Binding pnl_calc_types}"
|
||||
SelectedItem="{Binding selected_pnl_calc_type}"
|
||||
ItemTemplate="{StaticResource EnumDescriptionTemplate}">
|
||||
</ComboBox>
|
||||
<c:DataGridEx Grid.Row="1" Grid.Column="0" x:Name="_grid" IsReadOnly="True" BorderThickness="0" GridLinesVisibility="None" CanUserSortColumns="True" SelectionUnit="FullRow"
|
||||
ItemsSource="{Binding pnl_view}" uc:DataGridBehavior.NotifyColumnsOfSorting="True">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn x:Name="account" Header="Account" Binding="{Binding account}" IsReadOnly="true"
|
||||
@@ -58,49 +64,85 @@
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.security_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource RegexTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn x:Name="sod" Header="SOD" Binding="{Binding sod_position}" IsReadOnly="true"
|
||||
<DataGridTextColumn x:Name="start_net_pos" Header="Start Net Pos" Binding="{Binding start_net_position}" IsReadOnly="true"
|
||||
ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.sod_position_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.sod_position_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.sod_position_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.start_position_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.start_position_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.start_position_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn x:Name="buy_qty" Header="Buy Qty" Binding="{Binding buy_quantity}" IsReadOnly="true"
|
||||
ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.buy_quantity_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.buy_quantity_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.buy_quantity_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn x:Name="sell_qty" Header="Sell Qty" Binding="{Binding sell_quantity}" IsReadOnly="true"
|
||||
ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.sell_quantity_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.sell_quantity_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.sell_quantity_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn x:Name="net_pos" Header="Net Pos" Binding="{Binding net_position}" IsReadOnly="true"
|
||||
ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.net_position_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.net_position_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.net_position_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn x:Name="pnl" Header="PnL" IsReadOnly="true" SortMemberPath="pnl"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.pnl_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.pnl_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.pnl_filter.open, Source={uc:DirectReference _grid}}"
|
||||
<DataGridTextColumn x:Name="start_pnl" Header="Start PnL" IsReadOnly="true" SortMemberPath="start_pnl"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.start_pnl_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.start_pnl_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.start_pnl_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}">
|
||||
<DataGridTextColumn.Binding>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}">
|
||||
<Binding Path="pnl"/>
|
||||
<Binding Path="start_pnl"/>
|
||||
<Binding Path="security"/>
|
||||
</MultiBinding>
|
||||
</DataGridTextColumn.Binding>
|
||||
<DataGridTextColumn.ElementStyle>
|
||||
<Style TargetType="{x:Type TextBlock}">
|
||||
<Setter Property="TextAlignment" Value="Right" />
|
||||
<Setter Property="Background" Value="{Binding pnl, Converter={StaticResource PnLBackgroundColorConverter}}" />
|
||||
<Setter Property="Background" Value="{Binding start_pnl, Converter={StaticResource PnLBackgroundColorConverter}}" />
|
||||
</Style>
|
||||
</DataGridTextColumn.ElementStyle>
|
||||
</DataGridTextColumn>
|
||||
<DataGridTextColumn x:Name="day_long" Header="Day Long" Binding="{Binding day_long_quantity}" IsReadOnly="true"
|
||||
ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.day_long_quantity_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.day_long_quantity_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.day_long_quantity_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn x:Name="day_short" Header="Day Short" Binding="{Binding day_short_quantity}" IsReadOnly="true"
|
||||
ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.day_short_quantity_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.day_short_quantity_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.day_short_quantity_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn x:Name="day_pnl" Header="Day PnL" IsReadOnly="true" SortMemberPath="day_pnl"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.day_pnl_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.day_pnl_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.day_pnl_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}">
|
||||
<DataGridTextColumn.Binding>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}">
|
||||
<Binding Path="day_pnl"/>
|
||||
<Binding Path="security"/>
|
||||
</MultiBinding>
|
||||
</DataGridTextColumn.Binding>
|
||||
<DataGridTextColumn.ElementStyle>
|
||||
<Style TargetType="{x:Type TextBlock}">
|
||||
<Setter Property="TextAlignment" Value="Right" />
|
||||
<Setter Property="Background" Value="{Binding day_pnl, Converter={StaticResource PnLBackgroundColorConverter}}" />
|
||||
</Style>
|
||||
</DataGridTextColumn.ElementStyle>
|
||||
</DataGridTextColumn>
|
||||
<DataGridTextColumn x:Name="total_net_pos" Header="Total Net Pos" Binding="{Binding total_net_position}" IsReadOnly="true"
|
||||
ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.total_net_position_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.total_net_position_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.total_net_position_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn x:Name="total_pnl" Header="Total PnL" IsReadOnly="true" SortMemberPath="total_pnl"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.total_pnl_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.total_pnl_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.total_pnl_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}">
|
||||
<DataGridTextColumn.Binding>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}">
|
||||
<Binding Path="total_pnl"/>
|
||||
<Binding Path="security"/>
|
||||
</MultiBinding>
|
||||
</DataGridTextColumn.Binding>
|
||||
<DataGridTextColumn.ElementStyle>
|
||||
<Style TargetType="{x:Type TextBlock}">
|
||||
<Setter Property="TextAlignment" Value="Right" />
|
||||
<Setter Property="Background" Value="{Binding total_pnl, Converter={StaticResource PnLBackgroundColorConverter}}" />
|
||||
</Style>
|
||||
</DataGridTextColumn.ElementStyle>
|
||||
</DataGridTextColumn>
|
||||
@@ -117,20 +159,13 @@
|
||||
</MultiBinding>
|
||||
</DataGridTextColumn.Binding>
|
||||
</DataGridTextColumn>
|
||||
<!-- we are hiding these until we have toggle on/off via settings
|
||||
<DataGridTextColumn x:Name="avg_buy" Header="AvgBuy" Binding="{Binding weighted_average_buy, Converter={StaticResource DoubleWithNaNHiddenConverter}}" IsReadOnly="true" SortMemberPath="weighted_average_buy"
|
||||
ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.weighted_average_buy_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.weighted_average_buy_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.weighted_average_buy_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}">
|
||||
<!--
|
||||
<DataGridTextColumn.Binding>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}">
|
||||
<Binding Path="weighted_average_buy"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</DataGridTextColumn.Binding>
|
||||
-->
|
||||
</DataGridTextColumn>
|
||||
<DataGridTextColumn x:Name="avg_sell" Header="AvgSell" Binding="{Binding weighted_average_sell, Converter={StaticResource DoubleWithNaNHiddenConverter}}" IsReadOnly="true" SortMemberPath="weighted_average_sell"
|
||||
ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
@@ -138,26 +173,19 @@
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.weighted_average_sell_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.weighted_average_sell_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}">
|
||||
<!--
|
||||
<DataGridTextColumn.Binding>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}">
|
||||
<Binding Path="weighted_average_sell"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</DataGridTextColumn.Binding>
|
||||
-->
|
||||
</DataGridTextColumn>
|
||||
-->
|
||||
</DataGrid.Columns>
|
||||
</c:DataGridEx>
|
||||
<StatusBar Grid.Row="1" Grid.Column="0" DockPanel.Dock="Bottom">
|
||||
<StatusBar Grid.Row="2" Grid.Column="0" DockPanel.Dock="Bottom">
|
||||
<StatusBarItem DockPanel.Dock="Right">
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Column="0" Text="Total PnL: "/>
|
||||
<TextBlock Grid.Column="1" Text="{Binding total_pnl, Converter={StaticResource DoubleWithNaNHiddenConverter}}" />
|
||||
<TextBlock Grid.Column="0" Text="Aggregate PnL: "/>
|
||||
<TextBlock Grid.Column="1" Text="{Binding total_aggregate_pnl, Converter={StaticResource DoubleWithNaNHiddenConverter}}" />
|
||||
</Grid>
|
||||
</StatusBarItem>
|
||||
<StatusBarItem DockPanel.Dock="Right">
|
||||
@@ -169,8 +197,8 @@
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Column="0" Text="Total Net Pos: "/>
|
||||
<TextBlock Grid.Column="1" Text="{Binding total_net_pos}"/>
|
||||
<TextBlock Grid.Column="0" Text="Aggregate Net Pos: "/>
|
||||
<TextBlock Grid.Column="1" Text="{Binding total_aggregate_net_pos}"/>
|
||||
</Grid>
|
||||
</StatusBarItem>
|
||||
<StatusBarItem DockPanel.Dock="Right">
|
||||
@@ -182,8 +210,8 @@
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Column="0" Text="Total Sell Qty: "/>
|
||||
<TextBlock Grid.Column="1" Text="{Binding total_sell_qty}"/>
|
||||
<TextBlock Grid.Column="0" Text="Aggregate Sell Qty: "/>
|
||||
<TextBlock Grid.Column="1" Text="{Binding total_aggregate_sell_qty}"/>
|
||||
</Grid>
|
||||
</StatusBarItem>
|
||||
<StatusBarItem DockPanel.Dock="Right">
|
||||
@@ -195,8 +223,8 @@
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Column="0" Text="Total Buy Qty: "/>
|
||||
<TextBlock Grid.Column="1" Text="{Binding total_buy_qty}"/>
|
||||
<TextBlock Grid.Column="0" Text="Aggregate Buy Qty: "/>
|
||||
<TextBlock Grid.Column="1" Text="{Binding total_aggregate_buy_qty}"/>
|
||||
</Grid>
|
||||
</StatusBarItem>
|
||||
<StatusBarItem />
|
||||
|
||||
@@ -14,26 +14,31 @@ namespace mbts.spark {
|
||||
internal class PAndLWindow : SparkWindow<PAndLWindowFactory.PAndLStartupData>, ISharedWindow {
|
||||
|
||||
const string _window_type = "PAndL", PNL_THREAD_ID = "PnL";
|
||||
PAndLWindowFactory.PAndLStartupData _state;
|
||||
PAndLWindowFactory.PAndLStartupData _startup_data;
|
||||
PAndLModel _model;
|
||||
PAndLView _view;
|
||||
PAndLVM _vm;
|
||||
PAndLWindowFactory _factory;
|
||||
public PAndLWindow(PAndLModel model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData state, ImageSource Icon ) : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) {
|
||||
public PAndLWindow(PAndLModel model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData startup_data, ImageSource Icon ) : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) {
|
||||
_model = model;
|
||||
_factory = factory;
|
||||
_state = state;
|
||||
_startup_data = startup_data;
|
||||
}
|
||||
|
||||
public override string window_type => _window_type;
|
||||
public override PAndLWindowFactory.PAndLStartupData SaveState => _state =
|
||||
new PAndLWindowFactory.PAndLStartupData();
|
||||
public override PAndLWindowFactory.PAndLStartupData SaveState => _startup_data =
|
||||
new PAndLWindowFactory.PAndLStartupData(
|
||||
_vm.get_sorting_states(),
|
||||
_vm.get_filters_states(),
|
||||
_vm.selected_pnl_calc_type,
|
||||
_vm.start_of_day
|
||||
);
|
||||
public override UIElement Content => _view;
|
||||
public override double MinWidth => 894;
|
||||
public string thread_id => PNL_THREAD_ID;
|
||||
|
||||
public void thread_init() {
|
||||
_vm = new PAndLVM(_bus, _model, _state);
|
||||
_vm = new PAndLVM(_bus, _model, _startup_data);
|
||||
_view = new PAndLView(_vm);
|
||||
_view.DataContext = _vm;
|
||||
}
|
||||
|
||||
@@ -77,20 +77,27 @@ namespace mbts.spark {
|
||||
|
||||
class PnLCalculator {
|
||||
readonly IEnumerable<PnLData> _data;
|
||||
readonly pnl_calc_type _calc_type;
|
||||
readonly DateTime _start_of_trading_day_utc;
|
||||
|
||||
public PnLCalculator(IEnumerable<PnLData> data) {
|
||||
public PnLCalculator(IEnumerable<PnLData> data, pnl_calc_type calc_type, TimeSpan start_of_day_ts) {
|
||||
_data = data;
|
||||
_calc_type = calc_type;
|
||||
// Note that this isn't the acutal start of the trading day
|
||||
// but it's close enough for pnl purposes.
|
||||
// In reality, instruments have different schedules
|
||||
_start_of_trading_day_utc = DateTime.Now.Date.Add(start_of_day_ts).AddDays(-1).ToUniversalTime();
|
||||
}
|
||||
|
||||
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
|
||||
let start_of_day = _get_start_of_day(grouping.Key.symbol)
|
||||
select new PnLSummaryRow(grouping.Key,
|
||||
grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => y.buy_quantity),
|
||||
grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => y.sell_quantity),
|
||||
grouping.Sum(y => y.buy_quantity - y.sell_quantity),
|
||||
grouping.Where(y => y.fill.Timestamp.ToDateTime() < start_of_day).Sum(y => y.buy_quantity - y.sell_quantity),
|
||||
/* let start_of_day = _get_start_of_day(grouping.Key.symbol) */
|
||||
select new PnLSummaryRow(_calc_type, _start_of_trading_day_utc, grouping.Key,
|
||||
grouping.Where(y => y.fill.Timestamp.ToDateTime() >= _start_of_trading_day_utc).Sum(y => (uint)y.buy_quantity),
|
||||
grouping.Where(y => y.fill.Timestamp.ToDateTime() >= _start_of_trading_day_utc).Sum(y => (uint)y.sell_quantity),
|
||||
grouping.Sum(y => (int)(y.buy_quantity - y.sell_quantity)),
|
||||
grouping.Where(y => y.fill.Timestamp.ToDateTime() < _start_of_trading_day_utc).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)
|
||||
@@ -98,21 +105,23 @@ namespace mbts.spark {
|
||||
);
|
||||
}
|
||||
|
||||
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);
|
||||
public static bool get_driver(ref ulong bbo_time, ref ulong last_trade_time, SecurityMD sec_md, pnl_calc_type pnl_request, ref double driver_price) {
|
||||
// check with eric as I'm not sure if i was every querying md correctly
|
||||
if (!sec_md.md.get_bbo(ref bbo_time, out NativePriceSize bid, out NativePriceSize ask)) return false;
|
||||
if (!sec_md.md.get_last_trade(ref last_trade_time, out NativePriceSize last, out DateTime unused1, out ulong unused2)) 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) {
|
||||
return last.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)) {
|
||||
return mid;
|
||||
driver_price = mid;
|
||||
return true;
|
||||
}
|
||||
|
||||
// last bid
|
||||
@@ -120,9 +129,11 @@ namespace mbts.spark {
|
||||
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;
|
||||
driver_price = last.price;
|
||||
return true;
|
||||
} else {
|
||||
return bid.price;
|
||||
driver_price = bid.price;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,15 +142,18 @@ namespace mbts.spark {
|
||||
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;
|
||||
driver_price = last.price;
|
||||
return true;
|
||||
} else {
|
||||
return ask.price;
|
||||
driver_price = ask.price;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// last only
|
||||
if (!Double.IsNaN(last.price)) {
|
||||
return last.price;
|
||||
driver_price = last.price;
|
||||
return true;
|
||||
}
|
||||
|
||||
// indicative, no support yet
|
||||
@@ -148,36 +162,43 @@ namespace mbts.spark {
|
||||
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;
|
||||
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) {
|
||||
return ask.price;
|
||||
driver_price = ask.price;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Settlement
|
||||
// neither bid or ask are available
|
||||
if (!Double.IsNaN(sec_md.md.settlement_price)) {
|
||||
return sec_md.md.settlement_price;
|
||||
driver_price = sec_md.md.settlement_price;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Close
|
||||
return sec_md.md.close_price;
|
||||
driver_price = sec_md.md.close_price;
|
||||
return true;
|
||||
|
||||
} // 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;
|
||||
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 = TickUtils.mid_tick(sec_md.security.ContractSpec, bid.price, ask.price); 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) { 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); }
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -197,10 +218,6 @@ namespace mbts.spark {
|
||||
return (price - driver) * quantity * cpp;
|
||||
}
|
||||
|
||||
private static DateTime _get_start_of_day(string symbool) {
|
||||
// I rekcon this will change according to the symbol's session
|
||||
return DateTime.Today;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,18 +1,26 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace mbts.spark {
|
||||
|
||||
[Flags]
|
||||
public enum pnl_calc_type {
|
||||
none = 0,
|
||||
[Description("Last Tradeable Price Avalanche")]
|
||||
waterfall = 1,
|
||||
[Description("Last")]
|
||||
last = 2,
|
||||
bid_and_ask = 4,
|
||||
mid = 8,
|
||||
[Description("Bid/Ask")]
|
||||
bid_or_ask = 4,
|
||||
[Description("Midpoint")]
|
||||
midpoint = 8,
|
||||
[Description("Open")]
|
||||
open = 16,
|
||||
[Description("Close")]
|
||||
close = 32,
|
||||
[Description("Settle")]
|
||||
settle = 64
|
||||
}
|
||||
|
||||
|
||||
@@ -12,70 +12,106 @@ namespace mbts.spark {
|
||||
|
||||
|
||||
internal class PnLSummaryRow : ViewModelBase, IEquatable<PnLSummaryRow> {
|
||||
DateTime _start_of_day;
|
||||
(string account, Venue? exchange, string symbol, SecurityImmutable? security, SecurityMD? sec_md) key;
|
||||
double _buy_quantity, _sell_quantity, _net_position, _sod_position, _pnl, _pnl_price, _weighted_average_buy, _weighted_average_sell;
|
||||
int _start_net_position, _total_net_position;
|
||||
uint _day_long_quantity, _day_short_quantity;
|
||||
double _total_pnl, _sod_pnl, _start_pnl, _pnl_price, _weighted_average_buy, _weighted_average_sell;
|
||||
IEnumerable<FillImmutable> _fills;
|
||||
//List<MarketSchedule> _schedules = new List<MarketSchedule>() { MarketSchedule.es_schedule };
|
||||
ulong _bbo_ref, _last_ref;
|
||||
double _driver;
|
||||
static readonly Dictionary<string, double> cpp_dict = config.Configuration.get<bts.config.CppConfig>().cpp_entry;
|
||||
|
||||
|
||||
|
||||
public PnLSummaryRow((string, Venue?, string, SecurityImmutable?, SecurityMD?) key,
|
||||
double buy_quant, double sell_quant, double net_pos, double sod_pos, double wavg_buy, double wavg_sell, IEnumerable<FillImmutable> fills) {
|
||||
public PnLSummaryRow(pnl_calc_type calc_type,
|
||||
DateTime start_of_day,
|
||||
(string, Venue?, string, SecurityImmutable?, SecurityMD?) key,
|
||||
uint day_buy_quant, uint day_sell_quant, int net_pos, int sod_pos, double wavg_buy, double wavg_sell, IEnumerable<FillImmutable> fills) {
|
||||
this._start_of_day = start_of_day;
|
||||
this.key = key;
|
||||
buy_quantity = buy_quant;
|
||||
sell_quantity = sell_quant;
|
||||
net_position = net_pos;
|
||||
sod_position = sod_pos;
|
||||
day_long_quantity = day_buy_quant;
|
||||
day_short_quantity = day_sell_quant;
|
||||
total_net_position = net_pos;
|
||||
start_net_position = sod_pos;
|
||||
weighted_average_buy = wavg_buy;
|
||||
weighted_average_sell = wavg_sell;
|
||||
_fills = fills;
|
||||
|
||||
// set the initial pnl and pnl_price when there has been a
|
||||
// position update i.e. this constructor
|
||||
update_pnl(pnl_calc_type.waterfall);
|
||||
update_pnl(calc_type);
|
||||
}
|
||||
|
||||
private double _get_cpp_for_security(SecurityImmutable s) {
|
||||
if (s.security_type() == SecurityType.Spread) {
|
||||
var distinct_cpps = s.Spread.Legs
|
||||
.Select(l => l.Security.ContractSpec.CashPerPoint)
|
||||
.Select(l => get_cpp_for_symbol(l.Security))
|
||||
.Distinct();
|
||||
bool all_legs_have_same_cpp = distinct_cpps.Count() < 2;
|
||||
if (all_legs_have_same_cpp) {
|
||||
return s.Spread.Legs.First().Security.ContractSpec.CashPerPoint;
|
||||
return distinct_cpps.First();
|
||||
} else {
|
||||
Logger.error(bts.log.Subsystem.Ui,
|
||||
Logger.error(bts.log.Subsystem.Ui,
|
||||
$"Security {s} contains legs with the following cash multipliers: { string.Join(",", distinct_cpps.Select(d => d.ToString())) } ");
|
||||
return Double.NaN;
|
||||
}
|
||||
} else {
|
||||
return get_cpp_for_symbol(s);
|
||||
}
|
||||
|
||||
// local function for looking up cpp for a single SecurityImmutable
|
||||
double get_cpp_for_symbol(SecurityImmutable si) {
|
||||
if (cpp_dict.TryGetValue(si.Symbol, out double cpp_out)) {
|
||||
return cpp_out;
|
||||
} else {
|
||||
Logger.error(bts.log.Subsystem.Ui, $"No cash multiplier found for Security {si}, Symbol {si.Symbol}");
|
||||
}
|
||||
return double.NaN;
|
||||
}
|
||||
return s.ContractSpec.CashPerPoint;
|
||||
}
|
||||
|
||||
|
||||
public void update_pnl(pnl_calc_type pnl_request) {
|
||||
if (key.sec_md.HasValue) {
|
||||
double cpp = _get_cpp_for_security(key.security.Value);
|
||||
if (pnl_request.HasFlag(pnl_calc_type.bid_and_ask)) {
|
||||
ulong unused = 0;
|
||||
key.sec_md.Value.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask);
|
||||
if (pnl_request.HasFlag(pnl_calc_type.bid_or_ask)) {
|
||||
key.sec_md.Value.md.get_bbo(ref _bbo_ref, out NativePriceSize bid, out NativePriceSize ask);
|
||||
|
||||
double summary_pnl = 0.0;
|
||||
double start_pnl_total = 0.0;
|
||||
double day_pnl_total = 0.0;
|
||||
foreach (var fill in _fills) {
|
||||
double _tick_pnl = PnLCalculator.get_pnl(bid.price, ask.price, fill, cpp);
|
||||
summary_pnl += _tick_pnl;
|
||||
if (fill.Timestamp.ToDateTime() >= _start_of_day) {
|
||||
day_pnl_total += _tick_pnl;
|
||||
} else {
|
||||
start_pnl_total += _tick_pnl;
|
||||
}
|
||||
}
|
||||
pnl = summary_pnl;
|
||||
total_pnl = summary_pnl;
|
||||
day_pnl = day_pnl_total;
|
||||
start_pnl = start_pnl_total;
|
||||
pnl_price = Double.NaN;
|
||||
} else {
|
||||
double driver = PnLCalculator.get_driver(key.sec_md.Value, pnl_request);
|
||||
|
||||
double summary_pnl = 0.0;
|
||||
foreach (var fill in _fills) {
|
||||
double _tick_pnl = PnLCalculator.get_pnl(driver, fill, cpp);
|
||||
summary_pnl += _tick_pnl;
|
||||
if (PnLCalculator.get_driver(ref _bbo_ref, ref _last_ref, key.sec_md.Value, pnl_request, ref _driver)) {
|
||||
double summary_pnl = 0.0;
|
||||
double start_pnl_total = 0.0;
|
||||
double day_pnl_total = 0.0;
|
||||
foreach (var fill in _fills) {
|
||||
double _tick_pnl = PnLCalculator.get_pnl(_driver, fill, cpp);
|
||||
summary_pnl += _tick_pnl;
|
||||
if (fill.Timestamp.ToDateTime() >= _start_of_day) {
|
||||
day_pnl_total += _tick_pnl;
|
||||
} else {
|
||||
start_pnl_total += _tick_pnl;
|
||||
}
|
||||
}
|
||||
total_pnl = summary_pnl;
|
||||
day_pnl = day_pnl_total;
|
||||
start_pnl = start_pnl_total;
|
||||
pnl_price = _driver;
|
||||
}
|
||||
pnl = summary_pnl;
|
||||
pnl_price = driver;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,9 +125,9 @@ namespace mbts.spark {
|
||||
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;
|
||||
day_long_quantity == other.day_long_quantity &&
|
||||
day_short_quantity == other.day_short_quantity &&
|
||||
total_pnl == other.total_pnl;
|
||||
}
|
||||
|
||||
public override int GetHashCode() {
|
||||
@@ -104,7 +140,7 @@ namespace mbts.spark {
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
return $"account: {account} exchange: {exchange_string} symbol: {symbol} security: {security_text} buy_quantity: {buy_quantity} sell_quantity: {sell_quantity} pnl: {pnl}";
|
||||
return $"account: {account} exchange: {exchange_string} symbol: {symbol} security: {security_text} buy_quantity: {day_long_quantity} sell_quantity: {day_short_quantity} pnl: {total_pnl}";
|
||||
}
|
||||
|
||||
public string account {
|
||||
@@ -140,56 +176,77 @@ namespace mbts.spark {
|
||||
set => ignore();
|
||||
}
|
||||
|
||||
|
||||
public double buy_quantity {
|
||||
get => _buy_quantity;
|
||||
|
||||
public int start_net_position {
|
||||
get => _start_net_position;
|
||||
set {
|
||||
if (_buy_quantity != value) {
|
||||
_buy_quantity = value;
|
||||
RaisePropertyChanged(nameof(buy_quantity));
|
||||
if (_start_net_position != value) {
|
||||
_start_net_position = value;
|
||||
RaisePropertyChanged(nameof(start_net_position));
|
||||
}
|
||||
}
|
||||
}
|
||||
public double start_pnl {
|
||||
get => _start_pnl;
|
||||
set {
|
||||
if (_start_pnl != value) {
|
||||
_start_pnl = value;
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
public uint day_long_quantity {
|
||||
get => _day_long_quantity;
|
||||
set {
|
||||
if (_day_long_quantity != value) {
|
||||
_day_long_quantity = value;
|
||||
RaisePropertyChanged(nameof(day_long_quantity));
|
||||
}
|
||||
}
|
||||
}
|
||||
public uint day_short_quantity {
|
||||
get => _day_short_quantity;
|
||||
set {
|
||||
if (_day_short_quantity != value) {
|
||||
_day_short_quantity = value;
|
||||
RaisePropertyChanged(nameof(day_short_quantity));
|
||||
}
|
||||
}
|
||||
}
|
||||
public double day_pnl {
|
||||
get => _sod_pnl;
|
||||
set {
|
||||
if (_sod_pnl != value) {
|
||||
_sod_pnl = value;
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public double sell_quantity {
|
||||
get => _sell_quantity;
|
||||
|
||||
|
||||
public int total_net_position {
|
||||
get => _total_net_position;
|
||||
set {
|
||||
if (_sell_quantity != value) {
|
||||
_sell_quantity = value;
|
||||
RaisePropertyChanged(nameof(sell_quantity));
|
||||
if (_total_net_position != value) {
|
||||
_total_net_position = value;
|
||||
RaisePropertyChanged(nameof(total_net_position));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public double sod_position {
|
||||
get => _sod_position;
|
||||
public double total_pnl {
|
||||
get => _total_pnl;
|
||||
set {
|
||||
if (_sod_position != value) {
|
||||
_sod_position = value;
|
||||
RaisePropertyChanged(nameof(sod_position));
|
||||
if (_total_pnl != value) {
|
||||
_total_pnl = value;
|
||||
RaisePropertyChanged(nameof(total_pnl));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public double net_position {
|
||||
get => _net_position;
|
||||
set {
|
||||
if (_net_position != value) {
|
||||
_net_position = value;
|
||||
RaisePropertyChanged(nameof(net_position));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public double pnl {
|
||||
get => _pnl;
|
||||
set {
|
||||
if (_pnl != value) {
|
||||
_pnl = value;
|
||||
RaisePropertyChanged(nameof(pnl));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public double pnl_price {
|
||||
get => _pnl_price;
|
||||
@@ -221,6 +278,9 @@ namespace mbts.spark {
|
||||
}
|
||||
}
|
||||
|
||||
//public IEnumerable<FillImmutable> sod_fills => _fills.Where(f => f.Timestamp.ToDateTime() >= _start_of_day);
|
||||
//public IEnumerable<FillImmutable> pos_fills => _fills.Where(f => f.Timestamp.ToDateTime() < _start_of_day);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user