|
@ -6,6 +6,7 @@ using Binance.TradeRobot.Model.Db; |
|
|
using Binance.TradeRobot.Model.Dto; |
|
|
using Binance.TradeRobot.Model.Dto; |
|
|
using Microsoft.Extensions.Caching.Memory; |
|
|
using Microsoft.Extensions.Caching.Memory; |
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
|
|
using System.Collections.Generic; |
|
|
using System.Data.Common; |
|
|
using System.Data.Common; |
|
|
using Yitter.IdGenerator; |
|
|
using Yitter.IdGenerator; |
|
|
|
|
|
|
|
@ -44,28 +45,33 @@ namespace Binance.TradeRobot.Business |
|
|
throw new BusinessException("同一个交易所账号下只允许存在一个交易对"); |
|
|
throw new BusinessException("同一个交易所账号下只允许存在一个交易对"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private Robot CreateRobot(AddRobotRequest addRobotRequest) |
|
|
/// <summary>
|
|
|
|
|
|
/// 添加机器人和账户
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="addRobotRequest"></param>
|
|
|
|
|
|
/// <param name="tran"></param>
|
|
|
|
|
|
/// <returns>机器人Id</returns>
|
|
|
|
|
|
private long AddRobotWithTran(AddRobotRequest addRobotRequest, DbTransaction tran) |
|
|
{ |
|
|
{ |
|
|
return new Robot() |
|
|
var robotId = idGenerator.NewLong(); |
|
|
|
|
|
fsql.Insert(new Robot() |
|
|
{ |
|
|
{ |
|
|
Id = idGenerator.NewLong(), |
|
|
Id = robotId, |
|
|
Symbol = addRobotRequest.Symbol, |
|
|
Symbol = addRobotRequest.Symbol, |
|
|
TradePolicy = addRobotRequest.TradePolicy, |
|
|
TradePolicy = addRobotRequest.TradePolicy, |
|
|
BusinessType = addRobotRequest.TradePolicy.GetBusinessType(), |
|
|
BusinessType = addRobotRequest.TradePolicy.GetBusinessType(), |
|
|
ExchangeId = addRobotRequest.ExchangeId, |
|
|
ExchangeId = addRobotRequest.ExchangeId, |
|
|
}; |
|
|
}).WithTransaction(tran).ExecuteAffrows(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private RobotAccount CreateRobotAccount(long robotId) |
|
|
fsql.Insert(new RobotAccount() |
|
|
{ |
|
|
{ |
|
|
return new RobotAccount() { Id = idGenerator.NewLong(), RobotId = robotId }; |
|
|
Id = idGenerator.NewLong(), |
|
|
} |
|
|
RobotId = robotId |
|
|
|
|
|
}).WithTransaction(tran).ExecuteAffrows(); |
|
|
|
|
|
|
|
|
private void ExecuteAddRobotWithTran(Robot robot, RobotAccount robotAccount, long exchangeAPIKeyId, DbTransaction tran) |
|
|
fsql.Update<ExchangeAPIKey>(addRobotRequest.ExchangeAPIKeyId).WithTransaction(tran).Set(ek => ek.RobotId, robotId).ExecuteAffrows(); |
|
|
{ |
|
|
|
|
|
fsql.Insert(robot).WithTransaction(tran).ExecuteAffrows(); |
|
|
return robotId; |
|
|
fsql.Insert(robotAccount).WithTransaction(tran).ExecuteAffrows(); |
|
|
|
|
|
fsql.Update<ExchangeAPIKey>(exchangeAPIKeyId).WithTransaction(tran).Set(ek => ek.RobotId, robot.Id).ExecuteAffrows(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
@ -75,20 +81,70 @@ namespace Binance.TradeRobot.Business |
|
|
public void AddPyramidPolicyRobot(AddPyramidPolicyRobotRequest addPyramidPolicyRobotRequest) |
|
|
public void AddPyramidPolicyRobot(AddPyramidPolicyRobotRequest addPyramidPolicyRobotRequest) |
|
|
{ |
|
|
{ |
|
|
CheckRobotRegister(addPyramidPolicyRobotRequest, out ExchangeAPIKey exchangeAPIKey); |
|
|
CheckRobotRegister(addPyramidPolicyRobotRequest, out ExchangeAPIKey exchangeAPIKey); |
|
|
var robot = CreateRobot(addPyramidPolicyRobotRequest); |
|
|
|
|
|
var robotAccount = CreateRobotAccount(robot.Id); |
|
|
|
|
|
var pyramidPolicy = addPyramidPolicyRobotRequest.Map<PyramidPolicy>(); |
|
|
var pyramidPolicy = addPyramidPolicyRobotRequest.Map<PyramidPolicy>(); |
|
|
pyramidPolicy.Id = idGenerator.NewLong(); |
|
|
pyramidPolicy.Id = idGenerator.NewLong(); |
|
|
pyramidPolicy.RobotId = robot.Id; |
|
|
|
|
|
fsql.Transaction(() => |
|
|
fsql.Transaction(() => |
|
|
{ |
|
|
{ |
|
|
var tran = fsql.Ado.TransactionCurrentThread; |
|
|
var tran = fsql.Ado.TransactionCurrentThread; |
|
|
ExecuteAddRobotWithTran(robot, robotAccount, addPyramidPolicyRobotRequest.ExchangeAPIKeyId, tran); |
|
|
pyramidPolicy.RobotId = AddRobotWithTran(addPyramidPolicyRobotRequest, tran); |
|
|
fsql.Insert(pyramidPolicy).ExecuteAffrows(); |
|
|
fsql.Insert(pyramidPolicy).ExecuteAffrows(); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
//调整仓位杠杆倍数
|
|
|
//调整仓位杠杆倍数
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加动2.1策略机器人
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="addD21PolicyRobotRequest"></param>
|
|
|
|
|
|
public void AddD21PolicyRobot(AddD21PolicyRobotRequest addD21PolicyRobotRequest) |
|
|
|
|
|
{ |
|
|
|
|
|
CheckRobotRegister(addD21PolicyRobotRequest, out _); |
|
|
|
|
|
var d21Policy = addD21PolicyRobotRequest.Map<D21Policy>(); |
|
|
|
|
|
d21Policy.Id = idGenerator.NewLong(); |
|
|
|
|
|
fsql.Transaction(() => |
|
|
|
|
|
{ |
|
|
|
|
|
var tran = fsql.Ado.TransactionCurrentThread; |
|
|
|
|
|
d21Policy.RobotId = AddRobotWithTran(addD21PolicyRobotRequest, tran); |
|
|
|
|
|
fsql.Insert(d21Policy).ExecuteAffrows(); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public IList<D21PolicyRobotResponse> GetD21PolicyRobotList() |
|
|
|
|
|
{ |
|
|
|
|
|
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, |
|
|
|
|
|
D21StopLossRatio = d.StopLossRatio |
|
|
|
|
|
}).Map<IList<D21PolicyRobotResponse>>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return robotList; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|