Skip to content

Commit

Permalink
More accurate TIF filtering based on snapshot timestamp instead of cu…
Browse files Browse the repository at this point in the history
…rrent time. (#80)

* More accurate TIF filtering based on snapshot timestamp instead of current time.

* Fixed tests and bumped version
  • Loading branch information
Crusher authored Mar 5, 2022
1 parent b6c708f commit 3c93a50
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion SharedBuildProperties.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Product>Solnet.Mango</Product>
<Version>6.0.3.3</Version>
<Version>6.0.3.4</Version>
<Copyright>Copyright 2021 &#169; blockmountain</Copyright>
<Authors>blockmountain</Authors>
<PublisherName>blockmountain</PublisherName>
Expand Down
2 changes: 1 addition & 1 deletion Solnet.Mango.Test/MangoClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public void GetOrderBookSideWithExpired()
Assert.IsTrue(obs.ParsedResult.Metadata.IsInitialized);
Assert.AreEqual(DataType.Bids, obs.ParsedResult.Metadata.DataType);
var bids = obs.ParsedResult.GetOrders();
Assert.AreEqual(11, bids.Count); // there's 11 orders without expiry
Assert.AreEqual(14, bids.Count); // there's 14 orders without expiry
var bidsWithExpired = obs.ParsedResult.GetOrders(true);
Assert.AreEqual(16, bidsWithExpired.Count); // there's 16 orders total including expired
}
Expand Down
7 changes: 3 additions & 4 deletions Solnet.Mango/Models/Matching/LeafNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,13 @@ internal static class ExtraLayout
/// <remarks>
/// This is checked by the order's <see cref="TimeInForce"/> value.
/// If it is equal to<see cref="byte.MinValue"/> the order never expires,
/// otherwise the order expires after <see cref="Timestamp"/> plus <see cref="byte.MaxValue"/> seconds.</remarks>
/// otherwise the order expires at <see cref="Timestamp"/> plus <see cref="byte.MaxValue"/> seconds.</remarks>
/// </summary>
/// <returns>true if it is valid, otherwise false.</returns>
public bool IsValid()
public bool IsValid(ulong timestamp)
{
var expiry = Timestamp + TimeInForce;
var now = (ulong) DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
return TimeInForce == 0 || now <= expiry;
return TimeInForce == 0 || timestamp < expiry;
}

/// <summary>
Expand Down
40 changes: 19 additions & 21 deletions Solnet.Mango/Models/Matching/OrderBookSide.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,33 @@ internal static class Layout
/// <returns>A list of open orders on the book side.</returns>
public List<OpenOrder> GetOrders(bool includeExpired = false)
{
bool isBids = Metadata.DataType == DataType.Bids;

_orders = new List<OpenOrder>();

foreach (var node in Nodes)
var leafNodes = Nodes.Where(n => n is LeafNode).Cast<LeafNode>().ToList();
ulong timestamp = leafNodes.Select(n => n.Timestamp).Max();

foreach (LeafNode leafNode in leafNodes)
{
if (node is LeafNode leafNode)
var valid = leafNode.IsValid(timestamp);
if (valid || includeExpired)
{
var valid = leafNode.IsValid();
if (valid || includeExpired)
_orders.Add(new OpenOrder
{
_orders.Add(new OpenOrder
{
RawPrice = leafNode.Price,
RawQuantity = leafNode.Quantity,
ClientOrderId = leafNode.ClientOrderId,
Owner = leafNode.Owner,
OrderIndex = leafNode.OwnerSlot,
OrderId = new BigInteger(leafNode.Key),
Timestamp = leafNode.Timestamp,
ExpiryTimestamp = leafNode.TimeInForce != 0 ? leafNode.Timestamp + leafNode.TimeInForce : ulong.MaxValue,
TimeInForce = leafNode.TimeInForce,
OrderType = leafNode.OrderType,
});
}
RawPrice = leafNode.Price,
RawQuantity = leafNode.Quantity,
ClientOrderId = leafNode.ClientOrderId,
Owner = leafNode.Owner,
OrderIndex = leafNode.OwnerSlot,
OrderId = new BigInteger(leafNode.Key),
Timestamp = leafNode.Timestamp,
ExpiryTimestamp = leafNode.TimeInForce != 0 ? leafNode.Timestamp + leafNode.TimeInForce : ulong.MaxValue,
TimeInForce = leafNode.TimeInForce,
OrderType = leafNode.OrderType,
});
}
}

if (!isBids)
if (Metadata.DataType != DataType.Bids)
{
_orders.Sort(Comparer<OpenOrder>.Create((order, order1) => order.RawPrice.CompareTo(order1.RawPrice)));
}
Expand Down

0 comments on commit 3c93a50

Please sign in to comment.