币安量化交易
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.6 KiB

using Binance.Net.Clients;
using Binance.Net.Objects;
using CryptoExchange.Net.Authentication;
using SDKAdapter.Model;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SDKAdapter.APIClient
{
public class BinanceAPIClient : BaseAPIClient
{
private BinanceClient binanceClient;
public BinanceAPIClient(long uid, string apiKey, string secret) : base(uid, apiKey, secret)
{
var spotClientOption = new BinanceApiClientOptions()
{
BaseAddress = "https://api.binance.com",
ApiCredentials = new ApiCredentials(apiKey, secret)
};
var usdFuturesClientOption = new BinanceApiClientOptions()
{
BaseAddress = "https://fapi.binance.com",
ApiCredentials = new ApiCredentials(apiKey, secret)
};
binanceClient = new BinanceClient(new BinanceClientOptions()
{
UsdFuturesApiOptions = usdFuturesClientOption,
SpotApiOptions = spotClientOption
});
}
public override IList<IsolatedMarginAccountAsset> GetIsolatedMarginAccountAssets()
{
var r = binanceClient.SpotApi.Account.GetIsolatedMarginAccountAsync().Result;
if (!r.Success)
throw new Exception($"获取逐仓杠杆账户信息失败 {r.Error?.Message}");
return r.Data.Assets.Select(asset => new IsolatedMarginAccountAsset()
{
Symbol = asset.Symbol,
BaseAsset = asset.BaseAsset.Asset,
BaseBorrowed = asset.BaseAsset.Borrowed,
BaseFree = asset.BaseAsset.Free,
BaseInterest = asset.BaseAsset.Interest,
BaseLocked = asset.BaseAsset.Locked,
BaseNetAsset = asset.BaseAsset.NetAsset,
QuoteAsset = asset.QuoteAsset.Asset,
QuoteBorrowed = asset.QuoteAsset.Borrowed,
QuoteFree = asset.QuoteAsset.Free,
QuoteInterest = asset.QuoteAsset.Interest,
QuoteLocked = asset.QuoteAsset.Locked,
QuoteNetAsset = asset.QuoteAsset.NetAsset
}).ToList();
}
3 years ago
public override decimal QueryMaxLoanAmount(string symbol)
{
var r = binanceClient.SpotApi.Account.GetMarginMaxBorrowAmountAsync("USDT", symbol).Result;
if (!r.Success)
throw new Exception($"查询最大借币额度失败 {r.Error?.Message}");
return r.Data.Quantity;
}
3 years ago
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);
}
}
}