code
This commit is contained in:
@ -464,7 +464,9 @@ func (e LinePreOrder) CancelOpenOrder(c *gin.Context) {
|
||||
}
|
||||
errs := make([]error, 0)
|
||||
errStr := make([]string, 0)
|
||||
s.CancelOpenOrder(&req, &errs)
|
||||
if req.ExchangeType == global.EXCHANGE_BINANCE {
|
||||
s.CancelOpenOrder(&req, &errs)
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
for _, err2 := range errs {
|
||||
errStr = append(errStr, err2.Error())
|
||||
@ -520,10 +522,13 @@ func (e LinePreOrder) ClosePosition(c *gin.Context) {
|
||||
}
|
||||
errs := make([]error, 0)
|
||||
errStr := make([]string, 0)
|
||||
if req.CloseType == 1 {
|
||||
s.SpotClosePosition(&req, &errs)
|
||||
} else {
|
||||
s.FutClosePosition(&req, &errs)
|
||||
|
||||
if req.ExchangeType == global.EXCHANGE_BINANCE {
|
||||
if req.CloseType == 1 {
|
||||
s.SpotClosePosition(&req, &errs)
|
||||
} else {
|
||||
s.FutClosePosition(&req, &errs)
|
||||
}
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
for _, err2 := range errs {
|
||||
|
||||
@ -198,7 +198,6 @@ type LineAddPreOrderReq struct {
|
||||
CoverType int `json:"cover_type"` //对冲类型 0=无对冲 1= 现货对合约 2=合约对合约 3 合约对现货
|
||||
ExpireHour int `json:"expire_hour"` // 过期时间 单位小时
|
||||
MainOrderType string `json:"main_order_type"` //主单类型:限价(LIMIT)或市价(MARKET)
|
||||
TakeProfitRatio decimal.Decimal `json:"take_profit"` //主单止盈比例
|
||||
ReducePriceRatio decimal.Decimal `json:"reduce_price"` //主单减仓价格百分比
|
||||
ReduceNumRatio decimal.Decimal `json:"reduce_num"` //主单减仓数量百分比
|
||||
ReduceTakeProfitRatio decimal.Decimal `json:"reduce_take_profit"` //主单减仓后止盈价百分比
|
||||
@ -255,7 +254,6 @@ type LineBatchAddPreOrderReq struct {
|
||||
CoverType int `json:"cover_type"` //对冲类型 1= 现货对合约 2=合约对合约 3 合约对现货
|
||||
ExpireHour int `json:"expire_hour"` // 过期时间 单位小时
|
||||
MainOrderType string `json:"main_order_type"` //主单类型:限价(LIMIT)或市价(MARKET)
|
||||
TakeProfitRatio decimal.Decimal `json:"take_profit"` //主单止盈比例
|
||||
ReducePriceRatio decimal.Decimal `json:"reduce_price"` //主单减仓价格百分比
|
||||
ReduceNumRatio decimal.Decimal `json:"reduce_num"` //主单减仓数量百分比
|
||||
ReduceTakeProfitRatio decimal.Decimal `json:"reduce_take_profit"` //主单减仓后止盈价百分比
|
||||
@ -300,11 +298,12 @@ type QuickAddPreOrderReq struct {
|
||||
|
||||
// LeverReq 设置杠杆请求
|
||||
type LeverReq struct {
|
||||
ApiUserIds string `json:"api_user_ids"`
|
||||
Symbol string `json:"symbol"`
|
||||
Leverage int `json:"leverage"`
|
||||
GroupId int `json:"group_id"`
|
||||
IsAll int `json:"is_all"` // 1= 全部 0=不是全部
|
||||
ApiUserIds string `json:"api_user_ids"`
|
||||
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
|
||||
}
|
||||
|
||||
func (c LeverReq) CheckParams() error {
|
||||
@ -318,18 +317,20 @@ 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=不是全部
|
||||
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
|
||||
}
|
||||
|
||||
type CancelOpenOrderReq struct {
|
||||
ApiId int `json:"api_id"`
|
||||
Symbol string `json:"symbol"`
|
||||
OrderSn string `json:"order_sn"`
|
||||
OrderType int `json:"order_type"`
|
||||
ApiId int `json:"api_id"`
|
||||
Symbol string `json:"symbol"`
|
||||
OrderSn string `json:"order_sn"`
|
||||
OrderType int `json:"order_type"`
|
||||
ExchangeType string `json:"exchange_type"` //交易所类型 字典exchange_type
|
||||
}
|
||||
|
||||
type GetChildOrderReq struct {
|
||||
@ -409,10 +410,11 @@ func (m *ManuallyCover) CheckParams() error {
|
||||
|
||||
// ClosePosition 平仓请求
|
||||
type ClosePosition struct {
|
||||
ApiId int `json:"api_id"` //api-user 用户id
|
||||
Symbol string `json:"symbol"` // 交易对
|
||||
Rate string `json:"rate"` //限价平仓百分比
|
||||
CloseType int `json:"close_type"` //现货平仓=1 合约平仓=2
|
||||
ApiId int `json:"api_id"` //api-user 用户id
|
||||
Symbol string `json:"symbol"` // 交易对
|
||||
Rate string `json:"rate"` //限价平仓百分比
|
||||
CloseType int `json:"close_type"` //现货平仓=1 合约平仓=2
|
||||
ExchangeType string `json:"exchange_type"` //交易所类型 字典exchange_type
|
||||
}
|
||||
|
||||
func (m *ClosePosition) CheckParams() error {
|
||||
|
||||
@ -255,7 +255,7 @@ func (e *LinePreOrder) Remove(d *dto.LinePreOrderDeleteReq, p *actions.DataPermi
|
||||
}
|
||||
|
||||
if len(ints) > 0 {
|
||||
e.Orm.Model(&models.LinePreOrder{}).Where("pid >0 AND pid in ?", ints).Unscoped().Delete(&models.LinePreOrder{})
|
||||
e.Orm.Model(&models.LinePreOrder{}).Where("main_id >0 AND main_id in ?", ints).Unscoped().Delete(&models.LinePreOrder{})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -397,7 +397,7 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
|
||||
//订单配置信息
|
||||
preOrderExts := make([]models.LinePreOrderExt, 0)
|
||||
defultExt := models.LinePreOrderExt{
|
||||
TakeProfitRatio: req.TakeProfitRatio,
|
||||
TakeProfitRatio: utility.StringToDecimal(req.Profit),
|
||||
ReducePriceRatio: req.ReducePriceRatio,
|
||||
ReduceNumRatio: req.ReduceNumRatio,
|
||||
ReduceTakeProfitRatio: req.ReduceTakeProfitRatio,
|
||||
@ -441,6 +441,10 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
|
||||
preOrderExts[index].MainOrderId = AddOrder.Id
|
||||
}
|
||||
|
||||
err = tx.Model(&models.LinePreOrderExt{}).Create(&preOrderExts).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
list := dto.PreOrderRedisList{
|
||||
Id: AddOrder.Id,
|
||||
Symbol: AddOrder.Symbol,
|
||||
@ -650,7 +654,7 @@ func (e *LinePreOrder) QuickAddPreOrder(quickReq *dto.QuickAddPreOrderReq, p *ac
|
||||
// Lever 设置杠杆
|
||||
func (e *LinePreOrder) Lever(req *dto.LeverReq, p *actions.DataPermission, errs *[]error) {
|
||||
users := make([]models.LineApiUser, 0)
|
||||
err := e.Orm.Model(&models.LineApiUser{}).Where("id in ? ", strings.Split(req.ApiUserIds, ",")).Find(&users).Error
|
||||
err := e.Orm.Model(&models.LineApiUser{}).Where("id in ? AND exchange_type = ?", strings.Split(req.ApiUserIds, ","), req.ExchangeType).Find(&users).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
*errs = append(*errs, errors.New(fmt.Sprintf("设置杠杆失败:%+v", err.Error())))
|
||||
return
|
||||
@ -660,14 +664,14 @@ func (e *LinePreOrder) Lever(req *dto.LeverReq, p *actions.DataPermission, errs
|
||||
symbols = req.Symbol
|
||||
} else {
|
||||
var symbolGroupInfo models.LineSymbolGroup
|
||||
e.Orm.Model(&models.LineSymbolGroup{}).Where("id = ?", req.GroupId).Find(&symbolGroupInfo)
|
||||
e.Orm.Model(&models.LineSymbolGroup{}).Where("id = ? AND exchange_type = ?", req.GroupId, req.ExchangeType).Find(&symbolGroupInfo)
|
||||
symbols = symbolGroupInfo.Symbol
|
||||
}
|
||||
|
||||
if req.IsAll == 1 {
|
||||
lineSymbols := make([]models.LineSymbol, 0)
|
||||
futSymbols := make([]string, 0)
|
||||
e.Orm.Model(&models.LineSymbol{}).Where("type = '2' AND switch = '1'").Find(&lineSymbols)
|
||||
e.Orm.Model(&models.LineSymbol{}).Where("type = '2' AND switch = '1' AND exchange_type = ?", req.ExchangeType).Find(&lineSymbols)
|
||||
for _, symbol := range lineSymbols {
|
||||
futSymbols = append(futSymbols, symbol.Symbol)
|
||||
}
|
||||
@ -676,6 +680,15 @@ func (e *LinePreOrder) Lever(req *dto.LeverReq, p *actions.DataPermission, errs
|
||||
}
|
||||
}
|
||||
|
||||
if req.ExchangeType == global.EXCHANGE_BINANCE {
|
||||
e.SetBinanceLever(req, users, symbols, errs)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SetBinanceLever 设置币安杠杆
|
||||
func (e *LinePreOrder) SetBinanceLever(req *dto.LeverReq, users []models.LineApiUser, symbols string, errs *[]error) {
|
||||
for _, user := range users {
|
||||
var client *helper.BinanceClient
|
||||
if user.UserPass == "" {
|
||||
@ -707,38 +720,10 @@ func (e *LinePreOrder) Lever(req *dto.LeverReq, p *actions.DataPermission, errs
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// MarginType 设置仓位模式
|
||||
func (e *LinePreOrder) MarginType(req *dto.MarginTypeReq, p *actions.DataPermission, errs *[]error) {
|
||||
users := make([]models.LineApiUser, 0)
|
||||
err := e.Orm.Model(&models.LineApiUser{}).Where("id in ? ", strings.Split(req.ApiUserIds, ",")).Find(&users).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
*errs = append(*errs, errors.New(fmt.Sprintf("设置杠杆失败:%+v", err.Error())))
|
||||
return
|
||||
}
|
||||
var symbols string
|
||||
if req.Symbol != "" {
|
||||
symbols = req.Symbol
|
||||
} else {
|
||||
var symbolGroupInfo models.LineSymbolGroup
|
||||
e.Orm.Model(&models.LineSymbolGroup{}).Where("id = ?", req.GroupId).Find(&symbolGroupInfo)
|
||||
symbols = symbolGroupInfo.Symbol
|
||||
}
|
||||
|
||||
if req.IsAll == 1 {
|
||||
lineSymbols := make([]models.LineSymbol, 0)
|
||||
futSymbols := make([]string, 0)
|
||||
e.Orm.Model(&models.LineSymbol{}).Where("type = '2' AND switch = '1'").Find(&lineSymbols)
|
||||
for _, symbol := range lineSymbols {
|
||||
futSymbols = append(futSymbols, symbol.Symbol)
|
||||
}
|
||||
if len(futSymbols) != 0 {
|
||||
symbols = strings.Join(futSymbols, ",")
|
||||
}
|
||||
}
|
||||
|
||||
// SetBinanceMarginType 设置币安保证金模式
|
||||
func (e *LinePreOrder) SetBinanceMarginType(req *dto.MarginTypeReq, users []models.LineApiUser, symbols string, errs *[]error) {
|
||||
for _, user := range users {
|
||||
var client *helper.BinanceClient
|
||||
if user.UserPass == "" {
|
||||
@ -776,6 +761,40 @@ func (e *LinePreOrder) MarginType(req *dto.MarginTypeReq, p *actions.DataPermiss
|
||||
}
|
||||
}
|
||||
|
||||
// MarginType 设置仓位模式
|
||||
func (e *LinePreOrder) MarginType(req *dto.MarginTypeReq, p *actions.DataPermission, errs *[]error) {
|
||||
users := make([]models.LineApiUser, 0)
|
||||
err := e.Orm.Model(&models.LineApiUser{}).Where("id in ? ", strings.Split(req.ApiUserIds, ",")).Find(&users).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
*errs = append(*errs, errors.New(fmt.Sprintf("设置杠杆失败:%+v", err.Error())))
|
||||
return
|
||||
}
|
||||
var symbols string
|
||||
if req.Symbol != "" {
|
||||
symbols = req.Symbol
|
||||
} else {
|
||||
var symbolGroupInfo models.LineSymbolGroup
|
||||
e.Orm.Model(&models.LineSymbolGroup{}).Where("id = ?", req.GroupId).Find(&symbolGroupInfo)
|
||||
symbols = symbolGroupInfo.Symbol
|
||||
}
|
||||
|
||||
if req.IsAll == 1 {
|
||||
lineSymbols := make([]models.LineSymbol, 0)
|
||||
futSymbols := make([]string, 0)
|
||||
e.Orm.Model(&models.LineSymbol{}).Where("type = '2' AND switch = '1'").Find(&lineSymbols)
|
||||
for _, symbol := range lineSymbols {
|
||||
futSymbols = append(futSymbols, symbol.Symbol)
|
||||
}
|
||||
if len(futSymbols) != 0 {
|
||||
symbols = strings.Join(futSymbols, ",")
|
||||
}
|
||||
}
|
||||
|
||||
if req.ExchangeType == global.EXCHANGE_BINANCE {
|
||||
e.SetBinanceMarginType(req, users, symbols, errs)
|
||||
}
|
||||
}
|
||||
|
||||
// CancelOpenOrder 取消委托
|
||||
func (e *LinePreOrder) CancelOpenOrder(req *dto.CancelOpenOrderReq, errs *[]error) {
|
||||
newClientOrderIdList := make([]string, 0)
|
||||
@ -805,14 +824,14 @@ func (e *LinePreOrder) CancelOpenOrder(req *dto.CancelOpenOrderReq, errs *[]erro
|
||||
}
|
||||
} else {
|
||||
var orderList []models.LinePreOrder
|
||||
e.Orm.Model(&models.LinePreOrder{}).Where("api_id = ? AND pid = 0 status = '5' AND order_type = ?", req.ApiId, strconv.Itoa(req.OrderType)).Find(&orderList)
|
||||
e.Orm.Model(&models.LinePreOrder{}).Where("api_id = ? AND main_id = 0 status = '5'", req.ApiId).Find(&orderList)
|
||||
if len(orderList) <= 0 {
|
||||
*errs = append(*errs, fmt.Errorf("没有可撤销的委托"))
|
||||
return
|
||||
}
|
||||
for _, order := range orderList {
|
||||
|
||||
if order.OrderType == global.SYMBOL_SPOT {
|
||||
if order.SymbolType == global.SYMBOL_SPOT {
|
||||
err := spotApi.CancelOpenOrderByOrderSn(apiUserInfo, req.Symbol, req.OrderSn)
|
||||
if err != nil {
|
||||
*errs = append(*errs, fmt.Errorf("取消委托失败:%+v", err.Error()))
|
||||
@ -1030,6 +1049,7 @@ func (e *LinePreOrder) SpotClosePosition(position *dto.ClosePosition, errs *[]er
|
||||
order := models.LinePreOrder{
|
||||
ExchangeType: global.EXCHANGE_BINANCE,
|
||||
Pid: list.Id,
|
||||
MainId: list.Id,
|
||||
ApiId: list.ApiId,
|
||||
GroupId: "0",
|
||||
Symbol: list.Symbol,
|
||||
@ -1049,6 +1069,7 @@ func (e *LinePreOrder) SpotClosePosition(position *dto.ClosePosition, errs *[]er
|
||||
ExpireTime: time.Now().Add(time.Hour * 24 * 30),
|
||||
MainOrderType: list.MainOrderType,
|
||||
Child: nil,
|
||||
OrderCategory: 1,
|
||||
}
|
||||
err := e.Orm.Model(&models.LinePreOrder{}).Create(&order).Error
|
||||
if err != nil {
|
||||
@ -1137,6 +1158,7 @@ func (e *LinePreOrder) FutClosePosition(position *dto.ClosePosition, errs *[]err
|
||||
order := models.LinePreOrder{
|
||||
Pid: parentId,
|
||||
ApiId: list.ApiId,
|
||||
MainId: list.Id,
|
||||
GroupId: "0",
|
||||
Symbol: list.Symbol,
|
||||
QuoteSymbol: list.QuoteSymbol,
|
||||
@ -1155,6 +1177,7 @@ func (e *LinePreOrder) FutClosePosition(position *dto.ClosePosition, errs *[]err
|
||||
ExpireTime: time.Now().Add(24 * 30 * time.Hour),
|
||||
MainOrderType: list.MainOrderType,
|
||||
Child: nil,
|
||||
OrderCategory: 1,
|
||||
}
|
||||
err = e.Orm.Model(&models.LinePreOrder{}).Create(&order).Error
|
||||
if err != nil {
|
||||
@ -1190,7 +1213,7 @@ func (e *LinePreOrder) FutClosePosition(position *dto.ClosePosition, errs *[]err
|
||||
// ClearUnTriggered 清除待触发的交易对
|
||||
func (e *LinePreOrder) ClearUnTriggered() error {
|
||||
var orderLists []models.LinePreOrder
|
||||
e.Orm.Model(&models.LinePreOrder{}).Where("pid = 0 AND status = '0'").Find(&orderLists).Unscoped().Delete(&models.LinePreOrder{})
|
||||
e.Orm.Model(&models.LinePreOrder{}).Where("main_id = 0 AND pid = 0 AND status = '0'").Find(&orderLists).Unscoped().Delete(&models.LinePreOrder{})
|
||||
|
||||
for _, order := range orderLists {
|
||||
redisList := dto.PreOrderRedisList{
|
||||
@ -1212,7 +1235,7 @@ func (e *LinePreOrder) ClearUnTriggered() error {
|
||||
key := fmt.Sprintf(rediskey.PreSpotOrderList, order.ExchangeType)
|
||||
helper.DefaultRedis.LRem(key, string(marshal))
|
||||
}
|
||||
e.Orm.Model(&models.LinePreOrder{}).Where("pid = ?", order.Id).Unscoped().Delete(&models.LinePreOrder{})
|
||||
e.Orm.Model(&models.LinePreOrder{}).Where("main_id = ?", order.Id).Unscoped().Delete(&models.LinePreOrder{})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@ -31,7 +31,6 @@ require (
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/prometheus/client_golang v1.17.0
|
||||
github.com/qiniu/go-sdk/v7 v7.18.2
|
||||
github.com/redis/go-redis/v9 v9.3.0
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/rs/xid v1.2.1
|
||||
github.com/shirou/gopsutil/v3 v3.23.10
|
||||
@ -143,6 +142,7 @@ require (
|
||||
github.com/prometheus/client_model v0.5.0 // indirect
|
||||
github.com/prometheus/common v0.45.0 // indirect
|
||||
github.com/prometheus/procfs v0.12.0 // indirect
|
||||
github.com/redis/go-redis/v9 v9.3.0 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/shamsher31/goimgext v1.0.0 // indirect
|
||||
github.com/shoenig/go-m1cpu v0.1.6 // indirect
|
||||
|
||||
Reference in New Issue
Block a user