1、累计亏损

This commit is contained in:
2025-03-03 17:35:41 +08:00
parent a1a390aa9c
commit ca92638554
5 changed files with 65 additions and 28 deletions

View File

@ -160,7 +160,7 @@ func handleReduceFilled(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
if v.OrderType == 1 {
//亏损大于0 重新计算比例
if positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 {
percentag := positionData.TotalLoss.Div(totalNum).Div(price)
percentag := positionData.TotalLoss.Div(totalNum).Div(price).Mul(decimal.NewFromInt(100))
percentag = percentag.Add(orderExt.TakeProfitRatio).Truncate(2)
v.Rate = percentag.String()
percentag = percentag.Div(decimal.NewFromInt(100))
@ -434,6 +434,7 @@ func handleFutMainOrderFilled(db *gorm.DB, preOrder *models.LinePreOrder) {
// 获取和保存持仓数据
positionData := savePosition(db, preOrder)
orderExt := models.LinePreOrderExt{}
db.Model(&orderExt).Where("order_id =?", preOrder.Id).First(&orderExt)
num := getFuturesPositionAvailableQuantity(db, apiInfo, preOrder, tradeSet).Truncate(int32(tradeSet.AmountDigit))
// 更新订单数量并处理止盈、止损、减仓
@ -449,7 +450,7 @@ func handleFutMainOrderFilled(db *gorm.DB, preOrder *models.LinePreOrder) {
case 1: // 止盈
//亏损大于0 重新计算比例
if positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 {
percentag := positionData.TotalLoss.Div(num).Div(price)
percentag := positionData.TotalLoss.Div(num).Div(price).Mul(decimal.NewFromInt(100))
percentag = percentag.Add(orderExt.TakeProfitRatio).Truncate(2)
order.Rate = percentag.String()
percentag = percentag.Div(decimal.NewFromInt(100))
@ -492,8 +493,13 @@ func cancelPositionOtherOrders(apiUserInfo DbModels.LineApiUser, db *gorm.DB, pr
mainIds := []int{}
for _, mainOrder := range mainOrders {
removeFutLossAndAddPosition(mainOrder.Id, mainOrder.OrderSn)
mainIds = append(mainIds, mainOrder.Id)
mainId := mainOrder.Id
if mainOrder.MainId > 0 {
mainId = mainOrder.MainId
}
removeFutLossAndAddPosition(mainId, mainOrder.OrderSn)
mainIds = append(mainIds, mainId)
}
if len(mainIds) > 0 {
@ -512,6 +518,10 @@ func cancelPositionOtherOrders(apiUserInfo DbModels.LineApiUser, db *gorm.DB, pr
logger.Errorf("批量取消订单失败 orderSns:%v", item)
}
}
if err := db.Exec("UPDATE line_pre_order SET `status`=4, `desc`=CONCAT(`desc`, ' 新单触发取消') WHERE id IN ? AND `status` =6", mainIds).Error; err != nil {
logger.Errorf("合约 新下单成功后更新主单取消状态失败, 新主单号:%s, 错误信息:%v", preOrder.MainId, err)
}
}
return nil
}