币安量化交易
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.

73 lines
3.2 KiB

using Binance.TradeRobot.Model.Base;
using Binance.TradeRobot.Model.Db;
using Binance.TradeRobot.Model.Dto;
using Microsoft.Extensions.Caching.Memory;
using SDKAdapter.APIClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yitter.IdGenerator;
namespace Binance.TradeRobot.Business
{
public class BaseTradeBusiness : BaseBusiness
{
protected DingBusiness dingBusiness { get; private set; }
protected GlobalContext globalContext { get; private set; }
public BaseTradeBusiness(IFreeSql fsql, NLogManager logManager, IIdGenerator idGenerator, IMemoryCache memoryCache, DingBusiness dingBusiness, GlobalContext globalContext) : base(fsql, logManager, idGenerator, memoryCache)
{
this.dingBusiness = dingBusiness;
this.globalContext = globalContext;
}
public void HandleError(Exception ex,
Enums.SingalType singalType,
List<ExecutionLog> logList,
RobotResponse robot,
string step)
{
logList.Add(new ExecutionLog()
{
Id = idGenerator.NewLong(),
SourceSingal = singalType,
RobotId = robot.Id,
CreateTime = DateTime.Now,
Content = ex.Message
});
try { fsql.Insert(logList).ExecuteAffrows(); } catch { }
var errorMsg = $"交易警报,{singalType},{robot.ExecuteKey},robot {robot.Id},{step}";
logManager.GetLogger(robot.ExecuteKey).Error(ex, errorMsg);
dingBusiness.Send($"{errorMsg} {ex.Message}");
}
/// <summary>
/// 创建客户端订单号
/// </summary>
/// <param name="robotId"></param>
/// <param name="tradePolicy"></param>
/// <returns></returns>
protected string CreateClientOrderId(long robotId, Enums.TradePolicy tradePolicy)
{
var guid = Guid.NewGuid();
var random = new Random(guid.GetHashCode());
return $"{Convert.ToChar(random.Next(97, 123))}{guid.ToString().Substring(0, 4)}_{robotId}_{(int)tradePolicy}";
}
protected void CancelStopLossOrder(RobotResponse d21Robot, BaseAPIClient baseAPIClient = null)
{
if (baseAPIClient == null)
baseAPIClient = GetBaseAPIClient(d21Robot.ExchangeId, d21Robot.ExchangeAPIKey.AccountId, d21Robot.ExchangeAPIKey.APIKey, d21Robot.ExchangeAPIKey.SecretKey);
var stopLossOrderList = fsql.Select<SpotOrder>().Where(o => o.OrderType == Enums.OrderType.STOP_LOSS_LIMIT &&
o.State == Enums.SpotOrderState.Created &&
o.RobotId == d21Robot.Id).ToList(o => new { o.Id, o.ClientOrderId });
if (stopLossOrderList == null || stopLossOrderList.Count() == 0)
return;
foreach (var stopLossOrder in stopLossOrderList)
baseAPIClient.CancelIsolateMarginOrder(d21Robot.Symbol, stopLossOrder.Id, stopLossOrder.ClientOrderId);
}
}
}