From 0e4f7b366a7b279555c24964f8bdcac445c6c0f4 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 9 Sep 2024 14:41:27 +0800 Subject: [PATCH] testhelper: add more test helpers --- pkg/testing/testhelper/market.go | 43 +++++++++++++++++++ pkg/testing/testhelper/pricevolumeslice.go | 49 ++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 pkg/testing/testhelper/market.go create mode 100644 pkg/testing/testhelper/pricevolumeslice.go diff --git a/pkg/testing/testhelper/market.go b/pkg/testing/testhelper/market.go new file mode 100644 index 0000000..48cd30e --- /dev/null +++ b/pkg/testing/testhelper/market.go @@ -0,0 +1,43 @@ +package testhelper + +import ( + "fmt" + + "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/types" +) + +var markets = map[string]types.Market{ + "BTCUSDT": { + Symbol: "BTCUSDT", + PricePrecision: 2, + VolumePrecision: 8, + QuoteCurrency: "USDT", + BaseCurrency: "BTC", + MinNotional: fixedpoint.MustNewFromString("0.001"), + MinAmount: fixedpoint.MustNewFromString("10.0"), + MinQuantity: fixedpoint.MustNewFromString("0.001"), + TickSize: fixedpoint.MustNewFromString("0.01"), + }, + + "ETHUSDT": { + Symbol: "ETH", + PricePrecision: 2, + VolumePrecision: 8, + QuoteCurrency: "USDT", + BaseCurrency: "ETH", + MinNotional: fixedpoint.MustNewFromString("0.005"), + MinAmount: fixedpoint.MustNewFromString("10.0"), + MinQuantity: fixedpoint.MustNewFromString("0.001"), + TickSize: fixedpoint.MustNewFromString("0.01"), + }, +} + +func Market(symbol string) types.Market { + market, ok := markets[symbol] + if !ok { + panic(fmt.Errorf("%s market not found, valid markets: %+v", symbol, markets)) + } + + return market +} diff --git a/pkg/testing/testhelper/pricevolumeslice.go b/pkg/testing/testhelper/pricevolumeslice.go new file mode 100644 index 0000000..ea78380 --- /dev/null +++ b/pkg/testing/testhelper/pricevolumeslice.go @@ -0,0 +1,49 @@ +package testhelper + +import ( + "fmt" + "strings" + + "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/types" +) + +func PriceVolumeSliceFromText(str string) (slice types.PriceVolumeSlice) { + lines := strings.Split(str, "\n") + for _, line := range lines { + line = strings.TrimSpace(line) + if len(line) == 0 { + continue + } + + cols := strings.SplitN(line, ",", 2) + if len(cols) < 2 { + panic(fmt.Errorf("column length should be 2, got %d", len(cols))) + } + + price := fixedpoint.MustNewFromString(strings.TrimSpace(cols[0])) + volume := fixedpoint.MustNewFromString(strings.TrimSpace(cols[1])) + slice = append(slice, types.PriceVolume{ + Price: price, + Volume: volume, + }) + } + + return slice +} + +func PriceVolumeSlice(values ...fixedpoint.Value) (slice types.PriceVolumeSlice) { + if len(values)%2 != 0 { + panic("values should be paired") + } + + for i := 0; i < len(values); i += 2 { + slice = append(slice, types.PriceVolume{ + Price: values[i], + Volume: values[i+1], + }) + + } + + return slice +}