From 9dd85623b99f3b27502da87de94df8b3c143093f Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 17 Aug 2024 14:05:29 +0800 Subject: [PATCH] types,strategy: refactor price type and add more bbo (best bid offer) --- pkg/strategy/autobuy/strategy.go | 7 +++-- pkg/strategy/rebalance/strategy.go | 2 +- pkg/types/price_type.go | 44 ++++++++++++++++++++++-------- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/pkg/strategy/autobuy/strategy.go b/pkg/strategy/autobuy/strategy.go index 13a143161..aee4f0f3b 100644 --- a/pkg/strategy/autobuy/strategy.go +++ b/pkg/strategy/autobuy/strategy.go @@ -5,13 +5,14 @@ import ( "fmt" "sync" + "github.com/robfig/cron/v3" + "github.com/sirupsen/logrus" + "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2" "github.com/c9s/bbgo/pkg/strategy/common" "github.com/c9s/bbgo/pkg/types" - "github.com/robfig/cron/v3" - "github.com/sirupsen/logrus" ) const ID = "autobuy" @@ -128,7 +129,7 @@ func (s *Strategy) autobuy(ctx context.Context) { } side := types.SideTypeBuy - price := s.PriceType.Map(ticker, side) + price := s.PriceType.GetPrice(ticker, side) if price.Float64() > s.boll.UpBand.Last(0) { log.Infof("price %s is higher than upper band %f, skip", price.String(), s.boll.UpBand.Last(0)) diff --git a/pkg/strategy/rebalance/strategy.go b/pkg/strategy/rebalance/strategy.go index eab1de87c..6b4009063 100644 --- a/pkg/strategy/rebalance/strategy.go +++ b/pkg/strategy/rebalance/strategy.go @@ -262,7 +262,7 @@ func (s *Strategy) generateOrder(ctx context.Context) (*types.SubmitOrder, error } quantity = market.RoundDownQuantityByPrecision(quantity) - price := s.PriceType.Map(ticker, side) + price := s.PriceType.GetPrice(ticker, side) if s.MaxAmount.Float64() > 0 { quantity = bbgo.AdjustQuantityByMaxAmount(quantity, price, s.MaxAmount) diff --git a/pkg/types/price_type.go b/pkg/types/price_type.go index 93bc269e6..55db880a8 100644 --- a/pkg/types/price_type.go +++ b/pkg/types/price_type.go @@ -4,19 +4,37 @@ import ( "encoding/json" "strings" - "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/pkg/errors" + + "github.com/c9s/bbgo/pkg/fixedpoint" ) type PriceType string const ( - PriceTypeLast PriceType = "LAST" - PriceTypeBuy PriceType = "BUY" // BID - PriceTypeSell PriceType = "SELL" // ASK - PriceTypeMid PriceType = "MID" + // PriceTypeLast uses the last price from the given ticker + PriceTypeLast PriceType = "LAST" + + // PriceTypeBid uses the bid price from the given ticker + PriceTypeBid PriceType = "BID" + + // PriceTypeAsk uses the ask price from the given ticker + PriceTypeAsk PriceType = "ASK" + + // PriceTypeMid calculates the middle price from the given ticker + PriceTypeMid PriceType = "MID" + PriceTypeMaker PriceType = "MAKER" PriceTypeTaker PriceType = "TAKER" + + // See best bid offer types + // https://www.binance.com/en/support/faq/understanding-and-using-bbo-orders-on-binance-futures-7f93c89ef09042678cfa73e8a28612e8 + + PriceTypeBestBidOfferCounterParty1 PriceType = "COUNTERPARTY1" + PriceTypeBestBidOfferCounterParty5 PriceType = "COUNTERPARTY5" + + PriceTypeBestBidOfferQueue1 PriceType = "QUEUE1" + PriceTypeBestBidOfferQueue5 PriceType = "QUEUE5" ) var ErrInvalidPriceType = errors.New("invalid price type") @@ -24,7 +42,10 @@ var ErrInvalidPriceType = errors.New("invalid price type") func ParsePriceType(s string) (p PriceType, err error) { p = PriceType(strings.ToUpper(s)) switch p { - case PriceTypeLast, PriceTypeBuy, PriceTypeSell, PriceTypeMid, PriceTypeMaker, PriceTypeTaker: + case PriceTypeLast, PriceTypeBid, PriceTypeAsk, + PriceTypeMid, PriceTypeMaker, PriceTypeTaker, + PriceTypeBestBidOfferCounterParty1, PriceTypeBestBidOfferCounterParty5, + PriceTypeBestBidOfferQueue1, PriceTypeBestBidOfferQueue5: return p, err } return p, ErrInvalidPriceType @@ -47,25 +68,26 @@ func (p *PriceType) UnmarshalJSON(data []byte) error { return nil } -func (p PriceType) Map(ticker *Ticker, side SideType) fixedpoint.Value { +// GetPrice returns the price from the given ticker based on the price type +func (p PriceType) GetPrice(ticker *Ticker, side SideType) fixedpoint.Value { price := ticker.Last switch p { case PriceTypeLast: price = ticker.Last - case PriceTypeBuy: + case PriceTypeBid: price = ticker.Buy - case PriceTypeSell: + case PriceTypeAsk: price = ticker.Sell case PriceTypeMid: price = ticker.Buy.Add(ticker.Sell).Div(fixedpoint.NewFromInt(2)) - case PriceTypeMaker: + case PriceTypeMaker, PriceTypeBestBidOfferQueue1: if side == SideTypeBuy { price = ticker.Buy } else if side == SideTypeSell { price = ticker.Sell } - case PriceTypeTaker: + case PriceTypeTaker, PriceTypeBestBidOfferCounterParty1: if side == SideTypeBuy { price = ticker.Sell } else if side == SideTypeSell {