Files
spark/src/DataStreamSuite/SharedStreams/Business/MarketData/BookStream.cs
T
John Colvin a4efaa0089 cleanup: Fix a ton of build warnings (cs)
All these warnings have been bothering me for a while. The work isn't done, but
this commit chops them down from ~1000 to ~600.

* AppendCollection.cs
* AppendStreamModified.cs
* BenzingaNewsStream.cs
* BenzingaNewsSymbolStateStream.cs
* BookStream.cs
* CSVSaverStream.cs
* ChartsUtils.cs
* ColumnInfo.cs
* ContractUtils.cs
* ContractUtils.cs
* DiffStream.cs
* FuturesActiveContractStream.cs
* FuzzyJoinOverTimeStream.cs
* HistoricalBarsStream.cs
* HistoricalBarsStream.cs
* InstrumentsStream.cs
* JoinOperandRowIndex.cs
* JoinRow.cs
* JoinStream.cs
* JoinTable.cs
* JoinTableSchema.cs
* LatencyStream.cs
* LeftInternalJoinStream.cs
* LightForeignJoinRow.cs
* LightForeignJoinRowList.cs
* LightLeftForeignJoinRow.cs
* LiteralStream.cs
* NLStream.cs
* NLToDSLStream.cs
* NLToIndexedFewShotPromptStream.cs
* NonIncGroupByStream.cs
* NonIncJoinBookkeeper.cs
* NonIncJoinSpec.cs
* OHLCVStream.cs
* OpenAIPromptStream.cs
* PriceStatsStream.cs
* PriceStatsStream.cs
* ProjectHelper.cs
* ProjectStream.cs
* PubSubServerStateStream.cs
* PubsubHeartbeatStream.cs
* SimpleNode.cs
* SortRow.cs
* SpatialArbStatsStream.cs
* SpatialFuturePairsStream.cs
* SpreadPerformanceEvaluator.cs
* SpreadPerformanceStream.cs
* StreamIOCommon.cs
* SuperJoinStream.cs
* TableMetaDataStream.cs
* TextWriterStream.cs
* TimeAverageStream.cs
2025-11-19 13:57:47 -05:00

132 lines
5.3 KiB
C#

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<SecurityKey, BookTuple>? _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<BookTuple, uint> 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<BookTuple> 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<BookTuple, uint> 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<SecurityKey, Dictionary<int, int>> _id_map = new Dictionary<SecurityKey, Dictionary<int, int>>();
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<DataPacket>(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();
}
}
}
}