Merge pull request #1113 from c9s/narumi/fixedmaker/skew

strategy: fixedmaker: add skew to adjust bid/ask price
This commit is contained in:
なるみ 2023-03-16 02:39:02 +08:00 committed by GitHub
commit 52b2ffebd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,6 +33,10 @@ type Strategy struct {
OrderType types.OrderType `json:"orderType"` OrderType types.OrderType `json:"orderType"`
DryRun bool `json:"dryRun"` 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"`
// persistence fields // persistence fields
Position *types.Position `json:"position,omitempty" persistence:"position"` Position *types.Position `json:"position,omitempty" persistence:"position"`
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"` ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
@ -162,6 +166,8 @@ func (s *Strategy) replenish(ctx context.Context) {
} }
func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrder, error) { func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrder, error) {
orders := []types.SubmitOrder{}
baseBalance, ok := s.session.GetAccount().Balance(s.Market.BaseCurrency) baseBalance, ok := s.session.GetAccount().Balance(s.Market.BaseCurrency)
if !ok { if !ok {
return nil, fmt.Errorf("base currency %s balance not found", s.Market.BaseCurrency) return nil, fmt.Errorf("base currency %s balance not found", s.Market.BaseCurrency)
@ -181,24 +187,29 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrde
midPrice := ticker.Buy.Add(ticker.Sell).Div(fixedpoint.NewFromFloat(2.0)) midPrice := ticker.Buy.Add(ticker.Sell).Div(fixedpoint.NewFromFloat(2.0))
log.Infof("mid price: %+v", midPrice) log.Infof("mid price: %+v", midPrice)
orders := []types.SubmitOrder{} // 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(baseWeight.Sub(s.TargetWeight))
// calculate buy and sell price // calculate bid and ask price
// buy price = mid price * (1 - r) // bid price = mid price * (1 - max(r + skew, 0))
buyPrice := midPrice.Mul(fixedpoint.One.Sub(s.HalfSpreadRatio)) bidSpreadRatio := fixedpoint.Max(s.HalfSpreadRatio.Add(skew), fixedpoint.Zero)
log.Infof("buy price: %+v", buyPrice) bidPrice := midPrice.Mul(fixedpoint.One.Sub(bidSpreadRatio))
// sell price = mid price * (1 + r) log.Infof("bid price: %s", bidPrice.String())
sellPrice := midPrice.Mul(fixedpoint.One.Add(s.HalfSpreadRatio)) // ask price = mid price * (1 + max(r - skew, 0))
log.Infof("sell price: %+v", sellPrice) askSrasedRatio := fixedpoint.Max(s.HalfSpreadRatio.Sub(skew), fixedpoint.Zero)
askPrice := midPrice.Mul(fixedpoint.One.Add(askSrasedRatio))
log.Infof("ask price: %s", askPrice.String())
// check balance and generate orders // check balance and generate orders
amount := s.Quantity.Mul(buyPrice) amount := s.Quantity.Mul(bidPrice)
if quoteBalance.Available.Compare(amount) > 0 { if quoteBalance.Available.Compare(amount) > 0 {
orders = append(orders, types.SubmitOrder{ orders = append(orders, types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
Side: types.SideTypeBuy, Side: types.SideTypeBuy,
Type: s.OrderType, Type: s.OrderType,
Price: buyPrice, Price: bidPrice,
Quantity: s.Quantity, Quantity: s.Quantity,
}) })
} else { } else {
@ -210,7 +221,7 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context) ([]types.SubmitOrde
Symbol: s.Symbol, Symbol: s.Symbol,
Side: types.SideTypeSell, Side: types.SideTypeSell,
Type: s.OrderType, Type: s.OrderType,
Price: sellPrice, Price: askPrice,
Quantity: s.Quantity, Quantity: s.Quantity,
}) })
} else { } else {