1、交易对黑名单 交易所下架的交易对直接删除
This commit is contained in:
@ -306,34 +306,34 @@ func (e LinePreOrder) AddPreOrder(c *gin.Context) {
|
|||||||
e.OK(nil, "操作成功")
|
e.OK(nil, "操作成功")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 手动加仓
|
// // 手动加仓
|
||||||
func (e LinePreOrder) AddPosition(c *gin.Context) {
|
// func (e LinePreOrder) AddPosition(c *gin.Context) {
|
||||||
s := service.LinePreOrder{}
|
// s := service.LinePreOrder{}
|
||||||
req := dto.LinePreOrderAddPositionReq{}
|
// req := dto.LinePreOrderAddPositionReq{}
|
||||||
err := e.MakeContext(c).
|
// err := e.MakeContext(c).
|
||||||
MakeOrm().
|
// MakeOrm().
|
||||||
Bind(&req).
|
// Bind(&req).
|
||||||
MakeService(&s.Service).
|
// MakeService(&s.Service).
|
||||||
Errors
|
// Errors
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
e.Logger.Error(err)
|
// e.Logger.Error(err)
|
||||||
e.Error(500, err, err.Error())
|
// e.Error(500, err, err.Error())
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
if err := req.Valid(); err != nil {
|
// if err := req.Valid(); err != nil {
|
||||||
e.Error(500, err, err.Error())
|
// e.Error(500, err, err.Error())
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
err = s.AddPosition(&req)
|
// err = s.AddPosition(&req)
|
||||||
|
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
e.Error(500, nil, err.Error())
|
// e.Error(500, nil, err.Error())
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
e.OK(nil, "操作成功")
|
// e.OK(nil, "操作成功")
|
||||||
}
|
// }
|
||||||
|
|
||||||
// BatchAddOrder 批量添加
|
// BatchAddOrder 批量添加
|
||||||
func (e LinePreOrder) BatchAddOrder(c *gin.Context) {
|
func (e LinePreOrder) BatchAddOrder(c *gin.Context) {
|
||||||
@ -654,8 +654,7 @@ func (e LinePreOrder) CalculateBreakEevenRatio(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// data := dto.CalculateBreakEvenRatioResp{}
|
err = s.GenerateOrder(&req)
|
||||||
_, err = s.GenerateOrder(&req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e.Error(500, err, err.Error())
|
e.Error(500, err, err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
@ -192,3 +192,31 @@ func (e LineSymbolBlack) Delete(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
e.OK(req.GetId(), "删除成功")
|
e.OK(req.GetId(), "删除成功")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 重置交易对
|
||||||
|
func (e LineSymbolBlack) RelodSymbol(c *gin.Context) {
|
||||||
|
s := service.LineSymbolBlack{}
|
||||||
|
err := e.MakeContext(c).
|
||||||
|
MakeOrm().
|
||||||
|
MakeService(&s.Service).
|
||||||
|
Errors
|
||||||
|
if err != nil {
|
||||||
|
e.Logger.Error(err)
|
||||||
|
e.Error(500, err, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.ReloadSymbol("1")
|
||||||
|
if err != nil {
|
||||||
|
e.Error(500, err, fmt.Sprintf("重置现货交易对失败,\r\n失败信息 %s", err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.ReloadSymbol("2")
|
||||||
|
if err != nil {
|
||||||
|
e.Error(500, err, fmt.Sprintf("重置合约交易对失败,\r\n失败信息 %s", err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
e.OK(nil, "重置成功")
|
||||||
|
}
|
||||||
|
|||||||
@ -11,20 +11,19 @@ type LinePreOrderExt struct {
|
|||||||
|
|
||||||
MainOrderId int `json:"mainOrderId" gorm:"type:bigint;comment:主单id"`
|
MainOrderId int `json:"mainOrderId" gorm:"type:bigint;comment:主单id"`
|
||||||
OrderId int `json:"orderId" gorm:"type:bigint;comment:订单id"`
|
OrderId int `json:"orderId" gorm:"type:bigint;comment:订单id"`
|
||||||
TakeProfitRatio decimal.Decimal `json:"takeProfitRatio" gorm:"type:decimal(10,2);comment:止盈百分比"`
|
AddType int `json:"addType" gorm:"type:tinyint;comment:类型 1-加仓 2-减仓"`
|
||||||
ReTakeRatio decimal.Decimal `json:"reTakeRatio" gorm:"type:decimal(10,2);comment:亏损回本止盈百分比"`
|
OrderType string `json:"orderType" gorm:"type:varchar(20);comment:订单类型 LIMIT-限价 MARKET-市价"`
|
||||||
ReduceOrderType string `json:"reduceOrderType" gorm:"type:varchar(20);comment:减仓类型 LIMIT-限价 MARKET-市价"`
|
PriceRatio decimal.Decimal `json:"priceRatio" gorm:"type:decimal(10,2);comment: (加仓/减仓)触发价格百分比"`
|
||||||
ReducePriceRatio decimal.Decimal `json:"reducePriceRatio" gorm:"type:decimal(10,2);comment:减仓价格百分比"`
|
AddPositionType int `json:"addPositionType" gorm:"type:int;comment:(加仓/减仓)类型 1-百分比 2-实际金额"`
|
||||||
ReduceNumRatio decimal.Decimal `json:"reduceNumRatio" gorm:"type:decimal(10,2);comment:减仓数量百分比"`
|
|
||||||
ReduceTakeProfitRatio decimal.Decimal `json:"reduceTakeProfitRatio" gorm:"type:decimal(10,2);comment:减仓后止盈百分比"`
|
|
||||||
ReduceStopLossRatio decimal.Decimal `json:"reduceStopLossRatio" gorm:"type:decimal(10,2);comment:减仓后止损百分比"`
|
|
||||||
AddPositionOrderType string `json:"addPositionOrderType" gorm:"type:varchar(20);comment:加仓类型 LIMIT-限价 MARKET-市价"`
|
|
||||||
AddPositionPriceRatio decimal.Decimal `json:"addPositionPriceRatio" gorm:"type:decimal(10,2);comment:加仓价格百分比"`
|
|
||||||
AddPositionType int `json:"addPositionType" gorm:"type:int;comment:加仓类型 1-百分比 2-实际金额"`
|
|
||||||
AddPositionVal decimal.Decimal `json:"addPositionVal" gorm:"type:decimal(10,2);comment:加仓值"`
|
AddPositionVal decimal.Decimal `json:"addPositionVal" gorm:"type:decimal(10,2);comment:加仓值"`
|
||||||
ReduceReTakeRatio decimal.Decimal `json:"reduceReTakeRatio" gorm:"type:decimal(10,2);comment:减仓后亏损回本止盈百分比"`
|
TakeProfitRatio decimal.Decimal `json:"takeProfitRatio" gorm:"type:decimal(10,2);comment:止盈百分比"`
|
||||||
TotalAfterAdding decimal.Decimal `json:"totalAfterAdding" gorm:"-"` //加仓后总数
|
StopLossRatio decimal.Decimal `json:"stopLossRatio" gorm:"type:decimal(10,2);comment:止损百分比"`
|
||||||
TotalAfterReducing decimal.Decimal `json:"totalAfterReducing" gorm:"-"` //减仓后总数
|
TakeProfitNumRatio decimal.Decimal `json:"takeProfitNumRatio" gorm:"type:decimal(10,2);comment:止盈数量百分比"`
|
||||||
|
TpTpPriceRatio decimal.Decimal `json:"tpTpPriceRatio" gorm:"type:decimal(10,2);comment:止盈后止盈百分比"`
|
||||||
|
TpSlPriceRatio decimal.Decimal `json:"tpSlPriceRatio" gorm:"type:decimal(10,2);comment:止盈后止损百分比"`
|
||||||
|
ReTakeRatio decimal.Decimal `json:"reTakeRatio" gorm:"type:decimal(10,2);comment:亏损回本止盈百分比"`
|
||||||
|
TotalBefore decimal.Decimal `gorm:"-" comment:"初始总数"`
|
||||||
|
TotalAfter decimal.Decimal `gorm:"-" comment:"剩余总数"`
|
||||||
models.ModelTime
|
models.ModelTime
|
||||||
models.ControlBy
|
models.ControlBy
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,7 +26,7 @@ func registerLinePreOrderRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTM
|
|||||||
r.DELETE("", api.Delete)
|
r.DELETE("", api.Delete)
|
||||||
|
|
||||||
r.POST("addOrder", actions.PermissionAction(), api.AddPreOrder) //添加订单
|
r.POST("addOrder", actions.PermissionAction(), api.AddPreOrder) //添加订单
|
||||||
r.POST("position", actions.PermissionAction(), api.AddPosition) //手动加仓
|
// r.POST("position", actions.PermissionAction(), api.AddPosition) //手动加仓
|
||||||
r.POST("batchAddOrder", actions.PermissionAction(), api.BatchAddOrder) //批量添加订单
|
r.POST("batchAddOrder", actions.PermissionAction(), api.BatchAddOrder) //批量添加订单
|
||||||
r.POST("quickAddPreOrder", actions.PermissionAction(), api.QuickAddPreOrder) //快捷下单
|
r.POST("quickAddPreOrder", actions.PermissionAction(), api.QuickAddPreOrder) //快捷下单
|
||||||
r.POST("lever", actions.PermissionAction(), api.Lever) //设置杠杆
|
r.POST("lever", actions.PermissionAction(), api.Lever) //设置杠杆
|
||||||
|
|||||||
@ -5,8 +5,8 @@ import (
|
|||||||
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
||||||
|
|
||||||
"go-admin/app/admin/apis"
|
"go-admin/app/admin/apis"
|
||||||
"go-admin/common/middleware"
|
|
||||||
"go-admin/common/actions"
|
"go-admin/common/actions"
|
||||||
|
"go-admin/common/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -23,5 +23,7 @@ func registerLineSymbolBlackRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJ
|
|||||||
r.POST("", api.Insert)
|
r.POST("", api.Insert)
|
||||||
r.PUT("/:id", actions.PermissionAction(), api.Update)
|
r.PUT("/:id", actions.PermissionAction(), api.Update)
|
||||||
r.DELETE("", api.Delete)
|
r.DELETE("", api.Delete)
|
||||||
|
|
||||||
|
r.GET("reload-symbol", api.RelodSymbol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2,6 +2,7 @@ package dto
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"go-admin/app/admin/models"
|
"go-admin/app/admin/models"
|
||||||
@ -191,7 +192,9 @@ type LineAddPreOrderReq struct {
|
|||||||
PricePattern string `json:"price_pattern"` //价格模式
|
PricePattern string `json:"price_pattern"` //价格模式
|
||||||
Price string `json:"price" vd:"$>0"` //下单价百分比
|
Price string `json:"price" vd:"$>0"` //下单价百分比
|
||||||
Profit string `json:"profit" vd:"$>0"` //止盈价
|
Profit string `json:"profit" vd:"$>0"` //止盈价
|
||||||
// StopPrice string `json:"stop_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"` //止盈后止损价百分比
|
||||||
PriceType string `json:"price_type"` //价格类型
|
PriceType string `json:"price_type"` //价格类型
|
||||||
SaveTemplate string `json:"save_template"` //是否保存模板
|
SaveTemplate string `json:"save_template"` //是否保存模板
|
||||||
TemplateName string `json:"template_name"` //模板名字
|
TemplateName string `json:"template_name"` //模板名字
|
||||||
@ -275,23 +278,33 @@ func (req LineAddPreOrderReq) Valid() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range req.Ext {
|
for _, v := range req.Ext {
|
||||||
|
name := "加仓"
|
||||||
|
|
||||||
|
if v.AddType < 1 || v.AddType > 2 {
|
||||||
|
return errors.New("加、减仓单类型错误")
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.AddType == 2 {
|
||||||
|
name = "减仓"
|
||||||
|
}
|
||||||
|
|
||||||
if v.AddPositionVal.IsZero() {
|
if v.AddPositionVal.IsZero() {
|
||||||
return errors.New("加仓单数量不能为空")
|
return fmt.Errorf("%s单数量不能为空", name)
|
||||||
}
|
}
|
||||||
if v.AddPositionPriceRatio.IsZero() {
|
if v.PriceRatio.Cmp(decimal.Zero) <= 0 || v.PriceRatio.Cmp(decimal.NewFromInt(100)) >= 0 {
|
||||||
return errors.New("加仓单下跌价格不能为空")
|
return fmt.Errorf("%s单下跌价格不能为空", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.ReduceNumRatio.IsZero() || v.ReduceNumRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
if v.TakeProfitRatio.IsZero() || v.TakeProfitRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
||||||
return errors.New("减仓数量不正确")
|
return errors.New("止盈价格不正确")
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.ReducePriceRatio.IsZero() || v.ReducePriceRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
if v.TpTpPriceRatio.IsZero() || v.TpTpPriceRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
||||||
return errors.New("减仓下跌价格不正确")
|
return errors.New("止盈后止盈价格不正确")
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.ReduceTakeProfitRatio.IsZero() || v.ReduceTakeProfitRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
if v.TpSlPriceRatio.Cmp(decimal.Zero) <= 0 || v.TpSlPriceRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
||||||
return errors.New("减仓后止盈价格不正确")
|
return errors.New("止盈后止损价格不正确")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -392,23 +405,33 @@ func (req LineBatchAddPreOrderReq) CheckParams() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range req.Ext {
|
for _, v := range req.Ext {
|
||||||
|
name := "加仓"
|
||||||
|
|
||||||
|
if v.AddType < 1 || v.AddType > 2 {
|
||||||
|
return errors.New("加、减仓单类型错误")
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.AddType == 2 {
|
||||||
|
name = "减仓"
|
||||||
|
}
|
||||||
|
|
||||||
if v.AddPositionVal.IsZero() {
|
if v.AddPositionVal.IsZero() {
|
||||||
return errors.New("加仓单数量不能为空")
|
return fmt.Errorf("%s单数量不能为空", name)
|
||||||
}
|
}
|
||||||
if v.AddPositionPriceRatio.IsZero() {
|
if v.PriceRatio.Cmp(decimal.Zero) <= 0 || v.PriceRatio.Cmp(decimal.NewFromInt(100)) >= 0 {
|
||||||
return errors.New("加仓单下跌价格不能为空")
|
return fmt.Errorf("%s单下跌价格不能为空", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.ReduceNumRatio.IsZero() || v.ReduceNumRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
if v.TakeProfitRatio.IsZero() || v.TakeProfitRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
||||||
return errors.New("减仓数量不正确")
|
return errors.New("止盈价格不正确")
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.ReducePriceRatio.IsZero() || v.ReducePriceRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
if v.TpTpPriceRatio.IsZero() || v.TpTpPriceRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
||||||
return errors.New("减仓下跌价格不正确")
|
return errors.New("止盈后止盈价格不正确")
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.ReduceTakeProfitRatio.IsZero() || v.ReduceTakeProfitRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
if v.TpSlPriceRatio.Cmp(decimal.Zero) <= 0 || v.TpSlPriceRatio.Cmp(decimal.NewFromInt(100)) > 0 {
|
||||||
return errors.New("减仓后止盈价格不正确")
|
return errors.New("止盈后止损价格不正确")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -579,12 +602,12 @@ type CalculateBreakEevenRatioReq struct {
|
|||||||
Symbol string `form:"symbol"` //交易对
|
Symbol string `form:"symbol"` //交易对
|
||||||
ExchangeType string `form:"exchangeType"` //交易所类型 字典exchange_type
|
ExchangeType string `form:"exchangeType"` //交易所类型 字典exchange_type
|
||||||
SymbolType int `form:"symbolType"`
|
SymbolType int `form:"symbolType"`
|
||||||
|
AddType int `form:"addType" comment:"类型 1-加仓 2-减仓"`
|
||||||
BuyPrice decimal.Decimal `form:"buyPrice"` //主单购买总金额
|
BuyPrice decimal.Decimal `form:"buyPrice"` //主单购买总金额
|
||||||
LossBeginPercent decimal.Decimal `form:"lossBeginPercent"` //亏损开始百分比
|
LossBeginPercent decimal.Decimal `form:"lossBeginPercent"` //亏损开始百分比
|
||||||
LossEndPercent decimal.Decimal `form:"lossEndPercent"` //亏损截至百分比
|
LossEndPercent decimal.Decimal `form:"lossEndPercent"` //亏损截至百分比
|
||||||
AddPositionType int `form:"addPositionType"` //加仓类型 1-百分比 2-实际金额
|
AddPositionType int `form:"addPositionType"` //加仓/减仓 类型 1-百分比 2-实际金额
|
||||||
AddPositionVal decimal.Decimal `form:"addPositionVal"` //加仓金额
|
AddPositionVal decimal.Decimal `form:"addPositionVal"` //加仓/减仓 金额
|
||||||
ReducePercent decimal.Decimal `form:"reducePercent"` //减仓百分比
|
|
||||||
RemainingQuantity decimal.Decimal `form:"remainingQuantity"` //剩余数量
|
RemainingQuantity decimal.Decimal `form:"remainingQuantity"` //剩余数量
|
||||||
TotalLossAmountU decimal.Decimal `form:"totalLossAmountU"` //累计亏损金额
|
TotalLossAmountU decimal.Decimal `form:"totalLossAmountU"` //累计亏损金额
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,29 +37,30 @@ func (m *LinePreOrderExtGetPageReq) GetNeedSearch() interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LineAddPreOrderExtReq struct {
|
type LineAddPreOrderExtReq struct {
|
||||||
TakeProfitRatio decimal.Decimal `json:"takeProfitRatio" comment:"止盈百分比" `
|
AddType int `json:"addType" comment:"类型 1-加仓 2-减仓"`
|
||||||
ReTakeProfitRatio decimal.Decimal `json:"reTakeProfitRatio" comment:"亏损回本止盈百分比"`
|
OrderType string `json:"orderType" comment:"订单类型 LIMIT-限价 MARKET-市价"`
|
||||||
ReducePriceRatio decimal.Decimal `json:"reducePriceRatio" comment:"减仓价格百分比" `
|
PriceRatio decimal.Decimal `json:"priceRatio" comment:"价格百分比"`
|
||||||
ReduceNumRatio decimal.Decimal `json:"reduceNumRatio" comment:"减仓数量百分比" `
|
|
||||||
ReduceTakeProfitRatio decimal.Decimal `json:"reduceTakeProfitRatio" comment:"减仓后止盈百分比" `
|
|
||||||
ReduceStopLossRatio decimal.Decimal `json:"reduceStopLossRatio" comment:"减仓后止损百分比"`
|
|
||||||
ReduceReTakeProfitRatio decimal.Decimal `json:"reduceReTakeProfitRatio" comment:"减仓后回本止盈百分比"`
|
|
||||||
AddPositionPriceRatio decimal.Decimal `json:"addPositionPriceRatio" comment:"加仓价格百分比" `
|
|
||||||
AddPositionOrderType string `json:"addPositionOrderType" comment:"加仓订单类型 LIMIT-限价 MARKET-市价"`
|
|
||||||
AddPositionType int `json:"addPositionType" comment:"加仓类型 1-百分比 2-实际金额"`
|
AddPositionType int `json:"addPositionType" comment:"加仓类型 1-百分比 2-实际金额"`
|
||||||
AddPositionVal decimal.Decimal `json:"addPositionVal" comment:"加仓值"`
|
AddPositionVal decimal.Decimal `json:"addPositionVal" comment:"加仓值"`
|
||||||
|
TakeProfitRatio decimal.Decimal `json:"takeProfitRatio" comment:"止盈百分比"`
|
||||||
|
StopLossRatio decimal.Decimal `json:"stopLossRatio" comment:"止损百分比"`
|
||||||
|
TakeProfitNumRatio decimal.Decimal `json:"takeProfitNumRatio" comment:"止盈数量百分比"`
|
||||||
|
TpTpPriceRatio decimal.Decimal `json:"tpTpPriceRatio" comment:"止盈后止盈价格百分比"`
|
||||||
|
TpSlPriceRatio decimal.Decimal `json:"tpSlPriceRatio" comment:"止盈后止损价格百分比"`
|
||||||
|
ReTakeProfitRatio decimal.Decimal `json:"reTakeProfitRatio" comment:"亏损回本止盈百分比"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LinePreOrderExtInsertReq struct {
|
type LinePreOrderExtInsertReq struct {
|
||||||
Id int `json:"-" comment:"主键id"` // 主键id
|
Id int `json:"-" comment:"主键id"` // 主键id
|
||||||
MainOrderId int `json:"mainOrderId" comment:"主单id"`
|
MainOrderId int `json:"mainOrderId" comment:"主单id"`
|
||||||
OrderId int `json:"orderId" comment:"订单id"`
|
OrderId int `json:"orderId" comment:"订单id"`
|
||||||
|
AddType int `json:"addType" comment:"类型 1-加仓 2-减仓"`
|
||||||
|
OrderType string `json:"orderType" comment:"类型 LIMIT-限价 MARKET-市价"`
|
||||||
|
PriceRatio decimal.Decimal `json:"priceRatio" comment:"价格百分比"`
|
||||||
TakeProfitRatio decimal.Decimal `json:"takeProfitRatio" comment:"止盈百分比"`
|
TakeProfitRatio decimal.Decimal `json:"takeProfitRatio" comment:"止盈百分比"`
|
||||||
ReducePriceRatio decimal.Decimal `json:"reducePriceRatio" comment:"减仓价格百分比"`
|
TakeProfitNumRatio decimal.Decimal `json:"takeProfitNumRatio" comment:"止盈数量百分比"`
|
||||||
ReduceNumRatio decimal.Decimal `json:"reduceNumRatio" comment:"减仓数量百分比"`
|
TpTpPriceRatio decimal.Decimal `json:"tpTpPriceRatio" comment:"止盈后止盈价格百分比"`
|
||||||
ReduceTakeProfitRatio decimal.Decimal `json:"reduceTakeProfitRatio" comment:"减仓后止盈百分比"`
|
TpSlPriceRatio decimal.Decimal `json:"tpSlPriceRatio" comment:"止盈后止损价格百分比"`
|
||||||
ReduceStopLossRatio decimal.Decimal `json:"reduceStopLossRatio" comment:"减仓后止损百分比"`
|
|
||||||
AddPositionPriceRatio decimal.Decimal `json:"addPositionPriceRatio" comment:"加仓价格百分比"`
|
|
||||||
AddPositionType int `json:"addPositionType" comment:"加仓类型 1-百分比 2-实际金额"`
|
AddPositionType int `json:"addPositionType" comment:"加仓类型 1-百分比 2-实际金额"`
|
||||||
AddPositionVal decimal.Decimal `json:"addPositionVal" comment:"加仓值"`
|
AddPositionVal decimal.Decimal `json:"addPositionVal" comment:"加仓值"`
|
||||||
common.ControlBy
|
common.ControlBy
|
||||||
@ -72,11 +73,11 @@ func (s *LinePreOrderExtInsertReq) Generate(model *models.LinePreOrderExt) {
|
|||||||
model.MainOrderId = s.MainOrderId
|
model.MainOrderId = s.MainOrderId
|
||||||
model.OrderId = s.OrderId
|
model.OrderId = s.OrderId
|
||||||
model.TakeProfitRatio = s.TakeProfitRatio
|
model.TakeProfitRatio = s.TakeProfitRatio
|
||||||
model.ReducePriceRatio = s.ReducePriceRatio
|
model.AddType = s.AddType
|
||||||
model.ReduceNumRatio = s.ReduceNumRatio
|
model.OrderType = s.OrderType
|
||||||
model.ReduceTakeProfitRatio = s.ReduceTakeProfitRatio
|
model.PriceRatio = s.PriceRatio
|
||||||
model.ReduceStopLossRatio = s.ReduceStopLossRatio
|
model.TpSlPriceRatio = s.TpSlPriceRatio
|
||||||
model.AddPositionPriceRatio = s.AddPositionPriceRatio
|
model.TpSlPriceRatio = s.TpSlPriceRatio
|
||||||
model.AddPositionType = s.AddPositionType
|
model.AddPositionType = s.AddPositionType
|
||||||
model.AddPositionVal = s.AddPositionVal
|
model.AddPositionVal = s.AddPositionVal
|
||||||
model.CreateBy = s.CreateBy // 添加这而,需要记录是被谁创建的
|
model.CreateBy = s.CreateBy // 添加这而,需要记录是被谁创建的
|
||||||
@ -90,12 +91,13 @@ type LinePreOrderExtUpdateReq struct {
|
|||||||
Id int `uri:"id" comment:"主键id"` // 主键id
|
Id int `uri:"id" comment:"主键id"` // 主键id
|
||||||
MainOrderId int `json:"mainOrderId" comment:"主单id"`
|
MainOrderId int `json:"mainOrderId" comment:"主单id"`
|
||||||
OrderId int `json:"orderId" comment:"订单id"`
|
OrderId int `json:"orderId" comment:"订单id"`
|
||||||
|
AddType int `json:"addType" comment:"类型 1-加仓 2-减仓"`
|
||||||
|
OrderType string `json:"orderType" comment:"类型 LIMIT-限价 MARKET-市价"`
|
||||||
|
PriceRatio decimal.Decimal `json:"priceRatio" comment:"价格百分比"`
|
||||||
TakeProfitRatio decimal.Decimal `json:"takeProfitRatio" comment:"止盈百分比"`
|
TakeProfitRatio decimal.Decimal `json:"takeProfitRatio" comment:"止盈百分比"`
|
||||||
ReducePriceRatio decimal.Decimal `json:"reducePriceRatio" comment:"减仓价格百分比"`
|
TakeProfitNumRatio decimal.Decimal `json:"takeProfitNumRatio" comment:"止盈数量百分比"`
|
||||||
ReduceNumRatio decimal.Decimal `json:"reduceNumRatio" comment:"减仓数量百分比"`
|
TpTpPriceRatio decimal.Decimal `json:"tpTpPriceRatio" comment:"止盈后止盈价格百分比"`
|
||||||
ReduceTakeProfitRatio decimal.Decimal `json:"reduceTakeProfitRatio" comment:"减仓后止盈百分比"`
|
TpSlPriceRatio decimal.Decimal `json:"tpSlPriceRatio" comment:"止盈后止损价格百分比"`
|
||||||
ReduceStopLossRatio decimal.Decimal `json:"reduceStopLossRatio" comment:"减仓后止损百分比"`
|
|
||||||
AddPositionPriceRatio decimal.Decimal `json:"addPositionPriceRatio" comment:"加仓价格百分比"`
|
|
||||||
AddPositionType int `json:"addPositionType" comment:"加仓类型 1-百分比 2-实际金额"`
|
AddPositionType int `json:"addPositionType" comment:"加仓类型 1-百分比 2-实际金额"`
|
||||||
AddPositionVal decimal.Decimal `json:"addPositionVal" comment:"加仓值"`
|
AddPositionVal decimal.Decimal `json:"addPositionVal" comment:"加仓值"`
|
||||||
common.ControlBy
|
common.ControlBy
|
||||||
@ -108,11 +110,11 @@ func (s *LinePreOrderExtUpdateReq) Generate(model *models.LinePreOrderExt) {
|
|||||||
model.MainOrderId = s.MainOrderId
|
model.MainOrderId = s.MainOrderId
|
||||||
model.OrderId = s.OrderId
|
model.OrderId = s.OrderId
|
||||||
model.TakeProfitRatio = s.TakeProfitRatio
|
model.TakeProfitRatio = s.TakeProfitRatio
|
||||||
model.ReducePriceRatio = s.ReducePriceRatio
|
model.AddType = s.AddType
|
||||||
model.ReduceNumRatio = s.ReduceNumRatio
|
model.OrderType = s.OrderType
|
||||||
model.ReduceTakeProfitRatio = s.ReduceTakeProfitRatio
|
model.PriceRatio = s.PriceRatio
|
||||||
model.ReduceStopLossRatio = s.ReduceStopLossRatio
|
model.TpSlPriceRatio = s.TpSlPriceRatio
|
||||||
model.AddPositionPriceRatio = s.AddPositionPriceRatio
|
model.TpSlPriceRatio = s.TpSlPriceRatio
|
||||||
model.AddPositionType = s.AddPositionType
|
model.AddPositionType = s.AddPositionType
|
||||||
model.AddPositionVal = s.AddPositionVal
|
model.AddPositionVal = s.AddPositionVal
|
||||||
model.UpdateBy = s.UpdateBy // 添加这而,需要记录是被谁更新的
|
model.UpdateBy = s.UpdateBy // 添加这而,需要记录是被谁更新的
|
||||||
|
|||||||
@ -11,6 +11,7 @@ import (
|
|||||||
"go-admin/pkg/utility"
|
"go-admin/pkg/utility"
|
||||||
"go-admin/pkg/utility/snowflakehelper"
|
"go-admin/pkg/utility/snowflakehelper"
|
||||||
"go-admin/services/binanceservice"
|
"go-admin/services/binanceservice"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -338,6 +339,12 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
|
|||||||
if req.SaveTemplate == "2" {
|
if req.SaveTemplate == "2" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//重新排序下跌比例(顺序)
|
||||||
|
sort.Slice(req.Ext, func(i, j int) bool {
|
||||||
|
return req.Ext[i].PriceRatio.Cmp(req.Ext[j].PriceRatio) < 0
|
||||||
|
})
|
||||||
|
|
||||||
var key string
|
var key string
|
||||||
if req.SymbolType == global.SYMBOL_SPOT {
|
if req.SymbolType == global.SYMBOL_SPOT {
|
||||||
key = fmt.Sprintf(global.TICKER_SPOT, req.ExchangeType, req.Symbol)
|
key = fmt.Sprintf(global.TICKER_SPOT, req.ExchangeType, req.Symbol)
|
||||||
@ -356,11 +363,6 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
|
|||||||
|
|
||||||
//获取交易对
|
//获取交易对
|
||||||
tradeSet, _ := helper.GetObjString[models2.TradeSet](helper.DefaultRedis, key)
|
tradeSet, _ := helper.GetObjString[models2.TradeSet](helper.DefaultRedis, key)
|
||||||
// orderCount := e.CheckRepeatOrder(req.SymbolType, id, req.Site, tradeSet.Coin)
|
|
||||||
// if orderCount > 0 {
|
|
||||||
// *errs = append(*errs, fmt.Errorf("api_id:%s 获取交易对:%s 该交易对已存在,请勿重复下单", id, req.Symbol))
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
tickerPrice := utility.StrToDecimal(tradeSet.LastPrice)
|
tickerPrice := utility.StrToDecimal(tradeSet.LastPrice)
|
||||||
if tickerPrice.Equal(decimal.Zero) { //redis 没有这个值
|
if tickerPrice.Equal(decimal.Zero) { //redis 没有这个值
|
||||||
*errs = append(*errs, fmt.Errorf("api_id:%s 获取交易对:%s 交易行情出错", id, req.Symbol))
|
*errs = append(*errs, fmt.Errorf("api_id:%s 获取交易对:%s 交易行情出错", id, req.Symbol))
|
||||||
@ -454,15 +456,31 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
|
|||||||
//订单配置信息
|
//订单配置信息
|
||||||
preOrderExts := make([]models.LinePreOrderExt, 0)
|
preOrderExts := make([]models.LinePreOrderExt, 0)
|
||||||
defultExt := models.LinePreOrderExt{
|
defultExt := models.LinePreOrderExt{
|
||||||
|
AddType: 1, //主单等同于加仓0
|
||||||
|
AddPositionType: 1,
|
||||||
|
AddPositionVal: decimal.Zero,
|
||||||
|
OrderType: req.PriceType,
|
||||||
TakeProfitRatio: utility.StringToDecimal(req.Profit),
|
TakeProfitRatio: utility.StringToDecimal(req.Profit),
|
||||||
ReducePriceRatio: req.ReducePriceRatio,
|
TakeProfitNumRatio: req.ProfitNumRatio,
|
||||||
ReduceNumRatio: req.ReduceNumRatio,
|
TpTpPriceRatio: req.ProfitTpTpPriceRatio,
|
||||||
ReduceTakeProfitRatio: req.ReduceTakeProfitRatio,
|
TpSlPriceRatio: req.ProfitTpSlPriceRatio,
|
||||||
ReduceStopLossRatio: req.ReduceStopLossRatio,
|
}
|
||||||
|
//减仓单
|
||||||
|
defultExt2 := models.LinePreOrderExt{
|
||||||
|
AddType: 2,
|
||||||
|
OrderType: "LIMIT",
|
||||||
|
PriceRatio: req.ReducePriceRatio,
|
||||||
|
AddPositionType: 1,
|
||||||
|
AddPositionVal: req.ReduceNumRatio,
|
||||||
|
TakeProfitRatio: req.ReduceTakeProfitRatio,
|
||||||
|
TakeProfitNumRatio: decimal.NewFromInt(100), //减仓止盈默认100%
|
||||||
|
StopLossRatio: req.ReduceStopLossRatio,
|
||||||
}
|
}
|
||||||
mainPrice := utility.StringToDecimal(AddOrder.Price)
|
mainPrice := utility.StringToDecimal(AddOrder.Price)
|
||||||
mainAmount := buyPrice.Div(mainPrice)
|
mainAmount := buyPrice.Div(mainPrice)
|
||||||
defultExt.TotalAfterReducing = mainAmount.Mul(decimal.NewFromInt(100).Sub(req.ReduceNumRatio)).Div(decimal.NewFromInt(100)).Truncate(int32(tradeSet.AmountDigit))
|
defultExt.TotalAfter = utility.StrToDecimal(AddOrder.Num).Truncate(int32(tradeSet.AmountDigit))
|
||||||
|
defultExt2.TotalBefore = defultExt.TotalAfter
|
||||||
|
defultExt2.TotalAfter = mainAmount.Mul(decimal.NewFromInt(100).Sub(req.ReduceNumRatio)).Div(decimal.NewFromInt(100)).Truncate(int32(tradeSet.AmountDigit))
|
||||||
preOrderExts = append(preOrderExts, defultExt)
|
preOrderExts = append(preOrderExts, defultExt)
|
||||||
|
|
||||||
calculateResp := dto.CalculateBreakEvenRatioResp{}
|
calculateResp := dto.CalculateBreakEvenRatioResp{}
|
||||||
@ -476,56 +494,42 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
|
|||||||
LossEndPercent: req.ReducePriceRatio,
|
LossEndPercent: req.ReducePriceRatio,
|
||||||
AddPositionType: 2,
|
AddPositionType: 2,
|
||||||
AddPositionVal: decimal.Zero,
|
AddPositionVal: decimal.Zero,
|
||||||
ReducePercent: req.ReduceNumRatio,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//计算减仓后
|
//计算减仓后
|
||||||
mainParam.LossEndPercent = req.ReducePriceRatio
|
mainParam.LossEndPercent = req.ReducePriceRatio
|
||||||
mainParam.RemainingQuantity = mainAmount
|
mainParam.RemainingQuantity = mainAmount
|
||||||
e.CalculateBreakEvenRatio(&mainParam, &calculateResp)
|
e.CalculateBreakEvenRatio(&mainParam, &calculateResp, tradeSet)
|
||||||
mainParam.RemainingQuantity = calculateResp.RemainingQuantity //mainAmount.Mul(decimal.NewFromInt(100).Sub(req.ReduceNumRatio).Div(decimal.NewFromInt(100))).Truncate(int32(tradeSet.AmountDigit))
|
mainParam.RemainingQuantity = calculateResp.RemainingQuantity //mainAmount.Mul(decimal.NewFromInt(100).Sub(req.ReduceNumRatio).Div(decimal.NewFromInt(100))).Truncate(int32(tradeSet.AmountDigit))
|
||||||
mainParam.TotalLossAmountU = calculateResp.TotalLossAmountU //buyPrice.Mul(req.ReducePriceRatio.Div(decimal.NewFromInt(100)).Truncate(4)).Truncate(int32(tradeSet.PriceDigit))
|
mainParam.TotalLossAmountU = calculateResp.TotalLossAmountU //buyPrice.Mul(req.ReducePriceRatio.Div(decimal.NewFromInt(100)).Truncate(4)).Truncate(int32(tradeSet.PriceDigit))
|
||||||
req.ReduceReTakeProfitRatio = calculateResp.Ratio
|
req.ReduceReTakeProfitRatio = calculateResp.Ratio
|
||||||
mainParam.LossBeginPercent = req.ReducePriceRatio
|
mainParam.LossBeginPercent = req.ReducePriceRatio
|
||||||
defultExt.ReduceReTakeRatio = calculateResp.Ratio
|
defultExt.ReTakeRatio = calculateResp.Ratio
|
||||||
|
|
||||||
for index, addPosition := range req.Ext {
|
for index, addPosition := range req.Ext {
|
||||||
ext := models.LinePreOrderExt{
|
ext := models.LinePreOrderExt{
|
||||||
|
AddType: addPosition.AddType,
|
||||||
|
OrderType: addPosition.OrderType,
|
||||||
TakeProfitRatio: addPosition.TakeProfitRatio,
|
TakeProfitRatio: addPosition.TakeProfitRatio,
|
||||||
ReducePriceRatio: addPosition.ReducePriceRatio,
|
TakeProfitNumRatio: addPosition.TakeProfitNumRatio,
|
||||||
ReduceNumRatio: addPosition.ReduceNumRatio,
|
TpTpPriceRatio: addPosition.TpTpPriceRatio,
|
||||||
ReduceTakeProfitRatio: addPosition.ReduceTakeProfitRatio,
|
TpSlPriceRatio: addPosition.TpSlPriceRatio,
|
||||||
ReduceStopLossRatio: addPosition.ReduceStopLossRatio,
|
|
||||||
AddPositionPriceRatio: addPosition.AddPositionPriceRatio,
|
|
||||||
AddPositionOrderType: addPosition.AddPositionOrderType,
|
|
||||||
AddPositionType: addPosition.AddPositionType,
|
AddPositionType: addPosition.AddPositionType,
|
||||||
AddPositionVal: addPosition.AddPositionVal,
|
AddPositionVal: addPosition.AddPositionVal,
|
||||||
}
|
}
|
||||||
|
|
||||||
mainParam.LossEndPercent = req.Ext[index].AddPositionPriceRatio
|
mainParam.LossEndPercent = req.Ext[index].PriceRatio
|
||||||
mainParam.AddPositionType = req.Ext[index].AddPositionType
|
mainParam.AddPositionType = req.Ext[index].AddPositionType
|
||||||
mainParam.AddPositionVal = req.Ext[index].AddPositionVal
|
mainParam.AddPositionVal = req.Ext[index].AddPositionVal
|
||||||
mainParam.ReducePercent = decimal.Zero
|
|
||||||
e.CalculateBreakEvenRatio(&mainParam, &calculateResp)
|
|
||||||
|
|
||||||
ext.TotalAfterAdding = calculateResp.RemainingQuantity
|
e.CalculateBreakEvenRatio(&mainParam, &calculateResp, tradeSet)
|
||||||
req.Ext[index].ReTakeProfitRatio = calculateResp.Ratio
|
|
||||||
mainParam.LossBeginPercent = req.Ext[index].AddPositionPriceRatio
|
ext.TotalBefore = mainParam.RemainingQuantity //初始数量
|
||||||
|
ext.TotalAfter = calculateResp.RemainingQuantity //计算后数量
|
||||||
|
ext.ReTakeRatio = calculateResp.Ratio
|
||||||
|
mainParam.LossBeginPercent = addPosition.PriceRatio
|
||||||
mainParam.RemainingQuantity = calculateResp.RemainingQuantity
|
mainParam.RemainingQuantity = calculateResp.RemainingQuantity
|
||||||
mainParam.TotalLossAmountU = calculateResp.TotalLossAmountU
|
mainParam.TotalLossAmountU = calculateResp.TotalLossAmountU
|
||||||
mainParam.LossEndPercent = req.Ext[index].ReducePriceRatio
|
|
||||||
mainParam.AddPositionVal = decimal.Zero
|
|
||||||
mainParam.ReducePercent = req.Ext[index].ReduceNumRatio
|
|
||||||
e.CalculateBreakEvenRatio(&mainParam, &calculateResp)
|
|
||||||
|
|
||||||
req.Ext[index].ReduceReTakeProfitRatio = calculateResp.Ratio
|
|
||||||
mainParam.LossBeginPercent = req.Ext[index].ReducePriceRatio
|
|
||||||
mainParam.RemainingQuantity = calculateResp.RemainingQuantity
|
|
||||||
mainParam.TotalLossAmountU = calculateResp.TotalLossAmountU
|
|
||||||
|
|
||||||
ext.TotalAfterReducing = calculateResp.RemainingQuantity
|
|
||||||
ext.ReTakeRatio = req.Ext[index].ReTakeProfitRatio
|
|
||||||
ext.ReduceReTakeRatio = req.Ext[index].ReduceReTakeProfitRatio
|
|
||||||
preOrderExts = append(preOrderExts, ext)
|
preOrderExts = append(preOrderExts, ext)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -576,7 +580,26 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
|
|||||||
profitOrder.Rate = req.Profit
|
profitOrder.Rate = req.Profit
|
||||||
profitOrder.MainId = AddOrder.Id
|
profitOrder.MainId = AddOrder.Id
|
||||||
|
|
||||||
|
if req.ProfitNumRatio.Cmp(decimal.Zero) > 0 {
|
||||||
|
numPercent := req.ProfitNumRatio.Div(decimal.NewFromInt(100))
|
||||||
|
profitOrder.Num = utility.StrToDecimal(profitOrder.Num).Mul(numPercent).Truncate(int32(tradeSet.AmountDigit)).String()
|
||||||
|
|
||||||
|
}
|
||||||
tx.Model(&models.LinePreOrder{}).Omit("id", "save_template", "template_name").Create(&profitOrder)
|
tx.Model(&models.LinePreOrder{}).Omit("id", "save_template", "template_name").Create(&profitOrder)
|
||||||
|
|
||||||
|
//不全部止盈的时候
|
||||||
|
if req.ProfitNumRatio.Cmp(decimal.Zero) > 0 && req.ProfitNumRatio.Cmp(decimal.NewFromInt(100)) < 0 {
|
||||||
|
reminQuantity := utility.StrToDecimal(AddOrder.Num).Sub(utility.StrToDecimal(profitOrder.Num))
|
||||||
|
|
||||||
|
childrens, err := makeTpOrder(&profitOrder, reminQuantity, req.ProfitTpTpPriceRatio, req.ProfitTpSlPriceRatio, &tradeSet)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("生成止盈后子订单失败")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.Create(&childrens)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.ReducePriceRatio.Cmp(decimal.Zero) > 0 {
|
if req.ReducePriceRatio.Cmp(decimal.Zero) > 0 {
|
||||||
@ -619,39 +642,44 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
|
|||||||
preOrderExts[index].OrderId = AddOrder.Id
|
preOrderExts[index].OrderId = AddOrder.Id
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
var AddOrder models.LinePreOrder
|
||||||
|
|
||||||
addPosition := createPreAddPosition(&AddOrder, v, tradeSet)
|
if v.AddType == 1 {
|
||||||
|
AddOrder = createPreAddPosition(&AddOrder, v, tradeSet)
|
||||||
|
} else if v.AddType == 2 {
|
||||||
|
AddOrder = createPreReduceOrder(&AddOrder, v, tradeSet)
|
||||||
|
}
|
||||||
|
|
||||||
if addPosition.OrderSn == "" {
|
if AddOrder.OrderSn == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := e.Orm.Create(&addPosition).Error; err != nil {
|
if err := e.Orm.Create(&AddOrder).Error; err != nil {
|
||||||
logger.Error("保存加仓单失败")
|
logger.Error("保存加仓单失败")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
preOrderExts[index].OrderId = addPosition.Id
|
preOrderExts[index].OrderId = AddOrder.Id
|
||||||
//止盈、减仓
|
//止盈、减仓
|
||||||
orders, err := makeFuturesTakeAndReduce(&addPosition, v, tradeSet)
|
orders, err := makeFuturesTakeAndReduce(&AddOrder, v, tradeSet)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("构造加仓单止盈、减仓失败")
|
logger.Error("构造止盈、止损失败")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := e.Orm.Create(&orders).Error; err != nil {
|
if err := e.Orm.Create(&orders).Error; err != nil {
|
||||||
logger.Error("保存加仓单止盈、减仓失败")
|
logger.Error("保存止盈、止损失败")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for index := range orders {
|
for index := range orders {
|
||||||
//减仓单且 减仓比例大于0 小于100 就冲下止盈止损
|
//减仓单且 减仓比例大于0 小于100 就冲下止盈止损
|
||||||
if orders[index].OrderType == 4 && v.ReduceNumRatio.Cmp(decimal.Zero) > 0 && v.ReduceNumRatio.Cmp(decimal.NewFromInt(100)) < 0 {
|
if orders[index].OrderType == 1 && v.TakeProfitRatio.Cmp(decimal.Zero) > 0 && v.TakeProfitRatio.Cmp(decimal.NewFromInt(100)) < 0 {
|
||||||
reduceChildOrders, err := makeReduceTakeAndStoploss(&(orders[index]), v, tradeSet)
|
reduceChildOrders, err := makeReduceTakeAndStoploss(&(orders[index]), v, tradeSet)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("生产加仓单止盈、减仓失败")
|
logger.Error("生产止盈后止盈、减仓失败")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -660,7 +688,7 @@ func (e *LinePreOrder) AddPreOrder(req *dto.LineAddPreOrderReq, p *actions.DataP
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := e.Orm.Create(&reduceChildOrders).Error; err != nil {
|
if err := e.Orm.Create(&reduceChildOrders).Error; err != nil {
|
||||||
logger.Error("报错减仓后止盈止损失败")
|
logger.Error("报错止盈后止盈止损失败")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -695,16 +723,16 @@ func createPreAddPosition(preOrder *models.LinePreOrder, v models.LinePreOrderEx
|
|||||||
data.OrderSn = utility.Int64ToString(snowflakehelper.GetOrderId())
|
data.OrderSn = utility.Int64ToString(snowflakehelper.GetOrderId())
|
||||||
data.MainId = preOrder.Id
|
data.MainId = preOrder.Id
|
||||||
data.CreatedAt = time.Now()
|
data.CreatedAt = time.Now()
|
||||||
data.MainOrderType = v.AddPositionOrderType
|
data.MainOrderType = v.OrderType
|
||||||
data.Status = 0
|
data.Status = 0
|
||||||
data.OrderCategory = 3
|
data.OrderCategory = 3
|
||||||
data.Rate = v.AddPositionPriceRatio.String()
|
data.Rate = v.PriceRatio.String()
|
||||||
var percentage decimal.Decimal
|
var percentage decimal.Decimal
|
||||||
|
|
||||||
if data.Site == "BUY" {
|
if data.Site == "BUY" {
|
||||||
percentage = decimal.NewFromInt(1).Sub(v.AddPositionPriceRatio.Div(decimal.NewFromInt(100)))
|
percentage = decimal.NewFromInt(1).Sub(v.PriceRatio.Div(decimal.NewFromInt(100)))
|
||||||
} else {
|
} else {
|
||||||
percentage = decimal.NewFromInt(1).Add(v.AddPositionPriceRatio.Div(decimal.NewFromInt(100)))
|
percentage = decimal.NewFromInt(1).Add(v.PriceRatio.Div(decimal.NewFromInt(100)))
|
||||||
}
|
}
|
||||||
|
|
||||||
dataPrice := price.Mul(percentage).Truncate(int32(tradeSet.PriceDigit))
|
dataPrice := price.Mul(percentage).Truncate(int32(tradeSet.PriceDigit))
|
||||||
@ -722,10 +750,48 @@ func createPreAddPosition(preOrder *models.LinePreOrder, v models.LinePreOrderEx
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建合约止盈、减仓单
|
// 生成减仓单
|
||||||
|
func createPreReduceOrder(preOrder *models.LinePreOrder, ext models.LinePreOrderExt, tradeSet models2.TradeSet) models.LinePreOrder {
|
||||||
|
var stopOrder models.LinePreOrder
|
||||||
|
|
||||||
|
//减仓单
|
||||||
|
if ext.PriceRatio.Cmp(decimal.Zero) > 0 {
|
||||||
|
copier.Copy(&stopOrder, preOrder)
|
||||||
|
|
||||||
|
stopOrder.Id = 0
|
||||||
|
stopOrder.OrderSn = strconv.FormatInt(snowflakehelper.GetOrderId(), 10)
|
||||||
|
stopOrder.Pid = preOrder.Id
|
||||||
|
stopOrder.MainId = preOrder.MainId
|
||||||
|
stopOrder.OrderType = 4
|
||||||
|
stopOrder.Status = 0
|
||||||
|
stopOrder.Rate = ext.PriceRatio.String()
|
||||||
|
stopOrder.Num = ext.TotalAfter.Sub(ext.TotalBefore).Abs().Truncate(int32(tradeSet.AmountDigit)).String()
|
||||||
|
stopOrder.BuyPrice = "0"
|
||||||
|
|
||||||
|
if strings.ToUpper(preOrder.Site) == "BUY" {
|
||||||
|
stopOrder.Site = "SELL"
|
||||||
|
} else {
|
||||||
|
stopOrder.Site = "BUY"
|
||||||
|
}
|
||||||
|
|
||||||
|
binanceservice.SetPrice(&stopOrder, preOrder, tradeSet)
|
||||||
|
}
|
||||||
|
|
||||||
|
return stopOrder
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建止盈、止盈止损
|
||||||
func makeFuturesTakeAndReduce(preOrder *models.LinePreOrder, ext models.LinePreOrderExt, tradeSet models2.TradeSet) ([]models.LinePreOrder, error) {
|
func makeFuturesTakeAndReduce(preOrder *models.LinePreOrder, ext models.LinePreOrderExt, tradeSet models2.TradeSet) ([]models.LinePreOrder, error) {
|
||||||
num := ext.TotalAfterAdding.Truncate(int32(tradeSet.AmountDigit))
|
|
||||||
orders := make([]models.LinePreOrder, 0)
|
orders := make([]models.LinePreOrder, 0)
|
||||||
|
var side string
|
||||||
|
|
||||||
|
if strings.ToUpper(preOrder.Site) == "BUY" {
|
||||||
|
side = "SELL"
|
||||||
|
} else {
|
||||||
|
side = "BUY"
|
||||||
|
}
|
||||||
|
|
||||||
|
if ext.TakeProfitRatio.Cmp(decimal.Zero) > 0 {
|
||||||
// 止盈单
|
// 止盈单
|
||||||
profitOrder := models.LinePreOrder{}
|
profitOrder := models.LinePreOrder{}
|
||||||
copier.Copy(&profitOrder, preOrder)
|
copier.Copy(&profitOrder, preOrder)
|
||||||
@ -736,57 +802,91 @@ func makeFuturesTakeAndReduce(preOrder *models.LinePreOrder, ext models.LinePreO
|
|||||||
profitOrder.OrderType = 1
|
profitOrder.OrderType = 1
|
||||||
profitOrder.Status = 0
|
profitOrder.Status = 0
|
||||||
profitOrder.MainId = preOrder.MainId
|
profitOrder.MainId = preOrder.MainId
|
||||||
profitOrder.Num = num.String()
|
|
||||||
profitOrder.BuyPrice = "0"
|
profitOrder.BuyPrice = "0"
|
||||||
// profitOrder.Rate = ext.TakeProfitRatio.String()
|
profitOrder.Site = side
|
||||||
|
|
||||||
|
if ext.TakeProfitNumRatio.Cmp(decimal.Zero) <= 0 || ext.TakeProfitNumRatio.Cmp(decimal.NewFromInt(100)) >= 0 {
|
||||||
|
profitOrder.Num = ext.TotalAfter.Truncate(int32(tradeSet.AmountDigit)).String()
|
||||||
|
} else {
|
||||||
|
profitOrder.Num = ext.TotalAfter.Mul(ext.TakeProfitNumRatio).Div(decimal.NewFromInt(100)).Truncate(int32(tradeSet.AmountDigit)).String()
|
||||||
|
}
|
||||||
|
|
||||||
// 止盈需要累加之前的亏损
|
// 止盈需要累加之前的亏损
|
||||||
profitOrder.Rate = ext.TakeProfitRatio.Add(ext.ReTakeRatio).Truncate(2).String()
|
profitOrder.Rate = ext.TakeProfitRatio.Add(ext.ReTakeRatio).Truncate(2).String()
|
||||||
|
|
||||||
if strings.ToUpper(preOrder.Site) == "BUY" {
|
|
||||||
profitOrder.Site = "SELL"
|
|
||||||
} else {
|
|
||||||
profitOrder.Site = "BUY"
|
|
||||||
}
|
|
||||||
|
|
||||||
binanceservice.SetPrice(&profitOrder, preOrder, tradeSet)
|
binanceservice.SetPrice(&profitOrder, preOrder, tradeSet)
|
||||||
orders = append(orders, profitOrder)
|
orders = append(orders, profitOrder)
|
||||||
|
|
||||||
//减仓单
|
|
||||||
if ext.ReducePriceRatio.Cmp(decimal.Zero) > 0 {
|
|
||||||
stopOrder := models.LinePreOrder{}
|
|
||||||
copier.Copy(&stopOrder, preOrder)
|
|
||||||
|
|
||||||
stopOrder.Id = 0
|
|
||||||
stopOrder.OrderSn = strconv.FormatInt(snowflakehelper.GetOrderId(), 10)
|
|
||||||
stopOrder.Pid = preOrder.Id
|
|
||||||
stopOrder.MainId = preOrder.MainId
|
|
||||||
stopOrder.OrderType = 4
|
|
||||||
stopOrder.Status = 0
|
|
||||||
stopOrder.Rate = ext.ReducePriceRatio.String()
|
|
||||||
stopOrder.Num = num.String()
|
|
||||||
stopOrder.BuyPrice = "0"
|
|
||||||
|
|
||||||
if ext.ReduceNumRatio.Cmp(decimal.Zero) > 0 {
|
|
||||||
stopNum := num.Mul(ext.ReduceNumRatio.Div(decimal.NewFromInt(100)))
|
|
||||||
stopOrder.Num = stopNum.Truncate(int32(tradeSet.AmountDigit)).String()
|
|
||||||
}
|
|
||||||
if strings.ToUpper(preOrder.Site) == "BUY" {
|
|
||||||
stopOrder.Site = "SELL"
|
|
||||||
} else {
|
|
||||||
stopOrder.Site = "BUY"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
binanceservice.SetPrice(&stopOrder, preOrder, tradeSet)
|
if ext.StopLossRatio.Cmp(decimal.Zero) > 0 {
|
||||||
orders = append(orders, stopOrder)
|
lossOrder := models.LinePreOrder{}
|
||||||
|
copier.Copy(&lossOrder, preOrder)
|
||||||
|
|
||||||
|
lossOrder.Id = 0
|
||||||
|
lossOrder.OrderSn = strconv.FormatInt(snowflakehelper.GetOrderId(), 10)
|
||||||
|
lossOrder.Pid = preOrder.Id
|
||||||
|
lossOrder.OrderType = 2
|
||||||
|
lossOrder.Status = 0
|
||||||
|
lossOrder.MainId = preOrder.MainId
|
||||||
|
lossOrder.BuyPrice = "0"
|
||||||
|
lossOrder.Num = ext.TotalAfter.Truncate(int32(tradeSet.AmountDigit)).String()
|
||||||
|
lossOrder.Rate = ext.StopLossRatio.Truncate(2).String()
|
||||||
|
lossOrder.Site = side
|
||||||
|
|
||||||
|
binanceservice.SetPrice(&lossOrder, preOrder, tradeSet)
|
||||||
|
orders = append(orders, lossOrder)
|
||||||
}
|
}
|
||||||
|
|
||||||
return orders, nil
|
return orders, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 构建止盈后止盈止损
|
||||||
|
// parentOrder 父订单
|
||||||
|
// remainQuantity 剩余数量
|
||||||
|
// tpPriceRatio 止盈价格比例
|
||||||
|
// slPriceRatio 止损价格比例
|
||||||
|
func makeTpOrder(parentOrder *models.LinePreOrder, reminQuantity decimal.Decimal, tpPriceRatio, slPriceRatio decimal.Decimal, tradeSet *models2.TradeSet) ([]models.LinePreOrder, error) {
|
||||||
|
result := make([]models.LinePreOrder, 0)
|
||||||
|
tp := models.LinePreOrder{}
|
||||||
|
sl := models.LinePreOrder{}
|
||||||
|
copier.Copy(&tp, parentOrder)
|
||||||
|
|
||||||
|
tp.Id = 0
|
||||||
|
tp.Pid = parentOrder.Id
|
||||||
|
tp.OrderSn = utility.Int64ToString(snowflakehelper.GetOrderId())
|
||||||
|
tp.OrderType = 1
|
||||||
|
tp.Status = 0
|
||||||
|
tp.Rate = tpPriceRatio.String()
|
||||||
|
tp.Num = reminQuantity.String()
|
||||||
|
binanceservice.SetPrice(&tp, parentOrder, *tradeSet)
|
||||||
|
result = append(result, tp)
|
||||||
|
|
||||||
|
if (slPriceRatio).Cmp(decimal.Zero) > 0 {
|
||||||
|
copier.Copy(&sl, parentOrder)
|
||||||
|
sl.Pid = parentOrder.Id
|
||||||
|
sl.Id = 0
|
||||||
|
sl.OrderSn = utility.Int64ToString(snowflakehelper.GetOrderId())
|
||||||
|
sl.OrderType = 2
|
||||||
|
sl.Num = reminQuantity.Truncate(int32(tradeSet.AmountDigit)).String()
|
||||||
|
sl.Rate = slPriceRatio.String()
|
||||||
|
binanceservice.SetPrice(&sl, parentOrder, *tradeSet)
|
||||||
|
result = append(result, sl)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// 构建减仓后止盈止损
|
// 构建减仓后止盈止损
|
||||||
func makeReduceTakeAndStoploss(parentOrder *models.LinePreOrder, ext models.LinePreOrderExt, tradeSet models2.TradeSet) ([]models.LinePreOrder, error) {
|
func makeReduceTakeAndStoploss(parentOrder *models.LinePreOrder, ext models.LinePreOrderExt, tradeSet models2.TradeSet) ([]models.LinePreOrder, error) {
|
||||||
orders := make([]models.LinePreOrder, 0)
|
orders := make([]models.LinePreOrder, 0)
|
||||||
|
var num decimal.Decimal
|
||||||
|
|
||||||
|
if ext.TakeProfitNumRatio.Cmp(decimal.Zero) > 0 && ext.TakeProfitNumRatio.Cmp(decimal.NewFromInt(100)) < 0 {
|
||||||
|
percent := decimal.NewFromInt(1).Sub(ext.TakeProfitNumRatio.Div(decimal.NewFromInt(100)))
|
||||||
|
num = ext.TotalAfter.Mul(percent).Truncate(int32(tradeSet.AmountDigit))
|
||||||
|
}
|
||||||
|
|
||||||
|
if ext.TpTpPriceRatio.Cmp(decimal.Zero) > 0 && num.Cmp(decimal.Zero) > 0 {
|
||||||
takeProfitOrder := models.LinePreOrder{}
|
takeProfitOrder := models.LinePreOrder{}
|
||||||
copier.Copy(&takeProfitOrder, parentOrder)
|
copier.Copy(&takeProfitOrder, parentOrder)
|
||||||
takeProfitOrder.Id = 0
|
takeProfitOrder.Id = 0
|
||||||
@ -794,22 +894,21 @@ func makeReduceTakeAndStoploss(parentOrder *models.LinePreOrder, ext models.Line
|
|||||||
takeProfitOrder.OrderSn = utility.Int64ToString(snowflakehelper.GetOrderId())
|
takeProfitOrder.OrderSn = utility.Int64ToString(snowflakehelper.GetOrderId())
|
||||||
takeProfitOrder.Status = 0
|
takeProfitOrder.Status = 0
|
||||||
takeProfitOrder.OrderType = 1
|
takeProfitOrder.OrderType = 1
|
||||||
takeProfitOrder.Rate = ext.ReduceTakeProfitRatio.String()
|
takeProfitOrder.Rate = ext.TpTpPriceRatio.String()
|
||||||
takeProfitOrder.SignPrice = parentOrder.Price
|
takeProfitOrder.SignPrice = parentOrder.Price
|
||||||
takeProfitOrder.CreatedAt = time.Now()
|
takeProfitOrder.CreatedAt = time.Now()
|
||||||
takeProfitOrder.BuyPrice = "0"
|
takeProfitOrder.BuyPrice = "0"
|
||||||
takeProfitOrder.MainOrderType = "LIMIT"
|
takeProfitOrder.MainOrderType = "LIMIT"
|
||||||
takeProfitOrder.Num = ext.TotalAfterReducing.Truncate(int32(tradeSet.AmountDigit)).String()
|
takeProfitOrder.Num = num.String()
|
||||||
// takeProfitOrder.Rate = ext.ReduceTakeProfitRatio.String()
|
|
||||||
//止盈需要累加之前的亏损
|
//止盈需要累加之前的亏损
|
||||||
takeProfitOrder.Rate = ext.ReduceTakeProfitRatio.Add(ext.ReduceReTakeRatio).String()
|
takeProfitOrder.Rate = ext.TpTpPriceRatio.Truncate(2).String()
|
||||||
takeProfitOrder.BuyPrice = "0"
|
takeProfitOrder.BuyPrice = "0"
|
||||||
|
|
||||||
binanceservice.SetPrice(&takeProfitOrder, parentOrder, tradeSet)
|
binanceservice.SetPrice(&takeProfitOrder, parentOrder, tradeSet)
|
||||||
orders = append(orders, takeProfitOrder)
|
orders = append(orders, takeProfitOrder)
|
||||||
|
}
|
||||||
//有止损单
|
//有止损单
|
||||||
if ext.ReduceStopLossRatio.Cmp(decimal.Zero) > 0 {
|
if ext.TpSlPriceRatio.Cmp(decimal.Zero) > 0 && num.Cmp(decimal.Zero) > 0 {
|
||||||
var stoploss models.LinePreOrder
|
var stoploss models.LinePreOrder
|
||||||
|
|
||||||
copier.Copy(&stoploss, parentOrder)
|
copier.Copy(&stoploss, parentOrder)
|
||||||
@ -821,9 +920,9 @@ func makeReduceTakeAndStoploss(parentOrder *models.LinePreOrder, ext models.Line
|
|||||||
stoploss.OrderType = 2
|
stoploss.OrderType = 2
|
||||||
stoploss.SignPrice = parentOrder.Price
|
stoploss.SignPrice = parentOrder.Price
|
||||||
stoploss.BuyPrice = "0"
|
stoploss.BuyPrice = "0"
|
||||||
stoploss.Rate = ext.ReduceStopLossRatio.String()
|
stoploss.Rate = ext.TpSlPriceRatio.String()
|
||||||
stoploss.MainOrderType = "LIMIT"
|
stoploss.MainOrderType = "LIMIT"
|
||||||
stoploss.Num = ext.TotalAfterReducing.Truncate(int32(tradeSet.AmountDigit)).String()
|
stoploss.Num = num.String()
|
||||||
stoploss.BuyPrice = "0"
|
stoploss.BuyPrice = "0"
|
||||||
|
|
||||||
binanceservice.SetPrice(&stoploss, parentOrder, tradeSet)
|
binanceservice.SetPrice(&stoploss, parentOrder, tradeSet)
|
||||||
@ -1633,7 +1732,7 @@ func (e *LinePreOrder) QueryAiCoinPrice(req *dto.QueryAiCoinPriceReq) (models.Li
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 根据请求参数重新生成亏损回本止盈百分比
|
// 根据请求参数重新生成亏损回本止盈百分比
|
||||||
func (e *LinePreOrder) GenerateOrder(req *dto.LineAddPreOrderReq) ([]models.LineDirection, error) {
|
func (e *LinePreOrder) GenerateOrder(req *dto.LineAddPreOrderReq) error {
|
||||||
var tradeSet models2.TradeSet
|
var tradeSet models2.TradeSet
|
||||||
var tickerPrice decimal.Decimal
|
var tickerPrice decimal.Decimal
|
||||||
|
|
||||||
@ -1644,7 +1743,7 @@ func (e *LinePreOrder) GenerateOrder(req *dto.LineAddPreOrderReq) ([]models.Line
|
|||||||
}
|
}
|
||||||
|
|
||||||
if tradeSet.LastPrice == "" {
|
if tradeSet.LastPrice == "" {
|
||||||
return nil, errors.New("获取不到交易对信息")
|
return errors.New("获取不到交易对信息")
|
||||||
}
|
}
|
||||||
|
|
||||||
var price decimal.Decimal
|
var price decimal.Decimal
|
||||||
@ -1666,77 +1765,58 @@ func (e *LinePreOrder) GenerateOrder(req *dto.LineAddPreOrderReq) ([]models.Line
|
|||||||
buyPrice := utility.StrToDecimal(req.BuyPrice)
|
buyPrice := utility.StrToDecimal(req.BuyPrice)
|
||||||
mainAmount := buyPrice.Div(price).Truncate(int32(tradeSet.AmountDigit))
|
mainAmount := buyPrice.Div(price).Truncate(int32(tradeSet.AmountDigit))
|
||||||
calculateResp := dto.CalculateBreakEvenRatioResp{}
|
calculateResp := dto.CalculateBreakEvenRatioResp{}
|
||||||
|
lossBeginPercent := decimal.Zero
|
||||||
mainParam := dto.CalculateBreakEevenRatioReq{
|
mainParam := dto.CalculateBreakEevenRatioReq{
|
||||||
Price: price,
|
Price: price,
|
||||||
ExchangeType: req.ExchangeType,
|
ExchangeType: req.ExchangeType,
|
||||||
Symbol: req.Symbol,
|
Symbol: req.Symbol,
|
||||||
SymbolType: req.SymbolType,
|
SymbolType: req.SymbolType,
|
||||||
BuyPrice: buyPrice,
|
BuyPrice: buyPrice,
|
||||||
LossBeginPercent: decimal.Zero,
|
LossBeginPercent: lossBeginPercent,
|
||||||
LossEndPercent: req.ReducePriceRatio,
|
LossEndPercent: req.ReducePriceRatio,
|
||||||
AddPositionType: 2,
|
AddPositionType: 2,
|
||||||
AddPositionVal: decimal.Zero,
|
AddPositionVal: decimal.Zero,
|
||||||
ReducePercent: req.ReduceNumRatio,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//计算减仓后
|
//计算减仓后
|
||||||
mainParam.LossEndPercent = req.ReducePriceRatio
|
mainParam.LossEndPercent = req.ReducePriceRatio
|
||||||
mainParam.RemainingQuantity = mainAmount
|
mainParam.RemainingQuantity = mainAmount
|
||||||
e.CalculateBreakEvenRatio(&mainParam, &calculateResp)
|
mainParam.AddType = 2
|
||||||
|
e.CalculateBreakEvenRatio(&mainParam, &calculateResp, tradeSet)
|
||||||
mainParam.RemainingQuantity = calculateResp.RemainingQuantity
|
mainParam.RemainingQuantity = calculateResp.RemainingQuantity
|
||||||
mainParam.TotalLossAmountU = calculateResp.TotalLossAmountU
|
mainParam.TotalLossAmountU = calculateResp.TotalLossAmountU
|
||||||
req.ReduceReTakeProfitRatio = calculateResp.Ratio
|
req.ReduceReTakeProfitRatio = calculateResp.Ratio
|
||||||
mainParam.LossBeginPercent = req.ReducePriceRatio
|
lossBeginPercent = req.ReducePriceRatio
|
||||||
|
|
||||||
|
//顺序排序
|
||||||
|
sort.Slice(req.Ext, func(i, j int) bool {
|
||||||
|
return req.Ext[i].PriceRatio.Cmp(req.Ext[j].PriceRatio) < 0
|
||||||
|
})
|
||||||
|
|
||||||
for index := range req.Ext {
|
for index := range req.Ext {
|
||||||
mainParam.LossEndPercent = req.Ext[index].AddPositionPriceRatio
|
mainParam.LossBeginPercent = lossBeginPercent
|
||||||
|
mainParam.LossEndPercent = req.Ext[index].PriceRatio
|
||||||
mainParam.AddPositionType = req.Ext[index].AddPositionType
|
mainParam.AddPositionType = req.Ext[index].AddPositionType
|
||||||
mainParam.AddPositionVal = req.Ext[index].AddPositionVal
|
mainParam.AddPositionVal = req.Ext[index].AddPositionVal
|
||||||
mainParam.ReducePercent = decimal.Zero
|
|
||||||
e.CalculateBreakEvenRatio(&mainParam, &calculateResp)
|
e.CalculateBreakEvenRatio(&mainParam, &calculateResp, tradeSet)
|
||||||
|
|
||||||
req.Ext[index].ReTakeProfitRatio = calculateResp.Ratio
|
req.Ext[index].ReTakeProfitRatio = calculateResp.Ratio
|
||||||
mainParam.LossBeginPercent = req.Ext[index].AddPositionPriceRatio
|
lossBeginPercent = req.Ext[index].PriceRatio
|
||||||
mainParam.RemainingQuantity = calculateResp.RemainingQuantity
|
|
||||||
mainParam.TotalLossAmountU = calculateResp.TotalLossAmountU
|
|
||||||
mainParam.LossEndPercent = req.Ext[index].ReducePriceRatio
|
|
||||||
mainParam.AddPositionVal = decimal.Zero
|
|
||||||
mainParam.ReducePercent = req.Ext[index].ReduceNumRatio
|
|
||||||
e.CalculateBreakEvenRatio(&mainParam, &calculateResp)
|
|
||||||
|
|
||||||
req.Ext[index].ReduceReTakeProfitRatio = calculateResp.Ratio
|
|
||||||
mainParam.LossBeginPercent = req.Ext[index].ReducePriceRatio
|
|
||||||
mainParam.RemainingQuantity = calculateResp.RemainingQuantity
|
mainParam.RemainingQuantity = calculateResp.RemainingQuantity
|
||||||
mainParam.TotalLossAmountU = calculateResp.TotalLossAmountU
|
mainParam.TotalLossAmountU = calculateResp.TotalLossAmountU
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算亏损百分比
|
// 计算亏损百分比
|
||||||
func (e *LinePreOrder) CalculateBreakEvenRatio(req *dto.CalculateBreakEevenRatioReq, data *dto.CalculateBreakEvenRatioResp) error {
|
func (e *LinePreOrder) CalculateBreakEvenRatio(req *dto.CalculateBreakEevenRatioReq, data *dto.CalculateBreakEvenRatioResp, tradeSet models2.TradeSet) error {
|
||||||
var tradeSet models2.TradeSet
|
|
||||||
|
|
||||||
if req.LossEndPercent.Cmp(req.LossBeginPercent) < 0 {
|
if req.LossEndPercent.Cmp(req.LossBeginPercent) < 0 {
|
||||||
return errors.New("截至亏损百分比必须大于开始亏损百分比")
|
return errors.New("截至亏损百分比必须大于开始亏损百分比")
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.SymbolType == 1 {
|
|
||||||
val, _ := helper.DefaultRedis.GetString(fmt.Sprintf(global.TICKER_SPOT, req.ExchangeType, req.Symbol))
|
|
||||||
|
|
||||||
sonic.Unmarshal([]byte(val), &tradeSet)
|
|
||||||
} else if req.SymbolType == 2 {
|
|
||||||
val, _ := helper.DefaultRedis.GetString(fmt.Sprintf(global.TICKER_FUTURES, req.ExchangeType, req.Symbol))
|
|
||||||
|
|
||||||
sonic.Unmarshal([]byte(val), &tradeSet)
|
|
||||||
} else {
|
|
||||||
return errors.New("symbolType error")
|
|
||||||
}
|
|
||||||
|
|
||||||
if tradeSet.LastPrice == "" {
|
|
||||||
return errors.New("没有找到交易对行情")
|
|
||||||
}
|
|
||||||
|
|
||||||
var addPositionBuyPrice decimal.Decimal
|
var addPositionBuyPrice decimal.Decimal
|
||||||
|
|
||||||
if req.AddPositionType == 1 {
|
if req.AddPositionType == 1 {
|
||||||
@ -1759,8 +1839,8 @@ func (e *LinePreOrder) CalculateBreakEvenRatio(req *dto.CalculateBreakEevenRatio
|
|||||||
lossAmountU := req.Price.Mul(percentDiff.Div(decimal.NewFromInt(100).Truncate(4))).Mul(req.RemainingQuantity).Truncate(int32(tradeSet.PriceDigit))
|
lossAmountU := req.Price.Mul(percentDiff.Div(decimal.NewFromInt(100).Truncate(4))).Mul(req.RemainingQuantity).Truncate(int32(tradeSet.PriceDigit))
|
||||||
|
|
||||||
//计算减仓数量
|
//计算减仓数量
|
||||||
if req.ReducePercent.Cmp(decimal.NewFromInt(0)) > 0 {
|
if req.AddPositionType == 2 && req.AddPositionVal.Cmp(decimal.NewFromInt(0)) > 0 {
|
||||||
reduceAmount = totalAmount.Mul(req.ReducePercent.Div(decimal.NewFromInt(100).Truncate(4))).Truncate(int32(tradeSet.AmountDigit))
|
reduceAmount = totalAmount.Mul(req.AddPositionVal.Div(decimal.NewFromInt(100).Truncate(4))).Truncate(int32(tradeSet.AmountDigit))
|
||||||
}
|
}
|
||||||
|
|
||||||
data.RemainingQuantity = totalAmount.Sub(reduceAmount)
|
data.RemainingQuantity = totalAmount.Sub(reduceAmount)
|
||||||
@ -1776,163 +1856,163 @@ func (e *LinePreOrder) CalculateBreakEvenRatio(req *dto.CalculateBreakEevenRatio
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 手动加仓
|
// // 手动加仓
|
||||||
func (e *LinePreOrder) AddPosition(req *dto.LinePreOrderAddPositionReq) error {
|
// func (e *LinePreOrder) AddPosition(req *dto.LinePreOrderAddPositionReq) error {
|
||||||
lastPositionOrder := models.LinePreOrder{}
|
// lastPositionOrder := models.LinePreOrder{}
|
||||||
var tradeSet models2.TradeSet
|
// var tradeSet models2.TradeSet
|
||||||
|
|
||||||
if req.OrderType == 1 {
|
// if req.OrderType == 1 {
|
||||||
tradeSet, _ = binanceservice.GetTradeSet(req.Symbol, 0)
|
// tradeSet, _ = binanceservice.GetTradeSet(req.Symbol, 0)
|
||||||
} else if req.OrderType == 2 {
|
// } else if req.OrderType == 2 {
|
||||||
tradeSet, _ = binanceservice.GetTradeSet(req.Symbol, 1)
|
// tradeSet, _ = binanceservice.GetTradeSet(req.Symbol, 1)
|
||||||
} else {
|
// } else {
|
||||||
return fmt.Errorf("交易对:%s 订单类型错误", req.Symbol)
|
// return fmt.Errorf("交易对:%s 订单类型错误", req.Symbol)
|
||||||
}
|
// }
|
||||||
|
|
||||||
if tradeSet.LastPrice == "" {
|
// if tradeSet.LastPrice == "" {
|
||||||
return fmt.Errorf("交易对:%s 交易对配置错误", req.Symbol)
|
// return fmt.Errorf("交易对:%s 交易对配置错误", req.Symbol)
|
||||||
}
|
// }
|
||||||
|
|
||||||
if err := e.Orm.Model(&lastPositionOrder).Where("symbol =? AND status = 6 AND site =? AND api_id =? AND symbol_type =? AND exchange_type=?",
|
// if err := e.Orm.Model(&lastPositionOrder).Where("symbol =? AND status = 6 AND site =? AND api_id =? AND symbol_type =? AND exchange_type=?",
|
||||||
req.Symbol, req.Site, req.ApiUserId, req.OrderType, req.ExchangeType).Error; err != nil {
|
// req.Symbol, req.Site, req.ApiUserId, req.OrderType, req.ExchangeType).Error; err != nil {
|
||||||
logger.Errorf("交易对:%s查询已开仓订单失败", req.Symbol)
|
// logger.Errorf("交易对:%s查询已开仓订单失败", req.Symbol)
|
||||||
return fmt.Errorf("交易对:%s 没有已开仓的订单", req.Symbol)
|
// return fmt.Errorf("交易对:%s 没有已开仓的订单", req.Symbol)
|
||||||
}
|
// }
|
||||||
|
|
||||||
ext := models.LinePreOrderExt{
|
// ext := models.LinePreOrderExt{
|
||||||
MainOrderId: lastPositionOrder.Id,
|
// MainOrderId: lastPositionOrder.Id,
|
||||||
TakeProfitRatio: req.Profit,
|
// TakeProfitRatio: req.Profit,
|
||||||
ReducePriceRatio: req.ReducePriceRatio,
|
// ReducePriceRatio: req.ReducePriceRatio,
|
||||||
ReduceNumRatio: req.ReduceNumRatio,
|
// ReduceNumRatio: req.ReduceNumRatio,
|
||||||
ReduceTakeProfitRatio: req.ReduceTakeProfitRatio,
|
// ReduceTakeProfitRatio: req.ReduceTakeProfitRatio,
|
||||||
ReduceStopLossRatio: req.ReduceStopLossRatio,
|
// ReduceStopLossRatio: req.ReduceStopLossRatio,
|
||||||
AddPositionOrderType: req.AddPositionOrderType,
|
// AddPositionOrderType: req.AddPositionOrderType,
|
||||||
AddPositionType: 2,
|
// AddPositionType: 2,
|
||||||
AddPositionVal: req.BuyPrice,
|
// AddPositionVal: req.BuyPrice,
|
||||||
}
|
// }
|
||||||
|
|
||||||
addPosition := models.LinePreOrder{
|
// addPosition := models.LinePreOrder{
|
||||||
SignPrice: tradeSet.LastPrice,
|
// SignPrice: tradeSet.LastPrice,
|
||||||
Pid: lastPositionOrder.Id,
|
// Pid: lastPositionOrder.Id,
|
||||||
MainId: lastPositionOrder.Id,
|
// MainId: lastPositionOrder.Id,
|
||||||
Symbol: req.Symbol,
|
// Symbol: req.Symbol,
|
||||||
QuoteSymbol: tradeSet.Currency,
|
// QuoteSymbol: tradeSet.Currency,
|
||||||
SignPriceU: utility.StrToDecimal(tradeSet.LastPrice),
|
// SignPriceU: utility.StrToDecimal(tradeSet.LastPrice),
|
||||||
ApiId: req.ApiUserId,
|
// ApiId: req.ApiUserId,
|
||||||
Site: req.Site,
|
// Site: req.Site,
|
||||||
ExchangeType: req.ExchangeType,
|
// ExchangeType: req.ExchangeType,
|
||||||
OrderType: 0,
|
// OrderType: 0,
|
||||||
OrderCategory: 3,
|
// OrderCategory: 3,
|
||||||
BuyPrice: req.BuyPrice.String(),
|
// BuyPrice: req.BuyPrice.String(),
|
||||||
Status: 0,
|
// Status: 0,
|
||||||
SymbolType: req.OrderType,
|
// SymbolType: req.OrderType,
|
||||||
MainOrderType: req.AddPositionOrderType,
|
// MainOrderType: req.AddPositionOrderType,
|
||||||
ExpireTime: time.Now().Add(4),
|
// ExpireTime: time.Now().Add(4),
|
||||||
OrderSn: utility.Int64ToString(snowflakehelper.GetOrderId()),
|
// OrderSn: utility.Int64ToString(snowflakehelper.GetOrderId()),
|
||||||
}
|
// }
|
||||||
|
|
||||||
tickerPrice := utility.StrToDecimal(tradeSet.LastPrice)
|
// tickerPrice := utility.StrToDecimal(tradeSet.LastPrice)
|
||||||
if req.PricePattern == "percentage" {
|
// if req.PricePattern == "percentage" {
|
||||||
addPosition.Rate = req.Price.String()
|
// addPosition.Rate = req.Price.String()
|
||||||
priceRate := req.Price.Div(decimal.NewFromInt(100)) //下单价除100 =0.1
|
// priceRate := req.Price.Div(decimal.NewFromInt(100)) //下单价除100 =0.1
|
||||||
|
|
||||||
if strings.ToUpper(req.Site) == "BUY" { //购买方向
|
// if strings.ToUpper(req.Site) == "BUY" { //购买方向
|
||||||
//实际下单价格
|
// //实际下单价格
|
||||||
truncate := tickerPrice.Mul(decimal.NewFromInt(1).Sub(priceRate)).Truncate(int32(tradeSet.PriceDigit))
|
// truncate := tickerPrice.Mul(decimal.NewFromInt(1).Sub(priceRate)).Truncate(int32(tradeSet.PriceDigit))
|
||||||
addPosition.Price = truncate.String()
|
// addPosition.Price = truncate.String()
|
||||||
} else {
|
// } else {
|
||||||
truncate := tickerPrice.Mul(decimal.NewFromInt(1).Add(priceRate)).Truncate(int32(tradeSet.PriceDigit))
|
// truncate := tickerPrice.Mul(decimal.NewFromInt(1).Add(priceRate)).Truncate(int32(tradeSet.PriceDigit))
|
||||||
addPosition.Price = truncate.String()
|
// addPosition.Price = truncate.String()
|
||||||
}
|
// }
|
||||||
|
|
||||||
} else { //实际价格下单
|
// } else { //实际价格下单
|
||||||
addPosition.Price = req.Price.Truncate(int32(tradeSet.PriceDigit)).String()
|
// addPosition.Price = req.Price.Truncate(int32(tradeSet.PriceDigit)).String()
|
||||||
addPosition.SignPriceType = req.PricePattern
|
// addPosition.SignPriceType = req.PricePattern
|
||||||
addPosition.Rate = "0"
|
// addPosition.Rate = "0"
|
||||||
}
|
// }
|
||||||
if tradeSet.Currency != "USDT" { //不是U本位
|
// if tradeSet.Currency != "USDT" { //不是U本位
|
||||||
//获取币本位兑换u的价格
|
// //获取币本位兑换u的价格
|
||||||
ticker2 := models2.TradeSet{}
|
// ticker2 := models2.TradeSet{}
|
||||||
tickerVal, _ := helper.DefaultRedis.GetString(fmt.Sprintf(global.TICKER_SPOT, req.ExchangeType, strings.ToUpper(tradeSet.Coin+"USDT")))
|
// tickerVal, _ := helper.DefaultRedis.GetString(fmt.Sprintf(global.TICKER_SPOT, req.ExchangeType, strings.ToUpper(tradeSet.Coin+"USDT")))
|
||||||
|
|
||||||
if tickerVal == "" {
|
// if tickerVal == "" {
|
||||||
logger.Error("查询行情失败")
|
// logger.Error("查询行情失败")
|
||||||
return fmt.Errorf("交易对:%s 获取u本位行情失败", req.Symbol)
|
// return fmt.Errorf("交易对:%s 获取u本位行情失败", req.Symbol)
|
||||||
}
|
// }
|
||||||
|
|
||||||
err := sonic.Unmarshal([]byte(tickerVal), &ticker2)
|
// err := sonic.Unmarshal([]byte(tickerVal), &ticker2)
|
||||||
|
|
||||||
if ticker2.LastPrice == "" {
|
// if ticker2.LastPrice == "" {
|
||||||
logger.Errorf("查询行情失败 %s err:%v", strings.ToUpper(tradeSet.Coin+"USDT"), err)
|
// logger.Errorf("查询行情失败 %s err:%v", strings.ToUpper(tradeSet.Coin+"USDT"), err)
|
||||||
return fmt.Errorf("交易对:%s 获取u本位行情 反序列化失败", req.Symbol)
|
// return fmt.Errorf("交易对:%s 获取u本位行情 反序列化失败", req.Symbol)
|
||||||
}
|
// }
|
||||||
//LTCBTC --> LTCUSDT
|
// //LTCBTC --> LTCUSDT
|
||||||
uTickerPrice := utility.StrToDecimal(ticker2.LastPrice) //94069
|
// uTickerPrice := utility.StrToDecimal(ticker2.LastPrice) //94069
|
||||||
//换算成U
|
// //换算成U
|
||||||
//div := decimal.NewFromInt(1).Div(uTickerPrice) //0.0000106365
|
// //div := decimal.NewFromInt(1).Div(uTickerPrice) //0.0000106365
|
||||||
//在换算成对应交易对对应的价值
|
// //在换算成对应交易对对应的价值
|
||||||
//LTCBTC --> LTCUSDT == LTCUSDT -- 100.502
|
// //LTCBTC --> LTCUSDT == LTCUSDT -- 100.502
|
||||||
div := tickerPrice.Div(decimal.NewFromInt(1).Div(uTickerPrice))
|
// div := tickerPrice.Div(decimal.NewFromInt(1).Div(uTickerPrice))
|
||||||
//计算下单数量
|
// //计算下单数量
|
||||||
addPosition.Num = req.BuyPrice.Div(div).Truncate(int32(tradeSet.AmountDigit)).String()
|
// addPosition.Num = req.BuyPrice.Div(div).Truncate(int32(tradeSet.AmountDigit)).String()
|
||||||
} else {
|
// } else {
|
||||||
fromString, _ := decimal.NewFromString(addPosition.Price)
|
// fromString, _ := decimal.NewFromString(addPosition.Price)
|
||||||
addPosition.Num = req.BuyPrice.Div(fromString).Truncate(int32(tradeSet.AmountDigit)).String()
|
// addPosition.Num = req.BuyPrice.Div(fromString).Truncate(int32(tradeSet.AmountDigit)).String()
|
||||||
}
|
// }
|
||||||
//事务保存
|
// //事务保存
|
||||||
err := e.Orm.Transaction(func(tx *gorm.DB) error {
|
// err := e.Orm.Transaction(func(tx *gorm.DB) error {
|
||||||
//添加加仓单
|
// //添加加仓单
|
||||||
if err := tx.Create(&addPosition).Error; err != nil {
|
// if err := tx.Create(&addPosition).Error; err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
|
|
||||||
//止盈、减仓
|
// //止盈、减仓
|
||||||
orders, err := makeFuturesTakeAndReduce(&addPosition, ext, tradeSet)
|
// orders, err := makeFuturesTakeAndReduce(&addPosition, ext, tradeSet)
|
||||||
|
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
logger.Error("构造加仓单止盈、减仓失败")
|
// logger.Error("构造加仓单止盈、减仓失败")
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
|
|
||||||
if err := e.Orm.Create(&orders).Error; err != nil {
|
// if err := e.Orm.Create(&orders).Error; err != nil {
|
||||||
logger.Error("保存加仓单止盈、减仓失败")
|
// logger.Error("保存加仓单止盈、减仓失败")
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
|
|
||||||
//处理减仓单
|
// //处理减仓单
|
||||||
for index := range orders {
|
// for index := range orders {
|
||||||
//减仓单且 减仓比例大于0 小于100 就冲下止盈止损
|
// //减仓单且 减仓比例大于0 小于100 就冲下止盈止损
|
||||||
if orders[index].OrderType == 4 && ext.ReduceNumRatio.Cmp(decimal.Zero) > 0 && ext.ReduceNumRatio.Cmp(decimal.NewFromInt(100)) < 0 {
|
// if orders[index].OrderType == 4 && ext.ReduceNumRatio.Cmp(decimal.Zero) > 0 && ext.ReduceNumRatio.Cmp(decimal.NewFromInt(100)) < 0 {
|
||||||
reduceChildOrders, err := makeReduceTakeAndStoploss(&(orders[index]), ext, tradeSet)
|
// reduceChildOrders, err := makeReduceTakeAndStoploss(&(orders[index]), ext, tradeSet)
|
||||||
|
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
logger.Error("生产加仓单止盈、减仓失败")
|
// logger.Error("生产加仓单止盈、减仓失败")
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
|
|
||||||
if len(reduceChildOrders) == 0 {
|
// if len(reduceChildOrders) == 0 {
|
||||||
continue
|
// continue
|
||||||
}
|
// }
|
||||||
|
|
||||||
if err := e.Orm.Create(&reduceChildOrders).Error; err != nil {
|
// if err := e.Orm.Create(&reduceChildOrders).Error; err != nil {
|
||||||
logger.Error("报错减仓后止盈止损失败")
|
// logger.Error("报错减仓后止盈止损失败")
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
ext.OrderId = addPosition.Id
|
// ext.OrderId = addPosition.Id
|
||||||
if err := tx.Create(&ext).Error; err != nil {
|
// if err := tx.Create(&ext).Error; err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
|
|
||||||
return nil
|
// return nil
|
||||||
})
|
// })
|
||||||
|
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
logger.Errorf("交易对:%s 添加加仓订单失败", req.Symbol)
|
// logger.Errorf("交易对:%s 添加加仓订单失败", req.Symbol)
|
||||||
return fmt.Errorf("交易对:%s 添加加仓订单失败", req.Symbol)
|
// return fmt.Errorf("交易对:%s 添加加仓订单失败", req.Symbol)
|
||||||
}
|
// }
|
||||||
|
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|||||||
@ -294,17 +294,14 @@ func (e *LineSymbol) ResetSpotSymbol() error {
|
|||||||
logger.Error("获取币安现货交易对失败")
|
logger.Error("获取币安现货交易对失败")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
symbols := make([]models.LineSymbol, 0)
|
insertSymbols := make([]models.LineSymbol, 0)
|
||||||
|
|
||||||
sysConfig := SysConfig{Service: service.Service{Orm: e.Orm}}
|
sysConfig := SysConfig{Service: service.Service{Orm: e.Orm}}
|
||||||
|
|
||||||
var req = new(dto.SysConfigByKeyReq)
|
var req = new(dto.SysConfigByKeyReq)
|
||||||
var resp = new(dto.GetSysConfigByKEYForServiceResp)
|
var resp = new(dto.GetSysConfigByKEYForServiceResp)
|
||||||
req.ConfigKey = "quote_volume_24hr"
|
req.ConfigKey = "quote_volume_24hr"
|
||||||
sysConfig.GetWithKey(req, resp)
|
sysConfig.GetWithKey(req, resp)
|
||||||
symbolBlack := make([]models.LineSymbolBlack, 0)
|
symbolBlackMap := getSymbolBlackMap(e, "1")
|
||||||
e.Orm.Model(&models.LineSymbolBlack{}).Where("type = '1'").Find(&symbolBlack)
|
|
||||||
|
|
||||||
type Ticker struct {
|
type Ticker struct {
|
||||||
Symbol string `json:"symbol"`
|
Symbol string `json:"symbol"`
|
||||||
Price string `json:"price"`
|
Price string `json:"price"`
|
||||||
@ -317,18 +314,17 @@ func (e *LineSymbol) ResetSpotSymbol() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//判断是否在黑名单、是否需要修改
|
||||||
for symbol, tradeSet := range tradeSets {
|
for symbol, tradeSet := range tradeSets {
|
||||||
|
|
||||||
key := fmt.Sprintf(global.TICKER_SPOT, global.EXCHANGE_BINANCE, symbol)
|
key := fmt.Sprintf(global.TICKER_SPOT, global.EXCHANGE_BINANCE, symbol)
|
||||||
|
|
||||||
//判断是否在黑名单里面
|
//判断是否在黑名单里面
|
||||||
for _, black := range symbolBlack {
|
if _, ok := symbolBlackMap[symbol]; ok {
|
||||||
if black.Symbol == symbol {
|
|
||||||
helper.DefaultRedis.DeleteString(key)
|
helper.DefaultRedis.DeleteString(key)
|
||||||
deleteSymbols = append(deleteSymbols, symbol)
|
deleteSymbols = append(deleteSymbols, symbol)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
val := helper.DefaultRedis.Get(key).Val()
|
val := helper.DefaultRedis.Get(key).Val()
|
||||||
var spotTicker24h commonModels.TradeSet
|
var spotTicker24h commonModels.TradeSet
|
||||||
@ -369,7 +365,14 @@ func (e *LineSymbol) ResetSpotSymbol() error {
|
|||||||
if lineSymbol.Symbol == "" {
|
if lineSymbol.Symbol == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
symbols = append(symbols, lineSymbol)
|
insertSymbols = append(insertSymbols, lineSymbol)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//判断已经移除的交易对
|
||||||
|
for _, v := range oldMapSymbols {
|
||||||
|
if _, ok := tradeSets[v.Symbol]; !ok && !utility.ContainsStr(deleteSymbols, v.Symbol) {
|
||||||
|
deleteSymbols = append(deleteSymbols, v.Symbol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -393,8 +396,8 @@ func (e *LineSymbol) ResetSpotSymbol() error {
|
|||||||
batchDeleteBySymbols(deleteSymbols, "1", e)
|
batchDeleteBySymbols(deleteSymbols, "1", e)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(symbols) > 0 {
|
if len(insertSymbols) > 0 {
|
||||||
err := e.Orm.Model(&models.LineSymbol{}).Omit("api_id").Create(&symbols).Error
|
err := e.Orm.Model(&models.LineSymbol{}).Omit("api_id").Create(&insertSymbols).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -403,6 +406,18 @@ func (e *LineSymbol) ResetSpotSymbol() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSymbolBlackMap(e *LineSymbol, symbolType string) map[string]models.LineSymbolBlack {
|
||||||
|
symbolBlack := make([]models.LineSymbolBlack, 0)
|
||||||
|
symbolBlackMap := make(map[string]models.LineSymbolBlack)
|
||||||
|
|
||||||
|
e.Orm.Model(&models.LineSymbolBlack{}).Where("type = ?", symbolType).Find(&symbolBlack)
|
||||||
|
|
||||||
|
for _, v := range symbolBlack {
|
||||||
|
symbolBlackMap[v.Symbol] = v
|
||||||
|
}
|
||||||
|
return symbolBlackMap
|
||||||
|
}
|
||||||
|
|
||||||
// 批量删除 根据交易对名
|
// 批量删除 根据交易对名
|
||||||
// symbolType 1-现货 2-合约
|
// symbolType 1-现货 2-合约
|
||||||
func batchDeleteBySymbols(deleteSymbols []string, symbolType string, e *LineSymbol) {
|
func batchDeleteBySymbols(deleteSymbols []string, symbolType string, e *LineSymbol) {
|
||||||
@ -459,8 +474,7 @@ func (e *LineSymbol) ResetFuturesSymbol() error {
|
|||||||
req.ConfigKey = "quote_volume_24hr"
|
req.ConfigKey = "quote_volume_24hr"
|
||||||
sysConfig.GetWithKey(req, resp)
|
sysConfig.GetWithKey(req, resp)
|
||||||
symbols := make([]models.LineSymbol, 0)
|
symbols := make([]models.LineSymbol, 0)
|
||||||
symbolBlack := make([]models.LineSymbolBlack, 0)
|
symbolBlackMap := getSymbolBlackMap(e, "2")
|
||||||
|
|
||||||
type Ticker struct {
|
type Ticker struct {
|
||||||
Symbol string `json:"symbol"`
|
Symbol string `json:"symbol"`
|
||||||
Price string `json:"price"`
|
Price string `json:"price"`
|
||||||
@ -473,19 +487,17 @@ func (e *LineSymbol) ResetFuturesSymbol() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
e.Orm.Model(&models.LineSymbolBlack{}).Where("type = 2").Find(&symbolBlack)
|
//判断是否在黑名单、是否需要新增
|
||||||
for symbol, tradeSet := range tradeSets {
|
for symbol, tradeSet := range tradeSets {
|
||||||
|
|
||||||
key := fmt.Sprintf(global.TICKER_FUTURES, global.EXCHANGE_BINANCE, symbol)
|
key := fmt.Sprintf(global.TICKER_FUTURES, global.EXCHANGE_BINANCE, symbol)
|
||||||
|
|
||||||
//判断是否在黑名单里面
|
//判断是否在黑名单里面
|
||||||
for _, black := range symbolBlack {
|
if _, ok := symbolBlackMap[symbol]; ok {
|
||||||
if black.Symbol == symbol {
|
|
||||||
helper.DefaultRedis.DeleteString(key)
|
helper.DefaultRedis.DeleteString(key)
|
||||||
deleteSymbols = append(deleteSymbols, symbol)
|
deleteSymbols = append(deleteSymbols, symbol)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
|
||||||
val := helper.DefaultRedis.Get(key).Val()
|
val := helper.DefaultRedis.Get(key).Val()
|
||||||
var spotTicker24h commonModels.TradeSet
|
var spotTicker24h commonModels.TradeSet
|
||||||
sonic.Unmarshal([]byte(val), &spotTicker24h)
|
sonic.Unmarshal([]byte(val), &spotTicker24h)
|
||||||
@ -528,6 +540,13 @@ func (e *LineSymbol) ResetFuturesSymbol() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//交易所已经没有的交易对直接去除
|
||||||
|
for _, v := range oldMapSymbols {
|
||||||
|
if _, ok := tradeSets[v.Symbol]; !ok && !utility.ContainsStr(deleteSymbols, v.Symbol) {
|
||||||
|
deleteSymbols = append(deleteSymbols, v.Symbol)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
groups, err := getSymbolGroups(e, "2")
|
groups, err := getSymbolGroups(e, "2")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@ -79,15 +79,11 @@ func (e *LineSymbolBlack) Insert(c *dto.LineSymbolBlackInsertReq) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = e.Orm.Create(&data).Error
|
err = e.Orm.Create(&data).Error
|
||||||
if err != nil {
|
|
||||||
e.Log.Errorf("LineSymbolBlackService Insert error:%s \r\n", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return e.reloadSymbol(c.Type)
|
func (e *LineSymbolBlack) ReloadSymbol(symbolType string) error {
|
||||||
}
|
|
||||||
|
|
||||||
func (e *LineSymbolBlack) reloadSymbol(symbolType string) error {
|
|
||||||
symbolService := LineSymbol{Service: e.Service}
|
symbolService := LineSymbol{Service: e.Service}
|
||||||
|
|
||||||
if symbolType == "1" {
|
if symbolType == "1" {
|
||||||
@ -133,21 +129,12 @@ func (e *LineSymbolBlack) Update(c *dto.LineSymbolBlackUpdateReq, p *actions.Dat
|
|||||||
if db.RowsAffected == 0 {
|
if db.RowsAffected == 0 {
|
||||||
return errors.New("无权更新该数据")
|
return errors.New("无权更新该数据")
|
||||||
}
|
}
|
||||||
return e.reloadSymbol(c.Type)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove 删除LineSymbolBlack
|
// Remove 删除LineSymbolBlack
|
||||||
func (e *LineSymbolBlack) Remove(d *dto.LineSymbolBlackDeleteReq, p *actions.DataPermission) error {
|
func (e *LineSymbolBlack) Remove(d *dto.LineSymbolBlackDeleteReq, p *actions.DataPermission) error {
|
||||||
var data models.LineSymbolBlack
|
var data models.LineSymbolBlack
|
||||||
types := make([]string, 0)
|
|
||||||
|
|
||||||
e.Orm.Model(&data).Where("id in ?", d.GetId()).Select("type").Distinct().Find(&types)
|
|
||||||
|
|
||||||
for _, v := range types {
|
|
||||||
if v != "" {
|
|
||||||
e.reloadSymbol(v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
db := e.Orm.Model(&data).
|
db := e.Orm.Model(&data).
|
||||||
Scopes(
|
Scopes(
|
||||||
|
|||||||
@ -461,7 +461,7 @@ func getOpenPositionMainOrderId(db *gorm.DB, newId, apiId, symbolType int, excha
|
|||||||
mainOrders := make([]DbModels.LinePreOrder, 0)
|
mainOrders := make([]DbModels.LinePreOrder, 0)
|
||||||
|
|
||||||
if err := db.Model(&DbModels.LinePreOrder{}).
|
if err := db.Model(&DbModels.LinePreOrder{}).
|
||||||
Where("api_id =? AND status>4 AND status<7 AND symbol=? AND symbol_type =? AND site= ? AND exchange_type=? AND id!=?",
|
Where("api_id =? AND status>4 AND order_type =0 AND status<7 AND symbol=? AND symbol_type =? AND site= ? AND exchange_type=? AND id!=?",
|
||||||
apiId, symbol, symbolType, side, exchangeType, newId).
|
apiId, symbol, symbolType, side, exchangeType, newId).
|
||||||
Select("id", "main_id", "order_sn").Find(&mainOrders).Error; err != nil {
|
Select("id", "main_id", "order_sn").Find(&mainOrders).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@ -93,7 +93,7 @@ func handleFutOrderByType(db *gorm.DB, preOrder *DbModels.LinePreOrder, orderSta
|
|||||||
switch {
|
switch {
|
||||||
//主单成交
|
//主单成交
|
||||||
case preOrder.OrderType == 0 && orderStatus == 6:
|
case preOrder.OrderType == 0 && orderStatus == 6:
|
||||||
handleFutMainOrderFilled(db, preOrder)
|
handleFutMainOrderFilled(db, preOrder, preOrder.Id, true)
|
||||||
//止盈成交
|
//止盈成交
|
||||||
case preOrder.OrderType == 1 && orderStatus == 6:
|
case preOrder.OrderType == 1 && orderStatus == 6:
|
||||||
handleTakeProfit(db, preOrder)
|
handleTakeProfit(db, preOrder)
|
||||||
@ -327,6 +327,13 @@ func handleStopLoss(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
|
|||||||
|
|
||||||
// 止盈单成交
|
// 止盈单成交
|
||||||
func handleTakeProfit(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
|
func handleTakeProfit(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
|
||||||
|
childCount, _ := GetChildTpOrder(db, preOrder.Id)
|
||||||
|
|
||||||
|
if childCount > 0 {
|
||||||
|
extOrderId := preOrder.Pid //ext主单id
|
||||||
|
|
||||||
|
handleFutMainOrderFilled(db, preOrder, extOrderId, false)
|
||||||
|
} else {
|
||||||
removeFutLossAndAddPosition(preOrder.MainId, preOrder.OrderSn)
|
removeFutLossAndAddPosition(preOrder.MainId, preOrder.OrderSn)
|
||||||
removePosition(db, preOrder)
|
removePosition(db, preOrder)
|
||||||
|
|
||||||
@ -345,6 +352,7 @@ func handleTakeProfit(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
|
|||||||
logger.Errorf("主单止盈成功修改主单状态失败 订单号:%s:", err)
|
logger.Errorf("主单止盈成功修改主单状态失败 订单号:%s:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 清除合约缓存
|
// 清除合约缓存
|
||||||
func removeFutLossAndAddPosition(mainId int, orderSn string) {
|
func removeFutLossAndAddPosition(mainId int, orderSn string) {
|
||||||
@ -396,7 +404,7 @@ func removeFutLossAndAddPosition(mainId int, orderSn string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 处理主单成交,处理止盈、止损、减仓订单
|
// 处理主单成交,处理止盈、止损、减仓订单
|
||||||
func handleFutMainOrderFilled(db *gorm.DB, preOrder *models.LinePreOrder) {
|
func handleFutMainOrderFilled(db *gorm.DB, preOrder *models.LinePreOrder, extOrderId int, first bool) {
|
||||||
// 获取交易对配置和API信息
|
// 获取交易对配置和API信息
|
||||||
tradeSet, err := GetTradeSet(preOrder.Symbol, 1)
|
tradeSet, err := GetTradeSet(preOrder.Symbol, 1)
|
||||||
if err != nil || tradeSet.Coin == "" {
|
if err != nil || tradeSet.Coin == "" {
|
||||||
@ -434,7 +442,7 @@ func handleFutMainOrderFilled(db *gorm.DB, preOrder *models.LinePreOrder) {
|
|||||||
// 获取和保存持仓数据
|
// 获取和保存持仓数据
|
||||||
positionData := savePosition(db, preOrder)
|
positionData := savePosition(db, preOrder)
|
||||||
orderExt := models.LinePreOrderExt{}
|
orderExt := models.LinePreOrderExt{}
|
||||||
db.Model(&orderExt).Where("order_id =?", preOrder.Id).First(&orderExt)
|
db.Model(&orderExt).Where("order_id =?", extOrderId).First(&orderExt)
|
||||||
num := getFuturesPositionAvailableQuantity(db, apiInfo, preOrder, tradeSet).Truncate(int32(tradeSet.AmountDigit))
|
num := getFuturesPositionAvailableQuantity(db, apiInfo, preOrder, tradeSet).Truncate(int32(tradeSet.AmountDigit))
|
||||||
|
|
||||||
// 更新订单数量并处理止盈、止损、减仓
|
// 更新订单数量并处理止盈、止损、减仓
|
||||||
@ -443,13 +451,13 @@ func handleFutMainOrderFilled(db *gorm.DB, preOrder *models.LinePreOrder) {
|
|||||||
order.Price = price.String()
|
order.Price = price.String()
|
||||||
|
|
||||||
// 更新止盈止损订单数量
|
// 更新止盈止损订单数量
|
||||||
num = updateOrderQuantity(db, order, preOrder, num, tradeSet)
|
num = updateOrderQuantity(db, order, preOrder, &orderExt, num, first, tradeSet)
|
||||||
|
|
||||||
// 根据订单类型处理
|
// 根据订单类型处理
|
||||||
switch order.OrderType {
|
switch order.OrderType {
|
||||||
case 1: // 止盈
|
case 1: // 止盈
|
||||||
//亏损大于0 重新计算比例
|
//亏损大于0 重新计算比例
|
||||||
if positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 {
|
if first && positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 {
|
||||||
percentag := positionData.TotalLoss.Div(num).Div(price).Mul(decimal.NewFromInt(100))
|
percentag := positionData.TotalLoss.Div(num).Div(price).Mul(decimal.NewFromInt(100))
|
||||||
percentag = percentag.Add(orderExt.TakeProfitRatio).Truncate(2)
|
percentag = percentag.Add(orderExt.TakeProfitRatio).Truncate(2)
|
||||||
order.Rate = percentag.String()
|
order.Rate = percentag.String()
|
||||||
@ -460,10 +468,30 @@ func handleFutMainOrderFilled(db *gorm.DB, preOrder *models.LinePreOrder) {
|
|||||||
} else {
|
} else {
|
||||||
order.Price = price.Mul(decimal.NewFromInt(1).Sub(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
|
order.Price = price.Mul(decimal.NewFromInt(1).Sub(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||||
}
|
}
|
||||||
|
} else if !first && orderExt.TpTpPriceRatio.Cmp(decimal.Zero) > 0 {
|
||||||
|
//止盈后止盈
|
||||||
|
order.Rate = orderExt.TpTpPriceRatio.String()
|
||||||
|
|
||||||
|
if positionData.PositionSide == "LONG" {
|
||||||
|
order.Price = price.Mul(decimal.NewFromInt(1).Add(orderExt.TpTpPriceRatio.Div(decimal.NewFromInt(100)))).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||||
|
} else {
|
||||||
|
order.Price = price.Mul(decimal.NewFromInt(1).Sub(orderExt.TpTpPriceRatio.Div(decimal.NewFromInt(100)))).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
processFutTakeProfitOrder(db, FutRestApi{}, order, num)
|
processFutTakeProfitOrder(db, FutRestApi{}, order, num)
|
||||||
case 2: // 止损
|
case 2: // 止损
|
||||||
|
if !first && orderExt.TpSlPriceRatio.Cmp(decimal.Zero) > 0 {
|
||||||
|
//止盈后止损
|
||||||
|
order.Rate = orderExt.TpSlPriceRatio.String()
|
||||||
|
|
||||||
|
if positionData.PositionSide == "LONG" {
|
||||||
|
order.Price = price.Mul(decimal.NewFromInt(1).Sub(orderExt.TpSlPriceRatio.Div(decimal.NewFromInt(100)))).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||||
|
} else {
|
||||||
|
order.Price = price.Mul(decimal.NewFromInt(1).Add(orderExt.TpSlPriceRatio.Div(decimal.NewFromInt(100)))).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
processFutStopLossOrder(db, order, price, num)
|
processFutStopLossOrder(db, order, price, num)
|
||||||
case 4: // 减仓
|
case 4: // 减仓
|
||||||
processFutReduceOrder(order, price, num)
|
processFutReduceOrder(order, price, num)
|
||||||
@ -541,20 +569,19 @@ func getStopOrders(db *gorm.DB, preOrder *models.LinePreOrder) ([]models.LinePre
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 更新订单数量
|
// 更新订单数量
|
||||||
func updateOrderQuantity(db *gorm.DB, order models.LinePreOrder, preOrder *models.LinePreOrder, num decimal.Decimal, tradeSet models2.TradeSet) decimal.Decimal {
|
func updateOrderQuantity(db *gorm.DB, order models.LinePreOrder, preOrder *models.LinePreOrder, ext *models.LinePreOrderExt, num decimal.Decimal, first bool, tradeSet models2.TradeSet) decimal.Decimal {
|
||||||
// 处理减仓比例
|
// 处理减仓比例
|
||||||
if order.OrderType == 4 {
|
// if order.OrderType == 4 && ext.ReduceNumRatio.Cmp(decimal.Zero) > 0 {
|
||||||
ext := DbModels.LinePreOrderExt{}
|
// // 计算减仓数量
|
||||||
if err := db.Model(&ext).Where("order_id=?", preOrder.Id).Find(&ext).Error; err != nil {
|
// num = num.Mul(ext.ReduceNumRatio.Div(decimal.NewFromInt(100))).Truncate(int32(tradeSet.AmountDigit))
|
||||||
logger.Errorf("查询减仓比例失败, 订单号:%s, 错误信息: %v", order.OrderSn, err)
|
// order.Num = num.String()
|
||||||
}
|
// } else
|
||||||
|
|
||||||
// 计算减仓数量
|
if first && order.OrderCategory == 1 && ext.TakeProfitNumRatio.Cmp(decimal.Zero) > 0 && ext.TakeProfitNumRatio.Cmp(decimal.NewFromInt(100)) != 0 {
|
||||||
if ext.ReduceNumRatio.Cmp(decimal.Zero) > 0 {
|
// 计算止盈数量
|
||||||
num = num.Mul(ext.ReduceNumRatio.Div(decimal.NewFromInt(100))).Truncate(int32(tradeSet.AmountDigit))
|
num = num.Mul(ext.TakeProfitNumRatio.Div(decimal.NewFromInt(100))).Truncate(int32(tradeSet.AmountDigit))
|
||||||
order.Num = num.String()
|
order.Num = num.String()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 更新订单数量
|
// 更新订单数量
|
||||||
if err := db.Model(&order).Update("num", num).Error; err != nil {
|
if err := db.Model(&order).Update("num", num).Error; err != nil {
|
||||||
@ -613,17 +640,17 @@ func processFutTakeProfitOrder(db *gorm.DB, futApi FutRestApi, order models.Line
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("合约止盈下单失败:", order.OrderSn, " err:", err)
|
logger.Error("合约止盈下单失败:", order.OrderSn, " err:", err)
|
||||||
if err := db.Model(&DbModels.LinePreOrder{}).Where("id = ?", order.Id).
|
if err := db.Model(&DbModels.LinePreOrder{}).Where("id = ?", order.Id).
|
||||||
Updates(map[string]interface{}{"status": "2", "desc": err.Error(), "num": params.Quantity}).Error; err != nil {
|
Updates(map[string]interface{}{"status": "2", "desc": err.Error(), "num": params.Quantity, "rate": order.Rate, "price": order.Price}).Error; err != nil {
|
||||||
logger.Error("合约止盈下单失败,更新状态失败:", order.OrderSn, " err:", err)
|
logger.Error("合约止盈下单失败,更新状态失败:", order.OrderSn, " err:", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := db.Model(&DbModels.LinePreOrder{}).Where("id =? ", order.Id).
|
if err := db.Model(&DbModels.LinePreOrder{}).Where("id =? ", order.Id).
|
||||||
Updates(map[string]interface{}{"trigger_time": time.Now(), "rate": order.Rate}).Error; err != nil {
|
Updates(map[string]interface{}{"trigger_time": time.Now(), "rate": order.Rate, "num": num.String(), "price": order.Price}).Error; err != nil {
|
||||||
logger.Error("更新合约止盈单触发事件 ordersn:", order.OrderSn)
|
logger.Error("更新合约止盈单触发事件 ordersn:", order.OrderSn)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.Model(&DbModels.LinePreOrder{}).Where("id = ? and status =0", order.Id).
|
if err := db.Model(&DbModels.LinePreOrder{}).Where("id = ? and status =0", order.Id).
|
||||||
Updates(map[string]interface{}{"status": "1", "num": num.String(), "price": order.Price}).Error; err != nil {
|
Updates(map[string]interface{}{"status": "1"}).Error; err != nil {
|
||||||
logger.Error("合约止盈下单成功,更新状态失败:", order.OrderSn, " err:", err)
|
logger.Error("合约止盈下单成功,更新状态失败:", order.OrderSn, " err:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -159,3 +159,14 @@ func GetSymbolTriggerCount(db *gorm.DB, symbol string, apiId, symbolType int) (i
|
|||||||
// func GetOpenedOrders(db *gorm.DB, apiId int, exchange, symbol, symbolType, side string) ([]models.LinePreOrder, error) {
|
// func GetOpenedOrders(db *gorm.DB, apiId int, exchange, symbol, symbolType, side string) ([]models.LinePreOrder, error) {
|
||||||
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// 获取子订单止盈止损数量
|
||||||
|
func GetChildTpOrder(db *gorm.DB, pid int) (int, error) {
|
||||||
|
var count int64
|
||||||
|
|
||||||
|
if err := db.Model(&models.LinePreOrder{}).Where("pid =? AND order_type>0 and order_type <3 and status =0", pid).Count(&count).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return int(count), nil
|
||||||
|
}
|
||||||
|
|||||||
@ -404,6 +404,13 @@ func handleMainOrderClosePosition(db *gorm.DB, preOrder *DbModels.LinePreOrder)
|
|||||||
|
|
||||||
// 止盈成交
|
// 止盈成交
|
||||||
func handleSpotTakeProfitFilled(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
|
func handleSpotTakeProfitFilled(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
|
||||||
|
childCount, _ := GetChildTpOrder(db, preOrder.Id)
|
||||||
|
|
||||||
|
if childCount > 0 {
|
||||||
|
extOrderId := preOrder.Pid //ext主单id
|
||||||
|
positionData := savePosition(db, preOrder)
|
||||||
|
processTakeProfitAndStopLossOrders(db, preOrder, &positionData, extOrderId, false)
|
||||||
|
} else {
|
||||||
removeSpotLossAndAddPosition(preOrder.MainId, preOrder.OrderSn)
|
removeSpotLossAndAddPosition(preOrder.MainId, preOrder.OrderSn)
|
||||||
|
|
||||||
spotApi := SpotRestApi{}
|
spotApi := SpotRestApi{}
|
||||||
@ -429,15 +436,9 @@ func handleSpotTakeProfitFilled(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// if err := db.Model(&DbModels.LinePreOrder{}).Where("main_id =? AND status=0",).Update("status", 4).Error; err != nil {
|
|
||||||
// logger.Errorf("止盈订单回调失败, 回调订单号:%s 更新取消状态失败:%v", preOrder.OrderSn, err)
|
|
||||||
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 移除仓位信息
|
// 移除仓位信息
|
||||||
@ -550,7 +551,7 @@ func handleMainOrderFilled(db *gorm.DB, preOrder *DbModels.LinePreOrder) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
processTakeProfitAndStopLossOrders(db, preOrder, &positionData)
|
processTakeProfitAndStopLossOrders(db, preOrder, &positionData, preOrder.Id, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析订单状态
|
// 解析订单状态
|
||||||
@ -618,7 +619,8 @@ func updateOrderStatus(db *gorm.DB, preOrder *models.LinePreOrder, status int, r
|
|||||||
// 主单成交 处理止盈止损订单
|
// 主单成交 处理止盈止损订单
|
||||||
// preOrder 主单
|
// preOrder 主单
|
||||||
// positionData 持仓缓存信息
|
// positionData 持仓缓存信息
|
||||||
func processTakeProfitAndStopLossOrders(db *gorm.DB, preOrder *models.LinePreOrder, positionData *positiondto.PositionDto) {
|
// fist 首次止盈止损
|
||||||
|
func processTakeProfitAndStopLossOrders(db *gorm.DB, preOrder *models.LinePreOrder, positionData *positiondto.PositionDto, extOrderId int, fist bool) {
|
||||||
orders := []models.LinePreOrder{}
|
orders := []models.LinePreOrder{}
|
||||||
tradeSet, _ := GetTradeSet(preOrder.Symbol, 0)
|
tradeSet, _ := GetTradeSet(preOrder.Symbol, 0)
|
||||||
|
|
||||||
@ -651,18 +653,14 @@ func processTakeProfitAndStopLossOrders(db *gorm.DB, preOrder *models.LinePreOrd
|
|||||||
price := utility.StrToDecimal(preOrder.Price)
|
price := utility.StrToDecimal(preOrder.Price)
|
||||||
spotApi := SpotRestApi{}
|
spotApi := SpotRestApi{}
|
||||||
orderExt := models.LinePreOrderExt{}
|
orderExt := models.LinePreOrderExt{}
|
||||||
db.Model(&orderExt).Where("order_id =?", preOrder.Id).First(&orderExt)
|
db.Model(&orderExt).Where("order_id =?", extOrderId).First(&orderExt)
|
||||||
|
|
||||||
for _, order := range orders {
|
for _, order := range orders {
|
||||||
order.Num = num.Mul(decimal.NewFromFloat(0.998)).Truncate(int32(tradeSet.AmountDigit)).String()
|
order.Num = num.Mul(decimal.NewFromFloat(0.998)).Truncate(int32(tradeSet.AmountDigit)).String()
|
||||||
|
|
||||||
if order.OrderType == 4 {
|
if fist && order.OrderCategory == 1 && orderExt.TakeProfitNumRatio.Cmp(decimal.Zero) > 0 && orderExt.TakeProfitNumRatio.Cmp(decimal.NewFromInt(100)) != 0 {
|
||||||
ext := DbModels.LinePreOrderExt{}
|
//主单第一次且止盈数量不是100% 止盈数量
|
||||||
db.Model(&ext).Where("order_id=?", preOrder.Id).Find(&ext)
|
order.Num = num.Mul(orderExt.TakeProfitNumRatio.Div(decimal.NewFromInt(100))).Truncate(int32(tradeSet.AmountDigit)).String()
|
||||||
|
|
||||||
if ext.ReduceNumRatio.Cmp(decimal.Zero) > 0 {
|
|
||||||
order.Num = num.Mul(ext.ReduceNumRatio.Div(decimal.NewFromInt(100))).Truncate(int32(tradeSet.AmountDigit)).String()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.Model(&order).Update("num", order.Num).Error; err != nil {
|
if err := db.Model(&order).Update("num", order.Num).Error; err != nil {
|
||||||
@ -672,16 +670,29 @@ func processTakeProfitAndStopLossOrders(db *gorm.DB, preOrder *models.LinePreOrd
|
|||||||
switch order.OrderType {
|
switch order.OrderType {
|
||||||
case 1: // 止盈
|
case 1: // 止盈
|
||||||
//亏损大于0 重新计算比例
|
//亏损大于0 重新计算比例
|
||||||
if positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 {
|
if fist && positionData.TotalLoss.Cmp(decimal.Zero) > 0 && orderExt.Id > 0 {
|
||||||
percentag := positionData.TotalLoss.Div(num).Div(price).Mul(decimal.NewFromInt(100))
|
percentag := positionData.TotalLoss.Div(num).Div(price).Mul(decimal.NewFromInt(100))
|
||||||
|
|
||||||
|
if fist {
|
||||||
percentag = percentag.Add(orderExt.TakeProfitRatio).Truncate(2)
|
percentag = percentag.Add(orderExt.TakeProfitRatio).Truncate(2)
|
||||||
|
}
|
||||||
|
|
||||||
order.Rate = percentag.String()
|
order.Rate = percentag.String()
|
||||||
percentag = percentag.Div(decimal.NewFromInt(100))
|
percentag = percentag.Div(decimal.NewFromInt(100))
|
||||||
order.Price = price.Mul(decimal.NewFromInt(1).Add(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
|
order.Price = price.Mul(decimal.NewFromInt(1).Add(percentag)).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||||
|
} else if orderExt.Id > 0 {
|
||||||
|
percentag := orderExt.TpTpPriceRatio
|
||||||
|
order.Rate = percentag.String()
|
||||||
|
order.Price = price.Mul(decimal.NewFromInt(1).Add(percentag.Div(decimal.NewFromInt(100)))).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||||
}
|
}
|
||||||
|
|
||||||
processTakeProfitOrder(db, spotApi, order)
|
processTakeProfitOrder(db, spotApi, order)
|
||||||
case 2: // 止损
|
case 2: // 止损
|
||||||
|
if !fist {
|
||||||
|
order.Rate = orderExt.TpSlPriceRatio.Truncate(2).String()
|
||||||
|
order.Price = price.Mul(decimal.NewFromInt(1).Sub(orderExt.TpSlPriceRatio.Div(decimal.NewFromInt(100)))).Truncate(int32(tradeSet.PriceDigit)).String()
|
||||||
|
}
|
||||||
|
|
||||||
processStopLossOrder(order)
|
processStopLossOrder(order)
|
||||||
case 4: //减仓
|
case 4: //减仓
|
||||||
processSpotReduceOrder(order)
|
processSpotReduceOrder(order)
|
||||||
@ -761,16 +772,16 @@ func processTakeProfitOrder(db *gorm.DB, spotApi SpotRestApi, order models.LineP
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("现货止盈下单失败:", order.OrderSn, " err:", err)
|
logger.Error("现货止盈下单失败:", order.OrderSn, " err:", err)
|
||||||
if err := db.Model(&DbModels.LinePreOrder{}).Where("id = ?", order.Id).
|
if err := db.Model(&DbModels.LinePreOrder{}).Where("id = ?", order.Id).
|
||||||
Updates(map[string]interface{}{"status": "2", "desc": err.Error(), "num": params.Quantity}).Error; err != nil {
|
Updates(map[string]interface{}{"status": "2", "desc": err.Error(), "num": params.Quantity, "rate": order.Rate, "price": order.Price}).Error; err != nil {
|
||||||
logger.Error("现货止盈下单失败,更新状态失败:", order.OrderSn, " err:", err)
|
logger.Error("现货止盈下单失败,更新状态失败:", order.OrderSn, " err:", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := db.Model(&DbModels.LinePreOrder{}).Where("id =? ", order.Id).
|
if err := db.Model(&DbModels.LinePreOrder{}).Where("id =? ", order.Id).
|
||||||
Updates(map[string]interface{}{"trigger_time": time.Now(), "rate": order.Rate}).Error; err != nil {
|
Updates(map[string]interface{}{"trigger_time": time.Now(), "rate": order.Rate, "price": order.Price, "num": order.Num}).Error; err != nil {
|
||||||
logger.Error("更新现货止盈单触发事件 ordersn:", order.OrderSn)
|
logger.Error("更新现货止盈单触发事件 ordersn:", order.OrderSn)
|
||||||
}
|
}
|
||||||
if err := db.Model(&DbModels.LinePreOrder{}).Where("id = ? and status ='0'", order.Id).
|
if err := db.Model(&DbModels.LinePreOrder{}).Where("id = ? and status ='0'", order.Id).
|
||||||
Updates(map[string]interface{}{"status": "1", "num": order.Num, "price": order.Price}).Error; err != nil {
|
Updates(map[string]interface{}{"status": "1"}).Error; err != nil {
|
||||||
logger.Error("现货止盈下单成功,更新状态失败:", order.OrderSn, " err:", err)
|
logger.Error("现货止盈下单成功,更新状态失败:", order.OrderSn, " err:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user