Skip to content

Commit

Permalink
Implement Binance order source
Browse files Browse the repository at this point in the history
  • Loading branch information
Marfusios committed Feb 20, 2024
1 parent fce208c commit b06b12a
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- Project defaults -->

<PropertyGroup>
<Version>2.7.0</Version>
<Version>2.7.1</Version>
</PropertyGroup>

</Project>
27 changes: 13 additions & 14 deletions src/Crypto.Websocket.Extensions.Core/Orders/CryptoOrders.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
Expand Down Expand Up @@ -31,7 +32,7 @@ public class CryptoOrders : ICryptoOrders
/// <param name="source">Orders source</param>
/// <param name="orderPrefix">Select prefix if you want to distinguish orders</param>
/// <param name="targetPair">Select target pair, if you want to filter monitored orders</param>
public CryptoOrders(IOrderSource source, long? orderPrefix = null, string targetPair = null)
public CryptoOrders(IOrderSource source, long? orderPrefix = null, string? targetPair = null)
{
CryptoValidations.ValidateInput(source, nameof(source));

Expand Down Expand Up @@ -82,17 +83,17 @@ public CryptoOrders(IOrderSource source, long? orderPrefix = null, string target
/// <summary>
/// Originally provided target pair for this orders data
/// </summary>
public string TargetPairOriginal { get; private set; }
public string? TargetPairOriginal { get; private set; }

/// <summary>
/// Last executed (or partially filled) buy order
/// </summary>
public CryptoOrder LastExecutedBuyOrder { get; private set; }
public CryptoOrder? LastExecutedBuyOrder { get; private set; }

/// <summary>
/// Last executed (or partially filled) sell order
/// </summary>
public CryptoOrder LastExecutedSellOrder { get; private set; }
public CryptoOrder? LastExecutedSellOrder { get; private set; }


/// <summary>
Expand Down Expand Up @@ -145,7 +146,7 @@ public CryptoOrderCollectionReadonly GetAllOrders()
/// <summary>
/// Find active order by provided unique id
/// </summary>
public CryptoOrder FindActiveOrder(string id)
public CryptoOrder? FindActiveOrder(string id)
{
if (GetActiveOrders().ContainsKey(id))
return _idToOrder[id];
Expand All @@ -155,11 +156,9 @@ public CryptoOrder FindActiveOrder(string id)
/// <summary>
/// Find order by provided unique id
/// </summary>
public CryptoOrder FindOrder(string id)
public CryptoOrder? FindOrder(string id)
{
if (_idToOrder.ContainsKey(id))
return _idToOrder[id];
return null;
return _idToOrder.GetValueOrDefault(id);
}

/// <summary>
Expand Down Expand Up @@ -191,7 +190,7 @@ public bool IsOurOrder(CryptoOrder order)
/// <summary>
/// Returns true if client id matches prefix
/// </summary>
public bool IsOurOrder(string clientId)
public bool IsOurOrder(string? clientId)
{
if (string.IsNullOrWhiteSpace(ClientIdPrefixString))
return true;
Expand Down Expand Up @@ -235,7 +234,7 @@ private void Subscribe()
_source.OrderUpdatedStream.Subscribe(OnOrderUpdated);
}

private void OnOrdersUpdated(CryptoOrder[] orders)
private void OnOrdersUpdated(CryptoOrder[]? orders)
{
if (orders == null)
return;
Expand All @@ -246,15 +245,15 @@ private void OnOrdersUpdated(CryptoOrder[] orders)
}
}

private void OnOrderCreated(CryptoOrder order)
private void OnOrderCreated(CryptoOrder? order)
{
if (order == null)
return;

HandleOrderUpdated(order);
}

private void OnOrderUpdated(CryptoOrder order)
private void OnOrderUpdated(CryptoOrder? order)
{
if (order == null)
return;
Expand Down Expand Up @@ -291,7 +290,7 @@ private void HandleOrderUpdated(CryptoOrder order)
_ourOrderChanged.OnNext(order);
}

private bool IsFilteredOut(CryptoOrder order)
private bool IsFilteredOut(CryptoOrder? order)
{
if (order == null)
return true;
Expand Down
14 changes: 7 additions & 7 deletions src/Crypto.Websocket.Extensions.Core/Orders/ICryptoOrders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public interface ICryptoOrders
/// <summary>
/// Originally provided target pair for this orders data
/// </summary>
string TargetPairOriginal { get; }
string? TargetPairOriginal { get; }

/// <summary>
/// Last executed (or partially filled) buy order
/// </summary>
CryptoOrder LastExecutedBuyOrder { get; }
CryptoOrder? LastExecutedBuyOrder { get; }

/// <summary>
/// Last executed (or partially filled) sell order
/// </summary>
CryptoOrder LastExecutedSellOrder { get; }
CryptoOrder? LastExecutedSellOrder { get; }

/// <summary>
/// Generate a new client id (with prefix)
Expand All @@ -80,22 +80,22 @@ public interface ICryptoOrders
/// <summary>
/// Find active order by provided unique id
/// </summary>
CryptoOrder FindActiveOrder(string id);
CryptoOrder? FindActiveOrder(string id);

/// <summary>
/// Find order by provided unique id
/// </summary>
CryptoOrder FindOrder(string id);
CryptoOrder? FindOrder(string id);

/// <summary>
/// Find active order by provided client id
/// </summary>
CryptoOrder FindActiveOrderByClientId(string clientId);
CryptoOrder? FindActiveOrderByClientId(string clientId);

/// <summary>
/// Find order by provided client id
/// </summary>
CryptoOrder FindOrderByClientId(string clientId);
CryptoOrder? FindOrderByClientId(string clientId);

/// <summary>
/// Returns true if client id matches prefix
Expand Down
28 changes: 23 additions & 5 deletions src/Crypto.Websocket.Extensions.Core/Orders/Models/CryptoOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ public class CryptoOrder
/// <summary>
/// Unique order id (provided by exchange)
/// </summary>
public string Id { get; set; }
public string Id { get; set; } = null!;

/// <summary>
/// Group id of related orders (provided by client, supported only by a few exchanges)
/// </summary>
public string GroupId { get; set; }
public string? GroupId { get; set; }

/// <summary>
/// Unique client order id (provided by client)
/// </summary>
public string ClientId { get; set; }
public string? ClientId { get; set; }

/// <summary>
/// Pair to which this order belongs
/// </summary>
public string Pair { get; set; }
public string Pair { get; set; } = null!;

/// <summary>
/// Pair to which this order belongs (cleaned)
Expand Down Expand Up @@ -143,6 +143,11 @@ public double? AmountOrigQuote
/// </summary>
public CryptoOrderStatus OrderStatus { get; set; }

/// <summary>
/// Current order's status in a raw format
/// </summary>
public string OrderStatusRaw { get; set; } = string.Empty;

/// <summary>
/// Order's price
/// </summary>
Expand Down Expand Up @@ -172,8 +177,21 @@ public double? AmountOrigQuote
/// Whenever order was executed on margin
/// </summary>
public bool OnMargin { get; set; }

/// <summary>
/// Reason why order was canceled/rejected
/// </summary>
public string? CanceledReason { get; set; }


/// <summary>
/// Paid fee
/// </summary>
public double Fee { get; set; }

/// <summary>
/// Currency of the paid fee
/// </summary>
public string? FeeCurrency { get; set; }


private double? WithCorrectSign(double? value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class CryptoPairsHelper
/// <summary>
/// Clean pair from any unnecessary characters and make lowercase
/// </summary>
public static string Clean(string pair)
public static string Clean(string? pair)
{
return (pair ?? string.Empty)
.Trim()
Expand All @@ -21,7 +21,7 @@ public static string Clean(string pair)
/// <summary>
/// Compare two pairs, clean them before
/// </summary>
public static bool AreSame(string firstPair, string secondPair)
public static bool AreSame(string? firstPair, string? secondPair)
{
var first = Clean(firstPair);
var second = Clean(secondPair);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Binance.Client.Websocket" Version="2.2.1" />
<PackageReference Include="Binance.Client.Websocket" Version="2.3.1" />
<PackageReference Include="Bitfinex.Client.Websocket" Version="4.2.0" />
<PackageReference Include="Bitmex.Client.Websocket" Version="3.2.1" />
<PackageReference Include="Bitstamp.Client.Websocket" Version="1.1.1" />
Expand Down
Loading

0 comments on commit b06b12a

Please sign in to comment.