Skip to content

Commit

Permalink
Add AlgoOrderClient and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
eynzhang committed Sep 22, 2020
1 parent 4f08696 commit fa9044b
Show file tree
Hide file tree
Showing 11 changed files with 538 additions and 0 deletions.
96 changes: 96 additions & 0 deletions Huobi.SDK.Core/Client/AlgoOrderClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Threading.Tasks;
using Huobi.SDK.Core.RequestBuilder;
using Huobi.SDK.Model.Response.AlgoOrder;
using Huobi.SDK.Model.Request.AlgoOrder;

namespace Huobi.SDK.Core.Client
{
/// <summary>
/// Responsible to operate on order
/// </summary>
public class AlgoOrderClient
{
private const string GET_METHOD = "GET";
private const string POST_METHOD = "POST";

private const string DEFAULT_HOST = "api.huobi.pro";

private readonly PrivateUrlBuilder _urlBuilder;

/// <summary>
/// Constructor
/// </summary>
/// <param name="accessKey">Access Key</param>
/// <param name="secretKey">Secret Key</param>
/// <param name="host">the host that the client connects to</param>
public AlgoOrderClient(string accessKey, string secretKey, string host = DEFAULT_HOST)
{
_urlBuilder = new PrivateUrlBuilder(accessKey, secretKey, host);
}

/// <summary>
/// Place a new algo order
/// </summary>
/// <param name="request"></param>
/// <returns>PlaceOrderResponse</returns>
public async Task<PlaceOrderResponse> PlaceOrderAsync(PlaceOrderRequest request)
{
string url = _urlBuilder.Build(POST_METHOD, "/v2/algo-orders");

return await HttpRequest.PostAsync<PlaceOrderResponse>(url, request.ToJson());
}

/// <summary>
/// Cancel an algo order by client order Ids
/// </summary>
/// <param name="request"></param>
/// <returns>CancelOrdersResponse</returns>
public async Task<CancelOrdersResponse> CancelOrdersAsync(CancelOrdersRequest request)
{
string url = _urlBuilder.Build(POST_METHOD, $"/v2/algo-orders/cancellation");

return await HttpRequest.PostAsync<CancelOrdersResponse>(url, request.ToJson());
}

/// <summary>
/// Returns all open orders which have not been filled completely.
/// </summary>
/// <param name="request"></param>
/// <returns>GetOpenOrdersResponse</returns>
public async Task<GetOpenOrdersResponse> GetOpenOrdersAsync(GetRequest request)
{
string url = _urlBuilder.Build(GET_METHOD, "/v2/algo-orders/opening", request);

return await HttpRequest.GetAsync<GetOpenOrdersResponse>(url);
}

/// <summary>
/// Returns algo orders that have been inactive
/// </summary>
/// <param name="request"></param>
/// <returns>GetHistoryOrdersResponse</returns>
public async Task<GetHistoryOrdersResponse> GetHistoryOrdersAsync(GetRequest request)
{
string url = _urlBuilder.Build(GET_METHOD, $"/v2/algo-orders/history", request);

return await HttpRequest.GetAsync<GetHistoryOrdersResponse>(url);
}

/// <summary>
/// Returns a specific algo order
/// </summary>
/// <param name="clientOrderId"></param>
/// <returns>GetSpecificOrderResponse</returns>
public async Task<GetSpecificOrderResponse> GetSpecificOrderAsync(string clientOrderId)
{
var request = new GetRequest()
.AddParam("clientOrderId", clientOrderId);

string url = _urlBuilder.Build(GET_METHOD, $"/v2/algo-orders/specific", request);

return await HttpRequest.GetAsync<GetSpecificOrderResponse>(url);
}


}
}
163 changes: 163 additions & 0 deletions Huobi.SDK.Example/AlgoOrderClientExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
using Huobi.SDK.Core;
using Huobi.SDK.Core.Client;
using Huobi.SDK.Core.Log;
using Huobi.SDK.Model.Request.AlgoOrder;
using Huobi.SDK.Model.Response;

namespace Huobi.SDK.Example
{
public class AlgoOrderClientExample
{
private static PerformanceLogger _logger = PerformanceLogger.GetInstance();

public static void RunAll()
{
PlaceOrder();

GetOpenOrders();

CancelOrders();

GetHistoryOrders();

GetSpecificOrder();
}

private static void PlaceOrder()
{
var tradeClient = new AlgoOrderClient(Config.AccessKey, Config.SecretKey);

_logger.Start();
var request = new PlaceOrderRequest
{
accountId = 11136102,
orderSide = "buy",
orderType = "limit",
symbol = "htusdt",
orderSize = "5",
orderPrice = "2.1",
stopPrice = "2",
clientOrderId = "0922T1753"
};

var response = tradeClient.PlaceOrderAsync(request).Result;
_logger.StopAndLog();

if (response.code == (int)ResponseCode.Success)
{
AppLogger.Info($"Place algo order successfully, client order id: {response.data.clientOrderId}");
}
else
{
AppLogger.Info($"Place algo order fail, error code: {response.code}, error message: {response.message}");
}
}

private static void CancelOrders()
{
var tradeClient = new AlgoOrderClient(Config.AccessKey, Config.SecretKey);

_logger.Start();
var request = new CancelOrdersRequest
{
clientOrderIds = new string[]{ "0922T1753" }
};

var response = tradeClient.CancelOrdersAsync(request).Result;
_logger.StopAndLog();

if (response.code == (int)ResponseCode.Success)
{
foreach (string cid in response.data.accepted)
{
AppLogger.Info($"Cancel algo order successfully, client order id: {cid}");
}
foreach (string cid in response.data.rejected)
{
AppLogger.Info($"Cancel algo order fail, client order id: {cid}");
}
}
else
{
AppLogger.Info($"Place algo order fail, error code: {response.code}, error message: {response.message}");
}
}

private static void GetOpenOrders()
{
var client = new AlgoOrderClient(Config.AccessKey, Config.SecretKey);

_logger.Start();
var request = new GetRequest()
.AddParam("symbol", "htusdt");
var response = client.GetOpenOrdersAsync(request).Result;
_logger.StopAndLog();

if (response.code == (int)ResponseCode.Success)
{
if (response.data != null)
{
AppLogger.Info($"There are total {response.data.Length} open algo orders");
foreach (var o in response.data)
{
AppLogger.Info($"Algo order symbol: {o.symbol}, price: {o.orderPrice}, status: {o.orderStatus}");
}
}
}
else
{
AppLogger.Info($"Get open algo orders fail, error code: {response.code}, error message: {response.message}");
}
}

private static void GetHistoryOrders()
{
var client = new AlgoOrderClient(Config.AccessKey, Config.SecretKey);

_logger.Start();
var request = new GetRequest()
.AddParam("symbol", "htusdt")
.AddParam("orderStatus", "canceled");
var response = client.GetHistoryOrdersAsync(request).Result;
_logger.StopAndLog();

if (response.code == (int)ResponseCode.Success)
{
if (response.data != null)
{
AppLogger.Info($"There are total {response.data.Length} history algo orders");
foreach (var o in response.data)
{
AppLogger.Info($"Algo order client order id: {o.clientOrderId}, symbol: {o.symbol}, lastActTime: {o.lastActTime}");
}
}
}
else
{
AppLogger.Info($"Get history algo orders fail, error code: {response.code}, error message: {response.message}");
}
}

private static void GetSpecificOrder()
{
var client = new AlgoOrderClient(Config.AccessKey, Config.SecretKey);

_logger.Start();
var response = client.GetSpecificOrderAsync("0922T1653").Result;
_logger.StopAndLog();

if (response.code == (int)ResponseCode.Success)
{
if (response.data != null)
{
var o = response.data;
AppLogger.Info($"Get algo order success, client order id: {o.clientOrderId}, symbol: {o.symbol}, lastActTime: {o.lastActTime}");
}
}
else
{
AppLogger.Info($"Get algo order fail, error code: {response.code}, error message: {response.message}");
}
}
}
}
2 changes: 2 additions & 0 deletions Huobi.SDK.Model/Huobi.SDK.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
<Folder Include="Request\Wallet\" />
<Folder Include="Request\SubUser\" />
<Folder Include="Request\Account\" />
<Folder Include="Response\AlgoOrder\" />
<Folder Include="Request\AlgoOrder\" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions Huobi.SDK.Model/Request/AlgoOrder/CancelOrdersRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Huobi.SDK.Model.Request.AlgoOrder
{
public class CancelOrdersRequest : PostRequestBase
{
public string[] clientOrderIds;
}
}
27 changes: 27 additions & 0 deletions Huobi.SDK.Model/Request/AlgoOrder/PlaceOrderRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Huobi.SDK.Model.Request.AlgoOrder
{
public class PlaceOrderRequest : PostRequestBase
{
public int accountId;

public string symbol;

public string orderPrice;

public string orderSide;

public string orderSize;

public string orderValue;

public string timeInForce;

public string orderType;

public string clientOrderId;

public string stopPrice;

public string trailingRate;
}
}
12 changes: 12 additions & 0 deletions Huobi.SDK.Model/Request/PostRequestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace Huobi.SDK.Model.Request
{
public class PostRequestBase
{
public string ToJson()
{
return JsonConvert.SerializeObject(this);
}
}
}
27 changes: 27 additions & 0 deletions Huobi.SDK.Model/Response/AlgoOrder/CancelOrdersResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Huobi.SDK.Model.Response.AlgoOrder
{
public class CancelOrdersResponse
{
/// <summary>
/// Status code
/// </summary>
public int code;

/// <summary>
/// Error message (if any)
/// </summary>
public string message;

/// <summary>
/// Response body
/// </summary>
public Data data;

public class Data
{
public string[] accepted;

public string[] rejected;
}
}
}
Loading

0 comments on commit fa9044b

Please sign in to comment.