From 60e8267b1769260bb4ccd102fdbdb3378c94cb81 Mon Sep 17 00:00:00 2001 From: narumi Date: Sun, 2 Jun 2024 14:49:20 +0800 Subject: [PATCH] take profit by expected base balance --- pkg/strategy/atrpin/strategy.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/pkg/strategy/atrpin/strategy.go b/pkg/strategy/atrpin/strategy.go index 15b5c2c7d..408ad4d24 100644 --- a/pkg/strategy/atrpin/strategy.go +++ b/pkg/strategy/atrpin/strategy.go @@ -33,6 +33,9 @@ type Strategy struct { Multiplier float64 `json:"multiplier"` MinPriceRange fixedpoint.Value `json:"minPriceRange"` + TakeProfitByExpectedBaseBalance bool `json:"takeProfitByExpectedBaseBalance"` + ExpectedBaseBalance fixedpoint.Value `json:"expectedBaseBalance"` + bbgo.QuantityOrAmount // bbgo.OpenPositionOptions @@ -50,6 +53,14 @@ func (s *Strategy) Initialize() error { }) return nil } + +func (s *Strategy) Validate() error { + if s.ExpectedBaseBalance.Sign() < 0 { + return fmt.Errorf("expectedBaseBalance should be non-negative") + } + return nil +} + func (s *Strategy) ID() string { return ID } @@ -136,14 +147,20 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se position := s.Strategy.OrderExecutor.Position() s.logger.Infof("position: %+v", position) - side := types.SideTypeBuy - takerPrice := ticker.Sell - if position.IsLong() { - side = types.SideTypeSell - takerPrice = ticker.Buy + base := position.GetBase() + if s.TakeProfitByExpectedBaseBalance { + base = baseBalance.Available.Sub(s.ExpectedBaseBalance) + } + + side := types.SideTypeSell + takerPrice := ticker.Buy + if base.Sign() < 0 { + side = types.SideTypeBuy + takerPrice = ticker.Sell + base = base.Abs() } - if !position.IsDust(takerPrice) { + if !s.Market.IsDustQuantity(base, takerPrice) { s.logger.Infof("%s position is not dust", s.Symbol) orderForms = append(orderForms, types.SubmitOrder{ @@ -151,7 +168,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se Type: types.OrderTypeLimit, Side: side, Price: takerPrice, - Quantity: position.GetQuantity(), + Quantity: base, Market: s.Market, TimeInForce: types.TimeInForceGTC, Tag: "takeProfit",