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" "fmt"
"sync" "sync"
"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2" indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2"
"github.com/c9s/bbgo/pkg/strategy/common" "github.com/c9s/bbgo/pkg/strategy/common"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
) )
const ID = "autobuy" const ID = "autobuy"
@ -128,7 +129,7 @@ func (s *Strategy) autobuy(ctx context.Context) {
} }
side := types.SideTypeBuy side := types.SideTypeBuy
price := s.PriceType.Map(ticker, side) price := s.PriceType.GetPrice(ticker, side)
if price.Float64() > s.boll.UpBand.Last(0) { 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)) 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) quantity = market.RoundDownQuantityByPrecision(quantity)
price := s.PriceType.Map(ticker, side) price := s.PriceType.GetPrice(ticker, side)
if s.MaxAmount.Float64() > 0 { if s.MaxAmount.Float64() > 0 {
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, price, s.MaxAmount) quantity = bbgo.AdjustQuantityByMaxAmount(quantity, price, s.MaxAmount)

View File

@ -4,19 +4,37 @@ import (
"encoding/json" "encoding/json"
"strings" "strings"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/fixedpoint"
) )
type PriceType string type PriceType string
const ( const (
PriceTypeLast PriceType = "LAST" // PriceTypeLast uses the last price from the given ticker
PriceTypeBuy PriceType = "BUY" // BID PriceTypeLast PriceType = "LAST"
PriceTypeSell PriceType = "SELL" // ASK
PriceTypeMid PriceType = "MID" // 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" PriceTypeMaker PriceType = "MAKER"
PriceTypeTaker PriceType = "TAKER" 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") 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) { func ParsePriceType(s string) (p PriceType, err error) {
p = PriceType(strings.ToUpper(s)) p = PriceType(strings.ToUpper(s))
switch p { 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, err
} }
return p, ErrInvalidPriceType return p, ErrInvalidPriceType
@ -47,25 +68,26 @@ func (p *PriceType) UnmarshalJSON(data []byte) error {
return nil 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 price := ticker.Last
switch p { switch p {
case PriceTypeLast: case PriceTypeLast:
price = ticker.Last price = ticker.Last
case PriceTypeBuy: case PriceTypeBid:
price = ticker.Buy price = ticker.Buy
case PriceTypeSell: case PriceTypeAsk:
price = ticker.Sell price = ticker.Sell
case PriceTypeMid: case PriceTypeMid:
price = ticker.Buy.Add(ticker.Sell).Div(fixedpoint.NewFromInt(2)) price = ticker.Buy.Add(ticker.Sell).Div(fixedpoint.NewFromInt(2))
case PriceTypeMaker: case PriceTypeMaker, PriceTypeBestBidOfferQueue1:
if side == SideTypeBuy { if side == SideTypeBuy {
price = ticker.Buy price = ticker.Buy
} else if side == SideTypeSell { } else if side == SideTypeSell {
price = ticker.Sell price = ticker.Sell
} }
case PriceTypeTaker: case PriceTypeTaker, PriceTypeBestBidOfferCounterParty1:
if side == SideTypeBuy { if side == SideTypeBuy {
price = ticker.Sell price = ticker.Sell
} else if side == SideTypeSell { } else if side == SideTypeSell {