[add] 新增总收益的通知

This commit is contained in:
lychiyu 2024-07-29 23:19:43 +08:00
parent de167972ac
commit f83998420f
2 changed files with 66 additions and 36 deletions

View File

@ -44,10 +44,10 @@ exchangeStrategies:
longCCI: -150.0
shortCCI: 150.0
leverage: 5.0
profitRange: 2%
profitRange: 0.4%
lossRange: 1%
amount: 20
place: true
placePriceType: 0
# recalculate: false
# dry_run: false
# # quantity: 3

View File

@ -28,18 +28,18 @@ type Strategy struct {
markets map[string]types.Market
//Symbol string `json:"symbol"`
Symbols []string `json:"symbols"`
Interval types.Interval `json:"interval"`
NrCount int `json:"nrCount"`
StrictMode bool `json:"strictMode"`
Place bool `json:"place"`
DryRun bool `json:"dryRun"`
CCIWindow int `json:"cciWindow"`
LongCCI fixedpoint.Value `json:"longCCI"`
ShortCCI fixedpoint.Value `json:"shortCCI"`
Leverage fixedpoint.Value `json:"leverage"`
ProfitRange fixedpoint.Value `json:"profitRange"`
LossRange fixedpoint.Value `json:"lossRange"`
Symbols []string `json:"symbols"`
Interval types.Interval `json:"interval"`
NrCount int `json:"nrCount"`
StrictMode bool `json:"strictMode"`
PlacePriceType int `json:"placePriceType"`
DryRun bool `json:"dryRun"`
CCIWindow int `json:"cciWindow"`
LongCCI fixedpoint.Value `json:"longCCI"`
ShortCCI fixedpoint.Value `json:"shortCCI"`
Leverage fixedpoint.Value `json:"leverage"`
ProfitRange fixedpoint.Value `json:"profitRange"`
LossRange fixedpoint.Value `json:"lossRange"`
qbtrade.QuantityOrAmount
@ -73,6 +73,10 @@ type Strategy struct {
nr map[string]*indicatorv2.NRStrean
cci map[string]*indicatorv2.CCIStream
TotalProfit fixedpoint.Value
TotalFree fixedpoint.Value
TotalOrderCount int
}
func (s *Strategy) ID() string {
@ -143,33 +147,48 @@ func (s *Strategy) placeOrders(ctx context.Context, kline types.KLine) {
return
}
func (s *Strategy) getPlacePrice(ctx context.Context, kline types.KLine) fixedpoint.Value {
symbol := kline.Symbol
placePrice := fixedpoint.Value(0)
midPrice := (kline.High.Add(kline.Low)).Div(fixedpoint.Value(2.0))
switch s.PlacePriceType {
case 0:
if s.TradeType[symbol] == "long" {
placePrice = kline.High
} else if s.TradeType[symbol] == "short" {
placePrice = kline.Low
}
case 1:
if s.TradeType[symbol] == "long" {
placePrice = kline.Low
} else if s.TradeType[symbol] == "short" {
placePrice = kline.High
}
case 2:
if s.TradeType[symbol] == "long" {
placePrice = midPrice
} else if s.TradeType[symbol] == "short" {
placePrice = midPrice
}
}
return placePrice
}
func (s *Strategy) generateOrders(ctx context.Context, kline types.KLine) ([]types.SubmitOrder, error) {
var orders []types.SubmitOrder
symbol := kline.Symbol
log.Infof(fmt.Sprintf("place order keline info: symbol %s, high %v, low %v, open %v, close %v", symbol,
kline.High.Float64(), kline.Low.Float64(), kline.Open.Float64(), kline.Close.Float64()))
placePrice := fixedpoint.Value(0)
//midPrice := (kline.High.Add(kline.Low)).Div(fixedpoint.Value(2.0))
//midP := (kline.High + kline.Low) / 2
if s.TradeType[symbol] == "long" {
if s.Place {
placePrice = kline.High
} else {
placePrice = kline.Low
}
//placePrice = midP
} else if s.TradeType[symbol] == "short" {
if s.Place {
placePrice = kline.Low
} else {
placePrice = kline.High
}
//placePrice = midP
} else {
if s.TradeType[symbol] == "" {
return orders, nil
}
// 获取下单价格
placePrice := s.getPlacePrice(ctx, kline)
// 下单数量
placeQuantity := s.QuantityOrAmount.CalculateQuantity(placePrice).Mul(s.Leverage)
log.Infof(fmt.Sprintf("will place order, price %v, quantity %v", placePrice.Float64(),
@ -266,7 +285,7 @@ func (s *Strategy) notifyProfit(ctx context.Context, symbol string) {
if s.EndQuantity[symbol] != s.OpenQuantity[symbol] {
return
}
profit := 0.
profit := fixedpoint.Value(0)
openProfit := fixedpoint.Value(0)
endProfit := fixedpoint.Value(0)
free := fixedpoint.Value(0)
@ -292,16 +311,21 @@ func (s *Strategy) notifyProfit(ctx context.Context, symbol string) {
side := s.OpenTrade[symbol][0].Side
// 做多
if side == types.SideTypeBuy {
profit = (endProfit - openProfit - free).Float64()
profit = endProfit.Sub(openProfit).Sub(free)
}
// 做空
if side == types.SideTypeSell {
profit = (openProfit - endProfit - free).Float64()
profit = openProfit.Sub(endProfit).Sub(free)
}
msg := fmt.Sprintf("交易完成:\n 币种: %s, 方向:%v, 收益:%v, 手续费:%v \n Trade详情\n 开仓Trade\n %s\n 清仓Trade\n %s",
symbol, s.TradeType[symbol], profit, free.Float64(), strings.Join(openMsgs, "\n"), strings.Join(endMsgs, "\n"))
symbol, s.TradeType[symbol], profit.Float64(), free.Float64(), strings.Join(openMsgs, "\n"), strings.Join(endMsgs, "\n"))
s.TotalProfit = s.TotalProfit.Add(profit)
s.TotalFree = s.TotalFree.Add(free)
s.TotalOrderCount += 1
log.Infof(msg)
qbtrade.Notify(msg)
@ -313,6 +337,8 @@ func (s *Strategy) notifyProfit(ctx context.Context, symbol string) {
// 记得取消订单
s.cancelOrders(ctx, symbol)
qbtrade.Notify(fmt.Sprintf("总开仓:%v, 总收益:%v, 总手续费:%v", s.TotalOrderCount, s.TotalProfit.Float64(), s.TotalFree.Float64()))
}
func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor, session *qbtrade.ExchangeSession) error {
@ -341,6 +367,10 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor,
s.nr = make(map[string]*indicatorv2.NRStrean)
s.cci = make(map[string]*indicatorv2.CCIStream)
s.TotalProfit = fixedpoint.Value(0)
s.TotalFree = fixedpoint.Value(0)
s.TotalOrderCount = 0
for _, symbol := range s.Symbols {
s.Positions[symbol] = types.NewPositionFromMarket(s.markets[symbol])
s.ProfitStats[symbol] = types.NewProfitStats(s.markets[symbol])