Browse Source

信号执行,D21机器人列表接口改造

master
shanji 3 years ago
parent
commit
f48f980f97
  1. 6
      Binance.TradeRobot.API/Binance.TradeRobot.API.xml
  2. 26
      Binance.TradeRobot.API/Controllers/SingalController.cs
  3. 1
      Binance.TradeRobot.Business/Business/BaseBusiness.cs
  4. 103
      Binance.TradeRobot.Business/Business/RobotBusiness.cs
  5. 48
      Binance.TradeRobot.Business/Business/SingalBusiness.cs
  6. 24
      Binance.TradeRobot.Model/Base/Enums.cs
  7. 5
      Binance.TradeRobot.Model/Base/MappingProfiles.cs
  8. 38
      Binance.TradeRobot.Model/Binance.TradeRobot.Model.xml
  9. 4
      Binance.TradeRobot.Model/Db/Order/ExecutionLog.cs
  10. 9
      Binance.TradeRobot.Model/Db/Robot/Robot.cs
  11. 41
      Binance.TradeRobot.Model/Dto/Request/Singal/BaseSingalRequest.cs
  12. 7
      Binance.TradeRobot.Model/Dto/Request/Singal/D21SingalRequest.cs
  13. 7
      Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAPIKeyResponse.cs
  14. 5
      Binance.TradeRobot.Model/Dto/Response/Robot/RobotResponse.cs

6
Binance.TradeRobot.API/Binance.TradeRobot.API.xml

@ -61,6 +61,12 @@
</summary>
<returns></returns>
</member>
<member name="M:Binance.TradeRobot.API.Controllers.SingalController.D21Singnal(Binance.TradeRobot.Model.Dto.D21SingalRequest)">
<summary>
D21杠杆/合约信号接口
</summary>
<param name="d21SingalRequest"></param>
</member>
<member name="M:Binance.TradeRobot.API.Controllers.SpotPolicyController.EditD21Policy(Binance.TradeRobot.Model.Dto.EditD21PolicyRequest)">
<summary>
编辑动2.1策略

26
Binance.TradeRobot.API/Controllers/SingalController.cs

@ -0,0 +1,26 @@
using Binance.TradeRobot.Business.Business;
using Binance.TradeRobot.Model.Dto;
using Microsoft.AspNetCore.Mvc;
namespace Binance.TradeRobot.API.Controllers
{
public class SingalController : BaseApiController
{
private SingalBusiness singalBusiness;
public SingalController(SingalBusiness singalBusiness)
{
this.singalBusiness = singalBusiness;
}
/// <summary>
/// D21杠杆/合约信号接口
/// </summary>
/// <param name="d21SingalRequest"></param>
[HttpPost]
public void D21Singnal([FromBody] D21SingalRequest d21SingalRequest)
{
singalBusiness.D21Singnal(d21SingalRequest);
}
}
}

1
Binance.TradeRobot.Business/Business/BaseBusiness.cs

@ -21,6 +21,7 @@ namespace Binance.TradeRobot.Business
this.fsql = fsql;
this.logManager = logManager;
this.idGenerator = idGenerator;
this.memoryCache = memoryCache;
expirationTimeSpan = TimeSpan.FromDays(1);
}

103
Binance.TradeRobot.Business/Business/RobotBusiness.cs

@ -133,47 +133,74 @@ namespace Binance.TradeRobot.Business
/// <summary>
/// 获取动2.1策略机器人列表
/// </summary>
/// <param name="robotState">机器人状态</param>
/// <param name="signalPeriod">信号周期</param>
/// <param name="symbol">交易对</param>
/// <param name="isLoadRecentTradeProfit">是否加载近期交易利润,默认true</param>
/// <param name="isLoadAPIKey">是否加载机器人绑定的APIKey,默认false</param>
/// <returns></returns>
public IList<D21PolicyRobotResponse> GetD21PolicyRobotList()
public IList<D21PolicyRobotResponse> GetD21PolicyRobotList(Enums.RobotState? robotState = null,
Enums.SignalPeriod? signalPeriod = null,
string symbol = "",
bool isLoadRecentTradeProfit = true,
bool isLoadAPIKey = false)
{
var robotList = fsql.Select<Robot, RobotAccount, D21Policy>().InnerJoin((r, ra, d) => r.Id == ra.RobotId)
.InnerJoin((r, ra, d) => r.Id == d.RobotId)
.ToList((r, ra, d) => new Robot()
{
Id = r.Id,
BusinessType = r.BusinessType,
ExchangeId = r.ExchangeId,
Symbol = r.Symbol,
State = r.State,
RunningTime = r.RunningTime,
CreateTime = r.CreateTime,
TradePolicy = r.TradePolicy,
RobotAccountId = ra.Id,
ClosePositionCount = ra.ClosePositionCount,
WinCount = ra.WinCount,
LoanAmount = ra.LoanAmount,
SoptCurrentcyAmount = ra.SoptCurrentcyAmount,
SpotCurrencyQuantity = ra.SpotCurrencyQuantity,
TotalProfit = ra.TotalProfit,
D21ExecutionMode = d.ExecutionMode,
D21IsEnabledIncreasePurchase = d.IsEnabledIncreasePurchase,
D21IsEnableRemedyForErrorCrossSignal = d.IsEnableRemedyForErrorCrossSignal,
D21MaxFollowPurchaseRatio = d.MaxFollowPurchaseRatio,
D21PeriodicSignal = d.PeriodicSignal,
D21PolicyId = d.Id,
D21Position = d.Position,
D21Assets = d.Assets,
D21Level1PositionStopLossRatio = d.Level1PositionStopLossRatio,
D21Level1PriceStopLossRatio = d.Level1PriceStopLossRatio,
D21Level2PositionStopLossRatio = d.Level2PositionStopLossRatio,
D21Level2PriceStopLossRatio = d.Level2PriceStopLossRatio,
D21MaxExchangeLoanRatio = d.MaxExchangeLoanRatio,
D21MaxSystemLoanRatio = d.MaxSystemLoanRatio,
D21CreateTime = d.CreateTime
}).Map<IList<D21PolicyRobotResponse>>();
var robotList = fsql.Select<Robot, RobotAccount, D21Policy, ExchangeAPIKey>().InnerJoin((r, ra, d, e) => r.Id == ra.RobotId)
.InnerJoin((r, ra, d, e) => r.Id == d.RobotId)
.InnerJoin((r, ra, d, e) => r.Id == e.RobotId)
.WhereIf(robotState != null, (r, ra, d, e) => r.State == robotState)
.WhereIf(signalPeriod != null, (r, ra, d, e) => d.PeriodicSignal == signalPeriod)
.WhereIf(!string.IsNullOrEmpty(symbol), (r, ra, d, e) => r.Symbol == symbol)
.Where((r, ra, d, e) => r.TradePolicy == Enums.TradePolicy.v21)
.ToList((r, ra, d, e) => new Robot()
{
Id = r.Id,
BusinessType = r.BusinessType,
ExchangeId = r.ExchangeId,
Symbol = r.Symbol,
State = r.State,
RunningTime = r.RunningTime,
CreateTime = r.CreateTime,
TradePolicy = r.TradePolicy,
RobotAccountId = ra.Id,
ClosePositionCount = ra.ClosePositionCount,
WinCount = ra.WinCount,
LoanAmount = ra.LoanAmount,
SoptCurrentcyAmount = ra.SoptCurrentcyAmount,
SpotCurrencyQuantity = ra.SpotCurrencyQuantity,
TotalProfit = ra.TotalProfit,
ExchangeAccountId = e.AccountId,
ExchangeAPIKey = e.APIKey,
ExchangeSecretKey = e.SecretKey,
D21ExecutionMode = d.ExecutionMode,
D21IsEnabledIncreasePurchase = d.IsEnabledIncreasePurchase,
D21IsEnableRemedyForErrorCrossSignal = d.IsEnableRemedyForErrorCrossSignal,
D21MaxFollowPurchaseRatio = d.MaxFollowPurchaseRatio,
D21PeriodicSignal = d.PeriodicSignal,
D21PolicyId = d.Id,
D21Position = d.Position,
D21Assets = d.Assets,
D21Level1PositionStopLossRatio = d.Level1PositionStopLossRatio,
D21Level1PriceStopLossRatio = d.Level1PriceStopLossRatio,
D21Level2PositionStopLossRatio = d.Level2PositionStopLossRatio,
D21Level2PriceStopLossRatio = d.Level2PriceStopLossRatio,
D21MaxExchangeLoanRatio = d.MaxExchangeLoanRatio,
D21MaxSystemLoanRatio = d.MaxSystemLoanRatio,
D21CreateTime = d.CreateTime
}).Map<IList<D21PolicyRobotResponse>>();
if (isLoadRecentTradeProfit)
{
//统计近期订单利润
}
if (!isLoadAPIKey)
{
foreach (var r in robotList)
r.ExchangeAPIKey = null;
}
return robotList;
}

48
Binance.TradeRobot.Business/Business/SingalBusiness.cs

@ -0,0 +1,48 @@
using Binance.TradeRobot.Common.DI;
using Binance.TradeRobot.Model.Base;
using Binance.TradeRobot.Model.Dto;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using Yitter.IdGenerator;
namespace Binance.TradeRobot.Business.Business
{
[BatchRegistration(ServiceLifetime.Singleton, RegistrationType.Self)]
public class SingalBusiness : BaseBusiness
{
private RobotBusiness robotBusiness;
public SingalBusiness(IFreeSql fsql,
NLogManager logManager,
IIdGenerator idGenerator,
IMemoryCache memoryCache,
RobotBusiness robotBusiness) : base(fsql, logManager, idGenerator, memoryCache)
{
this.robotBusiness = robotBusiness;
}
public void D21Singnal(D21SingalRequest d21SingalRequest)
{
//logManager.GetLogger("D21").Info(JsonConvert.SerializeObject(d21SingalRequest));
var robotList = robotBusiness.GetD21PolicyRobotList(Enums.RobotState.Runing, d21SingalRequest.KLinePeriodic, d21SingalRequest.Symbol, false, true);
if (robotList == null || robotList.Count() == 0)
throw new BusinessException("未找到符合条件的机器人");
var robot = robotList.FirstOrDefault();
switch (d21SingalRequest.SingalType)
{
case Enums.SingalType.:
case Enums.SingalType.:
break;
case Enums.SingalType.:
break;
case Enums.SingalType.:
break;
}
}
}
}

24
Binance.TradeRobot.Model/Base/Enums.cs

@ -147,5 +147,29 @@ namespace Binance.TradeRobot.Model.Base
_1M
}
#endregion
#region 信号
/// <summary>
/// 信号类型
/// <para>
/// 买入=0,卖出=1,做多=2,做空=3,大趋势看多=4大趋势看空=5中趋势看多=6中趋势看空=7小趋势看多=8小趋势看空=9,多交叉=10,空交叉=11
/// </para>
/// </summary>
public enum SingalType
{
= 0,
= 1,
= 2,
= 3,
= 4,
= 5,
= 6,
= 7,
= 8,
= 9,
= 10,
= 11
}
#endregion
}
}

5
Binance.TradeRobot.Model/Base/MappingProfiles.cs

@ -23,7 +23,10 @@ namespace Binance.TradeRobot.Model.Base
.ForPath(t => t.RobotAccount.ClosePositionCount, opt => opt.MapFrom(f => f.ClosePositionCount))
.ForPath(t => t.RobotAccount.LoanAmount, opt => opt.MapFrom(f => f.LoanAmount))
.ForPath(t => t.RobotAccount.TotalProfit, opt => opt.MapFrom(f => f.TotalProfit))
.ForPath(t => t.RobotAccount.WinCount, opt => opt.MapFrom(f => f.WinCount));
.ForPath(t => t.RobotAccount.WinCount, opt => opt.MapFrom(f => f.WinCount))
.ForPath(t => t.ExchangeAPIKey.AccountId, opt => opt.MapFrom(f => f.ExchangeAccountId))
.ForPath(t => t.ExchangeAPIKey.APIKey, opt => opt.MapFrom(f => f.ExchangeAPIKey))
.ForPath(t => t.ExchangeAPIKey.SecretKey, opt => opt.MapFrom(f => f.ExchangeSecretKey));
CreateMap<Robot, D21PolicyRobotResponse>().IncludeBase<Robot, RobotResponse>()
.ForPath(t => t.D21Policy.Id, opt => opt.MapFrom(f => f.D21PolicyId))
.ForPath(t => t.D21Policy.RobotId, opt => opt.MapFrom(f => f.Id))

38
Binance.TradeRobot.Model/Binance.TradeRobot.Model.xml

@ -109,6 +109,14 @@
信号周期 1m=0,3m=1,5m=2,15m=3,30m=4,1h=5,2h=6,4h=7,6h=8,8h=9,12h=10,1d=11,3d=12,1w=13,1M=14
</summary>
</member>
<member name="T:Binance.TradeRobot.Model.Base.Enums.SingalType">
<summary>
信号类型
<para>
买入=0,卖出=1,做多=2,做空=3,大趋势看多=4大趋势看空=5中趋势看多=6中趋势看空=7小趋势看多=8小趋势看空=9,多交叉=10,空交叉=11
</para>
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Db.ExchangeAPIKey.AccountId">
<summary>
交易所账号UID
@ -504,6 +512,36 @@
交易所APIKeyId
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Dto.BaseSingalRequest.Symbol">
<summary>
交易对
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Dto.BaseSingalRequest.Remark">
<summary>
备注
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Dto.BaseSingalRequest.HighestPrice">
<summary>
最高价
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Dto.BaseSingalRequest.LowestPrice">
<summary>
最低价
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Dto.BaseSingalRequest.OpenPrice">
<summary>
开盘价
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Dto.BaseSingalRequest.ClosePrice">
<summary>
收盘价
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Dto.EditD21PolicyRequest.MaxSystemLoanRatio">
<summary>
最大系统借币比例

4
Binance.TradeRobot.Model/Db/Order/ExecutionLog.cs

@ -1,3 +1,4 @@
using Binance.TradeRobot.Model.Base;
using FreeSql.DataAnnotations;
using System;
@ -19,6 +20,9 @@ namespace Binance.TradeRobot.Model.Db
[Column(DbType = "bigint")]
public long RobotId { get; set; }
[Column(MapType = typeof(int), DbType = "int")]
public Enums.SingalType SourceSingal { get; set; }
}
}

9
Binance.TradeRobot.Model/Db/Robot/Robot.cs

@ -78,6 +78,15 @@ namespace Binance.TradeRobot.Model.Db
public decimal LoanAmount { get; set; } = 0.0M;
#endregion
#region ExchangeAPIKey Extension
[Column(IsIgnore = true)]
public long ExchangeAccountId { get; set; }
[Column(IsIgnore = true)]
public string ExchangeAPIKey { get; set; }
[Column(IsIgnore = true)]
public string ExchangeSecretKey { get; set; }
#endregion
#region D21Policy Extension
[Column(IsIgnore = true)]
public long D21PolicyId { get; set; }

41
Binance.TradeRobot.Model/Dto/Request/Singal/BaseSingalRequest.cs

@ -0,0 +1,41 @@
using Binance.TradeRobot.Model.Base;
namespace Binance.TradeRobot.Model.Dto
{
public class BaseSingalRequest
{
public Enums.SingalType SingalType { get; set; }
/// <summary>
/// 交易对
/// </summary>
public string Symbol { get; set; }
public Enums.SignalPeriod KLinePeriodic { get; set; }
/// <summary>
/// 备注
/// </summary>
public string Remark { get; set; }
/// <summary>
/// 最高价
/// </summary>
public decimal HighestPrice { get; set; }
/// <summary>
/// 最低价
/// </summary>
public decimal LowestPrice { get; set; }
/// <summary>
/// 开盘价
/// </summary>
public decimal OpenPrice { get; set; }
/// <summary>
/// 收盘价
/// </summary>
public decimal ClosePrice { get; set; }
}
}

7
Binance.TradeRobot.Model/Dto/Request/Singal/D21SingalRequest.cs

@ -0,0 +1,7 @@
namespace Binance.TradeRobot.Model.Dto
{
public class D21SingalRequest : BaseSingalRequest
{
}
}

7
Binance.TradeRobot.Model/Dto/Response/Exchange/ExchangeAPIKeyResponse.cs

@ -12,4 +12,11 @@
/// </summary>
public string RobotSymbol { get; set; }
}
public class SimpleExchangeAPIKeyResponse
{
public long AccountId { get; set; }
public string APIKey { get; set; }
public string SecretKey { get; set; }
}
}

5
Binance.TradeRobot.Model/Dto/Response/Robot/RobotResponse.cs

@ -26,5 +26,10 @@ namespace Binance.TradeRobot.Model.Dto
/// 机器人账户对象
/// </summary>
public RobotAccountResponse RobotAccount { get; set; }
/// <summary>
/// 机器人绑定的交易所APIKey对象
/// </summary>
public SimpleExchangeAPIKeyResponse ExchangeAPIKey { get; set; }
}
}

Loading…
Cancel
Save