1、主单增加减仓、加仓状态

2、bug修复
This commit is contained in:
2025-02-20 12:00:04 +08:00
parent b132ee879b
commit ade768c28a
13 changed files with 312 additions and 52 deletions

View File

@ -625,6 +625,10 @@ func (e LinePreOrder) CalculateBreakEevenRatio(c *gin.Context) {
return
}
if req.Symbol == "" {
return
}
// data := dto.CalculateBreakEvenRatioResp{}
_, err = s.GenerateOrder(&req)
if err != nil {

View File

@ -183,7 +183,7 @@ func (s *LinePreOrderDeleteReq) GetId() interface{} {
type LineAddPreOrderReq struct {
ExchangeType string `json:"exchange_type" vd:"len($)>0"` //交易所类型
OrderType int `json:"order_type"` //订单类型
Symbol string `json:"symbol" vd:"len($)>0"` //交易对
Symbol string `json:"symbol"` //交易对
ApiUserId string `json:"api_id" ` //下单用户
Site string `json:"site" ` //购买方向
BuyPrice string `json:"buy_price" vd:"$>0"` //购买金额 U
@ -341,15 +341,15 @@ func (req LineBatchAddPreOrderReq) CheckParams() error {
return errors.New("加仓单下跌价格不能为空")
}
if v.ReduceNumRatio.IsZero() || v.ReduceNumRatio.Cmp(decimal.Zero) > 0 {
if v.ReduceNumRatio.IsZero() || v.ReduceNumRatio.Cmp(decimal.NewFromInt(100)) > 0 {
return errors.New("减仓数量不正确")
}
if v.ReducePriceRatio.IsZero() || v.ReducePriceRatio.Cmp(decimal.Zero) > 0 {
if v.ReducePriceRatio.IsZero() || v.ReducePriceRatio.Cmp(decimal.NewFromInt(100)) > 0 {
return errors.New("减仓下跌价格不正确")
}
if v.ReduceTakeProfitRatio.IsZero() || v.ReduceTakeProfitRatio.Cmp(decimal.Zero) > 0 {
if v.ReduceTakeProfitRatio.IsZero() || v.ReduceTakeProfitRatio.Cmp(decimal.NewFromInt(100)) > 0 {
return errors.New("减仓后止盈价格不正确")
}
}
@ -371,8 +371,8 @@ type LeverReq struct {
Symbol string `json:"symbol"`
Leverage int `json:"leverage"`
GroupId int `json:"group_id"`
IsAll int `json:"is_all"` // 1= 全部 0=不是全部
ExchangeType string `json:"exchange_type"` //交易所类型 字典exchange_type
IsAll int `json:"is_all"` // 1= 全部 0=不是全部
ExchangeType string `json:"exchangeType"` //交易所类型 字典exchange_type
}
func (c LeverReq) CheckParams() error {
@ -387,11 +387,11 @@ func (c LeverReq) CheckParams() error {
type MarginTypeReq struct {
ApiUserIds string `json:"api_user_ids"`
Symbol string `json:"symbol"` // 交易对
MarginType string `json:"margin_type"` //全仓 CROSSED 逐仓 ISOLATED
GroupId int `json:"group_id"` // 交易对组id
IsAll int `json:"is_all"` // 1= 全部 0=不是全部
ExchangeType string `json:"exchange_type"` //交易所类型 字典exchange_type
Symbol string `json:"symbol"` // 交易对
MarginType string `json:"margin_type"` //全仓 CROSSED 逐仓 ISOLATED
GroupId int `json:"group_id"` // 交易对组id
IsAll int `json:"is_all"` // 1= 全部 0=不是全部
ExchangeType string `json:"exchangeType"` //交易所类型 字典exchange_type
}
type CancelOpenOrderReq struct {
@ -399,7 +399,7 @@ type CancelOpenOrderReq struct {
Symbol string `json:"symbol"`
OrderSn string `json:"order_sn"`
OrderType int `json:"order_type"`
ExchangeType string `json:"exchange_type"` //交易所类型 字典exchange_type
ExchangeType string `json:"exchangeType"` //交易所类型 字典exchange_type
}
type GetChildOrderReq struct {

View File

@ -323,7 +323,7 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
//获取交易对
tradeSet, _ := helper.GetObjString[models2.TradeSet](helper.DefaultRedis, key)
orderCount := e.CheckRepeatOrder(req.OrderType, id, req.Site, tradeSet.Coin)
orderCount := e.CheckRepeatOrder(req.SymbolType, id, req.Site, tradeSet.Coin)
if orderCount > 0 {
*errs = append(*errs, fmt.Errorf("api_id:%s 获取交易对:%s 该交易对已存在,请勿重复下单", id, req.Symbol))
continue
@ -571,6 +571,7 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
stopOrder.MainId = AddOrder.Id
stopOrder.OrderType = 4
stopOrder.Status = 0
stopOrder.BuyPrice = "0"
stopOrder.Rate = req.ReducePriceRatio.String()
stopNum := utility.StrToDecimal(AddOrder.Num).Mul(req.ReduceNumRatio.Div(decimal.NewFromInt(100)).Truncate(4))
stopOrder.Num = stopNum.Truncate(int32(tradeSet.AmountDigit)).String()
@ -807,9 +808,9 @@ func makeReduceTakeAndStoploss(parentOrder *models.LinePreOrder, ext models.Line
}
// CheckRepeatOrder 检查重复下单 检查基础货币
func (e *LinePreOrder) CheckRepeatOrder(orderType int, apiUserId, site, baseCoin string) int64 {
func (e *LinePreOrder) CheckRepeatOrder(symbolType int, apiUserId, site, baseCoin string) int64 {
var count int64
e.Orm.Model(&models.LinePreOrder{}).Where("api_id = ? AND pid=0 AND symbol like ? AND order_type = ? AND site = ? AND `status` IN (1,5,6)", apiUserId, baseCoin+"%", orderType, site).Count(&count)
e.Orm.Model(&models.LinePreOrder{}).Where("api_id = ? AND pid=0 AND symbol like ? AND symbol_type = ? AND site = ? AND `status` IN (1,5,6)", apiUserId, baseCoin+"%", symbolType, site).Count(&count)
return count
}
@ -897,6 +898,7 @@ func (e *LinePreOrder) AddBatchPreOrder(batchReq *dto.LineBatchAddPreOrderReq, p
req.Price = batchReq.Price
req.Profit = batchReq.Profit
req.Ext = batchReq.Ext
req.SymbolType = batchReq.SymbolType
// req.StopPrice = batchReq.StopPrice
req.ReducePriceRatio = batchReq.ReducePriceRatio
req.PriceType = batchReq.PriceType
@ -1176,6 +1178,8 @@ func (e *LinePreOrder) ClearAll() error {
"stop_loss_markt",
"_PreSpotOrderList_",
"_PreFutOrderList_",
"spot_reduce_list",
"futures_reduce_list",
}
err = helper.DefaultRedis.DeleteKeysByPrefix(prefixs...)
if err != nil {

View File

@ -25,6 +25,7 @@ import (
"time"
"github.com/bytedance/sonic"
"github.com/shopspring/decimal"
"gorm.io/driver/mysql"
"github.com/go-admin-team/go-admin-core/sdk"
@ -225,19 +226,28 @@ func (t LimitOrderTimeoutDuration) ReSpotOrderPlace(db *gorm.DB, order models.Li
logger.Error(fmt.Sprintf("取消现货委托失败:order_sn:%s err:%+v", order.OrderSn, err))
return err
} else {
var remainingQuantity decimal.Decimal
spotOrder, err := spotApi.GetOrderByOrderSnLoop(order.Symbol, order.OrderSn, apiUserinfo, 4)
if err == nil {
origQty := utility.StrToDecimal(spotOrder.OrigQuoteOrderQty)
excuteQty := utility.StrToDecimal(spotOrder.ExecutedQty)
remainingQuantity = origQty.Sub(excuteQty).Abs()
}
if remainingQuantity.Cmp(decimal.Zero) <= 0 {
logger.Errorf("剩余数量为0 无需重新下市价单mainid%v", order.MainId)
return nil
}
tradeSet, _ := binanceservice.GetTradeSet(order.Symbol, 0)
newClientOrderId := snowflakehelper.GetOrderId()
order.Num = remainingQuantity.Truncate(int32(tradeSet.AmountDigit)).String()
order.Desc = fmt.Sprintf("取消限价单,重下市价单源订单号:%s ", order.OrderSn)
order.OrderSn = utility.Int64ToString(snowflakehelper.GetOrderId())
order.MainOrderType = "MARKET"
// var newOrder models.LinePreOrder
// copier.Copy(&newOrder, order)
// newOrder.Id = 0
// newOrder.OrderSn = utility.Int64ToString(newClientOrderId)
// newOrder.CreatedAt = time.Now()
// newOrder.MainOrderType = "MARKET"
// err = db.Model(&models.LinePreOrder{}).Create(&newOrder).Error
err := db.Model(&order).Updates(map[string]interface{}{"desc": order.Desc, "order_sn": order.OrderSn, "main_order_type": order.MainOrderType}).Error
err = db.Model(&order).Updates(map[string]interface{}{"desc": order.Desc, "order_sn": order.OrderSn, "main_order_type": order.MainOrderType}).Error
if err != nil {
logger.Error(fmt.Sprintf("生成新市价单失败 err:%+v", err))
@ -251,7 +261,7 @@ func (t LimitOrderTimeoutDuration) ReSpotOrderPlace(db *gorm.DB, order models.Li
TimeInForce: "GTC",
Price: utility.StringToDecimal(order.Price),
StopPrice: utility.StrToDecimal(order.Price),
Quantity: utility.StringToDecimal(order.Num),
Quantity: remainingQuantity,
NewClientOrderId: utility.Int64ToString(newClientOrderId),
}
if err := spotApi.OrderPlace(db, params); err != nil {
@ -281,8 +291,24 @@ func (t LimitOrderTimeoutDuration) ReFutOrderPlace(db *gorm.DB, order models.Lin
logger.Error(fmt.Sprintf("取消现货委托失败:order_sn:%s err:%+v", order.OrderSn, err))
return err
} else {
var remainingQuantity decimal.Decimal
spotOrder, err := futApi.GetOrderByOrderSnLoop(order.Symbol, order.OrderSn, apiUserinfo, 4)
if err == nil {
origQty := utility.StrToDecimal(spotOrder.OrigQty)
excuteQty := utility.StrToDecimal(spotOrder.ExecutedQty)
remainingQuantity = origQty.Sub(excuteQty).Abs()
}
if remainingQuantity.Cmp(decimal.Zero) <= 0 {
logger.Errorf("剩余数量为0 无需重新下市价单mainid%v", order.MainId)
return nil
}
tradeSet, _ := binanceservice.GetTradeSet(order.Symbol, 0)
newClientOrderId := snowflakehelper.GetOrderId()
orderType := "MARKET"
order.Num = remainingQuantity.Truncate(int32(tradeSet.AmountDigit)).String()
order.Desc = fmt.Sprintf("取消限价单,重下市价单 源订单号:%s", order.OrderSn)
order.OrderSn = utility.Int64ToString(newClientOrderId)
@ -294,31 +320,32 @@ func (t LimitOrderTimeoutDuration) ReFutOrderPlace(db *gorm.DB, order models.Lin
// newOrder.MainOrderType = "MARKET"
// err = db.Model(&models.LinePreOrder{}).Create(&newOrder).Error
var positionSide string
if order.Site == "BUY" {
positionSide = "SHORT"
} else {
positionSide = "LONG"
}
err = db.Model(&order).Updates(map[string]interface{}{"desc": order.Desc, "order_sn": order.OrderSn}).Error
if err != nil {
logger.Error(fmt.Sprintf("生成合约新市价单失败 err:%+v", err))
return err
}
if order.OrderType == 4 {
orderType = "STOP_MARKET"
}
// params := binanceservice.FutOrderPlace{
// ApiId: order.ApiId,
// Symbol: order.Symbol,
// Side: order.Site,
// Quantity: remainingQuantity,
// Price: utility.StringToDecimal(order.Price),
// SideType: "MARKET",
// OpenOrder: 0,
// OrderType: "MARKET",
// NewClientOrderId: utility.Int64ToString(newClientOrderId),
// }
params := binanceservice.FutOrderPlace{
ApiId: order.ApiId,
Symbol: order.Symbol,
Side: order.Site,
Quantity: utility.StringToDecimal(order.Num),
Price: utility.StringToDecimal(order.Price),
SideType: "MARKET",
OpenOrder: 0,
StopPrice: utility.StringToDecimal(order.Price),
Profit: utility.StringToDecimal(order.Price),
OrderType: orderType,
NewClientOrderId: utility.Int64ToString(newClientOrderId),
}
if err := futApi.OrderPlace(db, params); err != nil {
if err := futApi.ClosePositionLoop(order.Symbol, order.OrderSn, remainingQuantity, order.Site, positionSide, apiUserinfo, "MARKET", "0", decimal.Zero, 3); err != nil {
logger.Error(fmt.Sprintf("重新下合约市价单失败 err:%+v", err))
err := db.Model(&order).Updates(map[string]interface{}{"status": "2", "desc": order.Desc + " err:" + err.Error()}).Error
if err != nil {