#nullable enable namespace bts.md { using bts.api; using bts.api.core; using bts.collections; using mbts; using mbts.co2; using mbts.spark; using Spark.Api.MarketData; using Spark.Core; using Spark.Core.Reactive; using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; 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), TimeSpan.FromSeconds(1)) { } 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; } public IObservable> securities( SecurityCriteria? criteria = null) => criteria.subscribe(_source.subscribe_securities()) .Select(secs => secs.ToDictionary(s => s.Id)); public IObservable> securities_incremental( SecurityCriteria? criteria = null) => criteria is null ? securities_incremental() : securities_incremental(criteria.includes); public IObservable> securities_incremental( Func filter) { var secs_src = _source.subscribe_securities().ToSelector( sec_mds => MappedDictionary.create(sec_mds, s => s.security)); if (filter is not null) { secs_src = secs_src.extend( secs => new FilteredDictionary(secs, filter)); } return secs_src.Table(s => s.Id); } public IObservable> books(SecurityCriteria? criteria = null) => books(criteria is null ? null : new Func(s => criteria.includes(s))); public IObservable> books(Func? filter) { // 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. var secs = _source.subscribe_securities(); var filtered_secs = filter is null ? secs.Select(mds => mds.Values) : secs.Select(mds => mds.Values.Where(md => filter(md.security))); return filtered_secs.Select(fixed_sec_market_data).FlattenTableSeries(); IObservable> fixed_sec_market_data( IEnumerable security_mds) { // Convert 'security_mds' to a list of PollableBooks. var cached_books = security_mds .Select(s => new PollableBook(s, _num_depth_levels)) .ToList(); return Observable.Interval(_poll_interval).Scan( TableUpdate.mk_empty(get_sec_id), poll_books); uint get_sec_id(BookTuple 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_tuple()); } } return bd.to_table(); } } } public IObservable> trades(TradesSubscriptionFilter filter) { var sec_mds = filter.security_criteria is null ? _source.subscribe_securities() : filter.security_criteria.subscribe_md_map(_source.subscribe_securities()); return TradesSource.create(_source.consumer, sec_mds, _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(); } } } 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; 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 BookTuple to_book_tuple() { var book = new Book( _security_md.security.Id, _to_book_side(_bids), _to_book_side(_asks)); return new(_security_md.security, book, _security_md.quantity_display); } 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(); } 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); } } 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); } } } }