add start of day to pnl settings

This commit is contained in:
Jake Norbut committed 2018-07-11 12:55:15 -05:00
1 parent 54676d5aa9
commit a233451333
4 files changed
+20 -16

No files matched your search

@@ -39,13 +39,11 @@ namespace bts.shell {
/// </summary>
public PAndLStartupData() { }
public PAndLStartupData(List<SortingCriteria> sorting_criteria, FilterStates filters) :
this(sorting_criteria, filters, pnl_calc_type.midpoint) { }
public PAndLStartupData(List<SortingCriteria> sorting_criteria, FilterStates filters, pnl_calc_type calc_type) {
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)]
@@ -54,6 +52,8 @@ namespace bts.shell {
public FilterStates filters;
[ProtoMember(15)]
public pnl_calc_type calc_type;
[ProtoMember(20)]
public TimeSpan? start_of_day_time;
}
+3 -1
View File
@@ -25,14 +25,16 @@ namespace mbts.spark {
// 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
_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;
var start_of_day_ts = (startup_data != null && startup_data.start_of_day_time.HasValue) ? startup_data.start_of_day_time.Value : new TimeSpan(17, 0, 0);
var pnl = model.on_pnl_data
.Sample(_POSITION_UPDATE_PERIOD)
.OnDispatcher()
.Subscribe(p => {
if (p != null) {
PnLCalculator calc = new PnLCalculator(p.state.Values, _selected_pnl_calc_type.Value);
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;
@@ -30,7 +30,8 @@ namespace mbts.spark {
new PAndLWindowFactory.PAndLStartupData(
_vm.get_sorting_states(),
_vm.get_filters_states(),
_vm.selected_pnl_calc_type
_vm.selected_pnl_calc_type,
new TimeSpan(17,0,0)
);
public override UIElement Content => _view;
public override double MinWidth => 894;
@@ -78,21 +78,26 @@ namespace mbts.spark {
class PnLCalculator {
readonly IEnumerable<PnLData> _data;
readonly pnl_calc_type _calc_type;
readonly DateTime _start_of_trading_day;
public PnLCalculator(IEnumerable<PnLData> data, pnl_calc_type calc_type) {
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 = DateTime.Now.Date.Add(start_of_day_ts).AddDays(-1);
}
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(_calc_type, start_of_day, grouping.Key,
grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => (uint)y.buy_quantity),
grouping.Where(y => y.fill.Timestamp.ToDateTime() >= start_of_day).Sum(y => (uint)y.sell_quantity),
/* let start_of_day = _get_start_of_day(grouping.Key.symbol) */
select new PnLSummaryRow(_calc_type, _start_of_trading_day, grouping.Key,
grouping.Where(y => y.fill.Timestamp.ToDateTime() < _start_of_trading_day).Sum(y => (uint)y.buy_quantity),
grouping.Where(y => y.fill.Timestamp.ToDateTime() < _start_of_trading_day).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_day).Sum(y => (int)(y.buy_quantity - y.sell_quantity)),
grouping.Where(y => y.fill.Timestamp.ToDateTime() >= _start_of_trading_day).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)
@@ -213,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;
}
}
}