Skip to content

Commit

Permalink
Added support for setting BrokerId
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Sep 30, 2023
1 parent 4dea2c5 commit d83beb5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 14 deletions.
4 changes: 4 additions & 0 deletions OKX.Net/Clients/UnifiedApi/OKXRestClientUnifiedApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal class OKXRestClientUnifiedApi : RestApiClient, IOKXRestClientUnifiedApi
#region Internal Fields
private static TimeSyncState _timeSyncState = new("Unified Api");

internal readonly string _ref = "078ee129065aBCDE";

public event Action<OrderId>? OnOrderPlaced;
public event Action<OrderId>? OnOrderCanceled;
#endregion
Expand All @@ -32,6 +34,8 @@ internal OKXRestClientUnifiedApi(ILogger logger, HttpClient? httpClient, OKXRest
Trading = new OKXRestClientUnifiedApiTrading(this);
SubAccounts = new OKXRestClientUnifiedApiSubAccounts(this);

_ref = !string.IsNullOrEmpty(options.BrokerId) ? options.BrokerId : "078ee129065aBCDE";

if (options.Environment.EnvironmentName == TradeEnvironmentNames.Testnet)
{
StandardRequestHeaders = new Dictionary<string, string>
Expand Down
17 changes: 8 additions & 9 deletions OKX.Net/Clients/UnifiedApi/OKXRestClientUnifiedApiTrading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace OKX.Net.Clients.UnifiedApi;
internal class OKXRestClientUnifiedApiTrading : IOKXRestClientUnifiedApiTrading
{
private const string _ref = "078ee129065aBCDE";
private const string _bodyParameterKey = "<BODY>";

private static Random _random = new Random();
Expand Down Expand Up @@ -70,8 +69,8 @@ public virtual async Task<WebCallResult<OKXOrderPlaceResponse>> PlaceOrderAsync(
{"side", JsonConvert.SerializeObject(side, new OrderSideConverter(false)) },
{"ordType", JsonConvert.SerializeObject(type, new OrderTypeConverter(false)) },
{"sz", quantity.ToString(CultureInfo.InvariantCulture) },
{"tag", _ref },
{"clOrdId", _ref + (clientOrderId ?? RandomString(15)) },
{"tag", _baseClient._ref },
{"clOrdId", _baseClient._ref + (clientOrderId ?? RandomString(15)) },
};
parameters.AddOptionalParameter("px", price?.ToString(CultureInfo.InvariantCulture));
parameters.AddOptionalParameter("ccy", asset);
Expand Down Expand Up @@ -119,8 +118,8 @@ public virtual async Task<WebCallResult<IEnumerable<OKXOrderPlaceResponse>>> Pla
{
foreach (var order in orders)
{
order.Tag = _ref;
order.ClientOrderId = _ref + (order.ClientOrderId ?? RandomString(15));
order.Tag = _baseClient._ref;
order.ClientOrderId = _baseClient._ref + (order.ClientOrderId ?? RandomString(15));
}

var parameters = new Dictionary<string, object>
Expand Down Expand Up @@ -276,8 +275,8 @@ public virtual async Task<WebCallResult<OKXClosePositionResponse>> ClosePosition
var parameters = new Dictionary<string, object> {
{"instId", symbol },
{"mgnMode", JsonConvert.SerializeObject(marginMode, new MarginModeConverter(false)) },
{"tag", "c84128021aecBCDE" },
{"clOrdId", _ref + RandomString(15) }
{"tag", _baseClient._ref },
{"clOrdId", _baseClient._ref + RandomString(15) }

};
if (positionSide.HasValue)
Expand Down Expand Up @@ -565,8 +564,8 @@ public virtual async Task<WebCallResult<OKXAlgoOrderResponse>> PlaceAlgoOrderAsy
{"side", JsonConvert.SerializeObject(orderSide, new OrderSideConverter(false)) },
{"ordType", JsonConvert.SerializeObject(algoOrderType, new AlgoOrderTypeConverter(false)) },
{"sz", quantity.ToString() },
{"tag", _ref },
{"clOrdId", _ref + RandomString(15) }
{"tag", _baseClient._ref },
{"clOrdId", _baseClient._ref + RandomString(15) }
};
parameters.AddOptionalParameter("ccy", asset);
parameters.AddOptionalParameter("reduceOnly", reduceOnly);
Expand Down
3 changes: 3 additions & 0 deletions OKX.Net/Clients/UnifiedApi/OKXSocketClientUnifiedApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class OKXSocketClientUnifiedApi : SocketApiClient, IOKXSocketClientUnifie
/// <inheritdoc />
public IOKXSocketClientUnifiedApiTrading Trading { get; }

internal readonly string _ref = "078ee129065aBCDE";
private bool _demoTrading;
#region ctor

Expand All @@ -33,6 +34,8 @@ internal OKXSocketClientUnifiedApi(ILogger logger, OKXSocketOptions options) :
ExchangeData = new OKXSocketClientUnifiedApiExchangeData(logger, this);
Trading = new OKXSocketClientUnifiedApiTrading(logger, this);

_ref = !string.IsNullOrEmpty(options.BrokerId) ? options.BrokerId : "078ee129065aBCDE";

_demoTrading = options.Environment.EnvironmentName == TradeEnvironmentNames.Testnet;
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class OKXSocketClientUnifiedApiTrading : IOKXSocketClientUnifiedApiTradin
{
private readonly OKXSocketClientUnifiedApi _client;
private static Random _random = new Random();
private const string _ref = "078ee129065aBCDE";

private readonly ILogger _logger;

Expand Down Expand Up @@ -53,8 +52,8 @@ public async Task<CallResult<OKXOrderPlaceResponse>> PlaceOrderAsync(string symb
};

parameters.AddOptionalParameter("ccy", asset);
parameters.AddOptionalParameter("clOrdId", _ref + (clientOrderId ?? RandomString(15)));
parameters.AddOptionalParameter("tag", _ref);
parameters.AddOptionalParameter("clOrdId", _client._ref + (clientOrderId ?? RandomString(15)));
parameters.AddOptionalParameter("tag", _client._ref);
parameters.AddOptionalParameter("posSide", EnumConverter.GetString(positionSide));
parameters.AddOptionalParameter("px", price?.ToString(CultureInfo.InvariantCulture));
parameters.AddOptionalParameter("reduceOnly", reduceOnly);
Expand All @@ -75,8 +74,8 @@ public async Task<CallResult<IEnumerable<OKXOrderPlaceResponse>>> PlaceMultipleO
{
foreach (var order in orders)
{
order.Tag = _ref;
order.ClientOrderId = _ref + (order.ClientOrderId ?? RandomString(15));
order.Tag = _client._ref;
order.ClientOrderId = _client._ref + (order.ClientOrderId ?? RandomString(15));
}

return await _client.QueryInternalAsync<IEnumerable<OKXOrderPlaceResponse>>(_client.GetUri("/ws/v5/private"), "batch-orders", orders, true, 1).ConfigureAwait(false);
Expand Down
10 changes: 10 additions & 0 deletions OKX.Net/OKX.Net.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions OKX.Net/Objects/Options/OKXRestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class OKXRestOptions : RestExchangeOptions<OKXEnvironment, OKXApiCredenti
/// </summary>
public bool SignPublicRequests { get; set; }

/// <summary>
/// Broker ID for earning rebates
/// </summary>
public string? BrokerId { get; set; }

/// <summary>
/// Options for the unified API
/// </summary>
Expand All @@ -30,6 +35,7 @@ internal OKXRestOptions Copy()
{
var options = Copy<OKXRestOptions>();
options.SignPublicRequests = SignPublicRequests;
options.BrokerId = BrokerId;
options.UnifiedOptions = UnifiedOptions.Copy<RestApiOptions>();
return options;
}
Expand Down
6 changes: 6 additions & 0 deletions OKX.Net/Objects/Options/OKXSocketOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public class OKXSocketOptions : SocketExchangeOptions<OKXEnvironment>
Environment = OKXEnvironment.Live
};

/// <summary>
/// Broker ID for earning rebates
/// </summary>
public string? BrokerId { get; set; }

/// <summary>
/// Options for the Unified API
/// </summary>
Expand All @@ -25,6 +30,7 @@ public class OKXSocketOptions : SocketExchangeOptions<OKXEnvironment>
internal OKXSocketOptions Copy()
{
var options = Copy<OKXSocketOptions>();
options.BrokerId = BrokerId;
options.UnifiedOptions = UnifiedOptions.Copy<SocketApiOptions>();
return options;
}
Expand Down

0 comments on commit d83beb5

Please sign in to comment.