feat (internal): Add price stats to md client

This commit is contained in:
John Colvin committed 2023-02-08 00:33:10 -05:00
1 parent d62f88691c
commit a0bd207d93
2 files changed
+109 -2

No files matched your search

@@ -8,6 +8,7 @@ namespace bts.md {
using mbts.spark;
using Spark.Api.MarketData;
using Spark.Core;
using System;
using System.Collections.Generic;
@@ -17,14 +18,23 @@ namespace bts.md {
public class MarketDataClient {
readonly MDSource _source;
readonly TimeSpan _poll_interval;
readonly TimeSpan _slow_poll_interval;
readonly uint _num_depth_levels;
public MarketDataClient(MDSource source)
: this(source, 5, TimeSpan.FromSeconds(0.1)) { }
: this(source, 5, TimeSpan.FromSeconds(0.1), TimeSpan.FromSeconds(1)) { }
public MarketDataClient(MDSource source, uint num_depth_levels, TimeSpan poll_interval) {
public MarketDataClient(
MDSource source,
uint num_depth_levels,
TimeSpan poll_interval,
TimeSpan slow_poll_interval) {
_source = source;
_poll_interval = poll_interval;
if (slow_poll_interval < poll_interval) {
Log.Api.warn("MarketDataClient 'slow_poll_interval' smaller than 'poll_interval'");
}
_slow_poll_interval = slow_poll_interval;
_num_depth_levels = num_depth_levels;
}
@@ -110,6 +120,41 @@ namespace bts.md {
}
}
public IObservable<TableUpdate<PriceStats, uint>> price_stats(
SecurityCriteria? criteria = null) {
// This MDSource's IObservable provides callbacks when a security is added or removed, but
// doesn't detect stats changes. To avoid maintaining complicated state, create a brand new
// IObservable of stats updates whenever the securities change.
return criteria.subscribe_md(_source.subscribe_securities())
.Select(fixed_sec_price_stats)
.FlattenTableSeries();
IObservable<TableUpdate<PriceStats, uint>> fixed_sec_price_stats(
IEnumerable<SecurityMD> security_mds) {
// Convert 'security_mds' to a list of PollablePriceStats.
var cached_statss = security_mds
.Select(s => new PollablePriceStats(s))
.ToList();
return Observable.Interval(_slow_poll_interval).Scan(
TableUpdate<PriceStats, uint>.mk_empty(get_sec_id),
poll_stats);
uint get_sec_id(PriceStats stats) =>
stats.security.Id;
TableUpdate<PriceStats, uint> poll_stats(TableUpdate<PriceStats, uint> table, long _) {
var bd = table.to_builder();
foreach (var cstats in cached_statss) {
if (cstats.poll_changes()) {
bd.add_or_update(cstats.to_stats());
}
}
return bd.to_table();
}
}
}
class PollableBook {
readonly SecurityMD _security_md;
readonly uint _num_depth_levels;
@@ -183,6 +228,27 @@ namespace bts.md {
}
}
class PollablePriceStats {
readonly SecurityMD _security_md;
PriceStats _stats;
public PollablePriceStats(SecurityMD security_md) =>
_security_md = security_md;
public PriceStats to_stats() =>
_stats;
public bool poll_changes() =>
Swap.toggle_to(ref _stats, _mk_stats());
PriceStats _mk_stats() {
var md = _security_md.md;
return new(
_security_md.security,
md.open_price,
md.close_price,
md.settlement_price);
}
}
}
}
@@ -0,0 +1,41 @@
#nullable enable
namespace Spark.Api.MarketData {
using bts.api;
using System;
/// <summary>
/// Price/trade statistics for a particular security (slow moving)
/// </summary>
public readonly struct PriceStats : IEquatable<PriceStats> {
public PriceStats(SecurityImmutable security, double open, double close, double settlement) {
this.security = security;
this.open = open;
this.close = close;
this.settlement = settlement;
}
public SecurityImmutable security { get; }
public double open { get; }
public double close { get; }
public double settlement { get; }
public bool Equals(PriceStats other) =>
security.Equals(other.security) &&
open.Equals(other.open) &&
close.Equals(other.close) &&
settlement.Equals(other.settlement);
public override bool Equals(object? obj) =>
obj is PriceStats other && Equals(other);
public override int GetHashCode() =>
HashCode.Combine(security.Id, open, close, settlement);
public static bool operator ==(PriceStats left, PriceStats right) =>
left.Equals(right);
public static bool operator !=(PriceStats left, PriceStats right) =>
!(left == right);
}
}