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

57 lines
2.2 KiB

using Binance.TradeRobot.Model.Base;
using Binance.TradeRobot.Model.Db;
using Binance.TradeRobot.Model.Dto;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
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.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}";
}
}
}