Skip to content

Commit

Permalink
types,strategy: refactor price type and add more bbo (best bid offer)
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Aug 17, 2024
1 parent 621a2b8 commit 9dd8562
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
7 changes: 4 additions & 3 deletions pkg/strategy/autobuy/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/rebalance/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 33 additions & 11 deletions pkg/types/price_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,48 @@ 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")

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
Expand All @@ -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 {
Expand Down

0 comments on commit 9dd8562

Please sign in to comment.