refactor: Remove hardcoded csv path from ReplayFutureLiveBBOStream
Add constructor overload to accept an input ViewStream
This commit is contained in:
1 parent
fb15b3fb24
commit
e7625341e3
3 files changed
+48
-15
No files matched your search
@@ -211,6 +211,12 @@ namespace BCG {
|
||||
return tsNanos.HasValue ? DateTime.UnixEpoch.Add(new TimeSpan(tsNanos.Value / 100)) : null;
|
||||
}
|
||||
|
||||
public static DateTime? GetDateTime(this IBlueRow row, string colName) =>
|
||||
GetDateTime(row[colName]);
|
||||
|
||||
public static DateTime? GetDateTime(this IBlueRow row, int colNo) =>
|
||||
GetDateTime(row[colNo]);
|
||||
|
||||
public static DateTime? GetDateTime(object obj) {
|
||||
var res = _get_datetime_value(obj, DateTime.MinValue);
|
||||
if (res == DateTime.MinValue) {
|
||||
|
||||
@@ -107,10 +107,13 @@ public class BlueTableImmutable : IBlueRowCollection, IImmutableRowCollection {
|
||||
}
|
||||
}
|
||||
|
||||
public DataPacket ToDataPacket(bool reset = true) {
|
||||
public DataPacket ToDataPacket(bool reset = true) =>
|
||||
ToDataPacket(out _, reset);
|
||||
|
||||
public DataPacket ToDataPacket(out BlueTableImmutable newRows, bool reset = true) {
|
||||
table.RemoveRange(removed);
|
||||
table.AddRange(added);
|
||||
var newRows = new BlueTableImmutable(schema, table.ToImmutable());
|
||||
newRows = new BlueTableImmutable(schema, table.ToImmutable());
|
||||
var addedRows = new BlueTableImmutable(schema, added.ToImmutable());
|
||||
var removedRows = new BlueTableImmutable(schema, removed.ToImmutable());
|
||||
if (reset) {
|
||||
@@ -365,10 +368,13 @@ public class BlueTableImmutableAdapter<T> : IBlueRowCollection<BlueRowAdapter<T>
|
||||
}
|
||||
}
|
||||
|
||||
public IDataPacket<BlueRowAdapter<T>> ToDataPacket(bool reset = true) {
|
||||
public IDataPacket<BlueRowAdapter<T>> ToDataPacket(bool reset = true) =>
|
||||
ToDataPacket(out _, reset);
|
||||
|
||||
public IDataPacket<BlueRowAdapter<T>> ToDataPacket(out BlueTableImmutableAdapter<T> newRows, bool reset = true) {
|
||||
table.RemoveRange(removed);
|
||||
table.AddRange(added);
|
||||
var newRows = new BlueTableImmutableAdapter<T>(schemaAdapter, table.ToImmutable());
|
||||
newRows = new BlueTableImmutableAdapter<T>(schemaAdapter, table.ToImmutable());
|
||||
var addedRows = new BlueTableImmutableAdapter<T>(schemaAdapter, added.ToImmutable());
|
||||
var removedRows = new BlueTableImmutableAdapter<T>(schemaAdapter, removed.ToImmutable());
|
||||
if (reset) {
|
||||
|
||||
+32
-11
@@ -2,11 +2,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using BCG;
|
||||
|
||||
using Bts.DataStream;
|
||||
|
||||
using Spark.Core.Disposables;
|
||||
|
||||
[DataStreamUserRespresentation("ReplayFutureLiveBBOStream")]
|
||||
public class ReplayFutureLiveBBOStream : ViewStream, BCG.Utilities.General.IWorker{
|
||||
private class LastFireTime {
|
||||
@@ -20,9 +22,10 @@
|
||||
|
||||
|
||||
private readonly ViewStreamDatabase _vsd;
|
||||
private readonly ContractUtils.FutureContinousContract _fcc;
|
||||
private readonly LastFireTime _last_fire_time = new LastFireTime();
|
||||
readonly ViewStream source;
|
||||
private RowsWithPointer _rows = new RowsWithPointer();
|
||||
IDisposable? sourceSub;
|
||||
|
||||
public ReplayFutureLiveBBOStream(string continous_contract) {
|
||||
Func<string> iso_8601_now = () => {
|
||||
@@ -51,29 +54,47 @@
|
||||
}
|
||||
|
||||
private ReplayFutureLiveBBOStream(string continous_contract, bool noop)
|
||||
: this(ViewStreamDatabase.instance, new ContractUtils.FutureContract(continous_contract))
|
||||
{}
|
||||
: this(ViewStreamDatabase.instance, new ContractUtils.FutureContract(continous_contract)) { }
|
||||
|
||||
public ReplayFutureLiveBBOStream(ViewStreamDatabase vsd, ContractUtils.FutureContract fc) {
|
||||
var fcc = fc.continous_constract;
|
||||
Log.assert(fcc != null);
|
||||
Log.notnull(fcc);
|
||||
_vsd = vsd;
|
||||
_fcc = fcc;
|
||||
source = new CSVReaderStream($"{SQLiteHelper.location}\\mbp1_{fcc.symbol}_small.csv", false);
|
||||
}
|
||||
|
||||
public ReplayFutureLiveBBOStream(ViewStreamDatabase vsd, ViewStream source) {
|
||||
_vsd = vsd;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
protected override void myOnFirstSubscriber() {
|
||||
var file_path = Path.Combine(SQLiteHelper.location, $"mbp1_{_fcc.symbol}_small.csv".ToLower());
|
||||
var csv_rd = new CSVReaderStream(file_path, false);
|
||||
_rows.rows = csv_rd.rows.to_list().OrderBy(r => r.get<string>("ts_event")).ToList();
|
||||
_rows.pointer = 0;
|
||||
_vsd.addWorker(this);
|
||||
source.onChange += OnSourceChanged;
|
||||
sourceSub = Disposable.Create(() => source.onChange -= OnSourceChanged);
|
||||
SyncRows();
|
||||
|
||||
void SyncRows() {
|
||||
if (source.rows is null) {
|
||||
_rows.rows = new List<IBlueRow>(0);
|
||||
return;
|
||||
}
|
||||
_rows.rows = source.rows.Cast<IBlueRow>().OrderBy(r => r.GetDateTime("ts_event")).ToList();
|
||||
}
|
||||
|
||||
void OnSourceChanged(object obj) =>
|
||||
SyncRows();
|
||||
}
|
||||
|
||||
protected override void myOnLastSubscriber() {
|
||||
Disposable.dispose(ref sourceSub);
|
||||
_vsd.removeWorker(this);
|
||||
}
|
||||
|
||||
public void doWork() {
|
||||
if (_rows.rows is null || _rows.rows.Count == 0) {
|
||||
return;
|
||||
}
|
||||
IBlueRow cand_row = _rows.rows[_rows.pointer];
|
||||
string cand_ts_event = cand_row.get<string>("ts_event");
|
||||
DateTime cand_ts_event_dt = DateTime.Parse(cand_ts_event, null, System.Globalization.DateTimeStyles.RoundtripKind);
|
||||
|
||||
Reference in new issue
Block a user