2020-09-16 06:05:03 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-10-11 08:46:15 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2020-09-16 06:05:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrQuoteBalanceLevelTooLow = errors.New("quote balance level is too low")
|
|
|
|
ErrInsufficientQuoteBalance = errors.New("insufficient quote balance")
|
|
|
|
|
|
|
|
ErrAssetBalanceLevelTooLow = errors.New("asset balance level too low")
|
|
|
|
ErrInsufficientAssetBalance = errors.New("insufficient asset balance")
|
|
|
|
ErrAssetBalanceLevelTooHigh = errors.New("asset balance level too high")
|
|
|
|
)
|
|
|
|
|
|
|
|
// OrderProcessor does:
|
|
|
|
// - Check quote balance
|
|
|
|
// - Check and control the order amount
|
|
|
|
// - Adjust order amount due to the minAmount configuration and maxAmount configuration
|
|
|
|
// - Canonicalize the volume precision base on the given exchange
|
|
|
|
type OrderProcessor struct {
|
|
|
|
// balance control
|
|
|
|
MinQuoteBalance float64 `json:"minQuoteBalance"`
|
|
|
|
MaxAssetBalance float64 `json:"maxBaseAssetBalance"`
|
|
|
|
MinAssetBalance float64 `json:"minBaseAssetBalance"`
|
2020-10-26 09:57:28 +00:00
|
|
|
MaxOrderAmount float64 `json:"maxOrderAmount"`
|
2020-09-16 06:05:03 +00:00
|
|
|
|
|
|
|
// MinProfitSpread is used when submitting sell orders, it check if there the selling can make the profit.
|
|
|
|
MinProfitSpread float64 `json:"minProfitSpread"`
|
|
|
|
|
|
|
|
|
2020-09-18 10:15:45 +00:00
|
|
|
Exchange types.Exchange `json:"-"`
|
2020-09-16 06:05:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 10:08:02 +00:00
|
|
|
func (p *OrderProcessor) Submit(ctx context.Context, order types.SubmitOrder) error {
|
2020-10-12 14:46:06 +00:00
|
|
|
/*
|
2020-10-26 08:45:09 +00:00
|
|
|
tradingCtx := p.OrderExecutor.Context
|
|
|
|
currentPrice := tradingCtx.CurrentPrice
|
|
|
|
market := order.Market
|
|
|
|
quantity := order.Quantity
|
|
|
|
|
|
|
|
tradingCtx.Lock()
|
|
|
|
defer tradingCtx.Unlock()
|
|
|
|
|
|
|
|
switch order.Side {
|
|
|
|
case types.SideTypeBuy:
|
|
|
|
|
|
|
|
if balance, ok := tradingCtx.Balances[market.QuoteCurrency]; ok {
|
|
|
|
if balance.Available < p.MinQuoteBalance {
|
|
|
|
return errors.Wrapf(ErrQuoteBalanceLevelTooLow, "quote balance level is too low: %s < %s",
|
|
|
|
types.USD.FormatMoneyFloat64(balance.Available),
|
|
|
|
types.USD.FormatMoneyFloat64(p.MinQuoteBalance))
|
|
|
|
}
|
2020-09-16 06:05:03 +00:00
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
if baseBalance, ok := tradingCtx.Balances[market.BaseCurrency]; ok {
|
|
|
|
if util.NotZero(p.MaxAssetBalance) && baseBalance.Available > p.MaxAssetBalance {
|
|
|
|
return errors.Wrapf(ErrAssetBalanceLevelTooHigh, "asset balance level is too high: %f > %f", baseBalance.Available, p.MaxAssetBalance)
|
|
|
|
}
|
2020-09-16 06:05:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
available := math.Max(0.0, balance.Available-p.MinQuoteBalance)
|
2020-09-16 06:05:03 +00:00
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
if available < market.MinAmount {
|
|
|
|
return errors.Wrapf(ErrInsufficientQuoteBalance, "insufficient quote balance: %f < min amount %f", available, market.MinAmount)
|
|
|
|
}
|
2020-09-16 06:05:03 +00:00
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
quantity = adjustQuantityByMinAmount(quantity, currentPrice, market.MinAmount*1.01)
|
|
|
|
quantity = adjustQuantityByMaxAmount(quantity, currentPrice, available)
|
|
|
|
amount := quantity * currentPrice
|
|
|
|
if amount < market.MinAmount {
|
|
|
|
return fmt.Errorf("amount too small: %f < min amount %f", amount, market.MinAmount)
|
|
|
|
}
|
2020-09-16 06:05:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
case types.SideTypeSell:
|
2020-09-16 06:05:03 +00:00
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
if balance, ok := tradingCtx.Balances[market.BaseCurrency]; ok {
|
|
|
|
if util.NotZero(p.MinAssetBalance) && balance.Available < p.MinAssetBalance {
|
|
|
|
return errors.Wrapf(ErrAssetBalanceLevelTooLow, "asset balance level is too low: %f > %f", balance.Available, p.MinAssetBalance)
|
|
|
|
}
|
2020-09-16 06:05:03 +00:00
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
quantity = adjustQuantityByMinAmount(quantity, currentPrice, market.MinNotional*1.01)
|
2020-09-16 06:05:03 +00:00
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
available := balance.Available
|
|
|
|
quantity = math.Min(quantity, available)
|
|
|
|
if quantity < market.MinQuantity {
|
|
|
|
return errors.Wrapf(ErrInsufficientAssetBalance, "insufficient asset balance: %f > minimal quantity %f", available, market.MinQuantity)
|
|
|
|
}
|
2020-09-16 06:05:03 +00:00
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
notional := quantity * currentPrice
|
|
|
|
if notional < tradingCtx.Market.MinNotional {
|
|
|
|
return fmt.Errorf("notional %f < min notional: %f", notional, market.MinNotional)
|
|
|
|
}
|
2020-09-16 06:05:03 +00:00
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
// price tick10
|
|
|
|
// 2 -> 0.01 -> 0.1
|
|
|
|
// 4 -> 0.0001 -> 0.001
|
|
|
|
tick10 := math.Pow10(-market.PricePrecision + 1)
|
|
|
|
minProfitSpread := math.Max(p.MinProfitSpread, tick10)
|
|
|
|
estimatedFee := currentPrice * 0.0015 * 2 // double the fee
|
|
|
|
targetPrice := currentPrice - estimatedFee - minProfitSpread
|
|
|
|
|
|
|
|
stockQuantity := tradingCtx.StockManager.Stocks.QuantityBelowPrice(targetPrice)
|
|
|
|
if math.Round(stockQuantity*1e8) == 0.0 {
|
|
|
|
return fmt.Errorf("profitable stock not found: target price %f, profit spread: %f", targetPrice, minProfitSpread)
|
|
|
|
}
|
2020-09-16 06:05:03 +00:00
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
quantity = math.Min(quantity, stockQuantity)
|
|
|
|
if quantity < market.MinLot {
|
|
|
|
return fmt.Errorf("quantity %f less than min lot %f", quantity, market.MinLot)
|
|
|
|
}
|
2020-09-16 06:05:03 +00:00
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
notional = quantity * currentPrice
|
|
|
|
if notional < tradingCtx.Market.MinNotional {
|
|
|
|
return fmt.Errorf("notional %f < min notional: %f", notional, market.MinNotional)
|
|
|
|
}
|
2020-09-16 06:05:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 08:45:09 +00:00
|
|
|
order.Quantity = quantity
|
|
|
|
order.QuantityString = market.FormatVolume(quantity)
|
|
|
|
*/
|
2020-10-12 14:46:06 +00:00
|
|
|
|
2020-10-25 11:18:03 +00:00
|
|
|
createdOrders, err := p.Exchange.SubmitOrders(ctx, order)
|
|
|
|
_ = createdOrders
|
|
|
|
return err
|
2020-09-16 06:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func adjustQuantityByMinAmount(quantity float64, currentPrice float64, minAmount float64) float64 {
|
|
|
|
// modify quantity for the min amount
|
|
|
|
amount := currentPrice * quantity
|
|
|
|
if amount < minAmount {
|
|
|
|
ratio := minAmount / amount
|
|
|
|
quantity *= ratio
|
|
|
|
}
|
|
|
|
|
|
|
|
return quantity
|
|
|
|
}
|
|
|
|
|
|
|
|
func adjustQuantityByMaxAmount(quantity float64, currentPrice float64, maxAmount float64) float64 {
|
|
|
|
amount := currentPrice * quantity
|
|
|
|
if amount > maxAmount {
|
|
|
|
ratio := maxAmount / amount
|
|
|
|
quantity *= ratio
|
|
|
|
}
|
|
|
|
|
|
|
|
return quantity
|
|
|
|
}
|