Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR: Make fixedmaker simpler #1330

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions config/fixedmaker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,9 @@
exchangeStrategies:
- on: max
fixedmaker:
interval: 5m
symbol: BTCUSDT
halfSpreadRatio: 0.05%
interval: 5m
halfSpread: 0.05%
quantity: 0.005
dryRun: false

- on: max
rebalance:
interval: 1h
quoteCurrency: USDT
targetWeights:
BTC: 50%
USDT: 50%
threshold: 2%
maxAmount: 200 # max amount to buy or sell per order
orderType: LIMIT_MAKER # LIMIT, LIMIT_MAKER or MARKET
dryRun: false
orderType: LIMIT_MAKER
dryRun: true
3 changes: 3 additions & 0 deletions pkg/strategy/common/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,8 @@ func (s *Strategy) Initialize(ctx context.Context, environ *bbgo.Environment, se
}

func (s *Strategy) IsHalted(t time.Time) bool {
if s.circuitBreakRiskControl == nil {
return false
}
return s.circuitBreakRiskControl.IsHalted(t)
}
95 changes: 19 additions & 76 deletions pkg/strategy/fixedmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"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"
)
Expand All @@ -27,39 +26,24 @@ func init() {
type Strategy struct {
*common.Strategy

Environment *bbgo.Environment
StandardIndicatorSet *bbgo.StandardIndicatorSet
Market types.Market
Environment *bbgo.Environment
Market types.Market

Interval types.Interval `json:"interval"`
Symbol string `json:"symbol"`
Quantity fixedpoint.Value `json:"quantity"`
HalfSpreadRatio fixedpoint.Value `json:"halfSpreadRatio"`
OrderType types.OrderType `json:"orderType"`
DryRun bool `json:"dryRun"`

// SkewFactor is used to calculate the skew of bid/ask price
SkewFactor fixedpoint.Value `json:"skewFactor"`
TargetWeight fixedpoint.Value `json:"targetWeight"`

// replace halfSpreadRatio by ATR
ATRMultiplier fixedpoint.Value `json:"atrMultiplier"`
ATRWindow int `json:"atrWindow"`
Symbol string `json:"symbol"`
Interval types.Interval `json:"interval"`
Quantity fixedpoint.Value `json:"quantity"`
HalfSpread fixedpoint.Value `json:"halfSpread"`
OrderType types.OrderType `json:"orderType"`
DryRun bool `json:"dryRun"`

activeOrderBook *bbgo.ActiveOrderBook
atr *indicatorv2.ATRStream
}

func (s *Strategy) Defaults() error {
if s.OrderType == "" {
log.Infof("order type is not set, using limit maker order type")
s.OrderType = types.OrderTypeLimitMaker
}

if s.ATRWindow == 0 {
log.Infof("atr window is not set, using default value 14")
s.ATRWindow = 14
}
return nil
}
func (s *Strategy) Initialize() error {
Expand All @@ -79,21 +63,9 @@ func (s *Strategy) Validate() error {
return fmt.Errorf("quantity should be positive")
}

if s.HalfSpreadRatio.Float64() <= 0 {
if s.HalfSpread.Float64() <= 0 {
return fmt.Errorf("halfSpreadRatio should be positive")
}

if s.SkewFactor.Float64() < 0 {
return fmt.Errorf("skewFactor should be non-negative")
}

if s.ATRMultiplier.Float64() < 0 {
return fmt.Errorf("atrMultiplier should be non-negative")
}

if s.ATRWindow < 0 {
return fmt.Errorf("atrWindow should be non-negative")
}
return nil
}

Expand All @@ -108,12 +80,6 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
s.activeOrderBook = bbgo.NewActiveOrderBook(s.Symbol)
s.activeOrderBook.BindStream(session.UserDataStream)

s.atr = session.Indicators(s.Symbol).ATR(s.Interval, s.ATRWindow)

session.UserDataStream.OnStart(func() {
// you can place orders here when bbgo is started, this will be called only once.
})

s.activeOrderBook.OnFilled(func(order types.Order) {
if s.activeOrderBook.NumOfOrders() == 0 {
log.Infof("no active orders, replenish")
Expand All @@ -122,9 +88,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
})

session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
log.Infof("%+v", kline)

s.cancelOrders(ctx)
log.Infof("%s", kline.String())
s.replenish(ctx, kline.EndTime.Time())
})

Expand All @@ -133,17 +97,14 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
defer wg.Done()
_ = s.OrderExecutor.GracefulCancel(ctx)
})

return nil
}

func (s *Strategy) cancelOrders(ctx context.Context) {
func (s *Strategy) replenish(ctx context.Context, t time.Time) {
if err := s.Session.Exchange.CancelOrders(ctx, s.activeOrderBook.Orders()...); err != nil {
log.WithError(err).Errorf("failed to cancel orders")
}
}

func (s *Strategy) replenish(ctx context.Context, t time.Time) {
if s.IsHalted(t) {
log.Infof("circuit break halted, not replenishing")
return
Expand Down Expand Up @@ -193,39 +154,21 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrde
midPrice := ticker.Buy.Add(ticker.Sell).Div(fixedpoint.NewFromFloat(2.0))
log.Infof("mid price: %+v", midPrice)

if s.ATRMultiplier.Float64() > 0 {
atr := fixedpoint.NewFromFloat(s.atr.Last(0))
log.Infof("atr: %s", atr.String())
s.HalfSpreadRatio = s.ATRMultiplier.Mul(atr).Div(midPrice)
log.Infof("half spread ratio: %s", s.HalfSpreadRatio.String())
}

// calcualte skew by the difference between base weight and target weight
baseValue := baseBalance.Total().Mul(midPrice)
baseWeight := baseValue.Div(baseValue.Add(quoteBalance.Total()))
skew := s.SkewFactor.Mul(s.HalfSpreadRatio).Mul(baseWeight.Sub(s.TargetWeight))

// let the skew be in the range of [-r, r]
skew = skew.Clamp(s.HalfSpreadRatio.Neg(), s.HalfSpreadRatio)

// calculate bid and ask price
// bid price = mid price * (1 - r - skew))
bidSpreadRatio := fixedpoint.Max(s.HalfSpreadRatio.Add(skew), fixedpoint.Zero)
bidPrice := midPrice.Mul(fixedpoint.One.Sub(bidSpreadRatio))
log.Infof("bid price: %s", bidPrice.String())
// ask price = mid price * (1 + r - skew))
askSrasedRatio := fixedpoint.Max(s.HalfSpreadRatio.Sub(skew), fixedpoint.Zero)
askPrice := midPrice.Mul(fixedpoint.One.Add(askSrasedRatio))
log.Infof("ask price: %s", askPrice.String())
// sell price = mid price * (1 + r))
// buy price = mid price * (1 - r))
sellPrice := midPrice.Mul(fixedpoint.One.Add(s.HalfSpread)).Round(s.Market.PricePrecision, fixedpoint.Up)
buyPrice := midPrice.Mul(fixedpoint.One.Sub(s.HalfSpread)).Round(s.Market.PricePrecision, fixedpoint.Down)
log.Infof("sell price: %s, buy price: %s", sellPrice.String(), buyPrice.String())

// check balance and generate orders
amount := s.Quantity.Mul(bidPrice)
amount := s.Quantity.Mul(buyPrice)
if quoteBalance.Available.Compare(amount) > 0 {
orders = append(orders, types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeBuy,
Type: s.OrderType,
Price: bidPrice,
Price: buyPrice,
Quantity: s.Quantity,
})
} else {
Expand All @@ -237,7 +180,7 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrde
Symbol: s.Symbol,
Side: types.SideTypeSell,
Type: s.OrderType,
Price: askPrice,
Price: sellPrice,
Quantity: s.Quantity,
})
} else {
Expand Down