Browse Source

binance订单监控keep live

master
shanji 3 years ago
parent
commit
b9b17a4e30
  1. 4
      Binance.TradeRobot.Business/Business/TradeBusiness/Spot/D21TradeBusiness.cs
  2. 26
      SDKAdapter/WebSockets/Order/Spot/BinanceSpotOrderWebSocketClient.cs
  3. 35
      SDKTestConsole/Program.cs

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

@ -131,7 +131,7 @@ namespace Binance.TradeRobot.Business
#region 验证借币 #region 验证借币
step = "验证借币"; step = "验证借币";
diffAmount = previewTradeAmount - balance; diffAmount = previewTradeAmount - balance;
var diffRatio = diffAmount / balance * 100; //借币比例 var diffRatio = Math.Round(diffAmount / balance * 100, 2); //借币比例
#region 验证策略中的最大借币比例 #region 验证策略中的最大借币比例
if (diffRatio > d21Robot.D21Policy.MaxExchangeLoanRatio) if (diffRatio > d21Robot.D21Policy.MaxExchangeLoanRatio)
@ -142,7 +142,7 @@ namespace Binance.TradeRobot.Business
SourceSingal = Enums.SingalType., SourceSingal = Enums.SingalType.,
RobotId = robot.Id, RobotId = robot.Id,
CreateTime = DateTime.Now, CreateTime = DateTime.Now,
Content = $"触发策略中交易所最大借币比例限制,交易所最大借币比例{d21Robot.D21Policy.MaxExchangeLoanRatio}%,当前借币比例{diffRatio}%,按交易所最大借币比例借币" Content = $"触发策略中交易所最大借币比例限制,交易所最大借币比例{d21Robot.D21Policy.MaxExchangeLoanRatio}%,当前借币比例{diffRatio}%,下单仓位:{previewTradeAmount},账户余额:{balance},按交易所最大借币比例借币"
}); });
diffAmount = previewTradeAmount * (d21Robot.D21Policy.MaxExchangeLoanRatio / 100M); diffAmount = previewTradeAmount * (d21Robot.D21Policy.MaxExchangeLoanRatio / 100M);
//previewTradeAmount = balance + diffAmount; //在策略允许的借币比例范围内的最大下单金额 //previewTradeAmount = balance + diffAmount; //在策略允许的借币比例范围内的最大下单金额

26
SDKAdapter/WebSockets/Order/Spot/BinanceSpotOrderWebSocketClient.cs

@ -19,6 +19,9 @@ namespace SDKAdapter.WebSockets.Order.Spot
private CancellationTokenSource cancellationTokenSource; private CancellationTokenSource cancellationTokenSource;
private string listenKey; private string listenKey;
private IList<Binance.Net.Enums.OrderStatus> ignoreOrderStateList; private IList<Binance.Net.Enums.OrderStatus> ignoreOrderStateList;
private System.Threading.Timer timer;
private readonly long extendListenKeyPeriod = 1000 * 60 * 30; //30分钟
private string isolateMarginSymbol = ""; //逐仓杠杆专用
public BinanceSpotOrderWebSocketClient(Enums.BusinessType businessType, public BinanceSpotOrderWebSocketClient(Enums.BusinessType businessType,
long accountId, long accountId,
@ -57,6 +60,8 @@ namespace SDKAdapter.WebSockets.Order.Spot
Binance.Net.Enums.OrderStatus.Insurance, Binance.Net.Enums.OrderStatus.Insurance,
Binance.Net.Enums.OrderStatus.Adl Binance.Net.Enums.OrderStatus.Adl
}; };
timer = new Timer(new TimerCallback(ExtendListenKey), null, -1, extendListenKeyPeriod);
} }
public override void Start(string symbol = "") public override void Start(string symbol = "")
@ -64,8 +69,9 @@ namespace SDKAdapter.WebSockets.Order.Spot
if (IsConnected) if (IsConnected)
return; return;
IsConnected = true; IsConnected = true;
isolateMarginSymbol = symbol;
cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource = new CancellationTokenSource();
var getListenKeyResponse = binanceClient.SpotApi.Account.StartIsolatedMarginUserStreamAsync(symbol).Result; var getListenKeyResponse = binanceClient.SpotApi.Account.StartIsolatedMarginUserStreamAsync(isolateMarginSymbol).Result;
if (!getListenKeyResponse.Success) if (!getListenKeyResponse.Success)
throw new Exception(getListenKeyResponse.Error?.Message ?? ""); throw new Exception(getListenKeyResponse.Error?.Message ?? "");
listenKey = getListenKeyResponse.Data; listenKey = getListenKeyResponse.Data;
@ -169,6 +175,11 @@ namespace SDKAdapter.WebSockets.Order.Spot
}, },
cancellationTokenSource.Token); cancellationTokenSource.Token);
try
{
timer.Change(extendListenKeyPeriod, extendListenKeyPeriod);
}
catch { }
} }
public override void Stop(string symbol = "") public override void Stop(string symbol = "")
@ -181,6 +192,19 @@ namespace SDKAdapter.WebSockets.Order.Spot
cancellationTokenSource = null; cancellationTokenSource = null;
_ = binanceClient.SpotApi.Account.CloseIsolatedMarginUserStreamAsync(symbol, listenKey).Result; _ = binanceClient.SpotApi.Account.CloseIsolatedMarginUserStreamAsync(symbol, listenKey).Result;
listenKey = string.Empty; 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);
} }
} }
} }

35
SDKTestConsole/Program.cs

@ -5,26 +5,25 @@ using CryptoExchange.Net.Authentication;
using Newtonsoft.Json; using Newtonsoft.Json;
using SDKAdapter.APIClient; using SDKAdapter.APIClient;
using System; using System;
using Binance.TradeRobot.Common.Extensions;
using SDKAdapter.WebSockets.Order.SpotOrder;
using NLog;
namespace SDKTestConsole namespace SDKTestConsole
{ {
internal class Program internal class Program
{ {
private static System.Threading.Timer timer;
static void Main(string[] args) static void Main(string[] args)
{ {
var apiKey = "NnLXgcdUAZ8FAye4Qge3zrrdg5o7ufoWbgtYsKzgfIXz0OMz27G1Kx4SykMzw7YS"; var apiKey = "RsQ5RuhYbNRXCTGAQXhwb5Dt3jgPnwKXfR1OXz0qWmf3IsAC7zPQd14WGIr18rDA";
var secret = "lpJ3t50osPx6lEUerVFMdoKsZ6uHPc769OFPGtfhcoPANpv97CEcvR3pz3Bezhhv"; var secret = "yxW4PPb65rVpdo7fMt2mZcbNOtjOe3J4fMSRVtX5YJfj7kRzHW3dY6xfoW4jje1I";
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 maxLoan = client.QueryMaxLoanAmount("ETHUSDT"); var maxLoan = client.QueryMaxLoanAmount("GMTUSDT");
//Console.WriteLine(maxLoan); //Console.WriteLine(maxLoan);
//借币 //借币
@ -134,7 +133,27 @@ namespace SDKTestConsole
//Console.WriteLine(s); //Console.WriteLine(s);
//var orderClient = new BinanceSpotOrderWebSocketClient(Enums.BusinessType.IsolateMargin, 0, apiKey, secret, LogManager.GetCurrentClassLogger()); //var orderClient = new BinanceSpotOrderWebSocketClient(Enums.BusinessType.IsolateMargin, 0, apiKey, secret, LogManager.GetCurrentClassLogger());
//orderClient.Start("ETHUSDT"); //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(); Console.ReadKey();
} }
private static void Callback(object? o)
{
Console.WriteLine($"Callback on {DateTime.Now}");
}
} }
} }

Loading…
Cancel
Save