Skip to content

Commit

Permalink
Client order id
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf authored Dec 3, 2024
1 parent b8ac858 commit 2b4d9d3
Show file tree
Hide file tree
Showing 31 changed files with 255 additions and 71 deletions.
1 change: 1 addition & 0 deletions Binance.Net.UnitTests/BinanceClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Binance.Net.Interfaces.Clients;
using System.Diagnostics;

namespace Binance.Net.UnitTests
{
Expand Down
6 changes: 3 additions & 3 deletions Binance.Net/Binance.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
Expand Down Expand Up @@ -48,7 +48,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="8.4.0" />
<PackageReference Include="CryptoExchange.Net" Version="8.4.2" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
45 changes: 33 additions & 12 deletions Binance.Net/Binance.Net.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@
<member name="M:Binance.Net.Clients.SpotApi.BinanceSocketClientSpotApiTrading.PlaceOcoOrderAsync(System.String,Binance.Net.Enums.OrderSide,System.Decimal,System.Decimal,System.Decimal,System.Nullable{System.Decimal},System.String,System.String,System.String,System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{Binance.Net.Enums.TimeInForce},System.Nullable{System.Int32},System.Nullable{System.Int32},System.Nullable{System.Int32},System.Nullable{System.Decimal},System.Nullable{System.Int32},System.Nullable{System.Int32},System.Nullable{System.Int32},System.Nullable{Binance.Net.Enums.SelfTradePreventionMode},System.Threading.CancellationToken)">
<inheritdoc />
</member>
<member name="M:Binance.Net.Clients.SpotApi.BinanceSocketClientSpotApiTrading.PlaceOcoOrderListAsync(System.String,Binance.Net.Enums.OrderSide,System.Decimal,Binance.Net.Enums.SpotOrderType,Binance.Net.Enums.SpotOrderType,System.String,System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{Binance.Net.Enums.TimeInForce},System.Nullable{System.Int32},System.Nullable{System.Int32},System.String,System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{Binance.Net.Enums.TimeInForce},System.Nullable{System.Int32},System.Nullable{System.Int32},System.Nullable{Binance.Net.Enums.SelfTradePreventionMode},System.Threading.CancellationToken)">
<member name="M:Binance.Net.Clients.SpotApi.BinanceSocketClientSpotApiTrading.PlaceOcoOrderListAsync(System.String,Binance.Net.Enums.OrderSide,System.Decimal,Binance.Net.Enums.SpotOrderType,Binance.Net.Enums.SpotOrderType,System.String,System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{Binance.Net.Enums.TimeInForce},System.Nullable{System.Int32},System.Nullable{System.Int32},System.String,System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{Binance.Net.Enums.TimeInForce},System.Nullable{System.Int32},System.Nullable{System.Int32},System.Nullable{Binance.Net.Enums.SelfTradePreventionMode},System.String,System.Threading.CancellationToken)">
<inheritdoc />
</member>
<member name="M:Binance.Net.Clients.SpotApi.BinanceSocketClientSpotApiTrading.GetOcoOrderAsync(System.Nullable{System.Int64},System.String,System.Threading.CancellationToken)">
Expand Down Expand Up @@ -11256,7 +11256,7 @@
<param name="ct">Cancellation token</param>
<returns>Order list info</returns>
</member>
<member name="M:Binance.Net.Interfaces.Clients.SpotApi.IBinanceSocketClientSpotApiTrading.PlaceOcoOrderListAsync(System.String,Binance.Net.Enums.OrderSide,System.Decimal,Binance.Net.Enums.SpotOrderType,Binance.Net.Enums.SpotOrderType,System.String,System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{Binance.Net.Enums.TimeInForce},System.Nullable{System.Int32},System.Nullable{System.Int32},System.String,System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{Binance.Net.Enums.TimeInForce},System.Nullable{System.Int32},System.Nullable{System.Int32},System.Nullable{Binance.Net.Enums.SelfTradePreventionMode},System.Threading.CancellationToken)">
<member name="M:Binance.Net.Interfaces.Clients.SpotApi.IBinanceSocketClientSpotApiTrading.PlaceOcoOrderListAsync(System.String,Binance.Net.Enums.OrderSide,System.Decimal,Binance.Net.Enums.SpotOrderType,Binance.Net.Enums.SpotOrderType,System.String,System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{Binance.Net.Enums.TimeInForce},System.Nullable{System.Int32},System.Nullable{System.Int32},System.String,System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{System.Decimal},System.Nullable{Binance.Net.Enums.TimeInForce},System.Nullable{System.Int32},System.Nullable{System.Int32},System.Nullable{Binance.Net.Enums.SelfTradePreventionMode},System.String,System.Threading.CancellationToken)">
<summary>
Place a new OCO order. An OCO has 2 legs called the above leg and below leg. One of the legs must be a LimitMaker order and the other leg must be StopLoss or StopLossLimit order.
<para><a href="https://binance-docs.github.io/apidocs/websocket_api/en/#place-new-order-list-oco-trade" /></para>
Expand All @@ -11283,6 +11283,7 @@
<param name="belowStrategyId">Strategy id for the below leg</param>
<param name="belowStrategyType">Strategy type for the below leg</param>
<param name="selfTradePreventionMode">Self trade prevention mode</param>
<param name="listClientOrderId">List client order id</param>
<param name="ct">Cancellation token</param>
<returns></returns>
</member>
Expand Down Expand Up @@ -29332,6 +29333,16 @@
Binance options
</summary>
</member>
<member name="P:Binance.Net.Objects.Options.BinanceOptions.AllowAppendingClientOrderId">
<summary>
Whether to allow the client to adjust the clientOrderId parameter send by the user when placing orders to include a client reference. This reference is used by the exchange to allocate a small percentage of the paid trading fees to developer of this library. Defaults to false.<br />
Note that:<br />
* It does not impact the amount of fees a user pays in any way<br />
* It does not impact functionality. The reference is added just before sending the request and removed again during data deserialization<br />
* It does respect client order id field limitations. For example if the user provided client order id parameter is too long to fit the reference it will not be added<br />
* Toggling this option might fail operations using a clientOrderId parameter for pre-existing orders which were placed before the toggle. Operations on orders placed after the toggle will work as expected. It's adviced to toggle when there are no open orders
</summary>
</member>
<member name="T:Binance.Net.Objects.Options.BinanceOrderBookOptions">
<summary>
Options for the Binance SymbolOrderBook
Expand Down Expand Up @@ -29377,11 +29388,6 @@
How often the trade rules should be updated. Only used when TradeRulesBehaviour is not None
</summary>
</member>
<member name="P:Binance.Net.Objects.Options.BinanceRestApiOptions.BrokerId">
<summary>
The broker reference id to use
</summary>
</member>
<member name="T:Binance.Net.Objects.Options.BinanceRestOptions">
<summary>
Options for the BinanceRestClient
Expand All @@ -29402,6 +29408,16 @@
The default receive window for requests
</summary>
</member>
<member name="P:Binance.Net.Objects.Options.BinanceRestOptions.AllowAppendingClientOrderId">
<summary>
Whether to allow the client to adjust the clientOrderId parameter send by the user when placing orders to include a client reference. This reference is used by the exchange to allocate a small percentage of the paid trading fees to developer of this library. Defaults to false.<br />
Note that:<br />
* It does not impact the amount of fees a user pays in any way<br />
* It does not impact functionality. The reference is added just before sending the request and removed again during data deserialization<br />
* It does respect client order id field limitations. For example if the user provided client order id parameter is too long to fit the reference it will not be added<br />
* Toggling this option might fail operations using a clientOrderId parameter for pre-existing orders which were placed before the toggle. Operations on orders placed after the toggle will work as expected. It's adviced to toggle when there are no open orders
</summary>
</member>
<member name="P:Binance.Net.Objects.Options.BinanceRestOptions.SpotOptions">
<summary>
Spot API options
Expand Down Expand Up @@ -29432,11 +29448,6 @@
How often the trade rules should be updated. Only used when TradeRulesBehaviour is not None
</summary>
</member>
<member name="P:Binance.Net.Objects.Options.BinanceSocketApiOptions.BrokerId">
<summary>
The broker reference id to use
</summary>
</member>
<member name="T:Binance.Net.Objects.Options.BinanceSocketOptions">
<summary>
Options for the BinanceSocketClient
Expand All @@ -29452,6 +29463,16 @@
ctor
</summary>
</member>
<member name="P:Binance.Net.Objects.Options.BinanceSocketOptions.AllowAppendingClientOrderId">
<summary>
Whether to allow the client to adjust the clientOrderId parameter send by the user when placing orders to include a client reference. This reference is used by the exchange to allocate a small percentage of the paid trading fees to developer of this library. Defaults to false.<br />
Note that:<br />
* It does not impact the amount of fees a user pays in any way<br />
* It does not impact functionality. The reference is added just before sending the request and removed again during data deserialization<br />
* It does respect client order id field limitations. For example if the user provided client order id parameter is too long to fit the reference it will not be added<br />
* Toggling this option might fail operations using a clientOrderId parameter for pre-existing orders which were placed before the toggle. Operations on orders placed after the toggle will work as expected. It's adviced to toggle when there are no open orders
</summary>
</member>
<member name="P:Binance.Net.Objects.Options.BinanceSocketOptions.SpotOptions">
<summary>
Options for the Spot API
Expand Down
5 changes: 5 additions & 0 deletions Binance.Net/BinanceExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public static class BinanceExchange
"https://binance-docs.github.io/apidocs/spot/en/#change-log"
};

internal const string ClientOrderIdSpot = "x-VICEW9VV";
internal const string ClientOrderIdFutures = "x-d63tKbx3";
internal const string ClientOrderIdPrefixSpot = ClientOrderIdSpot + LibraryHelpers.ClientOrderIdSeperator;
internal const string ClientOrderIdPrefixFutures = ClientOrderIdFutures + LibraryHelpers.ClientOrderIdSeperator;

/// <summary>
/// Format a base and quote asset to a Binance recognized symbol
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ internal partial class BinanceRestClientCoinFuturesApi : RestApiClient, IBinance

internal static TimeSyncState _timeSyncState = new TimeSyncState("Coin Futures Api");

internal readonly string _brokerId;

#endregion

#region Api clients
Expand Down Expand Up @@ -62,8 +60,6 @@ internal BinanceRestClientCoinFuturesApi(ILogger logger, HttpClient? httpClient,
RequestBodyEmptyContent = "";
RequestBodyFormat = RequestBodyFormat.FormData;
ArraySerialization = ArrayParametersSerialization.MultipleValues;

_brokerId = !string.IsNullOrEmpty(options.CoinFuturesOptions.BrokerId) ? options.CoinFuturesOptions.BrokerId! : "x-d63tKbx3";
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task<WebCallResult<BinanceFuturesOrder>> PlaceOrderAsync(
price = rulesCheck.Price;
stopPrice = rulesCheck.StopPrice;

string clientOrderId = newClientOrderId ?? ExchangeHelpers.AppendRandomString(_baseClient._brokerId, 32);
var clientOrderId = LibraryHelpers.ApplyBrokerId(newClientOrderId, BinanceExchange.ClientOrderIdFutures, 36, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
Expand Down Expand Up @@ -147,7 +147,7 @@ public async Task<WebCallResult<IEnumerable<CallResult<BinanceFuturesOrder>>>> P

orderParameters.AddEnum("side", order.Side);
orderParameters.AddEnum("type", order.Type);
var clientOrderId = order.NewClientOrderId ?? ExchangeHelpers.AppendRandomString(_baseClient._brokerId, 32);
var clientOrderId = LibraryHelpers.ApplyBrokerId(order.NewClientOrderId, BinanceExchange.ClientOrderIdFutures, 36, _baseClient.ClientOptions.AllowAppendingClientOrderId);
orderParameters.AddOptionalParameter("quantity", order.Quantity?.ToString(CultureInfo.InvariantCulture));
orderParameters.AddOptionalParameter("newClientOrderId", clientOrderId);
orderParameters.AddOptionalParameter("price", order.Price?.ToString(CultureInfo.InvariantCulture));
Expand Down Expand Up @@ -194,6 +194,9 @@ public async Task<WebCallResult<BinanceFuturesOrder>> GetOrderAsync(string symbo
if (orderId == null && origClientOrderId == null)
throw new ArgumentException("Either orderId or origClientOrderId must be sent");

if (origClientOrderId != null)
origClientOrderId = LibraryHelpers.ApplyBrokerId(origClientOrderId, BinanceExchange.ClientOrderIdFutures, 36, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection
{
{ "symbol", symbol }
Expand All @@ -217,6 +220,9 @@ public async Task<WebCallResult<BinanceFuturesOrder>> CancelOrderAsync(string sy
if (!orderId.HasValue && string.IsNullOrEmpty(origClientOrderId))
throw new ArgumentException("Either orderId or origClientOrderId must be sent");

if (origClientOrderId != null)
origClientOrderId = LibraryHelpers.ApplyBrokerId(origClientOrderId, BinanceExchange.ClientOrderIdFutures, 36, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection
{
{ "symbol", symbol }
Expand Down Expand Up @@ -283,6 +289,8 @@ public async Task<WebCallResult<IEnumerable<CallResult<BinanceFuturesOrder>>>> C
if (origClientOrderIdList?.Count() > 10)
throw new ArgumentException("origClientOrderIdList cannot contain more than 10 items");

var convertClientOrderIdList = origClientOrderIdList?.Select(x => LibraryHelpers.ApplyBrokerId(x, BinanceExchange.ClientOrderIdFutures, 36, _baseClient.ClientOptions.AllowAppendingClientOrderId));

var parameters = new ParameterCollection
{
{ "symbol", symbol }
Expand Down Expand Up @@ -323,6 +331,9 @@ public async Task<WebCallResult<BinanceFuturesOrder>> GetOpenOrderAsync(string s
if (orderId == null && origClientOrderId == null)
throw new ArgumentException("Either orderId or origClientOrderId must be sent");

if (origClientOrderId != null)
origClientOrderId = LibraryHelpers.ApplyBrokerId(origClientOrderId, BinanceExchange.ClientOrderIdFutures, 36, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection
{
{ "symbol", symbol }
Expand Down
6 changes: 1 addition & 5 deletions Binance.Net/Clients/SpotApi/BinanceRestClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ internal partial class BinanceRestClientSpotApi : RestApiClient, IBinanceRestCli
internal BinanceExchangeInfo? _exchangeInfo;
internal DateTime? _lastExchangeInfoUpdate;

internal readonly string _brokerId;

internal static TimeSyncState _timeSyncState = new TimeSyncState("Spot Api");
#endregion

Expand Down Expand Up @@ -63,8 +61,6 @@ internal BinanceRestClientSpotApi(ILogger logger, HttpClient? httpClient, Binanc
RequestBodyEmptyContent = "";
RequestBodyFormat = RequestBodyFormat.FormData;
ArraySerialization = ArrayParametersSerialization.MultipleValues;

_brokerId = !string.IsNullOrEmpty(options.SpotOptions.BrokerId) ? options.SpotOptions.BrokerId! : "x-VICEW9VV";
}
#endregion

Expand Down Expand Up @@ -124,7 +120,7 @@ internal async Task<WebCallResult<BinancePlacedOrder>> PlaceOrderInternal(Uri ur
stopPrice = rulesCheck.StopPrice;
quoteQuantity = rulesCheck.QuoteQuantity;

string clientOrderId = newClientOrderId ?? ExchangeHelpers.AppendRandomString(_brokerId, 32);
var clientOrderId = LibraryHelpers.ApplyBrokerId(newClientOrderId, BinanceExchange.ClientOrderIdSpot, 36, ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
Expand Down
Loading

0 comments on commit 2b4d9d3

Please sign in to comment.