This commit is contained in:
2025-03-10 11:13:08 +08:00
parent 0aa2ab7355
commit 00aa341c2f
8 changed files with 176 additions and 89 deletions

View File

@ -353,31 +353,34 @@ type LineTreeOrder struct {
// LineBatchAddPreOrderReq 批量添加订单请求参数
type LineBatchAddPreOrderReq struct {
ExchangeType string `json:"exchange_type"` //交易所类型 字典exchange_type
SymbolType int `json:"symbol_type"` //主单交易对类型 0-现货 1-合约
OrderType int `json:"order_type"` //订单类型
SymbolGroupId string `json:"symbol_group_id"` //交易对组id
Symbol string `json:"symbol"` //交易对
ApiUserId string `json:"api_id"` //下单用户
Site string `json:"site"` //购买方向
BuyPrice string `json:"buy_price"` //购买金额 U
PricePattern string `json:"price_pattern"` //价格模式
Price string `json:"price"` //下单价百分比
Profit string `json:"profit"` //止盈
StopPrice string `json:"stop_price"` //止损价
PriceType string `json:"price_type"` //价格类型
SaveTemplate string `json:"save_template"` //是否保存模板
TemplateName string `json:"template_name"` //模板名字
OrderNum int `json:"order_num"` //脚本运行次数
Script string `json:"script"` //是否是脚本运行 1 = 是 0= 否
CoverType int `json:"cover_type"` //对冲类型 1= 现货对合约 2=合约对合约 3 合约对现货
ExpireHour int `json:"expire_hour"` // 过期时间 单位小时
MainOrderType string `json:"main_order_type"` //主单类型:限价(LIMIT)或市价(MARKET)
ReducePriceRatio decimal.Decimal `json:"reduce_price"` //主单减仓价格百分比
ReduceNumRatio decimal.Decimal `json:"reduce_num"` //主单减仓数量百分比
ReduceTakeProfitRatio decimal.Decimal `json:"reduce_take_profit"` //主单减仓后止盈价百分比
ReduceStopLossRatio decimal.Decimal `json:"reduce_stop_price"` //主单减仓后止损价百分比
Ext []LineAddPreOrderExtReq `json:"ext"` //拓展字段
ExchangeType string `json:"exchange_type"` //交易所类型 字典exchange_type
SymbolType int `json:"symbol_type"` //主单交易对类型 0-现货 1-合约
OrderType int `json:"order_type"` //订单类型
SymbolGroupId string `json:"symbol_group_id"` //交易对组id
Symbol string `json:"symbol"` //交易对
ApiUserId string `json:"api_id"` //下单用户
Site string `json:"site"` //购买方向
BuyPrice string `json:"buy_price"` //购买金额 U
PricePattern string `json:"price_pattern"` //价格模式
Price string `json:"price"` //下单价百分比
ProfitNumRatio decimal.Decimal `json:"profit_num_ratio"` //止盈数量百分比
ProfitTpTpPriceRatio decimal.Decimal `json:"profit_tp_tp_price_ratio"` //止盈后止盈价百分比
ProfitTpSlPriceRatio decimal.Decimal `json:"profit_tp_sl_price_ratio"` //止盈后止损价百分比
Profit string `json:"profit"` //止盈价
StopPrice string `json:"stop_price"` //止损价
PriceType string `json:"price_type"` //价格类型
SaveTemplate string `json:"save_template"` //是否保存模板
TemplateName string `json:"template_name"` //模板名字
OrderNum int `json:"order_num"` //脚本运行次数
Script string `json:"script"` //是否是脚本运行 1 = 是 0= 否
CoverType int `json:"cover_type"` //对冲类型 1= 现货对合约 2=合约对合约 3 合约对现货
ExpireHour int `json:"expire_hour"` // 过期时间 单位小时
MainOrderType string `json:"main_order_type"` //主单类型:限价(LIMIT)或市价(MARKET)
ReducePriceRatio decimal.Decimal `json:"reduce_price"` //主单减仓价百分比
ReduceNumRatio decimal.Decimal `json:"reduce_num"` //主单减仓数量百分比
ReduceTakeProfitRatio decimal.Decimal `json:"reduce_take_profit"` //主单减仓后止盈价百分比
ReduceStopLossRatio decimal.Decimal `json:"reduce_stop_price"` //主单减仓后止损价百分比
Ext []LineAddPreOrderExtReq `json:"ext"` //拓展字段
}
func (req LineBatchAddPreOrderReq) CheckParams() error {

View File

@ -244,7 +244,49 @@ func (e *LinePreOrder) Remove(d *dto.LinePreOrderDeleteReq, p *actions.DataPermi
}
positions := map[string]positiondto.LinePreOrderPositioinDelReq{}
addPosition := binanceservice.AddPositionList{}
reduceItem := binanceservice.ReduceListItem{}
futAddPosition := map[int]string{}
spotAddPosition := map[int]string{}
futReduces := map[int]string{}
spotRedces := map[int]string{}
futAddPositionKey := fmt.Sprintf(rediskey.FuturesAddPositionList, global.EXCHANGE_BINANCE)
spotAddPositionKey := fmt.Sprintf(rediskey.FuturesReduceList, global.EXCHANGE_BINANCE)
futReduceKey := fmt.Sprintf(rediskey.SpotAddPositionList, global.EXCHANGE_BINANCE)
spotReduceKey := fmt.Sprintf(rediskey.SpotReduceList, global.EXCHANGE_BINANCE)
futAddPositionVal, _ := helper.DefaultRedis.GetAllList(futAddPositionKey)
futReduceVal, _ := helper.DefaultRedis.GetAllList(spotAddPositionKey)
spotAddPositionVal, _ := helper.DefaultRedis.GetAllList(futReduceKey)
spotReduceVal, _ := helper.DefaultRedis.GetAllList(spotReduceKey)
for _, v := range futAddPositionVal {
sonic.Unmarshal([]byte(v), &addPosition)
if addPosition.MainId > 0 {
futAddPosition[addPosition.MainId] = v
}
}
for _, v := range spotAddPositionVal {
sonic.Unmarshal([]byte(v), &addPosition)
if addPosition.MainId > 0 {
spotAddPosition[addPosition.MainId] = v
}
}
for _, v := range futReduceVal {
sonic.Unmarshal([]byte(v), &reduceItem)
if reduceItem.MainId > 0 {
futReduces[reduceItem.MainId] = v
}
}
for _, v := range spotReduceVal {
sonic.Unmarshal([]byte(v), &reduceItem)
if reduceItem.MainId > 0 {
spotRedces[reduceItem.MainId] = v
}
}
//删除的缓存
for _, order := range list {
redisList := dto.PreOrderRedisList{
@ -261,6 +303,21 @@ func (e *LinePreOrder) Remove(d *dto.LinePreOrderDeleteReq, p *actions.DataPermi
apiIds = append(apiIds, order.ApiId)
}
//清除待加仓、待减仓
if val, ok := futAddPosition[order.Id]; ok {
helper.DefaultRedis.LRem(futAddPositionKey, val)
}
if val, ok := spotAddPosition[order.Id]; ok {
helper.DefaultRedis.LRem(spotAddPositionKey, val)
}
if val, ok := futReduces[order.Id]; ok {
helper.DefaultRedis.LRem(futReduceKey, val)
}
if val, ok := spotRedces[order.Id]; ok {
helper.DefaultRedis.LRem(spotReduceKey, val)
}
tradeSet, _ := helper.GetObjString[models2.TradeSet](helper.DefaultRedis, fmt.Sprintf(global.TICKER_SPOT, order.ExchangeType, order.Symbol))
redisList.Price = utility.StringToDecimal(redisList.Price).Truncate(int32(tradeSet.PriceDigit)).String()
marshal, _ := sonic.Marshal(redisList)
@ -483,6 +540,7 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
defultExt2.TotalAfter = mainAmount.Mul(decimal.NewFromInt(100).Sub(req.ReduceNumRatio)).Div(decimal.NewFromInt(100)).Truncate(int32(tradeSet.AmountDigit))
defultExt2.ReTakeRatio = req.ReducePriceRatio.Div(decimal.NewFromInt(100).Sub(req.ReduceNumRatio).Div(decimal.NewFromInt(100))).Truncate(2)
preOrderExts = append(preOrderExts, defultExt)
preOrderExts = append(preOrderExts, defultExt2)
calculateResp := dto.CalculateBreakEvenRatioResp{}
mainParam := dto.CalculateBreakEevenRatioReq{
@ -550,7 +608,7 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
//加仓、减仓状态
tx.Model(&models.LinePreOrderStatus{}).Create(&preOrderStatus)
preOrderExts[0].OrderId = AddOrder.Id
list := dto.PreOrderRedisList{
Id: AddOrder.Id,
Symbol: AddOrder.Symbol,
@ -626,7 +684,7 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
stopOrder.Num = stopNum.Truncate(int32(tradeSet.AmountDigit)).String()
tx.Model(&models.LinePreOrder{}).Omit("id", "save_template", "template_name").Create(&stopOrder)
preOrderExts[1].OrderId = stopOrder.Id
if req.ReduceNumRatio.Cmp(decimal.Zero) > 0 && req.ReduceNumRatio.Cmp(decimal.NewFromInt(100)) < 0 {
if newOrders, err := makeReduceTakeAndStoploss(&stopOrder, defultExt2, tradeSet, false); err != nil {
logger.Errorf("主单减仓生成止盈、减仓失败 err:%v", err)
@ -643,10 +701,10 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
//添加止盈单
for index, v := range preOrderExts {
preOrderExts[index].MainOrderId = AddOrder.Id
// if index == 0 {
// preOrderExts[index].OrderId = AddOrder.Id
// continue
// }
if index < 2 {
// preOrderExts[index].OrderId = AddOrder.Id
continue
}
var newOrder models.LinePreOrder
if v.AddType == 1 {
@ -801,10 +859,10 @@ func makeFuturesTakeAndReduce(preOrder *models.LinePreOrder, ext models.LinePreO
orders := make([]models.LinePreOrder, 0)
var side string
if strings.ToUpper(preOrder.Site) == "BUY" {
side = "SELL"
} else {
if (preOrder.OrderType != 0 && strings.ToUpper(preOrder.Site) == "BUY") || (preOrder.OrderType == 0 && strings.ToUpper(preOrder.Site) == "SELL") {
side = "BUY"
} else {
side = "SELL"
}
if ext.TakeProfitRatio.Cmp(decimal.Zero) > 0 {
@ -1055,6 +1113,9 @@ func (e *LinePreOrder) AddBatchPreOrder(batchReq *dto.LineBatchAddPreOrderReq, p
req.PricePattern = batchReq.PricePattern
req.Price = batchReq.Price
req.Profit = batchReq.Profit
req.ProfitNumRatio = batchReq.ProfitNumRatio
req.ProfitTpTpPriceRatio = batchReq.ProfitTpSlPriceRatio
req.ProfitTpSlPriceRatio = batchReq.ProfitTpSlPriceRatio
req.Ext = batchReq.Ext
req.SymbolType = batchReq.SymbolType
// req.StopPrice = batchReq.StopPrice
@ -1663,8 +1724,10 @@ func (e *LinePreOrder) FutClosePosition(position *dto.ClosePosition, errs *[]err
continue
}
//撤销合约的委托
api.CancelAllFutOrder(apiUserInfo, list.Symbol)
//撤销合约的委托(根据方向撤)
orderSns, _ := binanceservice.GetOpenOrderSns(e.Orm, []int{list.Id})
api.CancelBatchFutOrder(apiUserInfo, list.Symbol, orderSns)
// api.CancelAllFutOrder(apiUserInfo, list.Symbol)
//side=BUY&positionSide=LONG是开多
//side=SELL&positionSide=LONG是平多
//side=SELL&positionSide=SHORT是开空