1、阶段减仓

This commit is contained in:
2025-04-07 18:36:36 +08:00
parent cdd3f951a2
commit 8e8c78ec0b
13 changed files with 393 additions and 59 deletions

View File

@ -11,6 +11,7 @@ type LineReduceStrategyItem struct {
ReduceStrategyId int `json:"reduceStrategyId" gorm:"type:bigint;comment:减仓策略id"`
LossPercent decimal.Decimal `json:"lossPercent" gorm:"type:decimal(10,2);comment:亏损百分比"`
QuantityPercent decimal.Decimal `json:"quantityPercent" gorm:"type:decimal(10,2);comment:减仓数量百分比"`
OrderType string `json:"orderType" gorm:"type:varchar(20);comment:订单类型 LIMIT-限价 MARKET-市价"`
ReduceStrategy LineReduceStrategy `json:"reduceStrategy" gorm:"foreignKey:ReduceStrategyId;"`
models.ModelTime

View File

@ -3,7 +3,8 @@ package dto
import "github.com/shopspring/decimal"
type LineOrderReduceStrategyResp struct {
OrderId int `json:"orderId"`
MainId int `json:"mainId" comment:"主单id"`
OrderId int `json:"orderId" comment:"减仓单id"`
Symbol string `json:"symbol"`
Side string `json:"side" comment:"BUY SELL"`
Items []LineOrderReduceStrategyRespItem `json:"items"`
@ -12,6 +13,7 @@ type LineOrderReduceStrategyResp struct {
// 减仓节点
type LineOrderReduceStrategyRespItem struct {
Price decimal.Decimal `json:"p" comment:"下单价"`
Num decimal.Decimal `json:"n" comment:"下单数量"`
TriggerPrice decimal.Decimal `json:"t" comment:"触发价"`
LossPercent decimal.Decimal `json:"l" comment:"亏损百分比"`
OrderType string `json:"o" comment:"订单类型 LIMIT-限价 MARKET-市价"`

View File

@ -56,7 +56,7 @@ func (s *LineReduceStrategyInsertReq) Valid() error {
return err
}
if index > 0 && item.LossPercent.Cmp(s.Items[index].LossPercent) <= 0 {
if index > 0 && item.LossPercent.Cmp(s.Items[index-1].LossPercent) <= 0 {
return errors.New("亏损比例必须递增")
}
}
@ -75,6 +75,7 @@ func (s *LineReduceStrategyInsertReq) Generate(model *models.LineReduceStrategy)
strategyItem := models.LineReduceStrategyItem{}
strategyItem.OrderType = item.OrderType
strategyItem.LossPercent = item.LossPercent
strategyItem.QuantityPercent = item.QuantityPercent
model.Items = append(model.Items, strategyItem)
}
@ -105,6 +106,7 @@ func (s *LineReduceStrategyUpdateReq) Generate(model *models.LineReduceStrategy)
strategyItem := models.LineReduceStrategyItem{}
strategyItem.OrderType = item.OrderType
strategyItem.LossPercent = item.LossPercent
strategyItem.QuantityPercent = item.QuantityPercent
model.Items = append(model.Items, strategyItem)
}

View File

@ -32,8 +32,9 @@ func (m *LineReduceStrategyItemGetPageReq) GetNeedSearch() interface{} {
}
type LineReduceStrategyItem struct {
LossPercent decimal.Decimal `json:"lossPercent" comment:"止损百分比"`
OrderType string `json:"orderType" comment:"订单类型 LIMIT-限价 MARKET-市价"`
LossPercent decimal.Decimal `json:"lossPercent" comment:"止损百分比"`
QuantityPercent decimal.Decimal `json:"quantityPercent" comment:"数量百分比"`
OrderType string `json:"orderType" comment:"订单类型 LIMIT-限价 MARKET-市价"`
}
func (s *LineReduceStrategyItem) Valid() error {
@ -46,6 +47,14 @@ func (s *LineReduceStrategyItem) Valid() error {
return errors.New("百分比不能大于等于100")
}
if s.QuantityPercent.Cmp(decimal.Zero) <= 0 {
return errors.New("百分比不能小于等于0")
}
if s.QuantityPercent.Cmp(decimal.NewFromInt(100)) >= 0 {
return errors.New("数量百分比不能大于等于100")
}
keys := []string{"LIMIT", "MARKET"}
if !utility.ContainsStr(keys, s.OrderType) {

View File

@ -1042,6 +1042,7 @@ func createPreReduceOrder(preOrder *models.LinePreOrder, ext models.LinePreOrder
// 构建止盈、止盈止损
func makeFuturesTakeAndReduce(preOrder *models.LinePreOrder, ext models.LinePreOrderExt, tradeSet models2.TradeSet) ([]models.LinePreOrder, error) {
orders := make([]models.LinePreOrder, 0)
mainId := preOrder.Id
var side string
if (preOrder.OrderType != 0 && strings.ToUpper(preOrder.Site) == "BUY") || (preOrder.OrderType == 0 && strings.ToUpper(preOrder.Site) == "SELL") {
@ -1050,6 +1051,10 @@ func makeFuturesTakeAndReduce(preOrder *models.LinePreOrder, ext models.LinePreO
side = "SELL"
}
if preOrder.MainId > 0 {
mainId = preOrder.MainId
}
if ext.TakeProfitRatio.Cmp(decimal.Zero) > 0 {
// 止盈单
profitOrder := models.LinePreOrder{}
@ -1060,11 +1065,7 @@ func makeFuturesTakeAndReduce(preOrder *models.LinePreOrder, ext models.LinePreO
profitOrder.Pid = preOrder.Id
profitOrder.OrderType = 1
profitOrder.Status = 0
profitOrder.MainId = preOrder.Id
if preOrder.MainId > 0 {
profitOrder.MainId = preOrder.MainId
}
profitOrder.MainId = mainId
profitOrder.BuyPrice = "0"
profitOrder.Site = side
@ -1090,7 +1091,7 @@ func makeFuturesTakeAndReduce(preOrder *models.LinePreOrder, ext models.LinePreO
lossOrder.Pid = preOrder.Id
lossOrder.OrderType = 2
lossOrder.Status = 0
lossOrder.MainId = preOrder.MainId
lossOrder.MainId = mainId
lossOrder.BuyPrice = "0"
lossOrder.Num = ext.TotalAfter.Truncate(int32(tradeSet.AmountDigit)).String()
lossOrder.Rate = ext.StopLossRatio.Truncate(2).String()
@ -1611,6 +1612,10 @@ func (e *LinePreOrder) ClearAll() error {
"futures_reduce_list",
"spot_reduce_strategy_list",
"fut_reduce_strategy_list",
"future_position",
"spot_position",
"strategy_spot_order_list",
"strategy_fut_order_list",
}
err = helper.DefaultRedis.DeleteKeysByPrefix(prefixs...)
if err != nil {