diff --git a/Binance.TradeRobot.Business/Business/TradeBusiness/Spot/D21TradeBusiness.cs b/Binance.TradeRobot.Business/Business/TradeBusiness/Spot/D21TradeBusiness.cs index c5b38a1..d851a28 100644 --- a/Binance.TradeRobot.Business/Business/TradeBusiness/Spot/D21TradeBusiness.cs +++ b/Binance.TradeRobot.Business/Business/TradeBusiness/Spot/D21TradeBusiness.cs @@ -131,7 +131,7 @@ namespace Binance.TradeRobot.Business #region 验证借币 step = "验证借币"; diffAmount = previewTradeAmount - balance; - var diffRatio = diffAmount / balance * 100; //借币比例 + var diffRatio = Math.Round(diffAmount / balance * 100, 2); //借币比例 #region 验证策略中的最大借币比例 if (diffRatio > d21Robot.D21Policy.MaxExchangeLoanRatio) @@ -142,7 +142,7 @@ namespace Binance.TradeRobot.Business SourceSingal = Enums.SingalType.多交叉, RobotId = robot.Id, CreateTime = DateTime.Now, - Content = $"触发策略中交易所最大借币比例限制,交易所最大借币比例{d21Robot.D21Policy.MaxExchangeLoanRatio}%,当前借币比例{diffRatio}%,按交易所最大借币比例借币" + Content = $"触发策略中交易所最大借币比例限制,交易所最大借币比例{d21Robot.D21Policy.MaxExchangeLoanRatio}%,当前借币比例{diffRatio}%,下单仓位:{previewTradeAmount},账户余额:{balance},按交易所最大借币比例借币" }); diffAmount = previewTradeAmount * (d21Robot.D21Policy.MaxExchangeLoanRatio / 100M); //previewTradeAmount = balance + diffAmount; //在策略允许的借币比例范围内的最大下单金额 diff --git a/SDKAdapter/WebSockets/Order/Spot/BinanceSpotOrderWebSocketClient.cs b/SDKAdapter/WebSockets/Order/Spot/BinanceSpotOrderWebSocketClient.cs index 4680749..aba61b2 100644 --- a/SDKAdapter/WebSockets/Order/Spot/BinanceSpotOrderWebSocketClient.cs +++ b/SDKAdapter/WebSockets/Order/Spot/BinanceSpotOrderWebSocketClient.cs @@ -19,6 +19,9 @@ namespace SDKAdapter.WebSockets.Order.Spot private CancellationTokenSource cancellationTokenSource; private string listenKey; private IList ignoreOrderStateList; + private System.Threading.Timer timer; + private readonly long extendListenKeyPeriod = 1000 * 60 * 30; //30分钟 + private string isolateMarginSymbol = ""; //逐仓杠杆专用 public BinanceSpotOrderWebSocketClient(Enums.BusinessType businessType, long accountId, @@ -57,6 +60,8 @@ namespace SDKAdapter.WebSockets.Order.Spot Binance.Net.Enums.OrderStatus.Insurance, Binance.Net.Enums.OrderStatus.Adl }; + timer = new Timer(new TimerCallback(ExtendListenKey), null, -1, extendListenKeyPeriod); + } public override void Start(string symbol = "") @@ -64,8 +69,9 @@ namespace SDKAdapter.WebSockets.Order.Spot if (IsConnected) return; IsConnected = true; + isolateMarginSymbol = symbol; cancellationTokenSource = new CancellationTokenSource(); - var getListenKeyResponse = binanceClient.SpotApi.Account.StartIsolatedMarginUserStreamAsync(symbol).Result; + var getListenKeyResponse = binanceClient.SpotApi.Account.StartIsolatedMarginUserStreamAsync(isolateMarginSymbol).Result; if (!getListenKeyResponse.Success) throw new Exception(getListenKeyResponse.Error?.Message ?? ""); listenKey = getListenKeyResponse.Data; @@ -169,6 +175,11 @@ namespace SDKAdapter.WebSockets.Order.Spot }, cancellationTokenSource.Token); + try + { + timer.Change(extendListenKeyPeriod, extendListenKeyPeriod); + } + catch { } } public override void Stop(string symbol = "") @@ -181,6 +192,19 @@ namespace SDKAdapter.WebSockets.Order.Spot cancellationTokenSource = null; _ = binanceClient.SpotApi.Account.CloseIsolatedMarginUserStreamAsync(symbol, listenKey).Result; listenKey = string.Empty; + try + { + timer.Change(-1, extendListenKeyPeriod); + } + catch { } + } + + private void ExtendListenKey(object? o) + { + if (string.IsNullOrEmpty(listenKey)) + return; + if (BusinessType == Enums.BusinessType.IsolateMargin) + _ = binanceClient.SpotApi.Account.KeepAliveIsolatedMarginUserStreamAsync(isolateMarginSymbol, listenKey); } } } diff --git a/SDKTestConsole/Program.cs b/SDKTestConsole/Program.cs index d855305..774b9a4 100644 --- a/SDKTestConsole/Program.cs +++ b/SDKTestConsole/Program.cs @@ -5,26 +5,25 @@ using CryptoExchange.Net.Authentication; using Newtonsoft.Json; using SDKAdapter.APIClient; using System; -using Binance.TradeRobot.Common.Extensions; -using SDKAdapter.WebSockets.Order.SpotOrder; -using NLog; namespace SDKTestConsole { internal class Program { + private static System.Threading.Timer timer; + static void Main(string[] args) { - var apiKey = "NnLXgcdUAZ8FAye4Qge3zrrdg5o7ufoWbgtYsKzgfIXz0OMz27G1Kx4SykMzw7YS"; - var secret = "lpJ3t50osPx6lEUerVFMdoKsZ6uHPc769OFPGtfhcoPANpv97CEcvR3pz3Bezhhv"; + var apiKey = "RsQ5RuhYbNRXCTGAQXhwb5Dt3jgPnwKXfR1OXz0qWmf3IsAC7zPQd14WGIr18rDA"; + var secret = "yxW4PPb65rVpdo7fMt2mZcbNOtjOe3J4fMSRVtX5YJfj7kRzHW3dY6xfoW4jje1I"; var client = BaseAPIClient.Create(Enums.Exchange.Binance, 0, apiKey, secret); //逐仓杠杆账户资产 - //var marginList = client.GetIsolatedMarginAccountAssets(); - //var s = JsonConvert.SerializeObject(marginList); + var marginList = client.GetIsolatedMarginAccountAssets(); + var s = JsonConvert.SerializeObject(marginList); //查询最大借币额度 - //var maxLoan = client.QueryMaxLoanAmount("ETHUSDT"); + var maxLoan = client.QueryMaxLoanAmount("GMTUSDT"); //Console.WriteLine(maxLoan); //借币 @@ -134,7 +133,27 @@ namespace SDKTestConsole //Console.WriteLine(s); //var orderClient = new BinanceSpotOrderWebSocketClient(Enums.BusinessType.IsolateMargin, 0, apiKey, secret, LogManager.GetCurrentClassLogger()); //orderClient.Start("ETHUSDT"); + + //timer = new System.Threading.Timer(new System.Threading.TimerCallback(Callback), null, -1, 5000); + //while (true) + //{ + // var input = Console.ReadLine(); + // if (input == "start") + // timer.Change(3000, 5000); + // else if (input == "stop") + // timer.Change(-1, 5000); + // else if (input == "close") + // { + // timer.Dispose(); + // break; + // } + //} Console.ReadKey(); } + + private static void Callback(object? o) + { + Console.WriteLine($"Callback on {DateTime.Now}"); + } } }