2022-01-06 17:17:07 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2022-02-09 10:48:40 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2022-01-06 17:17:07 +00:00
|
|
|
)
|
|
|
|
|
2022-01-06 17:23:54 +00:00
|
|
|
func TestTradeCollector_ShouldNotCountDuplicatedTrade(t *testing.T) {
|
2022-01-06 17:17:07 +00:00
|
|
|
symbol := "BTCUSDT"
|
|
|
|
position := types.NewPosition(symbol, "BTC", "USDT")
|
|
|
|
orderStore := NewOrderStore(symbol)
|
|
|
|
collector := NewTradeCollector(symbol, position, orderStore)
|
|
|
|
assert.NotNil(t, collector)
|
|
|
|
|
|
|
|
matched := collector.ProcessTrade(types.Trade{
|
|
|
|
ID: 1,
|
|
|
|
OrderID: 399,
|
|
|
|
Exchange: types.ExchangeBinance,
|
2022-02-09 10:48:40 +00:00
|
|
|
Price: fixedpoint.NewFromInt(40000),
|
|
|
|
Quantity: fixedpoint.One,
|
|
|
|
QuoteQuantity: fixedpoint.NewFromInt(40000),
|
2022-01-06 17:17:07 +00:00
|
|
|
Symbol: "BTCUSDT",
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
IsBuyer: true,
|
|
|
|
})
|
|
|
|
assert.False(t, matched, "should be added to the trade store")
|
|
|
|
assert.Equal(t, 1, len(collector.tradeStore.Trades()), "should have one trade in the trade store")
|
|
|
|
|
|
|
|
orderStore.Add(types.Order{
|
|
|
|
SubmitOrder: types.SubmitOrder{
|
|
|
|
Symbol: "BTCUSDT",
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeLimit,
|
2022-02-09 10:48:40 +00:00
|
|
|
Quantity: fixedpoint.One,
|
|
|
|
Price: fixedpoint.NewFromInt(40000),
|
2022-01-06 17:17:07 +00:00
|
|
|
},
|
|
|
|
Exchange: types.ExchangeBinance,
|
|
|
|
OrderID: 399,
|
|
|
|
Status: types.OrderStatusFilled,
|
2022-02-09 10:48:40 +00:00
|
|
|
ExecutedQuantity: fixedpoint.One,
|
2022-01-06 17:17:07 +00:00
|
|
|
IsWorking: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
matched = collector.Process()
|
|
|
|
assert.True(t, matched)
|
|
|
|
assert.Equal(t, 0, len(collector.tradeStore.Trades()), "the found trade should be removed from the trade store")
|
|
|
|
|
|
|
|
matched = collector.ProcessTrade(types.Trade{
|
|
|
|
ID: 1,
|
|
|
|
OrderID: 399,
|
|
|
|
Exchange: types.ExchangeBinance,
|
2022-02-09 10:48:40 +00:00
|
|
|
Price: fixedpoint.NewFromInt(40000),
|
|
|
|
Quantity: fixedpoint.One,
|
|
|
|
QuoteQuantity: fixedpoint.NewFromInt(40000),
|
2022-01-06 17:17:07 +00:00
|
|
|
Symbol: "BTCUSDT",
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
IsBuyer: true,
|
|
|
|
})
|
2022-01-07 18:20:30 +00:00
|
|
|
assert.False(t, matched, "the same trade should not match")
|
2022-01-06 17:17:07 +00:00
|
|
|
assert.Equal(t, 0, len(collector.tradeStore.Trades()), "the same trade should not be added to the trade store")
|
|
|
|
}
|