Browse Source

下单记录客户端订单号

master
shanji 3 years ago
parent
commit
577cb2e6f5
  1. 1
      Binance.TradeRobot.Business/Business/OrderPublishBusiness/Spot/D21OrderPublishBusiness.cs
  2. 10
      Binance.TradeRobot.Business/Business/TradeBusiness/BaseTradeBusiness.cs
  3. 2
      Binance.TradeRobot.Business/Business/TradeBusiness/Spot/D21TradeBusiness.cs
  4. 7
      Binance.TradeRobot.Model/Db/Order/SpotOrder.cs
  5. 3
      SDKAdapter/APIClient/BaseAPIClient.cs
  6. 4
      SDKAdapter/APIClient/BinanceAPIClient.cs
  7. 74
      SDKTestConsole/Program.cs

1
Binance.TradeRobot.Business/Business/OrderPublishBusiness/Spot/D21OrderPublishBusiness.cs

@ -275,6 +275,7 @@ namespace Binance.TradeRobot.Business
var stopLossOrder = new SpotOrder() var stopLossOrder = new SpotOrder()
{ {
Id = stopOrderId, Id = stopOrderId,
ClientOrderId = stopLossClientOrderId,
CreateTime = DateTime.Now, CreateTime = DateTime.Now,
ExchangeId = d21Robot.ExchangeId, ExchangeId = d21Robot.ExchangeId,
LoanAmount = 0M, LoanAmount = 0M,

10
Binance.TradeRobot.Business/Business/TradeBusiness/BaseTradeBusiness.cs

@ -60,14 +60,14 @@ namespace Binance.TradeRobot.Business
{ {
if (baseAPIClient == null) if (baseAPIClient == null)
baseAPIClient = GetBaseAPIClient(d21Robot.ExchangeId, d21Robot.ExchangeAPIKey.AccountId, d21Robot.ExchangeAPIKey.APIKey, d21Robot.ExchangeAPIKey.SecretKey); baseAPIClient = GetBaseAPIClient(d21Robot.ExchangeId, d21Robot.ExchangeAPIKey.AccountId, d21Robot.ExchangeAPIKey.APIKey, d21Robot.ExchangeAPIKey.SecretKey);
var stopLossOrderIdList = fsql.Select<SpotOrder>().Where(o => o.OrderType == Enums.OrderType.STOP_LOSS_LIMIT && var stopLossOrderList = fsql.Select<SpotOrder>().Where(o => o.OrderType == Enums.OrderType.STOP_LOSS_LIMIT &&
o.State == Enums.SpotOrderState.Created && o.State == Enums.SpotOrderState.Created &&
o.RobotId == d21Robot.Id).ToList(o => o.Id); o.RobotId == d21Robot.Id).ToList(o => new { o.Id, o.ClientOrderId });
if (stopLossOrderIdList == null || stopLossOrderIdList.Count() == 0) if (stopLossOrderList == null || stopLossOrderList.Count() == 0)
return; return;
foreach (var stopLossOrderId in stopLossOrderIdList) foreach (var stopLossOrder in stopLossOrderList)
baseAPIClient.CancelIsolateMarginOrder(d21Robot.Symbol, stopLossOrderId); baseAPIClient.CancelIsolateMarginOrder(d21Robot.Symbol, stopLossOrder.Id, stopLossOrder.ClientOrderId);
} }
} }
} }

2
Binance.TradeRobot.Business/Business/TradeBusiness/Spot/D21TradeBusiness.cs

@ -218,6 +218,7 @@ namespace Binance.TradeRobot.Business
var buyOrder = new SpotOrder() var buyOrder = new SpotOrder()
{ {
Id = orderId, Id = orderId,
ClientOrderId = clientOrderId,
CreateTime = DateTime.Now, CreateTime = DateTime.Now,
ExchangeId = robot.ExchangeId, ExchangeId = robot.ExchangeId,
LoanAmount = diffAmount, LoanAmount = diffAmount,
@ -332,6 +333,7 @@ namespace Binance.TradeRobot.Business
var sellOrder = new SpotOrder() var sellOrder = new SpotOrder()
{ {
Id = orderId, Id = orderId,
ClientOrderId = clientOrderId,
CreateTime = DateTime.Now, CreateTime = DateTime.Now,
ExchangeId = robot.ExchangeId, ExchangeId = robot.ExchangeId,
OrderType = Enums.OrderType.MARKET, OrderType = Enums.OrderType.MARKET,

7
Binance.TradeRobot.Model/Db/Order/SpotOrder.cs

@ -12,6 +12,13 @@ namespace Binance.TradeRobot.Model.Db
[Column(DbType = "bigint", IsPrimary = true)] [Column(DbType = "bigint", IsPrimary = true)]
public long Id { get; set; } public long Id { get; set; }
/// <summary>
/// 客户端订单号
/// </summary>
[Column(StringLength = 50)]
public string ClientOrderId { get; set; }
[Column(DbType = "datetime")] [Column(DbType = "datetime")]
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }

3
SDKAdapter/APIClient/BaseAPIClient.cs

@ -98,8 +98,9 @@ namespace SDKAdapter.APIClient
/// </summary> /// </summary>
/// <param name="symbol"></param> /// <param name="symbol"></param>
/// <param name="orderId"></param> /// <param name="orderId"></param>
/// <param name="clientOrderId"></param>
/// <exception cref="NotImplementedException"></exception> /// <exception cref="NotImplementedException"></exception>
public virtual void CancelIsolateMarginOrder(string symbol, long orderId) public virtual void CancelIsolateMarginOrder(string symbol, long orderId, string clientOrderId)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

4
SDKAdapter/APIClient/BinanceAPIClient.cs

@ -135,9 +135,9 @@ namespace SDKAdapter.APIClient
return r.Data.Id; return r.Data.Id;
} }
public override void CancelIsolateMarginOrder(string symbol, long orderId) public override void CancelIsolateMarginOrder(string symbol, long orderId, string clientOrderId)
{ {
_ = binanceClient.SpotApi.Trading.CancelMarginOrderAsync(symbol, orderId, isIsolated: true).Result; _ = binanceClient.SpotApi.Trading.CancelMarginOrderAsync(symbol, orderId, isIsolated: true, newClientOrderId: clientOrderId).Result;
} }
} }
} }

74
SDKTestConsole/Program.cs

@ -4,7 +4,9 @@ using Binance.TradeRobot.Model.Base;
using CryptoExchange.Net.Authentication; using CryptoExchange.Net.Authentication;
using Newtonsoft.Json; using Newtonsoft.Json;
using SDKAdapter.APIClient; using SDKAdapter.APIClient;
using SDKAdapter.WebSockets.Order.Spot;
using System; using System;
using System.Threading.Tasks;
namespace SDKTestConsole namespace SDKTestConsole
{ {
@ -12,34 +14,68 @@ namespace SDKTestConsole
{ {
private static System.Threading.Timer timer; private static System.Threading.Timer timer;
private static SpotOrderWebSocketClient orderWebSocketClient;
static void Main(string[] args) static void Main(string[] args)
{ {
var apiKey = "RsQ5RuhYbNRXCTGAQXhwb5Dt3jgPnwKXfR1OXz0qWmf3IsAC7zPQd14WGIr18rDA"; var apiKey = "pXKdUPbGN4DNar2f2HRoL2qxHoCWcLNf9V5uHTL2lBVCxz66eE8PMPwBw9h8RbKf";
var secret = "yxW4PPb65rVpdo7fMt2mZcbNOtjOe3J4fMSRVtX5YJfj7kRzHW3dY6xfoW4jje1I"; var secret = "c14tJd7kpMnePKRDoo4nzZk4bIn9bDO8ZCYo69amB4uspLgO5s4GlzYvTwD2zYav";
var client = BaseAPIClient.Create(Enums.Exchange.Binance, 0, apiKey, secret); var client = BaseAPIClient.Create(Enums.Exchange.Binance, 0, apiKey, secret);
//逐仓杠杆账户资产 //逐仓杠杆账户资产
var marginList = client.GetIsolatedMarginAccountAssets(); //var marginList = client.GetIsolatedMarginAccountAssets();
var s = JsonConvert.SerializeObject(marginList); //var s = JsonConvert.SerializeObject(marginList);
var spotClientOption = new BinanceApiClientOptions() //var spotClientOption = new BinanceApiClientOptions()
{ //{
BaseAddress = "https://api.binance.com", // BaseAddress = "https://api.binance.com",
ApiCredentials = new ApiCredentials(apiKey, secret) // ApiCredentials = new ApiCredentials(apiKey, secret)
}; //};
var usdFuturesClientOption = new BinanceApiClientOptions() //var usdFuturesClientOption = new BinanceApiClientOptions()
{ //{
BaseAddress = "https://fapi.binance.com", // BaseAddress = "https://fapi.binance.com",
ApiCredentials = new ApiCredentials(apiKey, secret) // ApiCredentials = new ApiCredentials(apiKey, secret)
}; //};
var binanceClient = new BinanceClient(new BinanceClientOptions() //var binanceClient = new BinanceClient(new BinanceClientOptions()
{ //{
UsdFuturesApiOptions = usdFuturesClientOption, // UsdFuturesApiOptions = usdFuturesClientOption,
SpotApiOptions = spotClientOption // SpotApiOptions = spotClientOption
}); //});
var binanceSocketClient = new BinanceSocketClient(); var binanceSocketClient = new BinanceSocketClient();
//var newClientOrderId = "abcd_286129845448773_1";
//var orderId = client.IsolatedMarginPlaceOrder("ETHUSDT", Enums.TradeDirection.Buy, Enums.OrderType.MARKET, quoteAmount: 20M, newClientOrderId: newClientOrderId);
var logger = NLog.LogManager.GetLogger("test");
var stopLossClientOrderId = "stoploss2_286129845448773_1";
long stopLossOrderId = 0;
while (true)
{
var input = Console.ReadLine();
if (input == "start")
{
orderWebSocketClient = SpotOrderWebSocketClient.Create(Enums.BusinessType.IsolateMargin,
Enums.Exchange.Binance,
0,
apiKey,
secret,
logger,
null);
orderWebSocketClient.Start("ETHUSDT");
}
if (input == "down")
{
stopLossOrderId = client.IsolatedMarginPlaceOrder("ETHUSDT", Enums.TradeDirection.Sell, Enums.OrderType.STOP_LOSS_LIMIT, quantity: 0.0101M, price: 1876M, stopPrice: 1876M, newClientOrderId: stopLossClientOrderId);
}
if (input == "cancel")
{
client.CancelIsolateMarginOrder("ETHUSDT", stopLossOrderId, stopLossClientOrderId);
}
}
Console.ReadKey(); Console.ReadKey();
} }
} }

Loading…
Cancel
Save