Files
spark/src/DataStreamSuite/SharedStreams/Business/MarketData/InstrumentsStream.cs
T
2025-05-18 01:10:33 -04:00

123 lines
5.1 KiB
C#

namespace bts.SilverPine {
using BCG.Utilities.General;
using bts.api;
using bts.md;
using BCG;
using Spark.Core.Reactive;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using SecurityKey = System.UInt32;
[DataStreamUserRespresentation("instruments")]
public class InstrumentsStream : 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<uint, SecurityImmutable> _cached_state = null;
MarketDataClient _md_client;
private bool _dirty = false;
private readonly IBlueTableSchema _instrument_schema = BlueDataUtility.form_schema("instruments",
Tuple.Create("id", TypeCode.Int32),
Tuple.Create("sec_key", TypeCode.UInt32),
Tuple.Create("sec", TypeCode.String),
Tuple.Create("sec_type", TypeCode.String),
Tuple.Create("venue", TypeCode.String),
Tuple.Create("symbol", TypeCode.String),
Tuple.Create("currency", TypeCode.String),
Tuple.Create("expiration", TypeCode.DateTime),
Tuple.Create("strike", TypeCode.Double),
Tuple.Create("callput", TypeCode.String)
);
// TODO: add cons for symbol, exchange etc
public InstrumentsStream()
: this(ViewStreamDatabase.instance) {
}
public InstrumentsStream(ViewStreamDatabase vsd) {
bts.Log.Gen.info("InstrumentsStream.cons");
_vsd = vsd;
_md_client = new MarketDataClient(new MDSource(TimeSpan.FromSeconds(0.5)));
}
protected override void myOnFirstSubscriber() {
_vsd.addWorker(this);
_md_client.securities().Subscribe(_on_instruments);
}
private void _log_updates(IReadOnlyDictionary<uint, SecurityImmutable> state) {
foreach (SecurityImmutable security in state.Values) {
bts.Log.Gen.info($"InstrumentsStream sec: {security.ToString()}");
}
}
private void _on_instruments(IReadOnlyDictionary<uint, SecurityImmutable> state) {
_dirty = true;
_log_updates(state);
// just cacching in here, will populate the rows in _poll_scan and fire the datapacket
bts.Log.Gen.info($"InstrumentsStream._on_instruments state: {state.Count()}");
_cached_state = state;
}
protected override void myOnLastSubscriber() {
_vsd.removeWorker(this);
}
private void _scan_instruments() {
// this shouldn be made incremental using the TableUpdate
var count = _cached_state == null ? 0 : _cached_state.Count;
bts.Log.Gen.info($"InstrumentsStream._scan_instruments _cached_state: {count}");
if (_cached_state == null)
return;
BlueTable new_state = new BlueTable(_instrument_schema);
foreach (var kv in _cached_state) {
SecurityKey skey = kv.Key;
int id = Convert.ToInt32(skey);
SecurityImmutable sec = kv.Value;
bts.api.SecurityType sec_type = sec.security_type();
object exp = DBNull.Value;
if (sec_type == bts.api.SecurityType.Future || sec_type == bts.api.SecurityType.Option)
exp = sec.security_expiration();
object strike = DBNull.Value;
if (sec_type == bts.api.SecurityType.Option)
strike = sec.strike();
object cp = DBNull.Value;
if (sec_type == bts.api.SecurityType.Option)
cp = sec.Option.CallPut.ToString();
IBlueRow r = new BlueRow(_instrument_schema, id, skey, sec.security_desc(), sec.security_type().ToString(), sec.display_exchange().ToString(), sec.Symbol, sec.Currency, exp, strike, cp);
new_state.addRow(r);
}
DataPacket dp = null;
if (myLastValue == null) {
dp = new DataPacket(new_state, new_state, new BlueTable(_instrument_schema));
} 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_instruments();
}
}
}
}