Skip to content

Commit

Permalink
Implement order update stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Marfusios committed Feb 19, 2024
1 parent c1b8083 commit 350bb7c
Show file tree
Hide file tree
Showing 26 changed files with 1,310 additions and 25 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.2.1</Version>
<Version>2.3.0</Version>
</PropertyGroup>

</Project>
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,24 @@ More usage examples:

### API coverage

| PUBLIC | Covered |
|------------------------|:--------------:|
| Aggregate trades ||
| Trades ||
| Kline/Candlesticks | |
| Individual mini tickers| |
| All mini tickers | |
| Individual tickers | |
| All tickers | |
| Partial orderbook ||
| Diff. orderbook ||
| PUBLIC | Covered |
|-------------------------|:-------:|
| Aggregate trades ||
| Trades ||
| Kline/Candlesticks | |
| Individual mini tickers | |
| All mini tickers | |
| Individual tickers | |
| All tickers | |
| Partial orderbook ||
| Diff. orderbook ||


| PRIVATE | Covered |
|-------------------------|:----------:|
| Account update | |
| Balance update | |
| Order update ||

**Pull Requests are welcome!**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Nullable>enable</Nullable>
<NoWarn>1591</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
12 changes: 11 additions & 1 deletion src/Binance.Client.Websocket/BinanceValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ namespace Binance.Client.Websocket
public static class BinanceValues
{
/// <summary>
/// Main Binance url to websocket API
/// Market data websocket API url
/// </summary>
public static readonly Uri ApiWebsocketUrl = new Uri("wss://stream.binance.com:9443");

/// <summary>
/// Futures data websocket API url
/// </summary>
public static readonly Uri FuturesApiWebsocketUrl = new Uri("wss://fstream.binance.com");

/// <summary>
/// User data websocket API rul
/// </summary>
/// <param name="listenKey">Create user's listen key via separate API</param>
public static Uri UserWebsocketUrl(string listenKey) =>
new Uri($"wss://stream.binance.com:9443/ws/{listenKey}");
}
}
12 changes: 11 additions & 1 deletion src/Binance.Client.Websocket/Client/BinanceClientStreams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Binance.Client.Websocket.Responses.Kline;
using Binance.Client.Websocket.Responses.MarkPrice;
using Binance.Client.Websocket.Responses.MiniTicker;
using Binance.Client.Websocket.Responses.Orders;
using Binance.Client.Websocket.Responses.Trades;

namespace Binance.Client.Websocket.Client
Expand Down Expand Up @@ -36,6 +37,7 @@ public class BinanceClientStreams
internal readonly Subject<MiniTickerResponse> MiniTickerSubject = new Subject<MiniTickerResponse>();
internal readonly Subject<AllMarketMiniTickerResponse> AllMarketMiniTickerSubject = new Subject<AllMarketMiniTickerResponse>();

internal readonly Subject<OrderUpdate> OrderUpdateSubject = new Subject<OrderUpdate>();

// PUBLIC

Expand Down Expand Up @@ -83,11 +85,19 @@ public class BinanceClientStreams
/// Mini-ticker specified symbol statistics for the previous 24hrs
/// </summary>
public IObservable<MiniTickerResponse> MiniTickerStream => MiniTickerSubject.AsObservable();

/// <summary>
/// Mini-ticker all symbol statistics for the previous 24hrs
/// </summary>
public IObservable<AllMarketMiniTickerResponse> AllMarketMiniTickerStream => AllMarketMiniTickerSubject.AsObservable();


// PRIVATE


/// <summary>
/// Order update stream - emits every update to the private order,
/// you need to be subscribed to authenticated API
/// </summary>
public IObservable<OrderUpdate> OrderUpdateStream => OrderUpdateSubject.AsObservable();
}
}
16 changes: 12 additions & 4 deletions src/Binance.Client.Websocket/Client/BinanceWebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Binance.Client.Websocket.Responses.Kline;
using Binance.Client.Websocket.Responses.MarkPrice;
using Binance.Client.Websocket.Responses.MiniTicker;
using Binance.Client.Websocket.Responses.Orders;
using Binance.Client.Websocket.Responses.Trades;
using Binance.Client.Websocket.Subscriptions;
using Binance.Client.Websocket.Validations;
Expand Down Expand Up @@ -156,7 +157,8 @@ private bool HandleRawMessage(string msg)
// ********************

return
PongResponse.TryHandle(msg, Streams.PongSubject);
PongResponse.TryHandle(msg, Streams.PongSubject) ||
LogUnhandled(msg);
}

private bool HandleObjectMessage(string msg)
Expand All @@ -168,17 +170,23 @@ private bool HandleObjectMessage(string msg)
// ********************

return

TradeResponse.TryHandle(response, Streams.TradesSubject) ||
AggregatedTradeResponse.TryHandle(response, Streams.TradeBinSubject) ||
OrderBookPartialResponse.TryHandle(response, Streams.OrderBookPartialSubject) ||
OrderBookDiffResponse.TryHandle(response, Streams.OrderBookDiffSubject) ||
OrderUpdate.TryHandle(response, Streams.OrderUpdateSubject) ||
FundingResponse.TryHandle(response, Streams.FundingSubject) ||
BookTickerResponse.TryHandle(response, Streams.BookTickerSubject) ||
KlineResponse.TryHandle(response, Streams.KlineSubject) ||
MiniTickerResponse.TryHandle(response, Streams.MiniTickerSubject) ||
AllMarketMiniTickerResponse.TryHandle(response, Streams.AllMarketMiniTickerSubject);

AllMarketMiniTickerResponse.TryHandle(response, Streams.AllMarketMiniTickerSubject) ||
LogUnhandled(msg);
}

private bool LogUnhandled(string message)
{
Logger.LogDebug("Received unhandled message: {message}", message);
return true;
}
}
}
35 changes: 35 additions & 0 deletions src/Binance.Client.Websocket/Exceptions/BinanceClientException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;

namespace Binance.Client.Websocket.Exceptions
{
/// <summary>
/// Binance exception class for any errors throw as a result of the misuse of the API or the library.
/// </summary>
public class BinanceClientException : BinanceHttpException
{
public BinanceClientException()
: base()
{
}

public BinanceClientException(string message, int code)
: base(message)
{
this.Code = code;
this.Message = message;
}

public BinanceClientException(string message, int code, Exception innerException)
: base(message, innerException)
{
this.Code = code;
this.Message = message;
}

[Newtonsoft.Json.JsonPropertyAttribute("code")]
public int Code { get; set; }

[Newtonsoft.Json.JsonPropertyAttribute("msg")]
public new string Message { get; protected set; }
}
}
30 changes: 30 additions & 0 deletions src/Binance.Client.Websocket/Exceptions/BinanceHttpException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;

namespace Binance.Client.Websocket.Exceptions
{
/// <summary>
/// Binance exception class for any errors throw as a result of communication via http.
/// </summary>
public class BinanceHttpException : Exception
{
public BinanceHttpException()

Check warning on line 11 in src/Binance.Client.Websocket/Exceptions/BinanceHttpException.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Headers' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 11 in src/Binance.Client.Websocket/Exceptions/BinanceHttpException.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Headers' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
: base()
{
}

public BinanceHttpException(string message)

Check warning on line 16 in src/Binance.Client.Websocket/Exceptions/BinanceHttpException.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Headers' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
: base(message)
{
}

public BinanceHttpException(string message, Exception innerException)

Check warning on line 21 in src/Binance.Client.Websocket/Exceptions/BinanceHttpException.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Headers' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
: base(message, innerException)
{
}

public int StatusCode { get; set; }

public Dictionary<string, IEnumerable<string>> Headers { get; set; }
}
}
29 changes: 29 additions & 0 deletions src/Binance.Client.Websocket/Exceptions/BinanceServerException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace Binance.Client.Websocket.Exceptions
{
/// <summary>
/// Binance exception class for any errors throw as a result internal server issues.
/// </summary>
public class BinanceServerException : BinanceHttpException
{
public BinanceServerException()

Check warning on line 10 in src/Binance.Client.Websocket/Exceptions/BinanceServerException.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Message' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in src/Binance.Client.Websocket/Exceptions/BinanceServerException.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Message' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
: base()
{
}

public BinanceServerException(string message)
: base(message)
{
this.Message = message;
}

public BinanceServerException(string message, Exception innerException)
: base(message, innerException)
{
this.Message = message;
}

public new string Message { get; protected set; }
}
}
48 changes: 48 additions & 0 deletions src/Binance.Client.Websocket/Responses/Orders/ExecutionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Binance.Client.Websocket.Responses.Orders
{
/// <summary>
/// The type of execution
/// </summary>
public enum ExecutionType
{
/// <summary>
/// New
/// </summary>
New,

/// <summary>
/// Canceled
/// </summary>
Canceled,

/// <summary>
/// Replaced
/// </summary>
Replaced,

/// <summary>
/// Rejected
/// </summary>
Rejected,

/// <summary>
/// Trade
/// </summary>
Trade,

/// <summary>
/// Expired
/// </summary>
Expired,

/// <summary>
/// Amendment
/// </summary>
Amendment,

/// <summary>
/// Self trade prevented
/// </summary>
TradePrevention
}
}
75 changes: 75 additions & 0 deletions src/Binance.Client.Websocket/Responses/Orders/OrderRejectReason.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Newtonsoft.Json;

namespace Binance.Client.Websocket.Responses.Orders
{
/// <summary>
/// The reason the order was rejected
/// </summary>
public enum OrderRejectReason
{
/// <summary>
/// Not rejected
/// </summary>
None,

/// <summary>
/// Unknown instrument
/// </summary>
[JsonProperty("UNKNOWN_INSTRUMENT")]
UnknownInstrument,

/// <summary>
/// Closed market
/// </summary>
[JsonProperty("MARKET_CLOSED")]
MarketClosed,

/// <summary>
/// Quantity out of bounds
/// </summary>
[JsonProperty("PRICE_QTY_EXCEED_HARD_LIMITS")]
PriceQuantityExceedsHardLimits,

/// <summary>
/// Unknown order
/// </summary>
[JsonProperty("UNKNOWN_ORDER")]
UnknownOrder,

/// <summary>
/// Duplicate
/// </summary>
[JsonProperty("DUPLICATE_ORDER")]
DuplicateOrder,

/// <summary>
/// Unkown account
/// </summary>
[JsonProperty("UNKNOWN_ACCOUNT")]
UnknownAccount,

/// <summary>
/// Not enough balance
/// </summary>
[JsonProperty("INSUFFICIENT_BALANCE")]
InsufficientBalance,

/// <summary>
/// Account not active
/// </summary>
[JsonProperty("ACCOUNT_INACTIVE")]
AccountInactive,

/// <summary>
/// Cannot settle
/// </summary>
[JsonProperty("ACCOUNT_CANNOT_SETTLE")]
AccountCannotSettle,

/// <summary>
/// Stop price would trigger immediately
/// </summary>
[JsonProperty("STOP_PRICE_WOULD_TRIGGER_IMMEDIATELY")]
StopPriceWouldTrigger
}
}
Loading

0 comments on commit 350bb7c

Please sign in to comment.