namespace bts.BlueOrb{ using BCG.Utilities.General; using BCG; using bts; using bts.md; using mbts.utils; using Spark.Core.Disposables; using Spark.Core.Reactive; using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using bts.api.core; using Spark.Api.MarketData; using SecurityKey = System.UInt32; using System.Diagnostics; [DataStreamUserRespresentation(false)] public class BookStream : ViewStream, IWorker { private readonly TimeSpan _poll_period = TimeSpan.FromMilliseconds(500); private DateTime _last_poll_time = DateTime.MinValue; private ViewStreamDatabase _vsd; private int _id = 0; IReadOnlyDictionary? _cached_state = null; MarketDataClient _md_client; readonly Stopwatch _timer = new(); private readonly IBlueTableSchema _bbo_schema = BlueDataUtility.form_schema("books", Tuple.Create("id", TypeCode.Int32), Tuple.Create("sec_key", TypeCode.UInt32), Tuple.Create("sec", TypeCode.String), Tuple.Create("level", TypeCode.UInt32), Tuple.Create("bid_price", TypeCode.Double), Tuple.Create("bid_sz", TypeCode.Single), Tuple.Create("ask_price", TypeCode.Double), Tuple.Create("ask_sz", TypeCode.Single)); IDisposable? subscription; // TODO: add cons for symbol, exchange etc public BookStream(MarketDataClient mdClient) : this(mdClient, ViewStreamDatabase.instance) { } public BookStream(MarketDataClient mdClient, ViewStreamDatabase vsd) { bts.Log.Gen.info("BookStream.cons"); _vsd = vsd; _md_client = mdClient; } protected override void myOnFirstSubscriber() { Log.isnull(subscription); _vsd.addWorker(this); subscription = _md_client.books().Subscribe(_on_books); } private void _log_updates(TableUpdate update) { int i = 0; foreach (BookTuple bt in update.added) { bts.Log.Gen.info($"BookStream added: idx: {i++} bid: {bt.book.bids[0].price} / {bt.book.bids[0].size} ask: {bt.book.asks[0].price} / {bt.book.asks[0].size}"); } foreach (Change c in update.changes) { var bt = c.New; bts.Log.Gen.info($"BookStream changes: idx: {i} bid: {bt.book.bids[0].price} / {bt.book.bids[0].size} ask: {bt.book.asks[0].price} / {bt.book.asks[0].size}"); } } private void _on_books(TableUpdate update) { //_log_updates(update); bts.Log.Gen.info($"BookStream._on_books state: {update.state.Count()} changes: {update.changes.Count()} added: {update.added.Count()}"); _cached_state = update.state; } protected override void myOnLastSubscriber() { Disposable.dispose(ref subscription); _vsd.removeWorker(this); } private Dictionary> _id_map = new Dictionary>(); private int _get_id(SecurityKey skey, int depth) { var d_map = _id_map.GetOrAdd(skey, _ => new() { {depth, _id++} }); if (!d_map.TryGetValue(depth, out int rv)) { rv = _id++; d_map.Add(depth, rv); } return rv; } private void _scan_bbo() { // this shouldn be made incremental using the TableUpdate bts.Log.Gen.info("BookStream._scan_bbo"); _timer.Restart(); if (_cached_state == null) return; BlueTable new_state = new BlueTable(_bbo_schema); foreach (var kv in _cached_state) { SecurityKey skey = kv.Key; BookTuple bt = kv.Value; for (int level = 0; level < bt.book.bids.Count; level++) { int id = _get_id(skey, level); IBlueRow r = new BlueRow(_bbo_schema, id, skey, bt.security.ToString(), Convert.ToUInt32(level), bt.book.bids[level].price, bt.book.bids[0].size, bt.book.asks[level].price, bt.book.asks[level].size); new_state.addRow(r); } } DataPacket? dp; if (myLastValue == null) { dp = new DataPacket(new_state, new_state, new BlueTable(_bbo_schema)); } else { var old_dp = ExceptionAssert.of_type(myLastValue); IBlueRowCollection added_rows, del_rows; BlueDataUtility.compute_differences_fast(new_state, old_dp.newRows, out del_rows, out added_rows); dp = new DataPacket(new_state, added_rows, del_rows); } if (dp.addedRows.Count == 0 && dp.deletedRows.Count == 0) return; _timer.Stop(); bts.Log.Gen.info($"BookStream._scan_bbo time: {_timer.Elapsed} msecs"); myFireEvent(dp); } public void doWork() { if (DateTime.Now - _last_poll_time > _poll_period) { _last_poll_time = DateTime.Now; _scan_bbo(); } } } }