diff --git a/src/client/spark_api/MarketData/BookTuple.cs b/src/client/spark_api/MarketData/BookTuple.cs index b88011e29..f7ebca4a8 100644 --- a/src/client/spark_api/MarketData/BookTuple.cs +++ b/src/client/spark_api/MarketData/BookTuple.cs @@ -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 { + 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, IComparable, IEquatable, IJsonFormatter { 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 {