Files
spark/src/DataStreamSuite/SharedStreams/Business/MarketData/PriceStatsStream.cs
T
2026-01-13 13:57:47 -05:00

109 lines
4.6 KiB
C#

namespace BCG {
using BCG.Utilities.General;
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;
// TODO: Use dependency injection to instantiate this stream, and others like it that depend on
// IMarketDataClient. Until then, hide it from DSQA.
[DataStreamUserRespresentation(/*"price_stats"*/ false)]
public class PriceStatsStream : ViewStream, IWorker {
private readonly TimeSpan _poll_period = TimeSpan.FromMilliseconds(500);
private DateTime _last_poll_time = DateTime.MinValue;
private ViewStreamDatabase _vsd;
IReadOnlyDictionary<SecurityKey, PriceStats>? _cached_state = null;
IMarketDataClient _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.UInt32),
Tuple.Create("sec", TypeCode.String),
Tuple.Create("date", TypeCode.DateTime),
Tuple.Create("open", TypeCode.Double),
Tuple.Create("close", TypeCode.Double),
Tuple.Create("settlement", TypeCode.Double));
IDisposable? subscription;
// TODO: add cons for symbol, exchange etc
public PriceStatsStream(IMarketDataClient mdClient, ViewStreamDatabase vsd) {
bts.Log.Gen.info("PriceStatsStream.cons");
_vsd = vsd;
_md_client = mdClient;
}
protected override void myOnFirstSubscriber() {
_vsd.addWorker(this);
subscription = _md_client.PriceStats().Subscribe(_on_price_stats);
}
private void _log_updates(TableUpdate<PriceStats, uint> 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<BookTuple> 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<PriceStats, uint> 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.Index.Count()} changes: {update.Changes.Count()} added: {update.AddedItems.Count()}");
_cached_state = update.Index;
_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;
var today = DateTime.Now.Date;
IBlueRow r = new BlueRow(_pstats_scheam, id, skey, pstats.Security.ToString(), today, pstats.Open, pstats.Close, pstats.Settlement);
new_state.addRow(r);
}
DataPacket dp;
if (myLastValue == null) {
dp = new DataPacket(new_state, new_state, new BlueTable(_pstats_scheam));
} else {
var old_dp = (IDataPacket)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;
myFireEvent(dp);
}
public void doWork() {
if (_dirty && DateTime.Now - _last_poll_time > _poll_period) {
_last_poll_time = DateTime.Now;
_dirty = false;
_scan();
}
}
}
}