This commit is contained in:
daichao
2025-02-10 17:55:34 +08:00
parent 365bde5848
commit ea7a51dd76
4 changed files with 131 additions and 7 deletions

View File

@ -510,7 +510,7 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
// CheckRepeatOrder 检查重复下单 检查基础货币
func (e *LinePreOrder) CheckRepeatOrder(orderType int, apiUserId, site, baseCoin string) int64 {
var count int64
e.Orm.Model(&models.LinePreOrder{}).Where("api_id = ? AND symbol like ? AND order_type = ? AND site = ? AND `status` IN (1,5)", apiUserId, baseCoin+"%", orderType, site).Count(&count)
e.Orm.Model(&models.LinePreOrder{}).Where("api_id = ? AND symbol like ? AND order_type = ? AND site = ? AND `status` IN (1,5,6)", apiUserId, baseCoin+"%", orderType, site).Count(&count)
return count
}
@ -1098,9 +1098,9 @@ func (e *LinePreOrder) FutClosePosition(position *dto.ClosePosition, errs *[]err
//查询已经开仓的合约交易对
var futList []models.LinePreOrder
if position.Symbol == "" {
e.Orm.Model(&models.LinePreOrder{}).Where("api_id = ? AND status = 6 AND order_category=2 AND order_type =0", position.ApiId).Find(&futList)
e.Orm.Model(&models.LinePreOrder{}).Where("api_id = ? AND status = 6 AND order_type =0 AND main_id = 0", position.ApiId).Find(&futList)
} else {
e.Orm.Model(&models.LinePreOrder{}).Where("api_id = ? AND symbol = ? AND status = 6 AND order_category=2 AND order_type = 0", position.ApiId, position.Symbol).Find(&futList)
e.Orm.Model(&models.LinePreOrder{}).Where("api_id = ? AND symbol = ? AND status = 6 AND order_type = 0 AND main_id = 0", position.ApiId, position.Symbol).Find(&futList)
}
if len(futList) <= 0 {
*errs = append(*errs, fmt.Errorf("api_id:%d 没有可平仓的交易对", position.ApiId))

View File

@ -2,9 +2,15 @@ package jobs
import (
"errors"
"fmt"
"github.com/jinzhu/copier"
"go-admin/app/admin/models"
"go-admin/app/admin/service"
"go-admin/app/admin/service/dto"
"go-admin/common/global"
"go-admin/pkg/utility"
"go-admin/pkg/utility/snowflakehelper"
"go-admin/services/binanceservice"
"go-admin/services/fileservice"
"strconv"
"strings"
@ -28,6 +34,9 @@ type ClearLogJob struct {
type AutoPlaceOrder struct {
}
type CancelOrderByTime struct {
}
// 初始化合约交易对
func (t InitFuturesSymbol) Exec(arg interface{}) error {
str := time.Now().Format(timeFormat) + " [INFO] JobCore InitFuturesSymbol exec success"
@ -123,3 +132,90 @@ func (t AutoPlaceOrder) Exec(arg interface{}) error {
}
return nil
}
func (t CancelOrderByTime) Exec(arg interface{}) error {
str := time.Now().Format(timeFormat) + " [INFO] JobCore ClearLogJob exec success"
defer logger.Info(str)
var db *gorm.DB
for _, item := range sdk.Runtime.GetDb() {
db = item
break
}
var req = new(dto.SysConfigByKeyReq)
var resp = new(dto.GetSysConfigByKEYForServiceResp)
req.ConfigKey = "limitOrderTimeoutDuration"
serviceConfig := service.SysConfig{}
serviceConfig.Orm = db
serviceConfig.GetWithKey(req, resp)
if resp.ConfigValue == "" {
return nil
}
limitOrderTimeoutDuration := utility.StringAsInt64(resp.ConfigValue)
orders := make([]models.LinePreOrder, 0)
err := db.Model(&models.LinePreOrder{}).Where("status = '5' AND main_order_type = 'LIMIT' AND updated_at < ?", time.Now().Add(-time.Duration(limitOrderTimeoutDuration)*time.Second)).Find(&orders).Error
if err != nil {
return err
}
spotApi := binanceservice.SpotRestApi{}
for _, order := range orders {
//现货
if order.SymbolType == global.SYMBOL_SPOT {
var apiUserinfo models.LineApiUser
db.Model(&models.LineApiUser{}).Where("id = ?", order.ApiId).Find(&apiUserinfo)
if order.ExchangeType == global.EXCHANGE_BINANCE {
for i := 0; i < 3; i++ {
err := spotApi.CancelOpenOrderByOrderSn(apiUserinfo, order.Symbol, order.OrderSn)
if err == nil || strings.Contains(err.Error(), "该交易对没有订单") {
break
}
}
if err != nil {
logger.Error(fmt.Sprintf("取消现货委托失败:order_sn:%s err:%+v", order.OrderSn, err))
continue
} else {
newClientOrderId := snowflakehelper.GetOrderId()
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
if err != nil {
logger.Error(fmt.Sprintf("生成新市价单失败 err:%+v", err))
continue
}
err = spotApi.OrderPlace(db, binanceservice.OrderPlacementService{
ApiId: order.ApiId,
Symbol: order.Symbol,
Side: order.Site,
Type: "MARKET",
TimeInForce: "GTC",
Price: utility.StringToDecimal(order.Price),
Quantity: utility.StringToDecimal(order.Num),
NewClientOrderId: utility.Int64ToString(newClientOrderId),
})
if err != nil {
logger.Error(fmt.Sprintf("重新下市价单失败 err:%+v", err))
continue
}
}
}
}
//合约
if order.SymbolType == global.SYMBOL_FUTURES {
}
}
return nil
}

View File

@ -108,11 +108,11 @@ func TestFutOrderPalce(t *testing.T) {
func TestCancelOpenOrderByOrderSn(t *testing.T) {
api := SpotRestApi{}
dsn := "root:root@tcp(192.168.1.12:3306)/gp-bian?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
dsn := "root:root@tcp(192.168.1.12:3306)/go_exchange_single?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
var apiUserInfo models.LineApiUser
db.Model(&models.LineApiUser{}).Where("id = ?", 10).Find(&apiUserInfo)
err := api.CancelOpenOrderByOrderSn(apiUserInfo, "DOGEUSDT", "367836524202426368")
db.Model(&models.LineApiUser{}).Where("id = ?", 50).Find(&apiUserInfo)
err := api.CancelOpenOrderByOrderSn(apiUserInfo, "TRUMPUSDT", "1698362708")
if err != nil {
t.Log("err:", err)
} else {
@ -194,3 +194,24 @@ func TestClosePosition(t *testing.T) {
fmt.Println("err:", err)
}
func TestFutOrderPlace(t *testing.T) {
futureApi := FutRestApi{}
dsn := "root:root@tcp(192.168.1.12:3306)/go_exchange_single?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"
db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
err := futureApi.OrderPlace(db, FutOrderPlace{
ApiId: 50,
Symbol: "ADAUSDT",
Side: "SELL",
Quantity: decimal.NewFromFloat(8),
Price: decimal.NewFromFloat(0.697),
SideType: "LIMIT",
OpenOrder: 0,
Profit: decimal.NewFromFloat(0.698),
StopPrice: decimal.NewFromFloat(0.698),
OrderType: "STOP",
NewClientOrderId: "5465465784222554477",
})
fmt.Println("err:", err)
}

View File

@ -449,8 +449,15 @@ func (e FutRestApi) OrderPlace(orm *gorm.DB, params FutOrderPlace) error {
paramsMaps["workingType"] = "MARK_PRICE"
paramsMaps["timeInForce"] = "GTC"
}
if strings.ToUpper(params.OrderType) == "STOP" {
paramsMaps["price"] = params.Price.String()
paramsMaps["stopprice"] = params.StopPrice.String()
paramsMaps["workingType"] = "MARK_PRICE"
paramsMaps["timeInForce"] = "GTC"
}
}
if strings.ToUpper(params.OrderType) != "STOP_MARKET" && strings.ToUpper(params.OrderType) != "TAKE_PROFIT_MARKET" {
if strings.ToUpper(params.OrderType) != "STOP_MARKET" && strings.ToUpper(params.OrderType) != "TAKE_PROFIT_MARKET" && strings.ToUpper(params.OrderType) != "STOP" && strings.ToUpper(params.OrderType) != "TAKE_PROFIT" {
if strings.ToUpper(params.Side) == "BUY" {
paramsMaps["positionSide"] = "LONG"
} else {