From d62f88691cd2177cfebd44faef059ee5e87ff6af Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 7 Feb 2023 23:55:10 -0500 Subject: [PATCH] feat (internal): Add price ticker to md client --- .../spark_api/MarketData/MarketDataClient.cs | 78 +++++++++++++++ .../spark_api/MarketData/PriceTicker.cs | 95 +++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 src/client/spark_api/MarketData/PriceTicker.cs diff --git a/src/client/spark_api/MarketData/MarketDataClient.cs b/src/client/spark_api/MarketData/MarketDataClient.cs index a72d42536..6e99156fd 100644 --- a/src/client/spark_api/MarketData/MarketDataClient.cs +++ b/src/client/spark_api/MarketData/MarketDataClient.cs @@ -75,6 +75,41 @@ namespace bts.md { TradesSource.create( _source.consumer, securities(filter.security_criteria), _poll_interval); + public IObservable> price_ticker( + SecurityCriteria? criteria = null) { + // This MDSource's IObservable provides callbacks when a security is added or removed, but + // doesn't detect ticker changes. To avoid maintaining complicated state, create a brand new + // IObservable of ticker updates whenever the securities change. + return criteria.subscribe_md(_source.subscribe_securities()) + .Select(fixed_sec_price_tickers) + .FlattenTableSeries(); + + IObservable> fixed_sec_price_tickers( + IEnumerable security_mds) { + // Convert 'security_mds' to a list of PollablePriceTickers. + var cached_tickers = security_mds + .Select(s => new PollablePriceTicker(s)) + .ToList(); + + return Observable.Interval(_poll_interval).Scan( + TableUpdate.mk_empty(get_sec_id), + poll_tickers); + + uint get_sec_id(PriceTicker ticker) => + ticker.security.Id; + + TableUpdate poll_tickers(TableUpdate table, long _) { + var bd = table.to_builder(); + foreach (var cticker in cached_tickers) { + if (cticker.poll_changes()) { + bd.add_or_update(cticker.to_ticker()); + } + } + return bd.to_table(); + } + } + } + class PollableBook { readonly SecurityMD _security_md; readonly uint _num_depth_levels; @@ -106,5 +141,48 @@ namespace bts.md { .Select(nps => new PriceSize(nps.size, nps.direct_size, nps.implied_size, nps.price)) .ToList(); } + + class PollablePriceTicker { + readonly SecurityMD _security_md; + ulong _bbo_poll_time = 0; + ulong _last_poll_time = 0; + NativePriceSize _bid; + NativePriceSize _ask; + NativePriceSize _last; + PriceTicker _ticker; + ulong _volume; + + public PollablePriceTicker(SecurityMD security_md) => + _security_md = security_md; + + public PriceTicker to_ticker() => + _ticker; + + public bool poll_changes() { + var md = _security_md.md; + md.get_bbo(ref _bbo_poll_time, out _bid, out _ask); + md.get_last_trade(ref _last_poll_time, out _last, out _, out _volume); + return Swap.toggle_to(ref _ticker, _mk_ticker()); + } + + PriceTicker _mk_ticker() { + var md = _security_md.md; + return new( + _security_md.security, + _security_md.quantity_display, + md.session_high, + md.session_low, + _volume, + _bid.price, + _bid.size, + _ask.price, + _ask.size, + md.fixing_price, + _last.price, + _last.size); + } + } + + } } } diff --git a/src/client/spark_api/MarketData/PriceTicker.cs b/src/client/spark_api/MarketData/PriceTicker.cs new file mode 100644 index 000000000..5e7d7e315 --- /dev/null +++ b/src/client/spark_api/MarketData/PriceTicker.cs @@ -0,0 +1,95 @@ +#nullable enable +namespace Spark.Api.MarketData { + using bts.api; + using bts.api.domain; + + using Spark.Core.Collections; + + using System; + + /// + /// Price/trade statistics for a particular security (fast moving) + /// + public readonly struct PriceTicker : IEquatable { + public PriceTicker( + SecurityImmutable security, + TickDisplaySettingsImmutable quantity_display, + double high, + double low, + double volume, + double bid, + double bid_quantity, + double ask, + double ask_quantity, + double mark, + double last, + double last_quantity + ) { + this.security = security; + this.quantity_display = quantity_display; + this.high = high; + this.low = low; + this.volume = volume; + this.bid = bid; + this.bid_quantity = bid_quantity; + this.ask = ask; + this.ask_quantity = ask_quantity; + this.mark = mark; + this.last = last; + this.last_quantity = last_quantity; + } + + public SecurityImmutable security { get; } + public TickDisplaySettingsImmutable quantity_display { get; } + public double high { get; } + public double low { get; } + public double volume { get; } + public double bid { get; } + public double bid_quantity { get; } + public double ask { get; } + public double ask_quantity { get; } + public double mark { get; } + public double last { get; } + public double last_quantity { get; } + + public bool Equals(PriceTicker other) => + security.Equals(other.security) && + quantity_display.Equals(other.quantity_display) && + high.Equals(other.high) && + low.Equals(other.low) && + volume.Equals(other.volume) && + bid.Equals(other.bid) && + bid_quantity.Equals(other.bid_quantity) && + ask.Equals(other.ask) && + ask_quantity.Equals(other.ask_quantity) && + mark.Equals(other.mark) && + last.Equals(other.last) && + last_quantity.Equals(other.last_quantity); + + public override bool Equals(object? obj) => + obj is PriceTicker other && Equals(other); + + public override int GetHashCode() => + new HashCode() + .with_values(new int[] { + security.Id.GetHashCode(), + high.GetHashCode(), + low.GetHashCode(), + volume.GetHashCode(), + bid.GetHashCode(), + bid_quantity.GetHashCode(), + ask.GetHashCode(), + ask_quantity.GetHashCode(), + mark.GetHashCode(), + last.GetHashCode(), + last_quantity.GetHashCode() + }) + .ToHashCode(); + + public static bool operator ==(PriceTicker left, PriceTicker right) => + left.Equals(right); + + public static bool operator !=(PriceTicker left, PriceTicker right) => + !(left == right); + } +}