Skip to content

Commit 4a0fd0d

Browse files
committed
Fix Hyperliquid order handling
1 parent 0162c6c commit 4a0fd0d

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<PropertyGroup>
66
<LangVersion>latest</LangVersion>
7-
<Version>2.13.2</Version>
7+
<Version>2.13.3</Version>
88
</PropertyGroup>
99

1010
</Project>

src/Crypto.Websocket.Extensions/Orders/Sources/HyperliquidOrderSource.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System;
1111
using System.Collections.Generic;
1212
using System.Linq;
13+
using Crypto.Websocket.Extensions.Core.Orders;
1314

1415
namespace Crypto.Websocket.Extensions.Orders.Sources
1516
{
@@ -18,6 +19,7 @@ namespace Crypto.Websocket.Extensions.Orders.Sources
1819
/// </summary>
1920
public class HyperliquidOrderSource : OrderSourceBase
2021
{
22+
private readonly CryptoOrderCollection _partiallyFilledOrders = new();
2123
private HyperliquidWebsocketClient _client = null!;
2224
private IDisposable? _subscription;
2325
private IDisposable? _subscriptionFills;
@@ -132,7 +134,9 @@ public CryptoOrder ConvertOrder(UserOrderResponse response)
132134
var order = response.Order;
133135

134136
var id = order.OrderId.ToString();
135-
var existing = ExistingOrders.GetValueOrDefault(id);
137+
var existingCurrent = ExistingOrders.GetValueOrDefault(id);
138+
var existingPartial = _partiallyFilledOrders.GetValueOrDefault(id);
139+
var existing = existingPartial ?? existingCurrent;
136140

137141
var price = Math.Abs(FirstNonZero(order.LimitPrice, existing?.Price) ?? 0);
138142

@@ -166,13 +170,14 @@ public CryptoOrder ConvertOrder(UserOrderResponse response)
166170
OnMargin = existing?.OnMargin ?? true
167171
};
168172

169-
if (newOrder.OrderStatus == CryptoOrderStatus.Canceled)
173+
if (currentStatus == CryptoOrderStatus.PartiallyFilled)
170174
{
171-
ExistingOrders.TryRemove(newOrder.Id, out _);
175+
// save partially filled orders
176+
_partiallyFilledOrders[newOrder.Id] = newOrder;
172177
}
173178
else
174179
{
175-
ExistingOrders[newOrder.Id] = newOrder;
180+
_partiallyFilledOrders.TryRemove(newOrder.Id, out _);
176181
}
177182

178183
return newOrder;
@@ -181,7 +186,14 @@ public CryptoOrder ConvertOrder(UserOrderResponse response)
181186
private CryptoOrder ConvertFill(Fill fill)
182187
{
183188
var id = fill.OrderId.ToString();
184-
var existing = ExistingOrders.GetValueOrDefault(id);
189+
var existingCurrent = ExistingOrders.GetValueOrDefault(id);
190+
var existingPartial = _partiallyFilledOrders.GetValueOrDefault(id);
191+
var existing = existingPartial ?? existingCurrent;
192+
193+
if (existingCurrent?.OrderStatus == CryptoOrderStatus.Executed)
194+
return existingCurrent;
195+
if (existingPartial?.OrderStatus == CryptoOrderStatus.Executed)
196+
return existingPartial;
185197

186198
var price = Math.Abs(FirstNonZero(fill.Price, existing?.Price) ?? 0);
187199

@@ -214,14 +226,14 @@ private CryptoOrder ConvertFill(Fill fill)
214226
OnMargin = existing?.OnMargin ?? true
215227
};
216228

217-
if (currentStatus == CryptoOrderStatus.Executed)
229+
if (currentStatus == CryptoOrderStatus.PartiallyFilled)
218230
{
219-
ExistingOrders.TryRemove(newOrder.Id, out _);
231+
// save partially filled orders
232+
_partiallyFilledOrders[newOrder.Id] = newOrder;
220233
}
221234
else
222235
{
223-
// save partially filled orders
224-
ExistingOrders[newOrder.Id] = newOrder;
236+
_partiallyFilledOrders.TryRemove(newOrder.Id, out _);
225237
}
226238

227239
return newOrder;

0 commit comments

Comments
 (0)