fixing no UTC to UTC time comparison bug; also, somehow swapped day vs start of day comparison

This commit is contained in:
Jake Norbut committed 2018-07-12 12:00:46 -05:00
1 parent b5950ab8c0
commit 77ad9ca011
2 files changed
+9 -9

No files matched your search

@@ -78,7 +78,7 @@ namespace mbts.spark {
class PnLCalculator {
readonly IEnumerable<PnLData> _data;
readonly pnl_calc_type _calc_type;
readonly DateTime _start_of_trading_day;
readonly DateTime _start_of_trading_day_utc;
public PnLCalculator(IEnumerable<PnLData> data, pnl_calc_type calc_type, TimeSpan start_of_day_ts) {
_data = data;
@@ -86,18 +86,18 @@ namespace mbts.spark {
// 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);
_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(_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),
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).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)
@@ -27,11 +27,11 @@ namespace mbts.spark {
public PnLSummaryRow(pnl_calc_type calc_type,
DateTime start_of_day,
(string, Venue?, string, SecurityImmutable?, SecurityMD?) key,
uint buy_quant, uint sell_quant, int net_pos, int sod_pos, double wavg_buy, double wavg_sell, IEnumerable<FillImmutable> fills) {
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;
day_long_quantity = buy_quant;
day_short_quantity = sell_quant;
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;