|
@ -25,7 +25,6 @@ namespace Binance.TradeRobot.Business |
|
|
|
|
|
|
|
|
public void OnSpotOrderPublish(SpotOrderPublishInfo spotOrderPublishInfo) |
|
|
public void OnSpotOrderPublish(SpotOrderPublishInfo spotOrderPublishInfo) |
|
|
{ |
|
|
{ |
|
|
//var logger = logManager.GetLogger(spotOrderPublishInfo.LoggerName);
|
|
|
|
|
|
var step = string.Empty; |
|
|
var step = string.Empty; |
|
|
var logList = new List<ExecutionLog>(); |
|
|
var logList = new List<ExecutionLog>(); |
|
|
try |
|
|
try |
|
@ -38,6 +37,8 @@ namespace Binance.TradeRobot.Business |
|
|
if (robot == null) |
|
|
if (robot == null) |
|
|
throw new BusinessException($"未找到机器人"); |
|
|
throw new BusinessException($"未找到机器人"); |
|
|
|
|
|
|
|
|
|
|
|
var apiClient = GetBaseAPIClient(robot.ExchangeId, robot.ExchangeAPIKey.AccountId, robot.ExchangeAPIKey.APIKey, robot.ExchangeAPIKey.SecretKey); |
|
|
|
|
|
|
|
|
IUpdate<SpotOrder> updateSpotOrder = fsql.Update<SpotOrder>(spotOrderPublishInfo.OrderId).Set(o => o.State, spotOrderPublishInfo.SpotOrderState); |
|
|
IUpdate<SpotOrder> updateSpotOrder = fsql.Update<SpotOrder>(spotOrderPublishInfo.OrderId).Set(o => o.State, spotOrderPublishInfo.SpotOrderState); |
|
|
IUpdate<RobotAccount> updateRobotAccount = null; |
|
|
IUpdate<RobotAccount> updateRobotAccount = null; |
|
|
IList<IUpdate<User>> updateUserList = null; |
|
|
IList<IUpdate<User>> updateUserList = null; |
|
@ -62,7 +63,6 @@ namespace Binance.TradeRobot.Business |
|
|
|
|
|
|
|
|
if (spotOrderPublishInfo.SpotOrderState == Enums.SpotOrderState.Filled) |
|
|
if (spotOrderPublishInfo.SpotOrderState == Enums.SpotOrderState.Filled) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
var avgTradePrice = spotOrderPublishInfo.CummulativeTradeAmount / spotOrderPublishInfo.CummulativeTradeQuantity; //计算成交均价
|
|
|
var avgTradePrice = spotOrderPublishInfo.CummulativeTradeAmount / spotOrderPublishInfo.CummulativeTradeQuantity; //计算成交均价
|
|
|
logList.Add(new ExecutionLog() |
|
|
logList.Add(new ExecutionLog() |
|
|
{ |
|
|
{ |
|
@ -85,9 +85,16 @@ namespace Binance.TradeRobot.Business |
|
|
if (spotOrderPublishInfo.TradeDirection == Enums.TradeDirection.Buy) |
|
|
if (spotOrderPublishInfo.TradeDirection == Enums.TradeDirection.Buy) |
|
|
{ |
|
|
{ |
|
|
var quantity = spotOrderPublishInfo.CummulativeTradeQuantity - spotOrderPublishInfo.Fee; //扣除基础币手续费,得到真实购买数量
|
|
|
var quantity = spotOrderPublishInfo.CummulativeTradeQuantity - spotOrderPublishInfo.Fee; //扣除基础币手续费,得到真实购买数量
|
|
|
|
|
|
|
|
|
|
|
|
//更新机器人账户
|
|
|
updateRobotAccount = fsql.Update<RobotAccount>(robot.RobotAccount.Id).Set(ra => ra.SpotCurrencyQuantity + quantity) |
|
|
updateRobotAccount = fsql.Update<RobotAccount>(robot.RobotAccount.Id).Set(ra => ra.SpotCurrencyQuantity + quantity) |
|
|
.Set(ra => ra.SpotCurrencyAmount + spotOrderPublishInfo.CummulativeTradeAmount); |
|
|
.Set(ra => ra.SpotCurrencyAmount + spotOrderPublishInfo.CummulativeTradeAmount); |
|
|
|
|
|
|
|
|
|
|
|
//交易所账户真实持币数量
|
|
|
|
|
|
var exchangAccountSpotCurrencyQuantity = apiClient.GetIsolatedMarginAccountAssets().FirstOrDefault(x => x.Symbol == spotOrderPublishInfo.Symbol).BaseFree; |
|
|
|
|
|
updateSpotOrder = updateSpotOrder.Set(o => o.RobotAccountSpotCurrencyQuantity, robot.RobotAccount.SpotCurrencyQuantity + quantity) |
|
|
|
|
|
.Set(o => o.ExchangeAccountSpotCurrencyQuantity, exchangAccountSpotCurrencyQuantity); |
|
|
|
|
|
|
|
|
if (spotOrderPublishInfo.OrderType == Enums.OrderType.MARKET) |
|
|
if (spotOrderPublishInfo.OrderType == Enums.OrderType.MARKET) |
|
|
{ |
|
|
{ |
|
|
//市价买单完全成交,根据策略挂止损单
|
|
|
//市价买单完全成交,根据策略挂止损单
|
|
@ -110,7 +117,6 @@ namespace Binance.TradeRobot.Business |
|
|
if (loanAmount > 0M) |
|
|
if (loanAmount > 0M) |
|
|
{ |
|
|
{ |
|
|
//还币
|
|
|
//还币
|
|
|
var apiClient = GetBaseAPIClient(robot.ExchangeId, robot.ExchangeAPIKey.AccountId, robot.ExchangeAPIKey.APIKey, robot.ExchangeAPIKey.SecretKey); |
|
|
|
|
|
interest = apiClient.IsolatedMarginRepay(robot.Symbol, loanAmount); |
|
|
interest = apiClient.IsolatedMarginRepay(robot.Symbol, loanAmount); |
|
|
logList[0].Content = $"{logList[0].Content},借币金额:{loanAmount},还币利息:{interest}"; |
|
|
logList[0].Content = $"{logList[0].Content},借币金额:{loanAmount},还币利息:{interest}"; |
|
|
} |
|
|
} |
|
@ -120,20 +126,25 @@ namespace Binance.TradeRobot.Business |
|
|
var profit = spotOrderPublishInfo.CummulativeTradeQuantity * (avgTradePrice - robot.RobotAccount.SpotCurrencyAvgPrice) - spotOrderPublishInfo.Fee - interest; //计算利润
|
|
|
var profit = spotOrderPublishInfo.CummulativeTradeQuantity * (avgTradePrice - robot.RobotAccount.SpotCurrencyAvgPrice) - spotOrderPublishInfo.Fee - interest; //计算利润
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateSpotOrder = updateSpotOrder.SetIf(interest > 0M, o => o.LoanInterest, interest) |
|
|
|
|
|
.Set(o => o.Profit, profit) |
|
|
|
|
|
.Set(o => o.HistoryTotalProfit, robot.RobotAccount.TotalProfit + profit); |
|
|
|
|
|
|
|
|
|
|
|
updateRobotAccount = fsql.Update<RobotAccount>(robot.RobotAccount.Id).Set(ra => ra.SpotCurrencyQuantity - spotOrderPublishInfo.CummulativeTradeQuantity) |
|
|
updateRobotAccount = fsql.Update<RobotAccount>(robot.RobotAccount.Id).Set(ra => ra.SpotCurrencyQuantity - spotOrderPublishInfo.CummulativeTradeQuantity) |
|
|
.Set(ra => ra.SpotCurrencyAmount - buyAmount) |
|
|
.Set(ra => ra.SpotCurrencyAmount - buyAmount) |
|
|
.Set(ra => ra.TotalProfit + profit) |
|
|
.Set(ra => ra.TotalProfit + profit) |
|
|
.Set(ra => ra.ClosePositionCount + 1) |
|
|
.Set(ra => ra.ClosePositionCount + 1) |
|
|
.SetIf(profit > 0M, ra => ra.WinCount + 1) |
|
|
.SetIf(profit > 0M, ra => ra.WinCount + 1) |
|
|
.SetIf(interest > 0M, ra => ra.LoanAmount - loanAmount); |
|
|
.SetIf(interest > 0M, ra => ra.LoanAmount - loanAmount); |
|
|
|
|
|
//交易所账户真实持币数量
|
|
|
|
|
|
var exchangAccountSpotCurrencyQuantity = apiClient.GetIsolatedMarginAccountAssets().FirstOrDefault(x => x.Symbol == spotOrderPublishInfo.Symbol).BaseFree; |
|
|
|
|
|
|
|
|
|
|
|
updateSpotOrder = updateSpotOrder.SetIf(interest > 0M, o => o.LoanInterest, interest) |
|
|
|
|
|
.Set(o => o.Profit, profit) |
|
|
|
|
|
.Set(o => o.HistoryTotalProfit, robot.RobotAccount.TotalProfit + profit) |
|
|
|
|
|
.Set(o => o.RobotAccountSpotCurrencyQuantity, robot.RobotAccount.SpotCurrencyQuantity - spotOrderPublishInfo.CummulativeTradeQuantity) |
|
|
|
|
|
.Set(o => o.ExchangeAccountSpotCurrencyQuantity, exchangAccountSpotCurrencyQuantity); |
|
|
|
|
|
|
|
|
if (profit > 0) //盈利复投
|
|
|
if (profit > 0) //盈利复投
|
|
|
updateD21Policy = fsql.Update<D21Policy>(robot.D21Policy.Id).Set(p => p.Position + profit); |
|
|
updateD21Policy = fsql.Update<D21Policy>(robot.D21Policy.Id).Set(p => p.Position + profit); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var capitalChangeType = profit > 0M ? Enums.CapitalChangeType.Add : Enums.CapitalChangeType.Reduce; |
|
|
var capitalChangeType = profit > 0M ? Enums.CapitalChangeType.Add : Enums.CapitalChangeType.Reduce; |
|
|
var userList = userBusiness.GetUserList(multiplyBy100: false); |
|
|
var userList = userBusiness.GetUserList(multiplyBy100: false); |
|
|
foreach (var user in userList) |
|
|
foreach (var user in userList) |
|
@ -162,6 +173,9 @@ namespace Binance.TradeRobot.Business |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fsql.Transaction(() => |
|
|
fsql.Transaction(() => |
|
|