code
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user