112 lines
4.2 KiB
C#
112 lines
4.2 KiB
C#
using bts;
|
|
using bts.api;
|
|
using bts.api.orderman;
|
|
using bts.utils;
|
|
|
|
using Spark.Api.Orders;
|
|
|
|
using System;
|
|
|
|
using static bts.api.orderman.Fill.Types;
|
|
|
|
namespace mbts.spark {
|
|
public sealed class FillTuple : IEquatable<FillTuple>, IJsonFormatter<FillTuple> {
|
|
public static readonly Func<FillTuple, string> key_fn = ft => ft.fill.FillKey;
|
|
public readonly FillImmutable fill;
|
|
public readonly SecurityImmutable? security;
|
|
|
|
OrderRecordTupleWithRoot? _order_tuple;
|
|
|
|
public FillTuple(
|
|
FillImmutable fill,
|
|
SecurityImmutable? security,
|
|
OrderRecordTupleWithRoot? order_tuple) {
|
|
this.fill = fill;
|
|
this.security = security;
|
|
_order_tuple = order_tuple;
|
|
}
|
|
|
|
public FillTuple((SecurityImmutable? sec, FillImmutable val) fill, OrderRecordTuple? order_tuple)
|
|
: this(fill.val, fill.sec, order_tuple.HasValue ? new(order_tuple.Value) : null) { }
|
|
|
|
public AlgoData? algo =>
|
|
_order_tuple.HasValue ? _order_tuple.Value.order.algo : (AlgoData?)null;
|
|
|
|
public AlgoDefinitionMetadata? algo_meta =>
|
|
_order_tuple.HasValue ? _order_tuple.Value.order.meta : null;
|
|
|
|
public OrderRecordImmutable? order =>
|
|
_order_tuple.HasValue ? _order_tuple.Value.order.order : (OrderRecordImmutable?)null;
|
|
|
|
public AlgoData? root_algo =>
|
|
_order_tuple.HasValue ? _order_tuple.Value.root.algo : (AlgoData?)null;
|
|
|
|
public AlgoDefinitionMetadata? root_algo_meta =>
|
|
_order_tuple.HasValue ? _order_tuple.Value.root.meta : null;
|
|
|
|
public OrderRecordTuple? root_order_security => _order_tuple?.root;
|
|
|
|
public AccountTuple account_tuple => new(account_id, account);
|
|
|
|
public string account {
|
|
get {
|
|
var account_string = string.Empty;
|
|
if (fill.FillKind != FillKind.Sod && order.HasValue && order.Value.HasDef) account_string = order.Value.Def.Account;
|
|
else account_string = fill.Account;
|
|
return account_string == null ? string.Empty : account_string;
|
|
}
|
|
}
|
|
|
|
public uint account_id =>
|
|
fill.FillKind != FillKind.Sod && order.HasValue ?
|
|
order.Value.account_id() :
|
|
fill.InternalAccountId;
|
|
|
|
public TimeInForce time_in_force =>
|
|
order?.time_in_force() ?? TimeInForce.Undefined;
|
|
|
|
public bool Equals(FillTuple other) =>
|
|
other != null &&
|
|
_order_tuple.Equals(other._order_tuple) &&
|
|
security.HasValue == other.security.HasValue &&
|
|
fill.Equals(other.fill);
|
|
|
|
public override bool Equals(object obj) =>
|
|
Equals(obj as FillTuple);
|
|
|
|
public override int GetHashCode() =>
|
|
EqualityComparer.aggregate_hash(
|
|
fill.FillKey.GetHashCode(),
|
|
order.HasValue ? order.Value.Key.GetHashCode() : 0,
|
|
security.HasValue ? security.Value.Id.GetHashCode() : 0);
|
|
public static Object ToJsonObj(FillTuple obj) => new {
|
|
fill_avg_price = obj.fill.AvgPrice,
|
|
security = obj.security.ToString(),
|
|
order = obj._order_tuple != null ? OrderRecordTupleWithRoot.ToJsonObj((OrderRecordTupleWithRoot)obj._order_tuple) : ""
|
|
};
|
|
}
|
|
|
|
static class FillTupleExtensions {
|
|
public static DateTime fill_time(this FillTuple fill_tuple) =>
|
|
fill_tuple.fill.Timestamp.ToDateTimeOffset().LocalDateTime;
|
|
|
|
public static TimeInForce time_in_force(this FillTuple fill_tuple) =>
|
|
fill_tuple.order.HasValue ? fill_tuple.order.Value.time_in_force() : TimeInForce.Undefined;
|
|
|
|
public static CustomOrderDefinition.CustomOneofCase order_type(this FillTuple fill_tuple) =>
|
|
fill_tuple.order.HasValue ? fill_tuple.order.Value.order_type() : CustomOrderDefinition.CustomOneofCase.None;
|
|
|
|
public static Venue exchange(this FillTuple fill_tuple) =>
|
|
fill_tuple.security.HasValue ? fill_tuple.security.Value.Exchange : Venue.Unset;
|
|
|
|
public static uint order_key(this FillTuple fill_tuple) =>
|
|
fill_tuple.order.HasValue ? fill_tuple.order.Value.Key : 0u;
|
|
|
|
public static string user(this FillTuple fill_tuple) =>
|
|
fill_tuple.order.HasValue ? fill_tuple.order.Value.user() : string.Empty;
|
|
|
|
public static uint user_id(this FillTuple fill_tuple) =>
|
|
fill_tuple.order?.user_id() ?? 0;
|
|
}
|
|
}
|