From a9149dfa87eed40f90111bf35b935c8bd76d2d12 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 7 Aug 2022 14:20:09 -0400 Subject: [PATCH] refactor: Clean up PNLNormalizedData * Use new lang feats: nullable, and 'init' prop setters * Group 'sod', 'day', and 'working' members in respective groups * Run formatter --- .../Trading/PNL/Data/PNLNormalizedData.cs | 193 ++++++++---------- .../spark/Modules/Trading/PNL/VM/PNLRowVM.cs | 59 +++--- 2 files changed, 120 insertions(+), 132 deletions(-) diff --git a/src/client/spark/Modules/Trading/PNL/Data/PNLNormalizedData.cs b/src/client/spark/Modules/Trading/PNL/Data/PNLNormalizedData.cs index 968882874..988005a67 100644 --- a/src/client/spark/Modules/Trading/PNL/Data/PNLNormalizedData.cs +++ b/src/client/spark/Modules/Trading/PNL/Data/PNLNormalizedData.cs @@ -1,63 +1,49 @@ -using bts.utils; - -using System; - +#nullable enable namespace bts.shell.pnl { + using bts.utils; + + using System; + readonly struct PNLNormalizedData : IEquatable { public static readonly PNLNormalizedData zero = - new PNLNormalizedData(0, 0, 0, 0, 0, 0, 0, 0); + new() { + // sod + sod_pnl = 0, + sod_long_pos = 0, + sod_short_pos = 0, + display_sod_long_pos = 0, + display_sod_short_pos = 0, - public PNLNormalizedData( - decimal day_pnl, - decimal day_long_pos, - decimal day_short_pos, - decimal sod_pnl, - decimal sod_long_pos, - decimal sod_short_pos, - decimal working_long_pos = 0, - decimal working_short_pos = 0, - decimal? display_day_long_pos = null, - decimal? display_day_short_pos = null, - decimal? display_sod_long_pos = null, - decimal? display_sod_short_pos = null, - decimal? display_working_long_pos = null, - decimal? display_working_short_pos = null) { - // - this.day_pnl = day_pnl; - this.day_long_pos = day_long_pos; - this.day_short_pos = day_short_pos; - this.sod_pnl = sod_pnl; - this.sod_long_pos = sod_long_pos; - this.sod_short_pos = sod_short_pos; - this.working_long_pos = working_long_pos; - this.working_short_pos = working_short_pos; - // display - this.display_day_long_pos = display_day_long_pos ?? day_long_pos; - this.display_day_short_pos = display_day_short_pos ?? day_short_pos; - this.display_sod_long_pos = display_sod_long_pos ?? sod_long_pos; - this.display_sod_short_pos = display_sod_short_pos ?? sod_short_pos; - this.display_working_long_pos = display_working_long_pos ?? working_long_pos; - this.display_working_short_pos = display_working_short_pos ?? working_short_pos; - } + // day + day_pnl = 0, + day_long_pos = 0, + day_short_pos = 0, + display_day_long_pos = 0, + display_day_short_pos = 0, + + // working + working_long_pos = 0, + working_short_pos = 0, + display_working_long_pos = 0, + display_working_short_pos = 0 + }; // sod - public decimal sod_pnl { get; } - public decimal sod_long_pos { get; } - public decimal sod_short_pos { get; } + public decimal sod_pnl { get; init; } + public decimal sod_long_pos { get; init; } + public decimal sod_short_pos { get; init; } public decimal sod_net_pos => sod_long_pos - sod_short_pos; - // sod display - public decimal display_sod_long_pos { get; } - public decimal display_sod_short_pos { get; } + public decimal display_sod_long_pos { get; init; } + public decimal display_sod_short_pos { get; init; } public decimal display_sod_net_pos => display_sod_long_pos - display_sod_short_pos; // day - public decimal day_pnl { get; } - public decimal day_long_pos { get; } - public decimal day_short_pos { get; } + public decimal day_pnl { get; init; } + public decimal day_long_pos { get; init; } + public decimal day_short_pos { get; init; } public decimal day_net_pos => day_long_pos - day_short_pos; - // display - public decimal display_day_long_pos { get; } - public decimal display_day_short_pos { get; } + public decimal display_day_long_pos { get; init; } + public decimal display_day_short_pos { get; init; } public decimal display_day_net_pos => display_day_long_pos - display_day_short_pos; // total @@ -65,40 +51,40 @@ namespace bts.shell.pnl { public decimal total_net_pos => total_long_pos - total_short_pos; public decimal total_long_pos => sod_long_pos + day_long_pos; public decimal total_short_pos => sod_short_pos + day_short_pos; - // display public decimal display_total_long_pos => display_sod_long_pos + display_day_long_pos; public decimal display_total_short_pos => display_sod_short_pos + display_day_short_pos; public decimal display_total_net_pos => display_total_long_pos - display_total_short_pos; // working - public decimal working_long_pos { get; } - public decimal working_short_pos { get; } + public decimal working_long_pos { get; init; } + public decimal working_short_pos { get; init; } public decimal working_net_pos => working_long_pos - working_short_pos; - // display - public decimal display_working_long_pos { get; } - public decimal display_working_short_pos { get; } + public decimal display_working_long_pos { get; init; } + public decimal display_working_short_pos { get; init; } public decimal display_working_net_pos => display_working_long_pos - display_working_short_pos; public bool Equals(PNLNormalizedData other) => - day_pnl == other.day_pnl && - day_long_pos == other.day_long_pos && - day_short_pos == other.day_short_pos && + // sod sod_pnl == other.sod_pnl && sod_long_pos == other.sod_long_pos && sod_short_pos == other.sod_short_pos && - working_long_pos == other.working_long_pos && - working_short_pos == other.working_short_pos && - // display - sod_long_pos == other.sod_long_pos && - sod_short_pos == other.sod_short_pos && + display_sod_long_pos == other.display_sod_long_pos && + display_sod_short_pos == other.display_sod_short_pos && + + // day + day_pnl == other.day_pnl && day_long_pos == other.day_long_pos && day_short_pos == other.day_short_pos && - total_long_pos == other.total_long_pos && - total_short_pos == other.total_short_pos && - working_long_pos == other.working_long_pos && - working_short_pos == other.working_short_pos; + display_day_long_pos == other.display_day_long_pos && + display_day_short_pos == other.display_day_short_pos && - public override bool Equals(object obj) => + // working + working_long_pos == other.working_long_pos && + working_short_pos == other.working_short_pos && + display_working_long_pos == other.display_working_long_pos && + display_working_short_pos == other.display_working_short_pos; + + public override bool Equals(object? obj) => obj is PNLNormalizedData other && Equals(other); public override int GetHashCode() => @@ -108,53 +94,48 @@ namespace bts.shell.pnl { public override string ToString() => string.Join(",", new[] { - $"SOD PNL : {sod_pnl}", - $"SOD Long : {sod_long_pos}", + $"SOD PNL: {sod_pnl}", + $"SOD Long: {sod_long_pos}", $"SOD Short: {sod_short_pos}", - $"Day PNL : {day_pnl}", - $"Day Long : {day_long_pos}", + $"Day PNL: {day_pnl}", + $"Day Long: {day_long_pos}", $"Day Short: {day_short_pos}", - $"Work Long : {working_long_pos}", - $"Work Short: {working_short_pos}", + $"Wrk Long: {working_long_pos}", + $"Wrk Short: {working_short_pos}", }); public static bool operator ==(PNLNormalizedData a, PNLNormalizedData b) => a.Equals(b); public static bool operator !=(PNLNormalizedData a, PNLNormalizedData b) => !a.Equals(b); public static PNLNormalizedData operator +(PNLNormalizedData a, PNLNormalizedData b) => - new PNLNormalizedData( - a.day_pnl + b.day_pnl, - a.day_long_pos + b.day_long_pos, - a.day_short_pos + b.day_short_pos, - a.sod_pnl + b.sod_pnl, - a.sod_long_pos + b.sod_long_pos, - a.sod_short_pos + b.sod_short_pos, - a.working_long_pos + b.working_long_pos, - a.working_short_pos + b.working_short_pos, - // display - a.display_day_long_pos + b.display_day_long_pos, - a.display_day_short_pos + b.display_day_short_pos, - a.display_sod_long_pos + b.display_sod_long_pos, - a.display_sod_short_pos + b.display_sod_short_pos, - a.display_working_long_pos + b.display_working_long_pos, - a.display_working_short_pos + b.display_working_short_pos); + _operate(a, b, (ax, bx) => ax + bx); public static PNLNormalizedData operator -(PNLNormalizedData a, PNLNormalizedData b) => - new PNLNormalizedData( - a.day_pnl - b.day_pnl, - a.day_long_pos - b.day_long_pos, - a.day_short_pos - b.day_short_pos, - a.sod_pnl - b.sod_pnl, - a.sod_long_pos - b.sod_long_pos, - a.sod_short_pos - b.sod_short_pos, - a.working_long_pos - b.working_long_pos, - a.working_short_pos - b.working_short_pos, - // display - a.display_day_long_pos - b.display_day_long_pos, - a.display_day_short_pos - b.display_day_short_pos, - a.display_sod_long_pos - b.display_sod_long_pos, - a.display_sod_short_pos - b.display_sod_short_pos, - a.display_working_long_pos - b.display_working_long_pos, - a.display_working_short_pos - b.display_working_short_pos); + _operate(a, b, (ax, bx) => ax - bx); + + static PNLNormalizedData _operate( + PNLNormalizedData a, PNLNormalizedData b, Func op) => + new() { + // sod + sod_pnl = op(a.sod_pnl, b.sod_pnl), + sod_long_pos = op(a.sod_long_pos, b.sod_long_pos), + sod_short_pos = op(a.sod_short_pos, b.sod_short_pos), + display_sod_long_pos = op(a.display_sod_long_pos, b.display_sod_long_pos), + display_sod_short_pos = op(a.display_sod_short_pos, b.display_sod_short_pos), + + // day + day_pnl = op(a.day_pnl, b.day_pnl), + day_long_pos = op(a.day_long_pos, b.day_long_pos), + day_short_pos = op(a.day_short_pos, b.day_short_pos), + display_day_long_pos = op(a.display_day_long_pos, b.display_day_long_pos), + display_day_short_pos = op(a.display_day_short_pos, b.display_day_short_pos), + + // working + working_long_pos = op(a.working_long_pos, b.working_long_pos), + working_short_pos = op(a.working_short_pos, b.working_short_pos), + display_working_long_pos = op(a.display_working_long_pos, b.display_working_long_pos), + display_working_short_pos = op( + a.display_working_short_pos, b.display_working_short_pos), + }; } } diff --git a/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs b/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs index 2cb412704..e01f9e4df 100644 --- a/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs +++ b/src/client/spark/Modules/Trading/PNL/VM/PNLRowVM.cs @@ -143,32 +143,39 @@ namespace bts.shell.pnl { get { if (_refresh_normalized_data) { _refresh_normalized_data = false; - // only count futures for [non-raw] positions (they already take spread legs into account) - decimal day_long, day_short, sod_long, sod_short; - if (security.HasValue && security.Value.ContractCase != Security.ContractOneofCase.Future) { - day_long = day_short = sod_long = sod_short = 0; - } else { - day_long = data.day.long_pos.round_to_decimal(); - day_short = data.day.short_pos.round_to_decimal(); - sod_long = data.sod.long_pos.round_to_decimal(); - sod_short = data.sod.short_pos.round_to_decimal(); - } - var nd = new PNLNormalizedData( - day_pnl.value.round_to_decimal(), - day_long, - day_short, - sod_pnl.value.round_to_decimal(), - sod_long, - sod_short, - data.working_long_pos.round_to_decimal(), - data.working_short_pos.round_to_decimal(), - // display - data.day_long_pos.round_to_decimal(), - data.day_short_pos.round_to_decimal(), - data.sod_long_pos.round_to_decimal(), - data.sod_short_pos.round_to_decimal(), - data.working_long_pos.round_to_decimal(), - data.working_short_pos.round_to_decimal()); + // Only count outrights (spread positions should be normalized into outrights) + var sec_type = security?.ContractCase ?? Security.ContractOneofCase.None; + var (sod_long, sod_short, day_long, day_short) = sec_type switch { + Security.ContractOneofCase.Future or Security.ContractOneofCase.Option => ( + data.sod.long_pos.round_to_decimal(), + data.sod.short_pos.round_to_decimal(), + data.day.long_pos.round_to_decimal(), + data.day.short_pos.round_to_decimal()), + _ => (0, 0, 0, 0) + }; + var working_long = data.working_long_pos.round_to_decimal(); + var working_short = data.working_short_pos.round_to_decimal(); + var nd = new PNLNormalizedData() { + // sod + sod_pnl = sod_pnl.value.round_to_decimal(), + sod_long_pos = sod_long, + sod_short_pos = sod_short, + display_sod_long_pos = sod_long, + display_sod_short_pos = sod_short, + + // day + day_pnl = day_pnl.value.round_to_decimal(), + day_long_pos = day_long, + day_short_pos = day_short, + display_day_long_pos = day_long, + display_day_short_pos = day_short, + + // working + working_long_pos = working_long, + working_short_pos = working_short, + display_working_long_pos = working_long, + display_working_short_pos = working_short + }; if (_normalized_data != nd) { _normalized_data = nd; updated?.Invoke(nd);