1\交易对返回价格
This commit is contained in:
@ -602,3 +602,29 @@ func (e LinePreOrder) QueryAiCoinPrice(c *gin.Context) {
|
||||
}
|
||||
e.OK(res, "操作成功")
|
||||
}
|
||||
|
||||
// 计算回本盈利比例
|
||||
func (e LinePreOrder) CalculateBreakEevenRatio(c *gin.Context) {
|
||||
s := service.LinePreOrder{}
|
||||
req := dto.CalculateBreakEevenRatioReq{}
|
||||
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(&req).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
data := dto.CalculateBreakEvenRatioResp{}
|
||||
err = s.CalculateBreakEvenRatio(&req, &data)
|
||||
if err != nil {
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
e.OK(data, "操作成功")
|
||||
}
|
||||
|
||||
@ -2,19 +2,22 @@ package models
|
||||
|
||||
import (
|
||||
"go-admin/common/models"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type LineSymbol struct {
|
||||
models.Model
|
||||
|
||||
ExchangeType string `json:"exchangeType" gorm:"type:varchar(20);comment:交易所类型 字典 exchange_type"`
|
||||
ApiId string `json:"apiId" gorm:"type:int;comment:api账户id"`
|
||||
Symbol string `json:"symbol" gorm:"type:varchar(32);comment:交易对"`
|
||||
BaseAsset string `json:"baseAsset" gorm:"type:varchar(255);comment:基础货币"`
|
||||
QuoteAsset string `json:"quoteAsset" gorm:"type:varchar(255);comment:计价货币"`
|
||||
Switch string `json:"switch" gorm:"type:enum('0','1');comment:状态"`
|
||||
Type string `json:"type" gorm:"type:enum('1','2');comment:交易对类型"`
|
||||
Number int `json:"number" gorm:"->"`
|
||||
ExchangeType string `json:"exchangeType" gorm:"type:varchar(20);comment:交易所类型 字典 exchange_type"`
|
||||
ApiId string `json:"apiId" gorm:"type:int;comment:api账户id"`
|
||||
Symbol string `json:"symbol" gorm:"type:varchar(32);comment:交易对"`
|
||||
BaseAsset string `json:"baseAsset" gorm:"type:varchar(255);comment:基础货币"`
|
||||
QuoteAsset string `json:"quoteAsset" gorm:"type:varchar(255);comment:计价货币"`
|
||||
Switch string `json:"switch" gorm:"type:enum('0','1');comment:状态"`
|
||||
Type string `json:"type" gorm:"type:enum('1','2');comment:交易对类型"`
|
||||
Number int `json:"number" gorm:"->"`
|
||||
LastPrice decimal.Decimal `json:"lastPrice"gorm:"->"`
|
||||
models.ModelTime
|
||||
models.ControlBy
|
||||
}
|
||||
|
||||
@ -37,6 +37,8 @@ func registerLinePreOrderRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTM
|
||||
r.GET("getOrderPage", actions.PermissionAction(), api.GetOrderPage) //订单列表
|
||||
r.POST("clearUnTriggered", actions.PermissionAction(), api.ClearUnTriggered) // 清除待触发的交易对
|
||||
r.POST("aiCoinPrice", actions.PermissionAction(), api.QueryAiCoinPrice) //获取aiCoin买入点
|
||||
|
||||
r.GET("/calculate", api.CalculateBreakEevenRatio) //计算亏损后止盈百分比
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -446,6 +446,29 @@ type QueryAiCoinPriceReq struct {
|
||||
Symbol string `json:"symbol"` //交易对
|
||||
}
|
||||
|
||||
// 计算亏损止盈百分比
|
||||
type CalculateBreakEevenRatioReq struct {
|
||||
Price decimal.Decimal `form:"price"`
|
||||
Symbol string `form:"symbol"` //交易对
|
||||
ExchangeType string `form:"exchangeType"` //交易所类型 字典exchange_type
|
||||
SymbolType int `form:"symbolType"`
|
||||
BuyPrice decimal.Decimal `form:"buyPrice"` //主单购买总金额
|
||||
LossBeginPercent decimal.Decimal `form:"lossBeginPercent"` //亏损开始百分比
|
||||
LossEndPercent decimal.Decimal `form:"lossEndPercent"` //亏损截至百分比
|
||||
AddPositionType int `form:"addPositionType"` //加仓类型 1-百分比 2-实际金额
|
||||
AddPositionVal decimal.Decimal `form:"addPositionVal"` //加仓金额
|
||||
ReducePercent decimal.Decimal `form:"reducePercent"` //减仓百分比
|
||||
RemainingQuantity decimal.Decimal `form:"remainingQuantity"` //剩余数量
|
||||
TotalLossAmountU decimal.Decimal `form:"totalLossAmountU"` //累计亏损金额
|
||||
}
|
||||
|
||||
// 计算亏损返回值
|
||||
type CalculateBreakEvenRatioResp struct {
|
||||
RemainingQuantity decimal.Decimal `json:"remainingQuantity"` //剩余数量
|
||||
Ratio decimal.Decimal `json:"ratio"` //亏损止盈百分比
|
||||
TotalLossAmountU decimal.Decimal `json:"totalLossAmountU"` //总亏损金额
|
||||
}
|
||||
|
||||
type SpotQueryOrderResp struct {
|
||||
Symbol string `json:"symbol"`
|
||||
OrderId int `json:"orderId"`
|
||||
|
||||
@ -1316,3 +1316,65 @@ func (e *LinePreOrder) QueryAiCoinPrice(req *dto.QueryAiCoinPriceReq) (models.Li
|
||||
err := e.Orm.Model(&models.LineDirection{}).Where("symbol = ?", req.Symbol).Find(&info).Error
|
||||
return info, err
|
||||
}
|
||||
|
||||
// 计算亏损百分比
|
||||
func (e *LinePreOrder) CalculateBreakEvenRatio(req *dto.CalculateBreakEevenRatioReq, data *dto.CalculateBreakEvenRatioResp) error {
|
||||
var tradeSet models2.TradeSet
|
||||
|
||||
if req.LossEndPercent.Cmp(req.LossBeginPercent) < 0 {
|
||||
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
|
||||
|
||||
if req.AddPositionType == 1 {
|
||||
addPositionBuyPrice = req.BuyPrice.Mul(req.AddPositionVal.Div(decimal.NewFromInt(100).Truncate(4))).Truncate(2)
|
||||
} else {
|
||||
addPositionBuyPrice = req.BuyPrice.Add(req.AddPositionVal).Truncate(2)
|
||||
}
|
||||
|
||||
var percentDiff decimal.Decimal
|
||||
var reduceAmount decimal.Decimal
|
||||
nowPrice := req.Price.Mul(decimal.NewFromInt(1).Sub(req.LossEndPercent.Div(decimal.NewFromInt(100).Truncate(4))))
|
||||
addPositionAmount := addPositionBuyPrice.Div(nowPrice).Truncate(int32(tradeSet.AmountDigit))
|
||||
|
||||
//计算价格下跌价差
|
||||
if req.LossEndPercent.Cmp(req.LossBeginPercent) > 0 {
|
||||
percentDiff = req.LossEndPercent.Sub(req.LossBeginPercent).Abs()
|
||||
}
|
||||
|
||||
totalAmount := req.RemainingQuantity.Add(addPositionAmount)
|
||||
lossAmountU := req.Price.Mul(percentDiff.Div(decimal.NewFromInt(100)).Truncate(4)).Mul(req.RemainingQuantity).Truncate(int32(tradeSet.AmountDigit))
|
||||
|
||||
//计算减仓数量
|
||||
if req.ReducePercent.Cmp(decimal.NewFromInt(0)) > 0 {
|
||||
reduceAmount = totalAmount.Mul(req.ReducePercent.Div(decimal.NewFromInt(100).Truncate(4))).Truncate(int32(tradeSet.AmountDigit))
|
||||
}
|
||||
|
||||
data.RemainingQuantity = totalAmount.Sub(reduceAmount)
|
||||
data.TotalLossAmountU = lossAmountU.Add(req.TotalLossAmountU)
|
||||
|
||||
//计算百分比
|
||||
if data.RemainingQuantity.Cmp(decimal.Zero) > 0 {
|
||||
data.Ratio = data.TotalLossAmountU.Div(data.RemainingQuantity).Div(nowPrice).Mul(decimal.NewFromInt(100)).Truncate(2)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
@ -44,6 +44,35 @@ func (e *LineSymbol) GetPage(c *dto.LineSymbolGetPageReq, p *actions.DataPermiss
|
||||
e.Log.Errorf("LineSymbolService GetPage error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
var key string
|
||||
|
||||
if c.Type == "1" {
|
||||
key = fmt.Sprintf(global.TICKER_SPOT, c.ExchangeType, "*")
|
||||
} else {
|
||||
key = fmt.Sprintf(global.TICKER_FUTURES, c.ExchangeType, "*")
|
||||
}
|
||||
|
||||
tickers, _ := helper.DefaultRedis.GetAllKeysAndValues(key)
|
||||
allTicker := make(map[string]commonModels.TradeSet)
|
||||
tradeSet := commonModels.TradeSet{}
|
||||
|
||||
for _, v := range tickers {
|
||||
sonic.Unmarshal([]byte(v), &tradeSet)
|
||||
|
||||
if tradeSet.Coin != "" && tradeSet.Currency != "" {
|
||||
allTicker[tradeSet.Coin+tradeSet.Currency] = tradeSet
|
||||
}
|
||||
}
|
||||
|
||||
for index := range *list {
|
||||
symbol := (*list)[index].Symbol
|
||||
|
||||
if v, ok := allTicker[symbol]; ok {
|
||||
(*list)[index].LastPrice = utility.StrToDecimal(v.LastPrice)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user