From db7b35236df74b4ce0918aae25206fb5236ec4c2 Mon Sep 17 00:00:00 2001
From: shanji <18996038927@163.com>
Date: Fri, 13 May 2022 18:45:24 +0800
Subject: [PATCH] =?UTF-8?q?=E5=80=9F=E5=B8=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../TradeBusiness/D21TradeBusiness.cs | 17 +++++++++++---
.../Exchange/ExchangeAPIKeyResponse.cs | 1 +
SDKAdapter/APIClient/BaseAPIClient.cs | 22 +++++++++++++++++++
SDKAdapter/APIClient/BinanceAPIClient.cs | 22 +++++++++++++++++++
.../Model/IsolatedMarginLoanResponse.cs | 18 +++++++++++++++
SDKTestConsole/Program.cs | 6 +----
6 files changed, 78 insertions(+), 8 deletions(-)
create mode 100644 SDKAdapter/Model/IsolatedMarginLoanResponse.cs
diff --git a/Binance.TradeRobot.Business/Business/TradeBusiness/D21TradeBusiness.cs b/Binance.TradeRobot.Business/Business/TradeBusiness/D21TradeBusiness.cs
index f3765b2..5d2bf40 100644
--- a/Binance.TradeRobot.Business/Business/TradeBusiness/D21TradeBusiness.cs
+++ b/Binance.TradeRobot.Business/Business/TradeBusiness/D21TradeBusiness.cs
@@ -140,7 +140,7 @@ namespace Binance.TradeRobot.Business
#region 计算下单数量
step = "计算下单数量";
- var diffAmount = 0M; //借币金额
+ var diffAmount = 0M; //下单缺口金额
var previewTradeAmount = d21Robot.D21Policy.Position; //预估交易额
if (balance < previewTradeAmount)
{
@@ -170,9 +170,12 @@ namespace Binance.TradeRobot.Business
{
var exchangeMaxLoanAmount = apiClient.QueryMaxLoanAmount(robot.Symbol);
if (exchangeMaxLoanAmount < diffAmount)
- {
-
+ {
+ if (exchangeMaxLoanAmount == 0M)
+ throw new Exception("可借额度为0");
+ diffAmount = exchangeMaxLoanAmount;
}
+ previewTradeAmount = balance + diffAmount;
}
catch (Exception borrowex)
{
@@ -185,6 +188,7 @@ namespace Binance.TradeRobot.Business
Content = $"验证交易所的最大可借额度失败 {borrowex.Message}"
});
previewTradeAmount = balance; //无法借币,使用余额下单
+ diffAmount = 0M;
}
#endregion
@@ -192,6 +196,13 @@ namespace Binance.TradeRobot.Business
}
#endregion
+ #region 借币
+ if (diffAmount > 0M)
+ {
+
+ }
+ #endregion
+
#region 下单
#endregion
diff --git a/Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAPIKeyResponse.cs b/Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAPIKeyResponse.cs
index eeea34f..9d931e4 100644
--- a/Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAPIKeyResponse.cs
+++ b/Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAPIKeyResponse.cs
@@ -2,6 +2,7 @@
{
public class ExchangeAPIKeyResponse : Db.ExchangeAPIKey
{
+ //public ExchangeAPIKeyResponse(long id) { this.Id = id; }
///
/// 基础币金额(标的数量)
///
diff --git a/SDKAdapter/APIClient/BaseAPIClient.cs b/SDKAdapter/APIClient/BaseAPIClient.cs
index c6c3469..3fa81ef 100644
--- a/SDKAdapter/APIClient/BaseAPIClient.cs
+++ b/SDKAdapter/APIClient/BaseAPIClient.cs
@@ -45,5 +45,27 @@ namespace SDKAdapter.APIClient
{
throw new NotImplementedException();
}
+
+ ///
+ /// 逐仓杠杆账户借币
+ ///
+ ///
+ ///
+ /// 借币结果对象
+ public virtual IsolatedMarginLoanResponse IsolatedMarginLoan(string symbol, decimal loanAmount)
+ {
+ throw new NotImplementedException();
+ }
+
+ ///
+ /// 逐仓杠杆账户还币
+ ///
+ ///
+ ///
+ /// 还币利息
+ public virtual decimal IsolatedMarginRepayLoan(string symbol, decimal repayAmount)
+ {
+ throw new NotImplementedException();
+ }
}
}
diff --git a/SDKAdapter/APIClient/BinanceAPIClient.cs b/SDKAdapter/APIClient/BinanceAPIClient.cs
index d732024..3df96fa 100644
--- a/SDKAdapter/APIClient/BinanceAPIClient.cs
+++ b/SDKAdapter/APIClient/BinanceAPIClient.cs
@@ -64,5 +64,27 @@ namespace SDKAdapter.APIClient
throw new Exception($"查询最大借币额度失败 {r.Error?.Message}");
return r.Data.Quantity;
}
+
+ public override IsolatedMarginLoanResponse IsolatedMarginLoan(string symbol, decimal loanAmount)
+ {
+ var r = binanceClient.SpotApi.Account.MarginBorrowAsync("USDT", loanAmount, true, symbol).Result;
+ if (!r.Success)
+ throw new Exception($"借币失败 {r.Error?.Message}");
+
+ var isolatedMarginAccountAssetList = GetIsolatedMarginAccountAssets();
+ var currentAsset = isolatedMarginAccountAssetList.FirstOrDefault(s => s.Symbol == symbol);
+ if (currentAsset == null)
+ throw new Exception($"借币已完成,但未查询到{symbol}的资产信息 {r.Error?.Message}");
+ return new IsolatedMarginLoanResponse()
+ {
+ CurrentLoanAmount = loanAmount,
+ AccountLoanAmount = currentAsset.QuoteBorrowed
+ };
+ }
+
+ public override decimal IsolatedMarginRepayLoan(string symbol, decimal repayAmount)
+ {
+ return base.IsolatedMarginRepayLoan(symbol, repayAmount);
+ }
}
}
diff --git a/SDKAdapter/Model/IsolatedMarginLoanResponse.cs b/SDKAdapter/Model/IsolatedMarginLoanResponse.cs
new file mode 100644
index 0000000..028995d
--- /dev/null
+++ b/SDKAdapter/Model/IsolatedMarginLoanResponse.cs
@@ -0,0 +1,18 @@
+namespace SDKAdapter.Model
+{
+ ///
+ /// 逐仓杠杆借币结果对象
+ ///
+ public class IsolatedMarginLoanResponse
+ {
+ ///
+ /// 本次借币金额
+ ///
+ public decimal CurrentLoanAmount { get; set; }
+
+ ///
+ /// 账户借币金额
+ ///
+ public decimal AccountLoanAmount { get; set; }
+ }
+}
diff --git a/SDKTestConsole/Program.cs b/SDKTestConsole/Program.cs
index 069a352..232873c 100644
--- a/SDKTestConsole/Program.cs
+++ b/SDKTestConsole/Program.cs
@@ -1,8 +1,4 @@
-using Binance.Net.Clients;
-using Binance.Net.Objects;
-using Binance.TradeRobot.Model.Base;
-using CryptoExchange.Net.Authentication;
-using Newtonsoft.Json;
+using Binance.TradeRobot.Model.Base;
using SDKAdapter.APIClient;
using System;