1、重写

This commit is contained in:
2025-02-06 18:03:09 +08:00
parent 07847a2d9e
commit c0b8749eef
15 changed files with 546 additions and 720 deletions

View File

@ -498,41 +498,6 @@ func (e LinePreOrder) ClearAll(c *gin.Context) {
e.OK(nil, "操作成功")
}
// ManuallyCover 手动加仓
func (e LinePreOrder) ManuallyCover(c *gin.Context) {
s := service.LinePreOrder{}
req := dto.ManuallyCover{}
err := e.MakeContext(c).
MakeOrm().
Bind(&req).
MakeService(&s.Service).
Errors
if err != nil {
e.Logger.Error(err)
e.Error(500, err, err.Error())
return
}
err = req.CheckParams()
if err != nil {
e.Logger.Error(err)
e.Error(500, err, err.Error())
return
}
p := actions.GetPermissionFromContext(c)
errs := make([]error, 0)
errStr := make([]string, 0)
s.ManuallyCover(req, p, &errs)
if len(errs) > 0 {
for _, err2 := range errs {
errStr = append(errStr, err2.Error())
}
e.Error(500, nil, strings.Join(errStr, ","))
return
}
e.OK(nil, "操作成功")
}
// ClosePosition 平仓
func (e LinePreOrder) ClosePosition(c *gin.Context) {
s := service.LinePreOrder{}

View File

@ -9,34 +9,38 @@ import (
type LinePreOrder struct {
models.Model
ExchangeType string `json:"exchangeType" gorm:"type:varchar(20);comment:交易所类型 (字典 exchange_type"`
Pid int `json:"pid" gorm:"type:int unsigned;omitempty;comment:pid"`
ApiId int `json:"apiId" gorm:"type:varchar(255);omitempty;comment:api用户"`
GroupId string `json:"groupId" gorm:"type:int unsigned;omitempty;comment:交易对组id"`
Symbol string `json:"symbol" gorm:"type:varchar(255);omitempty;comment:交易对"`
QuoteSymbol string `json:"quoteSymbol" gorm:"type:varchar(255);omitempty;comment:计较货币"`
SignPrice string `json:"signPrice" gorm:"type:decimal(18,8);omitempty;comment:对标价"`
SignPriceU decimal.Decimal `json:"signPriceU" gorm:"type:decimal(18,8);omitempty;comment:交易对对标U的行情价"`
SignPriceType string `json:"signPriceType" gorm:"type:enum('new','tall','low','mixture','entrust','add');omitempty;comment:对标价类型: new=最新价格;tall=24小时最高;low=24小时最低;mixture=标记价;entrust=委托实价;add=补仓"`
Rate string `json:"rate" gorm:"type:decimal(18,2);omitempty;comment:下单百分比"`
Price string `json:"price" gorm:"type:decimal(18,8);omitempty;comment:触发价格"`
Num string `json:"num" gorm:"type:decimal(18,8);omitempty;comment:购买数量"`
BuyPrice string `json:"buyPrice" gorm:"type:decimal(18,8);omitempty;comment:购买金额"`
SymbolType int `json:"symbolType" gorm:"type:int;comment:交易对类型:1=现货;2=合约"`
Site string `json:"site" gorm:"type:enum('BUY','SELL');omitempty;comment:购买方向:BUY=买;SELL=卖"`
OrderSn string `json:"orderSn" gorm:"type:varchar(255);omitempty;comment:订单号"`
OrderType int `json:"orderType" gorm:"type:int;omitempty;comment:订单类型:0=主单 1=止盈 2=止损 3=平仓"`
Desc string `json:"desc" gorm:"type:text;omitempty;comment:订单描述"`
Status int `json:"status" gorm:"type:int;omitempty;comment:是否被触发:0=待触发;1=已触发;2=下单失败;4=已取消;5=委托中;6=已成交;9=已平仓"`
AdminId string `json:"adminId" gorm:"type:int unsigned;omitempty;comment:操作管理员id"`
CloseType int `json:"closeType" gorm:"type:int unsigned;omitempty;comment:平仓类型 是否为盈利平仓 1= 是 0 =否"`
CoverType int `json:"coverType" gorm:"type:int unsigned;omitempty;comment:对冲类型 1= 现货对合约 2=合约对合约 3 合约对现货"`
ExpireTime time.Time `json:"expireTime" gorm:"comment:过期时间"`
MainOrderType string `json:"mainOrderType" gorm:"type:enum;comment:第一笔(主单类型) 限价LIMIT市价(MARKET)"`
HedgeOrderType string `json:"hedgeOrderType" gorm:"type:enum;comment:第二笔类型 限价LIMIT市价(MARKET)"`
Child []LinePreOrder `json:"child" gorm:"-"`
ApiName string `json:"api_name" gorm:"->"`
ChildNum int64 `json:"child_num" gorm:"->"`
ExchangeType string `json:"exchangeType" gorm:"type:varchar(20);comment:交易所类型 (字典 exchange_type"`
Pid int `json:"pid" gorm:"type:int unsigned;omitempty;comment:pid"`
ApiId int `json:"apiId" gorm:"type:varchar(255);omitempty;comment:api用户"`
GroupId string `json:"groupId" gorm:"type:int unsigned;omitempty;comment:交易对组id"`
Symbol string `json:"symbol" gorm:"type:varchar(255);omitempty;comment:交易对"`
QuoteSymbol string `json:"quoteSymbol" gorm:"type:varchar(255);omitempty;comment:计较货币"`
SignPrice string `json:"signPrice" gorm:"type:decimal(18,8);omitempty;comment:对标价"`
SignPriceU decimal.Decimal `json:"signPriceU" gorm:"type:decimal(18,8);omitempty;comment:交易对对标U的行情价"`
SignPriceType string `json:"signPriceType" gorm:"type:enum('new','tall','low','mixture','entrust','add');omitempty;comment:对标价类型: new=最新价格;tall=24小时最高;low=24小时最低;mixture=标记价;entrust=委托实价;add=补仓"`
Rate string `json:"rate" gorm:"type:decimal(18,2);omitempty;comment:下单百分比"`
Price string `json:"price" gorm:"type:decimal(18,8);omitempty;comment:触发价格"`
Num string `json:"num" gorm:"type:decimal(18,8);omitempty;comment:购买数量"`
BuyPrice string `json:"buyPrice" gorm:"type:decimal(18,8);omitempty;comment:购买金额"`
SymbolType int `json:"symbolType" gorm:"type:int;comment:交易对类型:1=现货;2=合约"`
OrderCategory int `json:"orderCategory" gorm:"type:int;comment:订单类型: 1=主单 2=对冲单"`
Site string `json:"site" gorm:"type:enum('BUY','SELL');omitempty;comment:购买方向:BUY=买;SELL=卖"`
OrderSn string `json:"orderSn" gorm:"type:varchar(255);omitempty;comment:订单"`
OrderType int `json:"orderType" gorm:"type:int;omitempty;comment:订单类型:0=主单 1=止盈 2=止损 3=平仓"`
Desc string `json:"desc" gorm:"type:text;omitempty;comment:订单描述"`
Status int `json:"status" gorm:"type:int;omitempty;comment:是否被触发:0=待触发;1=已触发;2=下单失败;4=已取消;5=委托中;6=已成交;9=已平仓"`
AdminId string `json:"adminId" gorm:"type:int unsigned;omitempty;comment:操作管理员id"`
CloseType int `json:"closeType" gorm:"type:int unsigned;omitempty;comment:平仓类型 是否为盈利平仓 1= 是 0 =否"`
CoverType int `json:"coverType" gorm:"type:int unsigned;omitempty;comment:对冲类型 1= 现货对合约 2=合约对合约 3 合约对现货"`
ExpireTime time.Time `json:"expireTime" gorm:"comment:过期时间"`
MainOrderType string `json:"mainOrderType" gorm:"type:enum;comment:第一笔(主单类型 限价LIMIT市价(MARKET)"`
HedgeBuyTotal decimal.Decimal `json:"hedgeBuyTotal" gorm:"type:decimal(10,2);comment:对冲买总金额U"`
HedgeOrderType string `json:"hedgeOrderType" gorm:"type:enum;comment:第二笔类型 限价LIMIT市价(MARKET)"`
HedgeTakeProfit decimal.Decimal `json:"hedgeTakeProfit" gorm:"type:decimal(10,2);comment:对冲止盈百分比"`
HedgeStopLoss decimal.Decimal `json:"hedgeStopLoss" gorm:"type:decimal(10,2);comment:对冲止损百分比"`
Child []LinePreOrder `json:"child" gorm:"-"`
ApiName string `json:"api_name" gorm:"->"`
ChildNum int64 `json:"child_num" gorm:"->"`
// LinePreOrder 线上预埋单\
models.ModelTime
models.ControlBy

View File

@ -9,20 +9,11 @@ import (
type LineSystemSetting struct {
models.Model
Time int64 `json:"time" gorm:"type:int;comment:导入:挂单时长达到时间后失效"`
BatchTime int64 `json:"batchTime" gorm:"type:int;comment:批量:挂单时长达到时间后失效"`
ProfitRate string `json:"profitRate" gorm:"type:decimal(10,2);comment:平仓盈利比例"`
CoverOrderTypeBRate string `json:"coverOrderTypeBRate" gorm:"type:decimal(10,2);comment:b账户限价补单的买入百分比"`
ScaleOrderTypeARate string `json:"scaleOrderTypeARate" gorm:"type:decimal(10,2);comment:a账户限价加仓买入百分比"`
ScaleOrderTypeBRate string `json:"scaleOrderTypeBRate" gorm:"type:decimal(10,2);comment:b账户限价加仓买入百分比"`
ScaleUnrealizedProfitRate string `json:"scaleUnrealizedProfitRate" gorm:"type:decimal(10,5) unsigned;comment:亏损百分比加仓"`
ScaleType int `json:"scaleType" gorm:"type:int;comment:加仓类型 1-百分比 2-数值"`
ScaleNum string `json:"scaleNum" gorm:"type:decimal(18,2) unsigned;comment:加仓数值"`
ScaleSubordinate int64 `json:"scaleSubordinate" gorm:"type:int unsigned;comment:加仓账户:1=A账户;2=副账户;3=都加"`
AutoScaleTimes int64 `json:"autoScaleTimes" gorm:"type:int unsigned;comment:自动加仓次数"`
HedgePerformance decimal.Decimal `json:"hedgePerformance" gorm:"type:decimal(5,2);comment:对冲平仓涨跌幅"`
ProtectHedgeRate decimal.Decimal `json:"protectHedgeRate" gorm:"type:decimal(5,2);comment:保护对冲触发百分比"`
ProtectHedgeEnable int `json:"protectHedgeEnable" gorm:"type:int;comment:是否只开启保护对冲 1-开启 0-关闭"`
Time int64 `json:"time" gorm:"type:int;comment:导入:挂单时长达到时间后失效"`
BatchTime int64 `json:"batchTime" gorm:"type:int;comment:批量:挂单时长达到时间后失效"`
ProfitRate string `json:"profitRate" gorm:"type:decimal(10,2);comment:平仓盈利比例"`
CoverOrderTypeBRate string `json:"coverOrderTypeBRate" gorm:"type:decimal(10,2);comment:b账户限价补单的买入百分比"`
StopLossPremium decimal.Decimal `json:"stopLossPremium" gorm:"type:decimal(10,2);comment:限价止损溢价百分比"`
models.ModelTime
models.ControlBy
}

View File

@ -33,7 +33,6 @@ func registerLinePreOrderRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTM
r.POST("cancelOpenOrder", actions.PermissionAction(), api.CancelOpenOrder) //取消委托
r.POST("clearAll", actions.PermissionAction(), api.ClearAll) // 一键清除数据
r.POST("getChildOrder", actions.PermissionAction(), api.GetChildOrderList) // 获取子订单
r.POST("manuallyCover", actions.PermissionAction(), api.ManuallyCover) // 手动加仓
r.POST("closePosition", actions.PermissionAction(), api.ClosePosition) // 平仓
r.GET("getOrderPage", actions.PermissionAction(), api.GetOrderPage) //订单列表
r.POST("clearUnTriggered", actions.PermissionAction(), api.ClearUnTriggered) // 清除待触发的交易对

View File

@ -358,16 +358,15 @@ type PreOrderRedisList struct {
}
type StopLossRedisList struct {
// Id int `json:"id"`
PId int `json:"pid"`
OrderTye string `json:"orderType"`
// Num decimal.Decimal `json:"num"`
Symbol string `json:"symbol"`
Price decimal.Decimal `json:"price"` //触发价(根据主单价格触发)
Site string `json:"site"`
ApiId int `json:"api_id"`
Stoploss decimal.Decimal `json:"stoploss"` //亏损百分比对冲
Id int `json:"id"`
PId int `json:"pid"`
OrderTye int `json:"orderType"`
SymbolType int `json:"symbolType"`
OrderCategory int `json:"orderCategory"`
Symbol string `json:"symbol"`
Price decimal.Decimal `json:"price"` //触发价(根据主单价格触发)
Site string `json:"site"`
ApiId int `json:"api_id"`
}
// ManuallyCover 手动加仓请求体

View File

@ -13,7 +13,6 @@ import (
"go-admin/services/binanceservice"
"strconv"
"strings"
"sync"
"time"
"github.com/bytedance/sonic"
@ -340,18 +339,15 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
return nil
}
var key string
//var tickerSymbol string
if req.OrderType == global.SYMBOL_SPOT {
if req.SymbolType == global.SYMBOL_SPOT {
key = fmt.Sprintf("%s:%s", global.TICKER_SPOT, req.Symbol)
//tickerSymbol = helper.DefaultRedis.Get(rediskey.SpotSymbolTicker).Val()
} else {
key = fmt.Sprintf("%s:%s", global.TICKER_FUTURES, req.Symbol)
//tickerSymbol = helper.DefaultRedis.Get(rediskey.FutSymbolTicker).Val()
}
for _, id := range apiUserIds {
if req.Site == "SELL" && req.OrderType == global.SYMBOL_SPOT {
*errs = append(*errs, errors.New(fmt.Sprintf("api_id:%s 获取交易对:%s 现货不支持卖出操作", id, req.Symbol)))
if req.Site == "SELL" && req.SymbolType == global.SYMBOL_SPOT {
*errs = append(*errs, fmt.Errorf("api_id:%s 获取交易对:%s 现货不支持卖出操作", id, req.Symbol))
continue
}
var AddOrder models.LinePreOrder
@ -362,7 +358,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)
if orderCount > 0 {
*errs = append(*errs, errors.New(fmt.Sprintf("api_id:%s 获取交易对:%s 该交易对已存在,请勿重复下单", id, req.Symbol)))
*errs = append(*errs, fmt.Errorf("api_id:%s 获取交易对:%s 该交易对已存在,请勿重复下单", id, req.Symbol))
continue
}
var tickerPrice decimal.Decimal
@ -370,9 +366,9 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
tickerVal := ""
if req.SymbolType == 1 {
tickerVal, _ = helper.DefaultRedis.GetString(fmt.Sprintf(global.TICKER_SPOT, req.ExchangeType))
tickerVal, _ = helper.DefaultRedis.GetString(fmt.Sprintf(global.TICKER_SPOT, req.ExchangeType, req.Symbol))
} else {
tickerVal, _ = helper.DefaultRedis.GetString(fmt.Sprintf(global.TICKER_FUTURES, req.ExchangeType))
tickerVal, _ = helper.DefaultRedis.GetString(fmt.Sprintf(global.TICKER_FUTURES, req.ExchangeType, req.Symbol))
}
if tickerVal == "" {
@ -387,10 +383,11 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
}
if tickerPrice.Equal(decimal.Zero) { //redis 没有这个值
*errs = append(*errs, errors.New(fmt.Sprintf("api_id:%s 获取交易对:%s 交易行情出错", id, req.Symbol)))
*errs = append(*errs, fmt.Errorf("api_id:%s 获取交易对:%s 交易行情出错", id, req.Symbol))
continue
}
AddOrder.OrderCategory = 1
AddOrder.SignPriceType = "new"
AddOrder.BuyPrice = req.BuyPrice //购买多少U
AddOrder.SymbolType = req.SymbolType //交易对类型1= 现货 2 = 合约
@ -512,7 +509,7 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
}
profitOrder.OrderSn = strconv.FormatInt(snowflakehelper.GetOrderId(), 10)
profitOrder.Pid = AddOrder.Id
profitOrder.OrderType = 2
profitOrder.OrderType = 1
profitOrder.Status = 0
profitOrder.Rate = req.Profit
@ -529,7 +526,7 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
}
stopOrder.OrderSn = strconv.FormatInt(snowflakehelper.GetOrderId(), 10)
stopOrder.Pid = AddOrder.Id
stopOrder.OrderType = 1
stopOrder.OrderType = 2
stopOrder.Status = 0
stopOrder.Rate = req.StopPrice
@ -898,28 +895,6 @@ func (e *LinePreOrder) ClearAll() error {
return err
}
func (e *LinePreOrder) ManuallyCover(req dto.ManuallyCover, p *actions.DataPermission, errs *[]error) {
symbols := strings.Split(req.Symbols, ",")
futApi := binanceservice.FutRestApi{}
spotApi := binanceservice.SpotRestApi{}
addPositionService := binanceservice.AddPosition{Db: e.Orm}
var wg sync.WaitGroup // 用于等待所有协程完成
var mu sync.Mutex // 用于保护错误切片的并发访问
for _, symbol := range symbols {
wg.Add(1) // 增加协程计数
go func(symbol string) {
defer wg.Done() // 协程完成后减少计数
if err := addPositionService.ProcessSymbol(req, symbol, &futApi, &spotApi, errs); err != nil {
mu.Lock() // 加锁保护错误切片
*errs = append(*errs, err)
mu.Unlock() // 解锁
}
}(symbol)
}
wg.Wait() // 等待所有协程完成
}
// GetTargetSymbol 获取目标交易对信息
func (e *LinePreOrder) GetTargetSymbol(symbol string, symbolType int) (string, bool, models.LineSymbol, error) {
var targetSymbol string