types,strategy: refactor price type and add more bbo (best bid offer)

This commit is contained in:
c9s 2024-08-17 14:05:29 +08:00
parent 621a2b86cf
commit 9dd85623b9
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 38 additions and 15 deletions

View File

@ -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))

View File

@ -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)

View File

@ -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 {