From f83998420f7b47145b77eca135f0b10972b3aa70 Mon Sep 17 00:00:00 2001 From: lychiyu Date: Mon, 29 Jul 2024 23:19:43 +0800 Subject: [PATCH] =?UTF-8?q?[add]=20=E6=96=B0=E5=A2=9E=E6=80=BB=E6=94=B6?= =?UTF-8?q?=E7=9B=8A=E7=9A=84=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/ccinr.yaml | 4 +- pkg/strategy/ccinr/strategy.go | 98 ++++++++++++++++++++++------------ 2 files changed, 66 insertions(+), 36 deletions(-) diff --git a/config/ccinr.yaml b/config/ccinr.yaml index b0d1a97..79115b9 100644 --- a/config/ccinr.yaml +++ b/config/ccinr.yaml @@ -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 diff --git a/pkg/strategy/ccinr/strategy.go b/pkg/strategy/ccinr/strategy.go index 18d927d..77975fb 100644 --- a/pkg/strategy/ccinr/strategy.go +++ b/pkg/strategy/ccinr/strategy.go @@ -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])