Skip to content

Commit

Permalink
take profit by expected base balance
Browse files Browse the repository at this point in the history
  • Loading branch information
narumiruna committed Jun 19, 2024
1 parent 6b6bf2f commit 60e8267
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions pkg/strategy/atrpin/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}
Expand Down Expand Up @@ -136,22 +147,28 @@ 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{
Symbol: s.Symbol,
Type: types.OrderTypeLimit,
Side: side,
Price: takerPrice,
Quantity: position.GetQuantity(),
Quantity: base,
Market: s.Market,
TimeInForce: types.TimeInForceGTC,
Tag: "takeProfit",
Expand Down

0 comments on commit 60e8267

Please sign in to comment.