121 lines
4.1 KiB
C#
121 lines
4.1 KiB
C#
using bts;
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace mbts.co2 {
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public readonly struct NativePriceLevel {
|
|
readonly IntPrice _bid;
|
|
readonly IntPrice _ask;
|
|
readonly uint _bid_size;
|
|
readonly uint _ask_size;
|
|
public NativePriceLevel(IntPrice bid, uint bid_size, IntPrice ask, uint ask_size) {
|
|
_bid = bid;
|
|
_bid_size = bid_size;
|
|
_ask = ask;
|
|
_ask_size = ask_size;
|
|
}
|
|
|
|
public NativePriceSize bid => new NativePriceSize(_bid, _bid_size);
|
|
public NativePriceSize ask => new NativePriceSize(_ask, _ask_size);
|
|
|
|
public void Deconstruct(out NativePriceSize bid, out NativePriceSize ask) {
|
|
bid = this.bid;
|
|
ask = this.ask;
|
|
}
|
|
|
|
internal static NativePriceLevel empty => new NativePriceLevel(IntPrice.NaN, 0, IntPrice.NaN, 0);
|
|
public override string ToString() {
|
|
return $"b [{bid.price} {bid.size}] a [{ask.price} {ask.size}]";
|
|
}
|
|
}
|
|
public readonly struct ImpliedPriceLevel {
|
|
readonly IntPrice _bid;
|
|
readonly IntPrice _ask;
|
|
readonly uint _bid_size;
|
|
readonly uint _ask_size;
|
|
public ImpliedPriceLevel(IntPrice bid, uint bid_size, IntPrice ask, uint ask_size) {
|
|
_bid = bid;
|
|
_bid_size = bid_size;
|
|
_ask = ask;
|
|
_ask_size = ask_size;
|
|
}
|
|
public NativePriceSize bid => new NativePriceSize(_bid, 0, _bid_size);
|
|
public NativePriceSize ask => new NativePriceSize(_ask, 0, _ask_size);
|
|
public void Deconstruct(out NativePriceSize bid, out NativePriceSize ask) {
|
|
bid = this.bid;
|
|
ask = this.ask;
|
|
}
|
|
}
|
|
|
|
public readonly struct NativePriceSize : IEquatable<NativePriceSize>
|
|
{
|
|
readonly IntPrice _price;
|
|
readonly uint _size;
|
|
readonly uint _direct_size;
|
|
|
|
static NativePriceSize() {
|
|
Log.assert(Marshal.SizeOf<NativePriceSize>() == 16);
|
|
}
|
|
|
|
public NativePriceSize(IntPrice price, uint size) {
|
|
_price = price;
|
|
_size = size;
|
|
_direct_size = size;
|
|
}
|
|
public NativePriceSize(long mantissa, sbyte exponent, uint size) {
|
|
_price = new IntPrice(mantissa, exponent);
|
|
_size = size;
|
|
_direct_size = size;
|
|
}
|
|
public NativePriceSize(IntPrice price, uint direct_size, uint implied_size) {
|
|
_price = price;
|
|
_size = direct_size + implied_size;
|
|
_direct_size = direct_size;
|
|
}
|
|
|
|
public static NativePriceSize NO_MARKET = new NativePriceSize(IntPrice.NaN, 0);
|
|
|
|
public IntPrice int_price => _price;
|
|
public long mantissa => _price.mantissa;
|
|
public sbyte exponent => _price.exponent;
|
|
public uint size => _size;
|
|
public uint direct_size => _direct_size;
|
|
public uint implied_size => _size - _direct_size;
|
|
public double price => _price.price();
|
|
public bool is_nan => _price.is_nan;
|
|
|
|
|
|
public override string ToString() {
|
|
return "{0} {1} ({2})".fmt(price, size, implied_size);
|
|
}
|
|
|
|
public override bool Equals(object obj) {
|
|
if (obj is NativePriceSize p) return Equals(p);
|
|
return base.Equals(obj);
|
|
}
|
|
|
|
public override int GetHashCode() {
|
|
return bts.utils.EqualityComparer.aggregate_hash(_price.GetHashCode(), size.GetHashCode(), _direct_size.GetHashCode());
|
|
}
|
|
|
|
public bool Equals(NativePriceSize other) =>
|
|
size == other.size &&
|
|
_direct_size == other._direct_size &&
|
|
_price.Equals(other._price);
|
|
|
|
public static bool operator ==(NativePriceSize a, NativePriceSize b) {
|
|
return a.Equals(b);
|
|
}
|
|
public static bool operator !=(NativePriceSize a, NativePriceSize b) {
|
|
return !a.Equals(b);
|
|
}
|
|
}
|
|
|
|
static class NativePriceSizeExtensions {
|
|
public static NativePriceSize with(this NativePriceSize nps, uint? direct_size = null, uint? implied_size = null) =>
|
|
new NativePriceSize(nps.int_price, direct_size ?? nps.direct_size, implied_size ?? nps.implied_size);
|
|
}
|
|
}
|