chore: Add BookDelta class

This commit is contained in:
John Colvin committed 2025-11-15 12:46:24 -05:00
1 parent befa17d29d
commit ad0e9fa3c9
1 file changed
+64
@@ -6,8 +6,69 @@ namespace Spark.Api.MarketData {
using bts.utils;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
public enum BookDeltaFlags {
None,
BidPriceIncrease,
BidQuantityIncrease,
AskPriceIncrease,
AskQuantityIncrease,
}
public readonly struct BookDelta : IEquatable<BookDelta> {
readonly Book left;
readonly Book right;
public BookDelta(Book left, Book right) : this() {
this.left = left;
this.right = right;
}
public double BidPriceChange =>
left.bids.Count == 0 || right.bids.Count == 0
? double.NaN
: right.bids[0].price - left.bids[0].price;
public double AskPriceChange =>
left.asks.Count == 0 || right.asks.Count == 0
? double.NaN
: right.asks[0].price - left.asks[0].price;
public double BidQuantityChange =>
left.bids.Count == 0 || right.bids.Count == 0
? double.NaN
: right.bids[0].size - left.bids[0].size;
public double AskQuantityChange =>
left.asks.Count == 0 || right.asks.Count == 0
? double.NaN
: right.asks[0].size - left.asks[0].size;
public bool Equals(BookDelta other) =>
BidPriceChange.Equals(other.BidPriceChange)
&& AskPriceChange.Equals(other.AskPriceChange)
&& BidQuantityChange.Equals(other.BidQuantityChange)
&& AskQuantityChange.Equals(other.AskQuantityChange);
public override bool Equals([NotNullWhen(true)] object? obj) =>
obj is BookDelta other && Equals(other);
public override int GetHashCode() =>
HashCode.Combine(
MathUtils.epsilon_eq_comparer.GetHashCode(BidPriceChange),
MathUtils.epsilon_eq_comparer.GetHashCode(BidQuantityChange),
MathUtils.epsilon_eq_comparer.GetHashCode(AskPriceChange),
MathUtils.epsilon_eq_comparer.GetHashCode(AskQuantityChange));
public static bool operator ==(BookDelta left, BookDelta right) =>
left.Equals(right);
public static bool operator !=(BookDelta left, BookDelta right) =>
!left.Equals(right);
}
public readonly struct BookTuple : IComparable<BookTuple>, IComparable, IEquatable<BookTuple>, IJsonFormatter<BookTuple> {
readonly TickDisplaySettingsImmutable? _quantity_display;
@@ -66,6 +127,9 @@ namespace Spark.Api.MarketData {
public static bool operator !=(BookTuple left, BookTuple right) =>
!(left == right);
public static BookDelta operator -(BookTuple left, BookTuple right) =>
new(left.book, right.book);
}
public static class BookTupleExtensions {