Skip to content

Commit

Permalink
adjust hedge quantity according to the hedge account balances
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed May 10, 2021
1 parent c1ea9ff commit 95d58e9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
10 changes: 5 additions & 5 deletions pkg/bbgo/order_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ func (c *BasicRiskController) ProcessOrders(session *ExchangeSession, orders ...

// Increase the quantity if the amount is not enough,
// this is the only increase op, later we will decrease the quantity if it meets the criteria
quantity = adjustQuantityByMinAmount(quantity, price, market.MinAmount*1.01)
quantity = AdjustQuantityByMinAmount(quantity, price, market.MinAmount*1.01)

if c.MaxOrderAmount > 0 {
quantity = adjustQuantityByMaxAmount(quantity, price, c.MaxOrderAmount.Float64())
quantity = AdjustQuantityByMaxAmount(quantity, price, c.MaxOrderAmount.Float64())
}

quoteAssetQuota := math.Max(0.0, quoteBalance.Available.Float64()-c.MinQuoteBalance.Float64())
Expand All @@ -169,7 +169,7 @@ func (c *BasicRiskController) ProcessOrders(session *ExchangeSession, orders ...
continue
}

quantity = adjustQuantityByMaxAmount(quantity, price, quoteAssetQuota)
quantity = AdjustQuantityByMaxAmount(quantity, price, quoteAssetQuota)

// if MaxBaseAssetBalance is enabled, we should check the current base asset balance
if baseBalance, hasBaseAsset := balances[market.BaseCurrency]; hasBaseAsset && c.MaxBaseAssetBalance > 0 {
Expand Down Expand Up @@ -217,7 +217,7 @@ func (c *BasicRiskController) ProcessOrders(session *ExchangeSession, orders ...
}

// if the amount is too small, we should increase it.
quantity = adjustQuantityByMinAmount(quantity, price, market.MinNotional*1.01)
quantity = AdjustQuantityByMinAmount(quantity, price, market.MinNotional*1.01)

// we should not SELL too much
quantity = math.Min(quantity, baseAssetBalance.Available.Float64())
Expand All @@ -244,7 +244,7 @@ func (c *BasicRiskController) ProcessOrders(session *ExchangeSession, orders ...
}

if c.MaxOrderAmount > 0 {
quantity = adjustQuantityByMaxAmount(quantity, price, c.MaxOrderAmount.Float64())
quantity = AdjustQuantityByMaxAmount(quantity, price, c.MaxOrderAmount.Float64())
}

notional := quantity * lastPrice
Expand Down
6 changes: 3 additions & 3 deletions pkg/bbgo/order_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var (
ErrAssetBalanceLevelTooHigh = errors.New("asset balance level too high")
)

// adjustQuantityByMinAmount adjusts the quantity to make the amount greater than the given minAmount
func adjustQuantityByMinAmount(quantity, currentPrice, minAmount float64) float64 {
// AdjustQuantityByMinAmount adjusts the quantity to make the amount greater than the given minAmount
func AdjustQuantityByMinAmount(quantity, currentPrice, minAmount float64) float64 {
// modify quantity for the min amount
amount := currentPrice * quantity
if amount < minAmount {
Expand All @@ -25,7 +25,7 @@ func adjustQuantityByMinAmount(quantity, currentPrice, minAmount float64) float6
return quantity
}

func adjustQuantityByMaxAmount(quantity float64, price float64, maxAmount float64) float64 {
func AdjustQuantityByMaxAmount(quantity float64, price float64, maxAmount float64) float64 {
amount := price * quantity
if amount > maxAmount {
ratio := maxAmount / amount
Expand Down
2 changes: 1 addition & 1 deletion pkg/bbgo/order_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAdjustQuantityByMinAmount(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
q := adjustQuantityByMinAmount(test.args.quantity, test.args.price, test.args.minAmount)
q := AdjustQuantityByMinAmount(test.args.quantity, test.args.price, test.args.minAmount)
assert.Equal(t, test.wanted, q)
})
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,29 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
return
}

// adjust quantity according to the balances
account := s.sourceSession.Account
switch side {

case types.SideTypeBuy:
// check quote quantity
if quote, ok := account.Balance(s.sourceMarket.QuoteCurrency) ; ok {
if quote.Available < notional {
qf := bbgo.AdjustQuantityByMinAmount(quantity.Float64(), lastPrice, quote.Available.Float64() * 0.99999)
quantity = fixedpoint.NewFromFloat(qf)
}
}

case types.SideTypeSell:
// check quote quantity
if base, ok := account.Balance(s.sourceMarket.BaseCurrency) ; ok {
if base.Available < quantity {
quantity = base.Available
}
}

}

s.Notifiability.Notify("Submitting hedge order: %s %s %f", s.Symbol, side, quantity.Float64())
orderExecutor := &bbgo.ExchangeOrderExecutor{Session: s.sourceSession}
returnOrders, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
Expand Down

0 comments on commit 95d58e9

Please sign in to comment.