From 7fb7b1e4afe42d5d65d3afb52dd7325568fcf6b6 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 4 Feb 2024 22:13:07 -0600 Subject: [PATCH] Fix PriceStatsStream * Store subscription IDisposable in class (we need to control the life of the subscription) * Flip typo '_dirty = false' -> '_dirty = true' --- .../Business/MarketData/PriceStatsStream.cs | 252 +++++++++--------- 1 file changed, 127 insertions(+), 125 deletions(-) diff --git a/src/DataStreamSuite/SharedStreams/Business/MarketData/PriceStatsStream.cs b/src/DataStreamSuite/SharedStreams/Business/MarketData/PriceStatsStream.cs index 56e23cf1f..1d61802b8 100644 --- a/src/DataStreamSuite/SharedStreams/Business/MarketData/PriceStatsStream.cs +++ b/src/DataStreamSuite/SharedStreams/Business/MarketData/PriceStatsStream.cs @@ -1,125 +1,127 @@ -namespace BCG { - using BCG.Utilities.General; - - using bts.api; - using bts.api.domain; - using bts.api.synthetic; - using bts.md; - using bts.spark; - using bts.utils; - - using mbts; - using mbts.co2; - using mbts.config; - using mbts.spark; - using mbts.ui; - - using Spark.Core.Data; - using Spark.Core.Disposables; - using Spark.Core.Reactive; - - using System; - using System.Collections.Concurrent; - using System.Collections.Generic; - using System.Collections.ObjectModel; - using System.Linq; - using System.Reactive.Linq; - using bts.api.core; - using Spark.Api.MarketData; - - using SecurityKey = System.UInt32; - using System.Data; - using System.Reflection.Metadata; - - [DataStreamUserRespresentation("price_stats")] - public class PriceStatsStream : 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; - private bool _dirty = false; - private readonly IBlueTableSchema _pstats_scheam = BlueDataUtility.form_schema("bbo", - Tuple.Create("id", TypeCode.Int32), - Tuple.Create("sec_key", TypeCode.Int32), - Tuple.Create("sec", TypeCode.String), - Tuple.Create("open", TypeCode.UInt32), - Tuple.Create("close", TypeCode.Double), - Tuple.Create("settlement", TypeCode.Single)); - - // TODO: add cons for symbol, exchange etc - public PriceStatsStream() - : this(ViewStreamDatabase.instance) { - } - - public PriceStatsStream(ViewStreamDatabase vsd) { - bts.Log.Gen.info("PriceStatsStream.cons"); - _vsd = vsd; - _md_client = new MarketDataClient(new MDSource(TimeSpan.FromSeconds(0.5))); - } - - protected override void myOnFirstSubscriber() { - _vsd.addWorker(this); - _md_client.price_stats().Subscribe(_on_price_stats); - } - - private void _log_updates(TableUpdate update) { - int i = 0; - // foreach (BookTuple bt in update.added) { - // bts.Log.Gen.info($"PriceStatsStream 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($"PriceStatsStream 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_price_stats(TableUpdate update) { - _log_updates(update); - // just cacching in here, will populate the rows in _poll_scan and fire the datapacket - bts.Log.Gen.info($"PriceStatsStream._on_price_stats state: {update.state.Count()} changes: {update.changes.Count()} added: {update.added.Count()}"); - _cached_state = update.state; - _dirty = false; - } - - protected override void myOnLastSubscriber() { - _vsd.removeWorker(this); - } - - private void _scan() { - // this shouldn be made incremental using the TableUpdate - bts.Log.Gen.info("PriceStatsStream._scan_bbo"); - if (_cached_state == null) - return; - BlueTable new_state = new BlueTable(_pstats_scheam); - foreach (var kv in _cached_state) { - SecurityKey skey = kv.Key; - int id = Convert.ToInt32(skey); - PriceStats pstats = kv.Value; - IBlueRow r = new BlueRow(_pstats_scheam, id, skey, pstats.security.ToString(), pstats.open, pstats.close, pstats.settlement); - new_state.addRow(r); - } - DataPacket dp = null; - if (myLastValue == null) { - dp = new DataPacket(new_state, new_state, new BlueTable(_pstats_scheam)); - } else { - DataPacket old_dp = myLastValue as DataPacket; - 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; - myFireEvent(dp); - } - - public void doWork() { - if (_dirty && DateTime.Now - _last_poll_time > _poll_period) { - _last_poll_time = DateTime.Now; - _dirty = false; - _scan(); - } - } - } -} \ No newline at end of file +namespace BCG { + using BCG.Utilities.General; + + using bts.api; + using bts.api.domain; + using bts.api.synthetic; + using bts.md; + using bts.spark; + using bts.utils; + + using mbts; + using mbts.co2; + using mbts.config; + using mbts.spark; + using mbts.ui; + + using Spark.Core.Data; + using Spark.Core.Disposables; + using Spark.Core.Reactive; + + using System; + using System.Collections.Concurrent; + using System.Collections.Generic; + using System.Collections.ObjectModel; + using System.Linq; + using System.Reactive.Linq; + using bts.api.core; + using Spark.Api.MarketData; + + using SecurityKey = System.UInt32; + using System.Data; + using System.Reflection.Metadata; + + [DataStreamUserRespresentation("price_stats")] + public class PriceStatsStream : 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; + private bool _dirty = false; + private readonly IBlueTableSchema _pstats_scheam = BlueDataUtility.form_schema("bbo", + Tuple.Create("id", TypeCode.Int32), + Tuple.Create("sec_key", TypeCode.Int32), + Tuple.Create("sec", TypeCode.String), + Tuple.Create("open", TypeCode.UInt32), + Tuple.Create("close", TypeCode.Double), + Tuple.Create("settlement", TypeCode.Single)); + IDisposable? subscription; + + // TODO: add cons for symbol, exchange etc + public PriceStatsStream() + : this(ViewStreamDatabase.instance) { + } + + public PriceStatsStream(ViewStreamDatabase vsd) { + bts.Log.Gen.info("PriceStatsStream.cons"); + _vsd = vsd; + _md_client = new MarketDataClient(new MDSource(TimeSpan.FromSeconds(0.5))); + } + + protected override void myOnFirstSubscriber() { + _vsd.addWorker(this); + subscription = _md_client.price_stats().Subscribe(_on_price_stats); + } + + private void _log_updates(TableUpdate update) { + int i = 0; + // foreach (BookTuple bt in update.added) { + // bts.Log.Gen.info($"PriceStatsStream 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($"PriceStatsStream 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_price_stats(TableUpdate update) { + _log_updates(update); + // just cacching in here, will populate the rows in _poll_scan and fire the datapacket + bts.Log.Gen.info($"PriceStatsStream._on_price_stats state: {update.state.Count()} changes: {update.changes.Count()} added: {update.added.Count()}"); + _cached_state = update.state; + _dirty = true; + } + + protected override void myOnLastSubscriber() { + _vsd.removeWorker(this); + Disposable.dispose(ref subscription); + } + + private void _scan() { + // this shouldn be made incremental using the TableUpdate + bts.Log.Gen.info("PriceStatsStream._scan_bbo"); + if (_cached_state == null) + return; + BlueTable new_state = new BlueTable(_pstats_scheam); + foreach (var kv in _cached_state) { + SecurityKey skey = kv.Key; + int id = Convert.ToInt32(skey); + PriceStats pstats = kv.Value; + IBlueRow r = new BlueRow(_pstats_scheam, id, skey, pstats.security.ToString(), pstats.open, pstats.close, pstats.settlement); + new_state.addRow(r); + } + DataPacket dp = null; + if (myLastValue == null) { + dp = new DataPacket(new_state, new_state, new BlueTable(_pstats_scheam)); + } else { + DataPacket old_dp = myLastValue as DataPacket; + 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; + myFireEvent(dp); + } + + public void doWork() { + if (_dirty && DateTime.Now - _last_poll_time > _poll_period) { + _last_poll_time = DateTime.Now; + _dirty = false; + _scan(); + } + } + } +}