From d011bf275e35bfb7664821de0b94c0ec52396295 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 18 Oct 2020 11:34:36 +0800 Subject: [PATCH] move stock_test file and testdata --- pkg/accounting/cost_distribution_test.go | 183 ++++++++++++++++++ .../testdata/btcusdt-trades.json | 0 pkg/bbgo/stock_test.go | 182 ----------------- 3 files changed, 183 insertions(+), 182 deletions(-) create mode 100644 pkg/accounting/cost_distribution_test.go rename pkg/{bbgo => accounting}/testdata/btcusdt-trades.json (100%) diff --git a/pkg/accounting/cost_distribution_test.go b/pkg/accounting/cost_distribution_test.go new file mode 100644 index 000000000..2a899a6a4 --- /dev/null +++ b/pkg/accounting/cost_distribution_test.go @@ -0,0 +1,183 @@ +package accounting + +import ( + "encoding/json" + "io/ioutil" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/c9s/bbgo/pkg/types" +) + +func TestStockManager(t *testing.T) { + + t.Run("testdata", func(t *testing.T) { + var trades []types.Trade + + out, err := ioutil.ReadFile("testdata/btcusdt-trades.json") + assert.NoError(t, err) + + err = json.Unmarshal(out, &trades) + assert.NoError(t, err) + + var stockManager = &StockDistribution{ + TradingFeeCurrency: "BNB", + Symbol: "BTCUSDT", + } + + _, err = stockManager.AddTrades(trades) + assert.NoError(t, err) + assert.Equal(t, 0.72970242, stockManager.Stocks.Quantity()) + assert.NotEmpty(t, stockManager.Stocks) + assert.Equal(t, 20, len(stockManager.Stocks)) + assert.Equal(t, 0, len(stockManager.PendingSells)) + + }) + + t.Run("stock", func(t *testing.T) { + var trades = []types.Trade{ + {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, + {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, + {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.01, IsBuyer: false}, + } + + var stockManager = &StockDistribution{ + TradingFeeCurrency: "BNB", + Symbol: "BTCUSDT", + } + + _, err := stockManager.AddTrades(trades) + assert.NoError(t, err) + assert.Len(t, stockManager.Stocks, 2) + assert.Equal(t, StockSlice{ + { + Symbol: "BTCUSDT", + Price: 9100.0, + Quantity: 0.05, + IsBuyer: true, + }, + { + Symbol: "BTCUSDT", + Price: 9100.0, + Quantity: 0.04, + IsBuyer: true, + }, + }, stockManager.Stocks) + assert.Len(t, stockManager.PendingSells, 0) + }) + + t.Run("sold out", func(t *testing.T) { + var trades = []types.Trade{ + {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, + {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.05, IsBuyer: false}, + {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, + {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.05, IsBuyer: false}, + } + + var stockManager = &StockDistribution{ + TradingFeeCurrency: "BNB", + Symbol: "BTCUSDT", + } + + _, err := stockManager.AddTrades(trades) + assert.NoError(t, err) + assert.Len(t, stockManager.Stocks, 0) + assert.Len(t, stockManager.PendingSells, 0) + }) + + t.Run("oversell", func(t *testing.T) { + var trades = []types.Trade{ + {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, + {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.05, IsBuyer: false}, + {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.05, IsBuyer: false}, + } + + var stockManager = &StockDistribution{ + TradingFeeCurrency: "BNB", + Symbol: "BTCUSDT", + } + + _, err := stockManager.AddTrades(trades) + assert.NoError(t, err) + assert.Len(t, stockManager.Stocks, 0) + assert.Len(t, stockManager.PendingSells, 1) + }) + + t.Run("loss sell", func(t *testing.T) { + var trades = []types.Trade{ + {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, + {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.02, IsBuyer: false}, + {Symbol: "BTCUSDT", Price: 8000.0, Quantity: 0.01, IsBuyer: false}, + } + + var stockManager = &StockDistribution{ + TradingFeeCurrency: "BNB", + Symbol: "BTCUSDT", + } + + _, err := stockManager.AddTrades(trades) + assert.NoError(t, err) + assert.Len(t, stockManager.Stocks, 1) + assert.Equal(t, StockSlice{ + { + Symbol: "BTCUSDT", + Price: 9100.0, + Quantity: 0.02, + IsBuyer: true, + }, + }, stockManager.Stocks) + assert.Len(t, stockManager.PendingSells, 0) + }) + + t.Run("pending sell 1", func(t *testing.T) { + var trades = []types.Trade{ + {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.02}, + {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, + } + + var stockManager = &StockDistribution{ + TradingFeeCurrency: "BNB", + Symbol: "BTCUSDT", + } + + _, err := stockManager.AddTrades(trades) + assert.NoError(t, err) + assert.Len(t, stockManager.Stocks, 1) + assert.Equal(t, StockSlice{ + { + Symbol: "BTCUSDT", + Price: 9100.0, + Quantity: 0.03, + IsBuyer: true, + }, + }, stockManager.Stocks) + assert.Len(t, stockManager.PendingSells, 0) + }) + + t.Run("pending sell 2", func(t *testing.T) { + var trades = []types.Trade{ + {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.1}, + {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, + } + + var stockManager = &StockDistribution{ + TradingFeeCurrency: "BNB", + Symbol: "BTCUSDT", + } + + _, err := stockManager.AddTrades(trades) + assert.NoError(t, err) + assert.Len(t, stockManager.Stocks, 0) + assert.Len(t, stockManager.PendingSells, 1) + assert.Equal(t, StockSlice{ + { + Symbol: "BTCUSDT", + Price: 9200.0, + Quantity: 0.05, + IsBuyer: false, + }, + }, stockManager.PendingSells) + }) + +} diff --git a/pkg/bbgo/testdata/btcusdt-trades.json b/pkg/accounting/testdata/btcusdt-trades.json similarity index 100% rename from pkg/bbgo/testdata/btcusdt-trades.json rename to pkg/accounting/testdata/btcusdt-trades.json diff --git a/pkg/bbgo/stock_test.go b/pkg/bbgo/stock_test.go index 141db8f3c..920078f66 100644 --- a/pkg/bbgo/stock_test.go +++ b/pkg/bbgo/stock_test.go @@ -1,184 +1,2 @@ package bbgo -import ( - "encoding/json" - "io/ioutil" - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/c9s/bbgo/pkg/accounting" - "github.com/c9s/bbgo/pkg/types" -) - -func TestStockManager(t *testing.T) { - - t.Run("testdata", func(t *testing.T) { - var trades []types.Trade - - out, err := ioutil.ReadFile("testdata/btcusdt-trades.json") - assert.NoError(t, err) - - err = json.Unmarshal(out, &trades) - assert.NoError(t, err) - - var stockManager = &accounting.StockDistribution{ - TradingFeeCurrency: "BNB", - Symbol: "BTCUSDT", - } - - _, err = stockManager.AddTrades(trades) - assert.NoError(t, err) - assert.Equal(t, 0.72970242, stockManager.Stocks.Quantity()) - assert.NotEmpty(t, stockManager.Stocks) - assert.Equal(t, 20, len(stockManager.Stocks)) - assert.Equal(t, 0, len(stockManager.PendingSells)) - - }) - - t.Run("stock", func(t *testing.T) { - var trades = []types.Trade{ - {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, - {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, - {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.01, IsBuyer: false}, - } - - var stockManager = &accounting.StockDistribution{ - TradingFeeCurrency: "BNB", - Symbol: "BTCUSDT", - } - - _, err := stockManager.AddTrades(trades) - assert.NoError(t, err) - assert.Len(t, stockManager.Stocks, 2) - assert.Equal(t, accounting.StockSlice{ - { - Symbol: "BTCUSDT", - Price: 9100.0, - Quantity: 0.05, - IsBuyer: true, - }, - { - Symbol: "BTCUSDT", - Price: 9100.0, - Quantity: 0.04, - IsBuyer: true, - }, - }, stockManager.Stocks) - assert.Len(t, stockManager.PendingSells, 0) - }) - - t.Run("sold out", func(t *testing.T) { - var trades = []types.Trade{ - {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, - {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.05, IsBuyer: false}, - {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, - {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.05, IsBuyer: false}, - } - - var stockManager = &accounting.StockDistribution{ - TradingFeeCurrency: "BNB", - Symbol: "BTCUSDT", - } - - _, err := stockManager.AddTrades(trades) - assert.NoError(t, err) - assert.Len(t, stockManager.Stocks, 0) - assert.Len(t, stockManager.PendingSells, 0) - }) - - t.Run("oversell", func(t *testing.T) { - var trades = []types.Trade{ - {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, - {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.05, IsBuyer: false}, - {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.05, IsBuyer: false}, - } - - var stockManager = &accounting.StockDistribution{ - TradingFeeCurrency: "BNB", - Symbol: "BTCUSDT", - } - - _, err := stockManager.AddTrades(trades) - assert.NoError(t, err) - assert.Len(t, stockManager.Stocks, 0) - assert.Len(t, stockManager.PendingSells, 1) - }) - - t.Run("loss sell", func(t *testing.T) { - var trades = []types.Trade{ - {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, - {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.02, IsBuyer: false}, - {Symbol: "BTCUSDT", Price: 8000.0, Quantity: 0.01, IsBuyer: false}, - } - - var stockManager = &accounting.StockDistribution{ - TradingFeeCurrency: "BNB", - Symbol: "BTCUSDT", - } - - _, err := stockManager.AddTrades(trades) - assert.NoError(t, err) - assert.Len(t, stockManager.Stocks, 1) - assert.Equal(t, accounting.StockSlice{ - { - Symbol: "BTCUSDT", - Price: 9100.0, - Quantity: 0.02, - IsBuyer: true, - }, - }, stockManager.Stocks) - assert.Len(t, stockManager.PendingSells, 0) - }) - - t.Run("pending sell 1", func(t *testing.T) { - var trades = []types.Trade{ - {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.02}, - {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, - } - - var stockManager = &accounting.StockDistribution{ - TradingFeeCurrency: "BNB", - Symbol: "BTCUSDT", - } - - _, err := stockManager.AddTrades(trades) - assert.NoError(t, err) - assert.Len(t, stockManager.Stocks, 1) - assert.Equal(t, accounting.StockSlice{ - { - Symbol: "BTCUSDT", - Price: 9100.0, - Quantity: 0.03, - IsBuyer: true, - }, - }, stockManager.Stocks) - assert.Len(t, stockManager.PendingSells, 0) - }) - - t.Run("pending sell 2", func(t *testing.T) { - var trades = []types.Trade{ - {Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.1}, - {Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true}, - } - - var stockManager = &accounting.StockDistribution{ - TradingFeeCurrency: "BNB", - Symbol: "BTCUSDT", - } - - _, err := stockManager.AddTrades(trades) - assert.NoError(t, err) - assert.Len(t, stockManager.Stocks, 0) - assert.Len(t, stockManager.PendingSells, 1) - assert.Equal(t, accounting.StockSlice{ - { - Symbol: "BTCUSDT", - Price: 9200.0, - Quantity: 0.05, - IsBuyer: false, - }, - }, stockManager.PendingSells) - }) - -}