From a0bd207d93475849901e526ee80ceea66796e3ff Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 8 Feb 2023 00:20:41 -0500 Subject: [PATCH] feat (internal): Add price stats to md client --- .../spark_api/MarketData/MarketDataClient.cs | 70 ++++++++++++++++++- src/client/spark_api/MarketData/PriceStats.cs | 41 +++++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/client/spark_api/MarketData/PriceStats.cs diff --git a/src/client/spark_api/MarketData/MarketDataClient.cs b/src/client/spark_api/MarketData/MarketDataClient.cs index 6e99156fd..c09656504 100644 --- a/src/client/spark_api/MarketData/MarketDataClient.cs +++ b/src/client/spark_api/MarketData/MarketDataClient.cs @@ -8,6 +8,7 @@ namespace bts.md { using mbts.spark; using Spark.Api.MarketData; + using Spark.Core; using System; using System.Collections.Generic; @@ -17,14 +18,23 @@ namespace bts.md { public class MarketDataClient { readonly MDSource _source; readonly TimeSpan _poll_interval; + readonly TimeSpan _slow_poll_interval; readonly uint _num_depth_levels; public MarketDataClient(MDSource source) - : this(source, 5, TimeSpan.FromSeconds(0.1)) { } + : this(source, 5, TimeSpan.FromSeconds(0.1), TimeSpan.FromSeconds(1)) { } - public MarketDataClient(MDSource source, uint num_depth_levels, TimeSpan poll_interval) { + public MarketDataClient( + MDSource source, + uint num_depth_levels, + TimeSpan poll_interval, + TimeSpan slow_poll_interval) { _source = source; _poll_interval = poll_interval; + if (slow_poll_interval < poll_interval) { + Log.Api.warn("MarketDataClient 'slow_poll_interval' smaller than 'poll_interval'"); + } + _slow_poll_interval = slow_poll_interval; _num_depth_levels = num_depth_levels; } @@ -110,6 +120,41 @@ namespace bts.md { } } + public IObservable> price_stats( + SecurityCriteria? criteria = null) { + // This MDSource's IObservable provides callbacks when a security is added or removed, but + // doesn't detect stats changes. To avoid maintaining complicated state, create a brand new + // IObservable of stats updates whenever the securities change. + return criteria.subscribe_md(_source.subscribe_securities()) + .Select(fixed_sec_price_stats) + .FlattenTableSeries(); + + IObservable> fixed_sec_price_stats( + IEnumerable security_mds) { + // Convert 'security_mds' to a list of PollablePriceStats. + var cached_statss = security_mds + .Select(s => new PollablePriceStats(s)) + .ToList(); + + return Observable.Interval(_slow_poll_interval).Scan( + TableUpdate.mk_empty(get_sec_id), + poll_stats); + + uint get_sec_id(PriceStats stats) => + stats.security.Id; + + TableUpdate poll_stats(TableUpdate table, long _) { + var bd = table.to_builder(); + foreach (var cstats in cached_statss) { + if (cstats.poll_changes()) { + bd.add_or_update(cstats.to_stats()); + } + } + return bd.to_table(); + } + } + } + class PollableBook { readonly SecurityMD _security_md; readonly uint _num_depth_levels; @@ -183,6 +228,27 @@ namespace bts.md { } } + class PollablePriceStats { + readonly SecurityMD _security_md; + PriceStats _stats; + + public PollablePriceStats(SecurityMD security_md) => + _security_md = security_md; + + public PriceStats to_stats() => + _stats; + + public bool poll_changes() => + Swap.toggle_to(ref _stats, _mk_stats()); + + PriceStats _mk_stats() { + var md = _security_md.md; + return new( + _security_md.security, + md.open_price, + md.close_price, + md.settlement_price); + } } } } diff --git a/src/client/spark_api/MarketData/PriceStats.cs b/src/client/spark_api/MarketData/PriceStats.cs new file mode 100644 index 000000000..3058e442c --- /dev/null +++ b/src/client/spark_api/MarketData/PriceStats.cs @@ -0,0 +1,41 @@ +#nullable enable +namespace Spark.Api.MarketData { + using bts.api; + + using System; + + /// + /// Price/trade statistics for a particular security (slow moving) + /// + public readonly struct PriceStats : IEquatable { + public PriceStats(SecurityImmutable security, double open, double close, double settlement) { + this.security = security; + this.open = open; + this.close = close; + this.settlement = settlement; + } + + public SecurityImmutable security { get; } + public double open { get; } + public double close { get; } + public double settlement { get; } + + public bool Equals(PriceStats other) => + security.Equals(other.security) && + open.Equals(other.open) && + close.Equals(other.close) && + settlement.Equals(other.settlement); + + public override bool Equals(object? obj) => + obj is PriceStats other && Equals(other); + + public override int GetHashCode() => + HashCode.Combine(security.Id, open, close, settlement); + + public static bool operator ==(PriceStats left, PriceStats right) => + left.Equals(right); + + public static bool operator !=(PriceStats left, PriceStats right) => + !(left == right); + } +}