1撤销限价后 市价分单
This commit is contained in:
@ -20,6 +20,7 @@ import (
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/go-admin-team/go-admin-core/logger"
|
||||
log "github.com/go-admin-team/go-admin-core/logger"
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@ -173,34 +174,60 @@ func handleMainReduceFilled(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
|
||||
|
||||
orderExt := models.LinePreOrderExt{}
|
||||
positionData := savePosition(db, preOrder)
|
||||
orders := make([]models.LinePreOrder, 0)
|
||||
// rate := utility.StringAsFloat(preOrder.Rate)
|
||||
|
||||
if err := db.Model(&DbModels.LinePreOrderStatus{}).Where("order_id =? ", preOrder.MainId).Update("reduce_status", 1).Error; err != nil {
|
||||
logger.Errorf("handleMainReduceFilled 更新主单减仓状态失败,订单号:%s", preOrder.OrderSn)
|
||||
}
|
||||
db.Model(&orderExt).Where("order_id =?", preOrder.Id).Find(&orderExt)
|
||||
|
||||
// 100%减仓 终止流程
|
||||
if orderExt.AddPositionVal.Cmp(decimal.NewFromInt(100)) >= 0 {
|
||||
//缓存
|
||||
removeSpotLossAndAddPosition(preOrder.MainId, preOrder.OrderSn)
|
||||
removePosition(db, preOrder)
|
||||
lock := helper.NewRedisLock(fmt.Sprintf(rediskey.SpotReduceCallback, preOrder.ApiId, preOrder.Symbol), 120, 20, 100*time.Millisecond)
|
||||
|
||||
ids := []int{preOrder.MainId, preOrder.Pid}
|
||||
if err := db.Model(&DbModels.LinePreOrder{}).Where("id IN ? AND status =6", ids).Update("status", 9).Error; err != nil {
|
||||
logger.Info("100%减仓完毕,终结流程")
|
||||
}
|
||||
if ok, err := lock.AcquireWait(context.Background()); err != nil {
|
||||
log.Error("获取锁失败", err)
|
||||
return
|
||||
}
|
||||
} else if ok {
|
||||
defer lock.Release()
|
||||
// 100%减仓 终止流程
|
||||
if orderExt.AddPositionVal.Cmp(decimal.NewFromInt(100)) >= 0 {
|
||||
//缓存
|
||||
removeSpotLossAndAddPosition(preOrder.MainId, preOrder.OrderSn)
|
||||
removePosition(db, preOrder)
|
||||
|
||||
totalNum := getSpotPositionAvailableQuantity(db, apiUserInfo, preOrder, tradeSet) //getSpotTotalNum(apiUserInfo, preOrder, tradeSet)
|
||||
totalNum = totalNum.Mul(decimal.NewFromFloat(0.998)).Truncate(int32(tradeSet.AmountDigit))
|
||||
ids := []int{preOrder.MainId, preOrder.Pid}
|
||||
if err := db.Model(&DbModels.LinePreOrder{}).Where("id IN ? AND status =6", ids).Update("status", 9).Error; err != nil {
|
||||
logger.Info("100%减仓完毕,终结流程")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//市价单跳出
|
||||
if preOrder.MainOrderType == "MARKET" {
|
||||
return
|
||||
}
|
||||
|
||||
totalNum := getSpotPositionAvailableQuantity(db, apiUserInfo, preOrder, tradeSet) //getSpotTotalNum(apiUserInfo, preOrder, tradeSet)
|
||||
totalNum = totalNum.Mul(decimal.NewFromFloat(0.998)).Truncate(int32(tradeSet.AmountDigit))
|
||||
//亏损大于0 重新计算比例
|
||||
SpotTakeProfit(db, preOrder, totalNum, positionData, orderExt, tradeSet, decimal.Zero, decimal.Zero)
|
||||
|
||||
mainId := preOrder.Id
|
||||
|
||||
if preOrder.MainId > 0 {
|
||||
mainId = preOrder.MainId
|
||||
}
|
||||
|
||||
nextSpotReduceTrigger(db, mainId, totalNum, tradeSet)
|
||||
}
|
||||
}
|
||||
|
||||
func SpotTakeProfit(db *gorm.DB, preOrder *DbModels.LinePreOrder, totalNum decimal.Decimal,
|
||||
positionData positiondto.PositionDto, orderExt DbModels.LinePreOrderExt, tradeSet models2.TradeSet, manualTakeRatio, manualStopRatio decimal.Decimal) bool {
|
||||
price := utility.StrToDecimal(preOrder.Price)
|
||||
orders := make([]models.LinePreOrder, 0)
|
||||
|
||||
if err := db.Model(&models.LinePreOrder{}).Where("pid =? AND order_type IN (1,2) AND status=0", preOrder.Id).Find(&orders).Error; err != nil {
|
||||
logger.Errorf("获取减仓单止盈止损失败 err:%v", err)
|
||||
return
|
||||
return true
|
||||
}
|
||||
|
||||
spotApi := SpotRestApi{}
|
||||
@ -209,8 +236,17 @@ func handleMainReduceFilled(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
|
||||
orders[index].Num = totalNum.String()
|
||||
|
||||
if orders[index].OrderType == 1 {
|
||||
//亏损大于0 重新计算比例
|
||||
if positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 {
|
||||
//手动设置百分比
|
||||
if manualTakeRatio.Cmp(decimal.Zero) > 0 {
|
||||
orders[index].Rate = manualTakeRatio.String()
|
||||
percentag := manualTakeRatio.Div(decimal.NewFromInt(100))
|
||||
|
||||
if positionData.PositionSide == "LONG" {
|
||||
orders[index].Price = price.Mul(decimal.NewFromInt(1).Add(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||
} else {
|
||||
orders[index].Price = price.Mul(decimal.NewFromInt(1).Sub(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||
}
|
||||
} else if positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 {
|
||||
percentag := positionData.TotalLoss.Div(totalNum).Div(price).Mul(decimal.NewFromInt(100))
|
||||
percentag = percentag.Add(orderExt.TakeProfitRatio).Truncate(2)
|
||||
orders[index].Rate = percentag.String()
|
||||
@ -220,17 +256,20 @@ func handleMainReduceFilled(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
|
||||
|
||||
processTakeProfitOrder(db, spotApi, orders[index])
|
||||
} else if orders[index].OrderType == 2 {
|
||||
if manualStopRatio.Cmp(decimal.Zero) > 0 {
|
||||
orders[index].Rate = manualStopRatio.String()
|
||||
percentag := manualStopRatio.Div(decimal.NewFromInt(100))
|
||||
|
||||
if positionData.PositionSide == "LONG" {
|
||||
orders[index].Price = price.Mul(decimal.NewFromInt(1).Sub(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||
} else {
|
||||
orders[index].Price = price.Mul(decimal.NewFromInt(1).Add(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||
}
|
||||
}
|
||||
processStopLossOrder(orders[index])
|
||||
}
|
||||
}
|
||||
|
||||
mainId := preOrder.Id
|
||||
|
||||
if preOrder.MainId > 0 {
|
||||
mainId = preOrder.MainId
|
||||
}
|
||||
|
||||
nextSpotReduceTrigger(db, mainId, totalNum, tradeSet)
|
||||
return false
|
||||
}
|
||||
|
||||
// 缓存下一个减仓单
|
||||
@ -751,7 +790,7 @@ func processTakeProfitAndStopLossOrders(db *gorm.DB, preOrder *models.LinePreOrd
|
||||
switch order.OrderType {
|
||||
case 1: // 止盈
|
||||
//亏损大于0 重新计算比例
|
||||
if fist && positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 {
|
||||
if fist && positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 && preOrder.SignPriceType != "mixture" {
|
||||
percentag := positionData.TotalLoss.Div(num).Div(price).Mul(decimal.NewFromInt(100))
|
||||
|
||||
if fist {
|
||||
|
||||
Reference in New Issue
Block a user