diff --git a/src/client/spark/Modules/Trading/Ladder/FairValueModel.cs b/src/client/spark/Modules/Trading/Ladder/FairValueModel.cs index ce89b5b93..999640bc9 100644 --- a/src/client/spark/Modules/Trading/Ladder/FairValueModel.cs +++ b/src/client/spark/Modules/Trading/Ladder/FairValueModel.cs @@ -16,15 +16,19 @@ namespace Spark.Trading { class FairValueModel { readonly IObservable> _sec_mds_in; readonly IObservable> _sec_greeks_in; + readonly IObservable _pnl_data_in; public FairValueModel(IBus bus) - : this(bus.subscribe(SparkTags.SecuritiesTag), bus.subscribe(SparkTags.Greeks)) { } + : this(bus.subscribe(SparkTags.SecuritiesTag), bus.subscribe(SparkTags.Greeks), + bus.subscribe(SparkTags.ReportBlackboardTags.PNL.PNLData)) { } public FairValueModel( IObservable> sec_mds_in, - IObservable> sec_greeks_in) { + IObservable> sec_greeks_in, + IObservable pnl_data_in) { _sec_mds_in = sec_mds_in; _sec_greeks_in = sec_greeks_in; + _pnl_data_in = pnl_data_in; } public IObservable get_price_source(FairValueKey key) { @@ -33,6 +37,8 @@ namespace Spark.Trading { FairValueLine.PriceType.Ask => ask_source(), FairValueLine.PriceType.Mid => mid_source(), FairValueLine.PriceType.Theo => theo_source(), + FairValueLine.PriceType.LongAvgCost => long_avg_cost_source(), + FairValueLine.PriceType.ShortAvgCost => short_avg_cost_source(), _ => throw new NotImplementedException() }; @@ -51,6 +57,18 @@ namespace Spark.Trading { IObservable theo_source() => _sec_greeks_in.Select(sec_greeks => sec_greeks.TryGetValue(key.security_id, out var greeks) ? greeks.normalized_theo : double.NaN); + + IObservable long_avg_cost_source() => + _pnl_data_in.Select(pnl_data => + pnl_data.position_data.TryGetFirst(p => p!.security.Id == key.security_id, out var p) + ? p.total_long_avg_cost() + : double.NaN); + + IObservable short_avg_cost_source() => + _pnl_data_in.Select(pnl_data => + pnl_data.position_data.TryGetFirst(p => p!.security.Id == key.security_id, out var p) + ? p.total_short_avg_cost() + : double.NaN); } } } diff --git a/src/client/spark/Modules/Trading/Ladder/LadderModel.cs b/src/client/spark/Modules/Trading/Ladder/LadderModel.cs index d00ffe1f4..4b4a2505d 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderModel.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderModel.cs @@ -71,7 +71,7 @@ namespace mbts.spark { .Select(gs => gs.TryGetValue(source.security.Id) ?? GreeksTuple.empty) .DistinctUntilChanged(); var fair_value_model = new FairValueModel( - bus.subscribe(SparkTags.SecuritiesTag), all_sec_greeks); + bus.subscribe(SparkTags.SecuritiesTag), all_sec_greeks, bus.subscribe(SparkTags.ReportBlackboardTags.PNL.PNLData)); return new( source, ordergw, diff --git a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs index cca61f91a..1d31d05ce 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs @@ -252,7 +252,9 @@ namespace mbts.spark { Bid, Ask, Mid, - Theo + Theo, + LongAvgCost, + ShortAvgCost, } /// diff --git a/src/client/spark/Risk/PNL/IPNLUnit.cs b/src/client/spark/Risk/PNL/IPNLUnit.cs index f4d67d42e..15bf82534 100644 --- a/src/client/spark/Risk/PNL/IPNLUnit.cs +++ b/src/client/spark/Risk/PNL/IPNLUnit.cs @@ -80,6 +80,18 @@ namespace mbts.analytics { public static double total_long_pos(this IPNLUnit pnl_unit) => new[] { pnl_unit.start_long_pos, pnl_unit.day_long_pos }.sum_real(true); + public static double total_long_avg_cost(this IPNLUnit pnl_unit) { + var qty = pnl_unit.total_long_pos(); + if (!qty.is_real_positive()) { + return double.NaN; + } + var cost = new[] { + pnl_unit.start_long_avg_cost * pnl_unit.start_long_pos, + pnl_unit.day_long_avg_cost * pnl_unit.day_long_pos + }.sum_real(true); + return cost / qty; + } + /// /// Computes total short position as + /// @@ -90,6 +102,18 @@ namespace mbts.analytics { public static double total_short_pos(this IPNLUnit pnl_unit) => new[] { pnl_unit.start_short_pos, pnl_unit.day_short_pos }.sum_real(true); + public static double total_short_avg_cost(this IPNLUnit pnl_unit) { + var qty = pnl_unit.total_short_pos(); + if (!qty.is_real_positive()) { + return double.NaN; + } + var cost = new[] { + pnl_unit.start_short_avg_cost * pnl_unit.start_short_pos, + pnl_unit.day_short_avg_cost * pnl_unit.day_short_pos + }.sum_real(true); + return cost / qty; + } + /// /// Computes total pnl as + ///