Files
spark/src/DataStreamSuite/SharedStreams/Business/MarketData/InstrumentsStream.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

127 lines
5.3 KiB
C#

namespace bts.BlueOrb {
using BCG.Utilities.General;
using bts;
using bts.api;
using bts.md;
using BCG;
using mbts.utils;
using Spark.Core;
using Spark.Core.Disposables;
using Spark.Core.Reactive;
using System;
using System.Collections.Generic;
using System.Reactive.Linq;
[DataStreamUserRespresentation("InstrumentsStream")]
public class InstrumentsStream : ViewStream, IWorker {
private readonly TimeSpan _poll_period = TimeSpan.FromMilliseconds(500);
private DateTime _last_poll_time = DateTime.MinValue;
private ViewStreamDatabase _vsd;
IReadOnlyDictionary<uint, SecurityImmutable>? _cached_state;
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("cpp", TypeCode.Double),
Tuple.Create("expiration", TypeCode.DateTime),
Tuple.Create("strike", TypeCode.Double),
Tuple.Create("callput", TypeCode.String)
);
IDisposable? subscription;
// TODO: add cons for symbol, exchange etc
public InstrumentsStream(MarketDataClient mdClient)
: this(mdClient, ViewStreamDatabase.instance) { }
public InstrumentsStream(MarketDataClient mdClient, ViewStreamDatabase vsd) {
bts.Log.Gen.info("InstrumentsStream.cons");
_vsd = vsd;
_md_client = mdClient;
}
protected override void myOnFirstSubscriber() {
Log.isnull(subscription);
_vsd.addWorker(this);
subscription = _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() {
Disposable.dispose(ref subscription);
_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) {
var 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().assert_value();
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, sec.ContractSpec.CashPerPoint, exp, strike, cp);
new_state.addRow(r);
}
DataPacket? dp;
if (myLastValue == null) {
dp = new DataPacket(new_state, new_state, new BlueTable(_instrument_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;
myFireEvent(dp);
}
public void doWork() {
if (_dirty && DateTime.Now - _last_poll_time > _poll_period) {
_last_poll_time = DateTime.Now;
_dirty = false;
_scan_instruments();
}
}
}
}