ui: Add long/short avg cost lines to ladder

This commit is contained in:
John Colvin committed 2025-11-19 12:24:36 -05:00
1 parent acb86b2658
commit eb30e16ef6
4 files changed
+48 -4

No files matched your search

@@ -16,15 +16,19 @@ namespace Spark.Trading {
class FairValueModel {
readonly IObservable<IReadOnlyDictionary<uint, SecurityMD>> _sec_mds_in;
readonly IObservable<IReadOnlyDictionary<uint, GreeksTuple>> _sec_greeks_in;
readonly IObservable<PNLData> _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<IReadOnlyDictionary<uint, SecurityMD>> sec_mds_in,
IObservable<IReadOnlyDictionary<uint, GreeksTuple>> sec_greeks_in) {
IObservable<IReadOnlyDictionary<uint, GreeksTuple>> sec_greeks_in,
IObservable<PNLData> pnl_data_in) {
_sec_mds_in = sec_mds_in;
_sec_greeks_in = sec_greeks_in;
_pnl_data_in = pnl_data_in;
}
public IObservable<double> 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<double> theo_source() =>
_sec_greeks_in.Select(sec_greeks =>
sec_greeks.TryGetValue(key.security_id, out var greeks) ? greeks.normalized_theo : double.NaN);
IObservable<double> 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<double> 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);
}
}
}
@@ -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,
@@ -252,7 +252,9 @@ namespace mbts.spark {
Bid,
Ask,
Mid,
Theo
Theo,
LongAvgCost,
ShortAvgCost,
}
/// <summary>
+24
View File
@@ -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;
}
/// <summary>
/// Computes total short position as <see cref="IPNLUnit.start_short_pos"/> +
/// <see cref="IPNLUnit.day_short_pos"/>
@@ -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;
}
/// <summary>
/// Computes total pnl as <see cref="IPNLUnit.start_pnl"/> + <see cref="IPNLUnit.day_pnl"/>
/// </summary>