bbgo_origin/pkg/service/trade_test.go

111 lines
4.1 KiB
Go
Raw Normal View History

package service
import (
"context"
"testing"
2022-03-07 04:12:06 +00:00
"time"
2021-02-15 13:07:55 +00:00
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
2021-02-16 07:34:01 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
2022-03-07 04:12:06 +00:00
"github.com/c9s/bbgo/pkg/types"
)
2021-02-15 13:07:55 +00:00
func Test_tradeService(t *testing.T) {
db, err := prepareDB(t)
if err != nil {
t.Fatal(err)
}
2021-02-16 07:34:01 +00:00
defer db.Close()
ctx := context.Background()
2021-02-15 13:07:55 +00:00
xdb := sqlx.NewDb(db.DB, "sqlite3")
service := &TradeService{DB: xdb}
2021-02-16 07:34:01 +00:00
err = service.Insert(types.Trade{
ID: 1,
OrderID: 1,
Exchange: "binance",
Price: fixedpoint.NewFromInt(1000),
Quantity: fixedpoint.NewFromFloat(0.1),
QuoteQuantity: fixedpoint.NewFromFloat(1000.0 * 0.1),
2021-02-16 07:34:01 +00:00
Symbol: "BTCUSDT",
Side: "BUY",
IsBuyer: true,
2022-03-07 04:12:06 +00:00
Time: types.Time(time.Now()),
2021-02-16 07:34:01 +00:00
})
assert.NoError(t, err)
2021-03-16 06:07:47 +00:00
err = service.Mark(ctx, 1, "grid")
2021-02-16 07:34:01 +00:00
assert.NoError(t, err)
tradeRecord, err := service.Load(ctx, 1)
assert.NoError(t, err)
assert.NotNil(t, tradeRecord)
2021-02-16 08:00:14 +00:00
assert.True(t, tradeRecord.StrategyID.Valid)
assert.Equal(t, "grid", tradeRecord.StrategyID.String)
2021-02-16 07:34:01 +00:00
err = service.UpdatePnL(ctx, 1, 10.0)
assert.NoError(t, err)
tradeRecord, err = service.Load(ctx, 1)
assert.NoError(t, err)
assert.NotNil(t, tradeRecord)
assert.True(t, tradeRecord.PnL.Valid)
assert.Equal(t, 10.0, tradeRecord.PnL.Float64)
}
func Test_queryTradingVolumeSQL(t *testing.T) {
t.Run("group by different period", func(t *testing.T) {
o := TradingVolumeQueryOptions{
GroupByPeriod: "month",
}
2021-02-06 08:05:21 +00:00
assert.Equal(t, "SELECT YEAR(traded_at) AS year, MONTH(traded_at) AS month, SUM(quantity * price) AS quote_volume FROM trades WHERE traded_at > :start_time GROUP BY MONTH(traded_at), YEAR(traded_at) ORDER BY year ASC, month ASC", generateMysqlTradingVolumeQuerySQL(o))
o.GroupByPeriod = "year"
2021-02-06 08:05:21 +00:00
assert.Equal(t, "SELECT YEAR(traded_at) AS year, SUM(quantity * price) AS quote_volume FROM trades WHERE traded_at > :start_time GROUP BY YEAR(traded_at) ORDER BY year ASC", generateMysqlTradingVolumeQuerySQL(o))
expectedDefaultSQL := "SELECT YEAR(traded_at) AS year, MONTH(traded_at) AS month, DAY(traded_at) AS day, SUM(quantity * price) AS quote_volume FROM trades WHERE traded_at > :start_time GROUP BY DAY(traded_at), MONTH(traded_at), YEAR(traded_at) ORDER BY year ASC, month ASC, day ASC"
for _, s := range []string{"", "day"} {
o.GroupByPeriod = s
2021-02-06 08:05:21 +00:00
assert.Equal(t, expectedDefaultSQL, generateMysqlTradingVolumeQuerySQL(o))
}
})
}
func Test_queryTradesSQL(t *testing.T) {
t.Run("generate order by clause by Ordering option", func(t *testing.T) {
2021-02-05 04:42:59 +00:00
assert.Equal(t, "SELECT * FROM trades ORDER BY gid ASC LIMIT 500", queryTradesSQL(QueryTradesOptions{Limit: 500}))
assert.Equal(t, "SELECT * FROM trades ORDER BY gid ASC LIMIT 500", queryTradesSQL(QueryTradesOptions{Ordering: "ASC", Limit: 500}))
assert.Equal(t, "SELECT * FROM trades ORDER BY gid DESC LIMIT 500", queryTradesSQL(QueryTradesOptions{Ordering: "DESC", Limit: 500}))
})
t.Run("filter by exchange name", func(t *testing.T) {
2021-02-05 04:42:59 +00:00
assert.Equal(t, "SELECT * FROM trades WHERE exchange = :exchange ORDER BY gid ASC LIMIT 500", queryTradesSQL(QueryTradesOptions{Exchange: "max", Limit: 500}))
})
t.Run("filter by symbol", func(t *testing.T) {
2021-02-05 04:42:59 +00:00
assert.Equal(t, "SELECT * FROM trades WHERE symbol = :symbol ORDER BY gid ASC LIMIT 500", queryTradesSQL(QueryTradesOptions{Symbol: "eth", Limit: 500}))
})
t.Run("GID ordering", func(t *testing.T) {
2021-02-05 04:42:59 +00:00
assert.Equal(t, "SELECT * FROM trades WHERE gid > :gid ORDER BY gid ASC LIMIT 500", queryTradesSQL(QueryTradesOptions{LastGID: 1, Limit: 500}))
assert.Equal(t, "SELECT * FROM trades WHERE gid > :gid ORDER BY gid ASC LIMIT 500", queryTradesSQL(QueryTradesOptions{LastGID: 1, Ordering: "ASC", Limit: 500}))
assert.Equal(t, "SELECT * FROM trades WHERE gid < :gid ORDER BY gid DESC LIMIT 500", queryTradesSQL(QueryTradesOptions{LastGID: 1, Ordering: "DESC", Limit: 500}))
})
t.Run("convert all options", func(t *testing.T) {
assert.Equal(t, "SELECT * FROM trades WHERE exchange = :exchange AND symbol = :symbol AND gid < :gid ORDER BY gid DESC LIMIT 500", queryTradesSQL(QueryTradesOptions{
Exchange: "max",
Symbol: "btc",
LastGID: 123,
Ordering: "DESC",
Limit: 500,
}))
})
}