Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade #1

Merged
merged 5 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
544 changes: 274 additions & 270 deletions OKX.Api.Examples/Program.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions OKX.Api/Authentication/OkxAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public override void AuthenticateRestApi(RestApiClient apiClient, Uri uri, HttpM
headers.Add("x-simulated-trading", "1");
}

public override void AuthenticateSocketApi()
public override void AuthenticateTcpSocketApi()
{
throw new NotImplementedException();
}

public override void AuthenticateStreamApi()
public override void AuthenticateWebSocketApi()
{
throw new NotImplementedException();
}
Expand Down
405 changes: 0 additions & 405 deletions OKX.Api/Clients/RestApi/OKXMarketDataRestApiClient.cs

This file was deleted.

374 changes: 374 additions & 0 deletions OKX.Api/Clients/RestApi/OKXRestApiAlgoTradingClient.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace OKX.Api.Clients.RestApi;
using OKX.Api.Models;

namespace OKX.Api.Clients.RestApi;

/// <summary>
/// OKX Base Rest Api Client
/// OKX Rest Api Base Client
/// </summary>
public abstract class OKXBaseRestApiClient : RestApiClient
public abstract class OKXRestApiBaseClient : RestApiClient
{
// Internal
internal Log Log { get => this.log; }
Expand All @@ -14,7 +16,7 @@ public abstract class OKXBaseRestApiClient : RestApiClient
internal OKXRestApiClient RootClient { get; }
internal new OKXRestApiClientOptions ClientOptions { get { return RootClient.ClientOptions; } }

internal OKXBaseRestApiClient(OKXRestApiClient root) : base("OKX RestApi", root.ClientOptions)
internal OKXRestApiBaseClient(OKXRestApiClient root) : base("OKX RestApi", root.ClientOptions)
{
RootClient = root;

Expand Down Expand Up @@ -83,7 +85,7 @@ internal void SetApiCredentials(OkxApiCredentials credentials)
/// <param name="apiKey">The api key</param>
/// <param name="apiSecret">The api secret</param>
/// <param name="passPhrase">The passphrase you specified when creating the API key</param>
internal virtual void SetApiCredentials(string apiKey, string apiSecret, string passPhrase)
internal void SetApiCredentials(string apiKey, string apiSecret, string passPhrase)
{
SetApiCredentials(new OkxApiCredentials(apiKey, apiSecret, passPhrase));
}
Expand Down
101 changes: 101 additions & 0 deletions OKX.Api/Clients/RestApi/OKXRestApiBlockTradingClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using OKX.Api.Models.BlockTrading;

namespace OKX.Api.Clients.RestApi;

/// <summary>
/// OKX Rest Api Block Trading Client
/// </summary>
public class OKXRestApiBlockTradingClient : OKXRestApiBaseClient
{
// Endpoints
// TODO: api/v5/rfq/counterparties
// TODO: api/v5/rfq/create-rfq
// TODO: api/v5/rfq/cancel-rfq
// TODO: api/v5/rfq/cancel-batch-rfqs
// TODO: api/v5/rfq/cancel-all-rfqs
// TODO: api/v5/rfq/execute-quote
// TODO: api/v5/rfq/maker-instrument-settings
// TODO: api/v5/rfq/maker-instrument-settings
// TODO: api/v5/rfq/mmp-reset
// TODO: api/v5/rfq/create-quote
// TODO: api/v5/rfq/cancel-quote
// TODO: api/v5/rfq/cancel-batch-quotes
// TODO: api/v5/rfq/cancel-all-quotes
// TODO: api/v5/rfq/rfqs
// TODO: api/v5/rfq/quotes
// TODO: api/v5/rfq/trades
// TODO: api/v5/rfq/public-trades
private const string v5MarketBlockTickers = "api/v5/market/block-tickers";
private const string v5MarketBlockTicker = "api/v5/market/block-ticker";
private const string v5MarketBlockTrades = "api/v5/market/block-trades";

internal OKXRestApiBlockTradingClient(OKXRestApiClient root) : base(root)
{
}

#region Block Trading API Endpoints

/// <summary>
/// Get block tickers
/// Retrieve the latest block trading volume in the last 24 hours.
/// Rate Limit: 20 requests per 2 seconds
/// </summary>
/// <param name="instrumentType">Instrument Type</param>
/// <param name="instrumentFamily">Instrument Family</param>
/// <param name="underlying">Underlying</param>
/// <param name="ct">Cancellation Token</param>
/// <returns></returns>
public async Task<RestCallResult<IEnumerable<OkxBlockTicker>>> GetBlockTickersAsync(
OkxInstrumentType instrumentType,
string instrumentFamily = null,
string underlying = null,
CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>
{
{ "instType", JsonConvert.SerializeObject(instrumentType, new InstrumentTypeConverter(false)) },
};
parameters.AddOptionalParameter("uly", underlying);
parameters.AddOptionalParameter("instFamily", instrumentFamily);

return await SendOKXRequest<IEnumerable<OkxBlockTicker>>(GetUri(v5MarketBlockTickers), HttpMethod.Get, ct, signed: false, queryParameters: parameters).ConfigureAwait(false);
}

/// <summary>
/// Get block ticker
/// Retrieve the latest block trading volume in the last 24 hours.
/// Rate Limit: 20 requests per 2 seconds
/// </summary>
/// <param name="instrumentId">Instrument ID</param>
/// <param name="ct">Cancellation Token</param>
/// <returns></returns>
public async Task<RestCallResult<OkxBlockTicker>> GetBlockTickerAsync(string instrumentId, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>
{
{ "instId", instrumentId },
};

return await SendOKXSingleRequest<OkxBlockTicker>(GetUri(v5MarketBlockTicker), HttpMethod.Get, ct, signed: false, queryParameters: parameters).ConfigureAwait(false);
}

/// <summary>
/// Get block trades
/// Retrieve the recent block trading transactions of an instrument. Descending order by tradeId.
/// Rate Limit: 20 requests per 2 seconds
/// </summary>
/// <param name="instrumentId">Instrument ID</param>
/// <param name="ct">Cancellation Token</param>
/// <returns></returns>
public async Task<RestCallResult<IEnumerable<OkxBlockTrade>>> GetBlockTradesAsync(string instrumentId, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>
{
{ "instId", instrumentId },
};

return await SendOKXRequest<IEnumerable<OkxBlockTrade>>(GetUri(v5MarketBlockTrades), HttpMethod.Get, ct, signed: false, queryParameters: parameters).ConfigureAwait(false);
}
#endregion

}
36 changes: 36 additions & 0 deletions OKX.Api/Clients/RestApi/OKXRestApiBrokerClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace OKX.Api.Clients.RestApi;

/// <summary>
/// OKX Rest Api Broker Client
/// </summary>
public class OKXRestApiBrokerClient
{
/// <summary>
/// Earn Client
/// </summary>
public OKXRestApiNonDisclosedBrokerClient NonDisclosedBroker { get; }

/// <summary>
/// Savings Client
/// </summary>
public OKXRestApiFullyDisclosedBrokerClient FullyDisclosedBroker { get; }

/// <summary>
/// OKXFinancialProductBaseClient Constructor
/// </summary>
public OKXRestApiBrokerClient(OKXRestApiClient root)
{
NonDisclosedBroker = new OKXRestApiNonDisclosedBrokerClient(root);
FullyDisclosedBroker = new OKXRestApiFullyDisclosedBrokerClient(root);
}

/// <summary>
/// Sets API Credentials
/// </summary>
/// <param name="credentials">OkxApiCredentials Object</param>
public void SetApiCredentials(OkxApiCredentials credentials)
{
NonDisclosedBroker.SetApiCredentials(credentials);
FullyDisclosedBroker.SetApiCredentials(credentials);
}
}
194 changes: 194 additions & 0 deletions OKX.Api/Clients/RestApi/OKXRestApiCopyTradingClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
using OKX.Api.Models.CopyTrading;

namespace OKX.Api.Clients.RestApi;

/// <summary>
/// OKX Rest Api Copy Trading Client
/// </summary>
public class OKXRestApiCopyTradingClient : OKXRestApiBaseClient
{
// Endpoints
private const string v5CopyTradingCurrentSubpositions = "api/v5/copytrading/current-subpositions";
private const string v5CopyTradingSubpositionsHistory = "api/v5/copytrading/subpositions-history";
private const string v5CopyTradingAlgoOrder = "api/v5/copytrading/algo-order";
private const string v5CopyTradingCloseSubposition = "api/v5/copytrading/close-subposition";
private const string v5CopyTradingInstruments = "api/v5/copytrading/instruments";
private const string v5CopyTradingSetInstruments = "api/v5/copytrading/set-instruments";
private const string v5CopyTradingProfitSharingDetails = "api/v5/copytrading/profit-sharing-details";
private const string v5CopyTradingTotalProfitSharing = "api/v5/copytrading/total-profit-sharing";
private const string v5CopyTradingUnrealizedProfitSharingDetails = "api/v5/copytrading/unrealized-profit-sharing-details";

internal OKXRestApiCopyTradingClient(OKXRestApiClient root) : base(root)
{
}

#region Copy Trading API Endpoints
/// <summary>
/// The leading trader gets leading positions that are not closed.
/// Returns reverse chronological order with openTime
/// </summary>
/// <param name="instrumentId">Instrument ID, e.g. BTC-USDT-SWAP</param>
/// <param name="ct">Cancellation Token</param>
/// <returns></returns>
public async Task<RestCallResult<IEnumerable<OkxLeadingPosition>>> GetExistingLeadingPositionsAsync(
string instrumentId = null,
CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("instId", instrumentId);

return await SendOKXRequest<IEnumerable<OkxLeadingPosition>>(GetUri(v5CopyTradingCurrentSubpositions), HttpMethod.Get, ct, signed: true, queryParameters: parameters).ConfigureAwait(false);
}

/// <summary>
/// The leading trader retrieves the completed leading position of the last 3 months.
/// Returns reverse chronological order with closeTime.
/// </summary>
/// <param name="instrumentId">Instrument ID, e.g. BTC-USDT-SWAP</param>
/// <param name="after">Pagination of data to return records earlier than the requested subPosId.</param>
/// <param name="before">Pagination of data to return records newer than the requested subPosId.</param>
/// <param name="limit">Number of results per request. Maximum is 100. Default is 100.</param>
/// <param name="ct">Cancellation Token</param>
/// <returns></returns>
public async Task<RestCallResult<IEnumerable<OkxLeadingPositionHistory>>> GetExistingLeadingPositionsHistoryAsync(
string instrumentId = null,
long? after = null,
long? before = null,
int limit = 100,
CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("instId", instrumentId);
parameters.AddOptionalParameter("after", after?.ToOkxString());
parameters.AddOptionalParameter("before", before?.ToOkxString());
parameters.AddOptionalParameter("limit", limit.ToOkxString());

return await SendOKXRequest<IEnumerable<OkxLeadingPositionHistory>>(GetUri(v5CopyTradingSubpositionsHistory), HttpMethod.Get, ct, signed: true, queryParameters: parameters).ConfigureAwait(false);
}

/// <summary>
/// The leading trader sets TP/SL for the current leading position that are not closed.
/// </summary>
/// <param name="leadingPositionId">Leading position ID</param>
/// <param name="takeProfitTriggerPrice">Take-profit trigger price. Take-profit order price will be the market price after triggering. At least one of tpTriggerPx and slTriggerPx must be filled</param>
/// <param name="takeProfitTriggerPriceType">Take-profit trigger price type. Default is last</param>
/// <param name="stopLossTriggerPrice">Stop-loss trigger price. Stop-loss order price will be the market price after triggering.</param>
/// <param name="stopLossTriggerPriceType">Stop-loss trigger price type. Default is last</param>
/// <param name="ct">Cancellation Token</param>
/// <returns></returns>
public async Task<RestCallResult<OkxLeadingPositionId>> PlaceLeadingStopOrderAsync(
long leadingPositionId,
decimal? takeProfitTriggerPrice = null,
OkxAlgoPriceType? takeProfitTriggerPriceType = null,
decimal? stopLossTriggerPrice = null,
OkxAlgoPriceType? stopLossTriggerPriceType = null,
CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>
{
{ "subPosId", leadingPositionId },
};
parameters.AddOptionalParameter("tpTriggerPx", takeProfitTriggerPrice?.ToOkxString());
parameters.AddOptionalParameter("slTriggerPx", stopLossTriggerPrice?.ToOkxString());
parameters.AddOptionalParameter("tpTriggerPxType", JsonConvert.SerializeObject(takeProfitTriggerPriceType, new AlgoPriceTypeConverter(false)));
parameters.AddOptionalParameter("slTriggerPxType", JsonConvert.SerializeObject(stopLossTriggerPriceType, new AlgoPriceTypeConverter(false)));

return await SendOKXSingleRequest<OkxLeadingPositionId>(GetUri(v5CopyTradingAlgoOrder), HttpMethod.Post, ct, signed: true, bodyParameters: parameters).ConfigureAwait(false);
}

/// <summary>
/// The leading trader can only close a leading position once a time.
/// It is required to pass subPosId which can get from Get existing leading positions.
/// </summary>
/// <param name="leadingPositionId">Leading position ID</param>
/// <param name="ct">Cancellation Token</param>
/// <returns></returns>
public async Task<RestCallResult<OkxLeadingPositionId>> CloseLeadingPositionAsync(
long leadingPositionId,
CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>
{
{ "subPosId", leadingPositionId },
};

return await SendOKXSingleRequest<OkxLeadingPositionId>(GetUri(v5CopyTradingCloseSubposition), HttpMethod.Post, ct, signed: true, bodyParameters: parameters).ConfigureAwait(false);
}

/// <summary>
/// The leading trader gets contracts that are supported to lead by the platform.
/// </summary>
/// <param name="ct">Cancellation Token</param>
/// <returns></returns>
public async Task<RestCallResult<IEnumerable<OkxLeadingInstrument>>> GetLeadingInstrumentsAsync(
CancellationToken ct = default)
{
return await SendOKXRequest<IEnumerable<OkxLeadingInstrument>>(GetUri(v5CopyTradingInstruments), HttpMethod.Get, ct, signed: true).ConfigureAwait(false);
}

/// <summary>
/// The leading trder can amend current leading instruments, need to set initial leading instruments while applying to become a leading trader.
/// All non-leading contracts can't have position or pending orders for the current request when setting non-leading contracts as leading contracts.
/// </summary>
/// <param name="instrumentIds">Instrument ID, e.g. BTC-USDT-SWAP. If there are multiple instruments, separate them with commas. Maximum of 31 instruments can be selected.</param>
/// <param name="ct">Cancellation Token</param>
/// <returns></returns>
public async Task<RestCallResult<IEnumerable<OkxLeadingInstrument>>> AmendLeadingInstrumentsAsync(
IEnumerable<string> instrumentIds,
CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>
{
{ "instId", string.Join(",", instrumentIds) },
};

return await SendOKXRequest<IEnumerable<OkxLeadingInstrument>>(GetUri(v5CopyTradingSetInstruments), HttpMethod.Post, ct, signed: true, bodyParameters: parameters).ConfigureAwait(false);
}

/// <summary>
/// The leading trader gets all profits shared details since joining the platform.
/// </summary>
/// <param name="after">Pagination of data to return records earlier than the requested profitSharingId</param>
/// <param name="before">Pagination of data to return records newer than the requested profitSharingId</param>
/// <param name="limit">Number of results per request. Maximum is 100. Default is 100.</param>
/// <param name="ct">Cancellation Token</param>
/// <returns></returns>
public async Task<RestCallResult<IEnumerable<OkxProfitSharingDetails>>> GetProfitSharingDetailsAsync(
long? after = null,
long? before = null,
int limit = 100,
CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("after", after?.ToOkxString());
parameters.AddOptionalParameter("before", before?.ToOkxString());
parameters.AddOptionalParameter("limit", limit.ToOkxString());

return await SendOKXRequest<IEnumerable<OkxProfitSharingDetails>>(GetUri(v5CopyTradingProfitSharingDetails), HttpMethod.Get, ct, signed: true, queryParameters: parameters).ConfigureAwait(false);
}

/// <summary>
/// The leading trader gets the total amount of profit shared since joining the platform.
/// </summary>
/// <param name="ct">Cancellation Token</param>
/// <returns></returns>
public async Task<RestCallResult<IEnumerable<OkxProfitSharingTotal>>> GetTotalProfitSharingAsync(
CancellationToken ct = default)
{
return await SendOKXRequest<IEnumerable<OkxProfitSharingTotal>>(GetUri(v5CopyTradingTotalProfitSharing), HttpMethod.Get, ct, signed: true).ConfigureAwait(false);
}

/// <summary>
/// The leading trader gets the profit sharing details that are expected to be shared in the next settlement cycle.
/// The unrealized profit sharing details will update once there copy position is closed.
/// </summary>
/// <param name="ct"></param>
/// <returns></returns>
public async Task<RestCallResult<IEnumerable<OkxProfitSharingUnrealized>>> GetUnrealizedProfitSharingDetailsAsync(
CancellationToken ct = default)
{
return await SendOKXRequest<IEnumerable<OkxProfitSharingUnrealized>>(GetUri(v5CopyTradingUnrealizedProfitSharingDetails), HttpMethod.Get, ct, signed: true).ConfigureAwait(false);
}
#endregion

}
Loading