diff --git a/Binance.TradeRobot.API/Controllers/RobotController.cs b/Binance.TradeRobot.API/Controllers/RobotController.cs index 8acb9b2..a228733 100644 --- a/Binance.TradeRobot.API/Controllers/RobotController.cs +++ b/Binance.TradeRobot.API/Controllers/RobotController.cs @@ -37,6 +37,15 @@ namespace Binance.TradeRobot.API.Controllers robotBusiness.StopRobot(robotId); } + /// + /// 刷新机器人运行时长 + /// + [HttpPost] + public void RefreshRobotRuningTime() + { + robotBusiness.RefreshRobotRuningTime(); + } + /// /// 创建金字塔策略机器人 /// @@ -66,5 +75,14 @@ namespace Binance.TradeRobot.API.Controllers { return robotBusiness.GetD21PolicyRobotList(); } + + /// + /// D21补救检查 + /// + [HttpPost] + public void D21Remedy() + { + robotBusiness.D21Remedy(); + } } } diff --git a/Binance.TradeRobot.Business/Business/RobotBusiness.cs b/Binance.TradeRobot.Business/Business/RobotBusiness.cs index 5ffe730..0a77602 100644 --- a/Binance.TradeRobot.Business/Business/RobotBusiness.cs +++ b/Binance.TradeRobot.Business/Business/RobotBusiness.cs @@ -4,12 +4,15 @@ using Binance.TradeRobot.Common.Extensions; using Binance.TradeRobot.Model.Base; using Binance.TradeRobot.Model.Db; using Binance.TradeRobot.Model.Dto; +using Binance.TradeRobot.Model.RuningInfo; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Data.Common; using System.Linq; +using System.Threading; +using System.Threading.Tasks; using Yitter.IdGenerator; namespace Binance.TradeRobot.Business @@ -20,9 +23,23 @@ namespace Binance.TradeRobot.Business private Lazy globalContextLazy; private GlobalContext globalContext => globalContextLazy.Value; - public RobotBusiness(IFreeSql fsql, NLogManager logManager, IIdGenerator idGenerator, IMemoryCache memoryCache, IServiceProvider serviceProvider) : base(fsql, logManager, idGenerator, memoryCache) + private IEnumerable tradeBusinessList; + private TaskSchedulerManager taskSchedulerManager; + private ExchangeBusiness exchangeBusiness; + + public RobotBusiness(IFreeSql fsql, + NLogManager logManager, + IIdGenerator idGenerator, + IMemoryCache memoryCache, + IEnumerable tradeBusinessList, + TaskSchedulerManager taskSchedulerManager, + ExchangeBusiness exchangeBusiness, + IServiceProvider serviceProvider) : base(fsql, logManager, idGenerator, memoryCache) { this.globalContextLazy = new Lazy(() => serviceProvider.GetService()); + this.tradeBusinessList = tradeBusinessList; + this.taskSchedulerManager = taskSchedulerManager; + this.exchangeBusiness = exchangeBusiness; } /// @@ -82,6 +99,14 @@ namespace Binance.TradeRobot.Business globalContext.UnSubscribeOrderPublish(robot); } + /// + /// 刷新机器人运行时长 + /// + public void RefreshRobotRuningTime() + { + fsql.Update().Set(r => r.RunningTime + 60).Where(r => r.State == Enums.RobotState.Runing).ExecuteAffrows(); + } + /// /// 添加机器人和账户 /// @@ -286,5 +311,55 @@ namespace Binance.TradeRobot.Business } return robotList; } + + public void D21Remedy() + { + var d21RobotList = GetD21PolicyRobotList(robotState: Enums.RobotState.Runing, isLoadRecentTradeProfit: false, isLoadAPIKey: true); + if (d21RobotList == null || d21RobotList.Count() == 0) + throw new BusinessException("未找到符合条件的机器人"); + + foreach (var d21Robot in d21RobotList) + { + var runingInfo = RedisHelper.Get(d21Robot.Id.ToString()) ?? new D21RuningInfo() { RobotId = d21Robot.Id }; + if (runingInfo.ErrorCrossSingal == null || runingInfo.ErrorCrossSingalTime == 0) + continue; + + var previouKID = DateTime.Now.GetKID(d21Robot.D21Policy.PeriodicSignal, true); + var currentKID = DateTime.Now.GetKID(d21Robot.D21Policy.PeriodicSignal, false); + var cycle = currentKID - previouKID; //单位周期的时间戳差距 + + if ((previouKID - runingInfo.ErrorCrossSingalTime) / cycle > 3) + { + var errorCrossSingal = runingInfo.ErrorCrossSingal.Value; + runingInfo.ErrorCrossSingal = null; + runingInfo.ErrorCrossSingalTime = 0; + RedisHelper.Set(d21Robot.Id.ToString(), runingInfo); + + var symbolInfo = exchangeBusiness.GetSymbol(d21Robot.ExchangeId, d21Robot.Symbol); + var d21TradeBusiness = tradeBusinessList.FirstOrDefault(t => t.TradePolicy == Enums.TradePolicy.D21); + + if (errorCrossSingal == Enums.SingalType.多交叉) + { + Task.Factory.StartNew(() => d21TradeBusiness.LongCross(new D21SingalRequest() + { + RobotId = d21Robot.Id, + KLinePeriodic = d21Robot.D21Policy.PeriodicSignal, + SingalType = errorCrossSingal, + Symbol = d21Robot.Symbol + }, d21Robot, true, symbolInfo), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SingalTaskScheduler); + } + else if (errorCrossSingal == Enums.SingalType.空交叉) + { + Task.Factory.StartNew(() => d21TradeBusiness.ShortCross(new D21SingalRequest() + { + RobotId = d21Robot.Id, + KLinePeriodic = d21Robot.D21Policy.PeriodicSignal, + SingalType = errorCrossSingal, + Symbol = d21Robot.Symbol + }, d21Robot, true, symbolInfo), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SingalTaskScheduler); + } + } + } + } } } diff --git a/Binance.TradeRobot.Business/Business/SingalBusiness.cs b/Binance.TradeRobot.Business/Business/SingalBusiness.cs index 0ae3fef..bd728fb 100644 --- a/Binance.TradeRobot.Business/Business/SingalBusiness.cs +++ b/Binance.TradeRobot.Business/Business/SingalBusiness.cs @@ -1,6 +1,7 @@ using Binance.TradeRobot.Common.DI; using Binance.TradeRobot.Model.Base; using Binance.TradeRobot.Model.Dto; +using Binance.TradeRobot.Model.RuningInfo; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.DependencyInjection; using System.Collections.Generic; @@ -75,11 +76,6 @@ namespace Binance.TradeRobot.Business } } - public void D21Remedy() - { - var d21RobotList = robotBusiness.GetD21PolicyRobotList(robotState: Enums.RobotState.Runing, isLoadRecentTradeProfit: false, isLoadAPIKey: true); - if (d21RobotList == null || d21RobotList.Count() == 0) - throw new BusinessException("未找到符合条件的机器人"); - } + } } diff --git a/Binance.TradeRobot.Model/Dto/Response/Robot/RobotResponse.cs b/Binance.TradeRobot.Model/Dto/Response/Robot/RobotResponse.cs index 4c702ea..0761657 100644 --- a/Binance.TradeRobot.Model/Dto/Response/Robot/RobotResponse.cs +++ b/Binance.TradeRobot.Model/Dto/Response/Robot/RobotResponse.cs @@ -27,9 +27,6 @@ namespace Binance.TradeRobot.Model.Dto public Enums.Exchange ExchangeId { get; set; } - /// - /// 订单推送日志Key - /// //public virtual string OrderPublishLogKey //{ // get @@ -38,8 +35,6 @@ namespace Binance.TradeRobot.Model.Dto // } //} - public SymbolInfoResponse SymbolInfo { get; set; } - /// /// 机器人账户对象 ///