Merge pull request #1652 from c9s/c9s/fix-xgap-spread-too-large-issue

FIX: [xgap] fix empty source book pricing issue
This commit is contained in:
c9s 2024-06-11 18:11:44 +08:00 committed by GitHub
commit 7a4f9347f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 7 deletions

View File

@ -1116,7 +1116,7 @@ func TestExchange_QueryClosedOrders(t *testing.T) {
assert = assert.New(t) assert = assert.New(t)
ex = New("key", "secret", "passphrase") ex = New("key", "secret", "passphrase")
expBtcSymbol = "BTCUSDT" expBtcSymbol = "BTCUSDT"
since = types.NewMillisecondTimestampFromInt(1709645944272).Time() since = time.Now().Add(-24 * time.Hour)
until = since.Add(time.Hour) until = since.Add(time.Hour)
lastOrderId = uint64(0) lastOrderId = uint64(0)
url = "/api/v2/spot/trade/history-orders" url = "/api/v2/spot/trade/history-orders"
@ -1417,7 +1417,7 @@ func TestExchange_QueryTrades(t *testing.T) {
assert = assert.New(t) assert = assert.New(t)
ex = New("key", "secret", "passphrase") ex = New("key", "secret", "passphrase")
expApeSymbol = "APEUSDT" expApeSymbol = "APEUSDT"
since = types.NewMillisecondTimestampFromInt(1709645944272).Time() since = time.Now().Add(-24 * time.Hour)
until = since.Add(time.Hour) until = since.Add(time.Hour)
options = &types.TradeQueryOptions{ options = &types.TradeQueryOptions{
StartTime: &since, StartTime: &since,

View File

@ -21,7 +21,7 @@ const ID = "xgap"
var log = logrus.WithField("strategy", ID) var log = logrus.WithField("strategy", ID)
var StepPercentageGap = fixedpoint.NewFromFloat(0.05) var maxStepPercentageGap = fixedpoint.NewFromFloat(0.05)
var Two = fixedpoint.NewFromInt(2) var Two = fixedpoint.NewFromInt(2)
@ -74,6 +74,7 @@ type Strategy struct {
DailyMaxVolume fixedpoint.Value `json:"dailyMaxVolume,omitempty"` DailyMaxVolume fixedpoint.Value `json:"dailyMaxVolume,omitempty"`
UpdateInterval types.Duration `json:"updateInterval"` UpdateInterval types.Duration `json:"updateInterval"`
SimulateVolume bool `json:"simulateVolume"` SimulateVolume bool `json:"simulateVolume"`
SimulatePrice bool `json:"simulatePrice"`
sourceSession, tradingSession *bbgo.ExchangeSession sourceSession, tradingSession *bbgo.ExchangeSession
sourceMarket, tradingMarket types.Market sourceMarket, tradingMarket types.Market
@ -274,8 +275,8 @@ func (s *Strategy) placeOrders(ctx context.Context) {
log.Infof("trading book spread=%s %s", log.Infof("trading book spread=%s %s",
spread.String(), spreadPercentage.Percentage()) spread.String(), spreadPercentage.Percentage())
// use the source book price if the spread percentage greater than 10% // use the source book price if the spread percentage greater than 5%
if spreadPercentage.Compare(StepPercentageGap) > 0 { if s.SimulatePrice && spreadPercentage.Compare(maxStepPercentageGap) > 0 {
log.Warnf("spread too large (%s %s), using source book", log.Warnf("spread too large (%s %s), using source book",
spread.String(), spreadPercentage.Percentage()) spread.String(), spreadPercentage.Percentage())
bestBid, hasBid = s.sourceBook.BestBid() bestBid, hasBid = s.sourceBook.BestBid()
@ -335,12 +336,12 @@ func (s *Strategy) placeOrders(ctx context.Context) {
minQuantity := s.tradingMarket.AdjustQuantityByMinNotional(s.tradingMarket.MinQuantity, price) minQuantity := s.tradingMarket.AdjustQuantityByMinNotional(s.tradingMarket.MinQuantity, price)
if baseBalance.Available.Compare(minQuantity) < 0 { if baseBalance.Available.Compare(minQuantity) <= 0 {
log.Infof("base balance: %s %s is not enough, skip", baseBalance.Available.String(), s.tradingMarket.BaseCurrency) log.Infof("base balance: %s %s is not enough, skip", baseBalance.Available.String(), s.tradingMarket.BaseCurrency)
return return
} }
if quoteBalance.Available.Div(price).Compare(minQuantity) < 0 { if quoteBalance.Available.Div(price).Compare(minQuantity) <= 0 {
log.Infof("quote balance: %s %s is not enough, skip", quoteBalance.Available.String(), s.tradingMarket.QuoteCurrency) log.Infof("quote balance: %s %s is not enough, skip", quoteBalance.Available.String(), s.tradingMarket.QuoteCurrency)
return return
} }