diff --git a/src/client/spark_api/MarketData/IncrementalMarketDataClient.cs b/src/client/spark_api/MarketData/IncrementalMarketDataClient.cs new file mode 100644 index 000000000..63a60f8d0 --- /dev/null +++ b/src/client/spark_api/MarketData/IncrementalMarketDataClient.cs @@ -0,0 +1,137 @@ +namespace bts.md { + using bts.api.core; + + using mbts; + using mbts.co2; + using mbts.spark; + + using System; + using System.Collections.Generic; + using System.Linq; + using System.Reactive.Linq; + + public readonly struct Book : IEquatable { + public Book(uint security_id, List bids, List asks) { + this.security_id = security_id; + this.bids = bids; + this.asks = asks; + } + + public uint security_id { get; } + public List bids { get; } + public List asks { get; } + + public static Book empty(uint security_id) => + new(security_id, new(0), new(0)); + + public bool Equals(Book other) => + security_id == other.security_id && + bids.SequenceEqual(other.bids) && + asks.SequenceEqual(other.asks); + + public override bool Equals(object obj) => + obj is Book other && Equals(other); + + public override int GetHashCode() => + security_id.GetHashCode(); + + public static bool operator ==(Book left, Book right) => + left.Equals(right); + + public static bool operator !=(Book left, Book right) => + !(left == right); + } + + public class IncrementalMarketDataClient { + readonly MDSource _source; + readonly TimeSpan _poll_interval; + readonly uint _num_depth_levels; + + public IncrementalMarketDataClient(MDSource source) + : this(source, 5, TimeSpan.FromSeconds(0.1)) { } + + public IncrementalMarketDataClient(MDSource source, uint num_depth_levels, TimeSpan poll_interval) { + _source = source; + _poll_interval = poll_interval; + _num_depth_levels = num_depth_levels; + } + IObservable> security_market_data(SecurityCriteria cr, uint security_id) { + return market_data() + .Select(table => { + var b = table.state.Values.Take(3).ToList(); + return b; + }); + } + public IObservable security_market_data(uint security_id) => + market_data() + .Select(table => { + var b = table.state.TryGetValue(security_id, out var book) ? + book : Book.empty(security_id); + return b; + }) + .DistinctUntilChanged(); + + public IObservable> market_data() { + // This MDSource's IObservable provides callbacks when a security is added or removed, but + // doesn't detect book changes. To avoid maintaining complicated state, create a brand new + // IObservable of book updates whenever the securities change. + return _source.subscribe_securities() + .Select(fixed_sec_market_data) + .FlattenTableSeries(); + + IObservable> fixed_sec_market_data( + IReadOnlyDictionary security_mds) { + // Convert 'security_mds' to a list of PollableBooks. + var cached_books = security_mds + .Select(kv => new PollableBook(kv.Value, _num_depth_levels)) + .ToList(); + + return Observable.Interval(_poll_interval).Scan( + TableUpdate.mk_empty(get_sec_id), + poll_books); + + uint get_sec_id(Book book) => + book.security_id; + + TableUpdate poll_books(TableUpdate table, long _) { + var bd = table.to_builder(); + foreach (var cbook in cached_books) { + if (cbook.poll_changes()) { + bd.add_or_update(cbook.to_book()); + } + } + return bd.to_table(); + } + } + } + + class PollableBook { + readonly SecurityMD _security_md; + readonly uint _num_depth_levels; + ulong _last_update_time = 0; + // Reuse the same arrays as an optimization to avoid reallocating memory on each poll. + // Note: this will break if the code isn't called in a thread-safe manner. + NativePriceSize[] _bids; + NativePriceSize[] _asks; + + public PollableBook(SecurityMD security_md, uint num_depth_levels) { + _security_md = security_md; + _num_depth_levels = num_depth_levels; + _bids = new NativePriceSize[_num_depth_levels]; + _asks = new NativePriceSize[_num_depth_levels]; + } + + public Book to_book() => + new(_security_md.security.Id, _to_book_side(_bids), _to_book_side(_asks)); + + public bool poll_changes() => + _security_md.md.get_depth( + ref _last_update_time, ref _bids, ref _asks, _num_depth_levels); + + static List _to_book_side(NativePriceSize[] native_book_side) => + native_book_side + .Select(nps => new PriceSize(nps.size, nps.direct_size, nps.implied_size, nps.price)) + .ToList(); + } + } +}