fix binance test, outptu for support and xgap strategies

This commit is contained in:
zenix 2022-02-09 19:48:40 +09:00
parent 05521a98b6
commit fad85d0992
7 changed files with 33 additions and 25 deletions

View File

@ -144,11 +144,11 @@ func (b *LocalActiveOrderBook) orderUpdateHandler(order types.Order) {
func (b *LocalActiveOrderBook) Print() { func (b *LocalActiveOrderBook) Print() {
for _, o := range b.Bids.Orders() { for _, o := range b.Bids.Orders() {
log.Infof("%s bid order: %d @ %f -> %s", o.Symbol, o.OrderID, o.Price, o.Status) log.Infof("%s bid order: %d @ %v -> %s", o.Symbol, o.OrderID, o.Price, o.Status)
} }
for _, o := range b.Asks.Orders() { for _, o := range b.Asks.Orders() {
log.Infof("%s ask order: %d @ %f -> %s", o.Symbol, o.OrderID, o.Price, o.Status) log.Infof("%s ask order: %d @ %v -> %s", o.Symbol, o.OrderID, o.Price, o.Status)
} }
} }

View File

@ -497,7 +497,7 @@ func (session *ExchangeSession) initSymbol(ctx context.Context, environ *Environ
} }
} }
log.Infof("%s last price: %f", symbol, session.lastPrices[symbol]) log.Infof("%s last price: %v", symbol, session.lastPrices[symbol])
session.initializedSymbols[symbol] = struct{}{} session.initializedSymbols[symbol] = struct{}{}
return nil return nil

View File

@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/fixedpoint"
) )
func TestTradeCollector_ShouldNotCountDuplicatedTrade(t *testing.T) { func TestTradeCollector_ShouldNotCountDuplicatedTrade(t *testing.T) {
@ -19,9 +20,9 @@ func TestTradeCollector_ShouldNotCountDuplicatedTrade(t *testing.T) {
ID: 1, ID: 1,
OrderID: 399, OrderID: 399,
Exchange: types.ExchangeBinance, Exchange: types.ExchangeBinance,
Price: 40000.0, Price: fixedpoint.NewFromInt(40000),
Quantity: 1.0, Quantity: fixedpoint.One,
QuoteQuantity: 40000.0, QuoteQuantity: fixedpoint.NewFromInt(40000),
Symbol: "BTCUSDT", Symbol: "BTCUSDT",
Side: types.SideTypeBuy, Side: types.SideTypeBuy,
IsBuyer: true, IsBuyer: true,
@ -34,13 +35,13 @@ func TestTradeCollector_ShouldNotCountDuplicatedTrade(t *testing.T) {
Symbol: "BTCUSDT", Symbol: "BTCUSDT",
Side: types.SideTypeBuy, Side: types.SideTypeBuy,
Type: types.OrderTypeLimit, Type: types.OrderTypeLimit,
Quantity: 1.0, Quantity: fixedpoint.One,
Price: 40000.0, Price: fixedpoint.NewFromInt(40000),
}, },
Exchange: types.ExchangeBinance, Exchange: types.ExchangeBinance,
OrderID: 399, OrderID: 399,
Status: types.OrderStatusFilled, Status: types.OrderStatusFilled,
ExecutedQuantity: 1.0, ExecutedQuantity: fixedpoint.One,
IsWorking: false, IsWorking: false,
}) })
@ -52,9 +53,9 @@ func TestTradeCollector_ShouldNotCountDuplicatedTrade(t *testing.T) {
ID: 1, ID: 1,
OrderID: 399, OrderID: 399,
Exchange: types.ExchangeBinance, Exchange: types.ExchangeBinance,
Price: 40000.0, Price: fixedpoint.NewFromInt(40000),
Quantity: 1.0, Quantity: fixedpoint.One,
QuoteQuantity: 40000.0, QuoteQuantity: fixedpoint.NewFromInt(40000),
Symbol: "BTCUSDT", Symbol: "BTCUSDT",
Side: types.SideTypeBuy, Side: types.SideTypeBuy,
IsBuyer: true, IsBuyer: true,

View File

@ -1005,7 +1005,14 @@ func (v *Value) UnmarshalJSON(data []byte) error {
*v = Zero *v = Zero
return nil return nil
} }
if len(data) == 0 {
*v = Zero
return nil
}
var err error var err error
if data[0] == '"' {
data = data[1:len(data)-1]
}
if *v, err = NewFromBytes(data); err != nil { if *v, err = NewFromBytes(data); err != nil {
return err return err
} }

View File

@ -320,16 +320,16 @@ func (s *Strategy) calculateQuantity(session *bbgo.ExchangeSession, side types.S
if s.MinQuoteAssetBalance.Sign() > 0 && if s.MinQuoteAssetBalance.Sign() > 0 &&
quoteBalance.Available.Sub(notional).Compare(s.MinQuoteAssetBalance) < 0 { quoteBalance.Available.Sub(notional).Compare(s.MinQuoteAssetBalance) < 0 {
log.Warnf("modifying quantity %s according to the min quote asset balance %s %s", log.Warnf("modifying quantity %v according to the min quote asset balance %v %s",
quantity.String(), quantity,
quoteBalance.Available.String(), quoteBalance.Available,
s.Market.QuoteCurrency) s.Market.QuoteCurrency)
quota := quoteBalance.Available.Sub(s.MinQuoteAssetBalance) quota := quoteBalance.Available.Sub(s.MinQuoteAssetBalance)
quantity = bbgo.AdjustQuantityByMinAmount(quantity, closePrice, quota) quantity = bbgo.AdjustQuantityByMinAmount(quantity, closePrice, quota)
} else if notional.Compare(quoteBalance.Available) > 0 { } else if notional.Compare(quoteBalance.Available) > 0 {
log.Warnf("modifying quantity %f according to the quote asset balance %f %s", log.Warnf("modifying quantity %v according to the quote asset balance %v %s",
quantity.String(), quantity,
quoteBalance.Available.String(), quoteBalance.Available,
s.Market.QuoteCurrency) s.Market.QuoteCurrency)
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quoteBalance.Available) quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quoteBalance.Available)
} }

View File

@ -289,8 +289,8 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
// if the spread is less than 100 ticks (100 pips), skip // if the spread is less than 100 ticks (100 pips), skip
if spread.Compare(s.tradingMarket.TickSize.MulExp(2)) < 0 { if spread.Compare(s.tradingMarket.TickSize.MulExp(2)) < 0 {
log.Warnf("spread too small, we can't place orders: spread=%s bid=%s ask=%s", log.Warnf("spread too small, we can't place orders: spread=%v bid=%v ask=%v",
spread.String(), bestBid.Price.String(), bestAsk.Price.String()) spread, bestBid.Price, bestAsk.Price)
continue continue
} }
@ -306,15 +306,15 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
var spread = bestAsk.Price.Sub(bestBid.Price) var spread = bestAsk.Price.Sub(bestBid.Price)
var spreadPercentage = spread.Div(bestAsk.Price) var spreadPercentage = spread.Div(bestAsk.Price)
log.Infof("spread=%s %s ask=%s bid=%s", log.Infof("spread=%v %s ask=%v bid=%v",
spread.String(), spreadPercentage.Percentage(), spread, spreadPercentage.Percentage(),
bestAsk.Price.String(), bestBid.Price.String()) bestAsk.Price, bestBid.Price)
// var spreadPercentage = spread.Float64() / bestBid.Price.Float64() // var spreadPercentage = spread.Float64() / bestBid.Price.Float64()
var midPrice = bestAsk.Price.Add(bestBid.Price).Div(Two) var midPrice = bestAsk.Price.Add(bestBid.Price).Div(Two)
var price = midPrice var price = midPrice
log.Infof("mid price %f", midPrice.String()) log.Infof("mid price %v", midPrice)
var balances = s.tradingSession.Account.Balances() var balances = s.tradingSession.Account.Balances()
var quantity = s.tradingMarket.MinQuantity var quantity = s.tradingMarket.MinQuantity

View File

@ -325,7 +325,7 @@ func (a *Account) UnlockBalance(currency string, unlocked fixedpoint.Value) erro
return fmt.Errorf("trying to unlocked inexisted balance: %s", currency) return fmt.Errorf("trying to unlocked inexisted balance: %s", currency)
} }
if unlocked.Compare(balance.Locked) >= 0 { if unlocked.Compare(balance.Locked) > 0 {
return fmt.Errorf("trying to unlocked more than locked %s: locked %v < want to unlock %v", currency, balance.Locked, unlocked) return fmt.Errorf("trying to unlocked more than locked %s: locked %v < want to unlock %v", currency, balance.Locked, unlocked)
} }