bbgo_origin/bbgo/stock_test.go

184 lines
4.6 KiB
Go
Raw Normal View History

2020-08-04 06:14:19 +00:00
package bbgo
import (
2020-08-04 09:03:57 +00:00
"encoding/json"
"io/ioutil"
2020-08-04 06:14:19 +00:00
"testing"
2020-10-05 06:25:58 +00:00
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/types"
2020-08-04 06:14:19 +00:00
)
func TestStockManager(t *testing.T) {
2020-08-04 09:03:57 +00:00
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 = &StockManager{
TradingFeeCurrency: "BNB",
Symbol: "BTCUSDT",
}
2020-08-04 11:05:20 +00:00
_, err = stockManager.AddTrades(trades)
2020-08-04 09:03:57 +00:00
assert.NoError(t, err)
assert.Equal(t, 0.72970242, stockManager.Stocks.Quantity())
assert.NotEmpty(t, stockManager.Stocks)
2020-08-04 09:12:01 +00:00
assert.Equal(t, 20, len(stockManager.Stocks))
2020-08-04 09:03:57 +00:00
assert.Equal(t, 0, len(stockManager.PendingSells))
})
2020-08-04 06:14:19 +00:00
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 = &StockManager{
TradingFeeCurrency: "BNB",
Symbol: "BTCUSDT",
}
2020-08-04 11:05:20 +00:00
_, err := stockManager.AddTrades(trades)
2020-08-04 06:14:19 +00:00
assert.NoError(t, err)
assert.Len(t, stockManager.Stocks, 2)
2020-08-04 09:03:57 +00:00
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)
2020-08-04 06:14:19 +00:00
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 = &StockManager{
TradingFeeCurrency: "BNB",
Symbol: "BTCUSDT",
}
2020-08-04 11:05:20 +00:00
_, err := stockManager.AddTrades(trades)
2020-08-04 06:14:19 +00:00
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 = &StockManager{
TradingFeeCurrency: "BNB",
Symbol: "BTCUSDT",
}
2020-08-04 11:05:20 +00:00
_, err := stockManager.AddTrades(trades)
2020-08-04 06:14:19 +00:00
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 = &StockManager{
TradingFeeCurrency: "BNB",
Symbol: "BTCUSDT",
}
2020-08-04 11:05:20 +00:00
_, err := stockManager.AddTrades(trades)
2020-08-04 06:14:19 +00:00
assert.NoError(t, err)
assert.Len(t, stockManager.Stocks, 1)
2020-08-04 09:03:57 +00:00
assert.Equal(t, StockSlice{
{
Symbol: "BTCUSDT",
Price: 9100.0,
Quantity: 0.02,
IsBuyer: true,
},
}, stockManager.Stocks)
2020-08-04 06:14:19 +00:00
assert.Len(t, stockManager.PendingSells, 0)
})
2020-08-04 09:03:57 +00:00
t.Run("pending sell 1", func(t *testing.T) {
2020-08-04 07:25:09 +00:00
var trades = []types.Trade{
2020-08-04 09:03:57 +00:00
{Symbol: "BTCUSDT", Price: 9200.0, Quantity: 0.02},
2020-08-04 07:25:09 +00:00
{Symbol: "BTCUSDT", Price: 9100.0, Quantity: 0.05, IsBuyer: true},
}
var stockManager = &StockManager{
TradingFeeCurrency: "BNB",
Symbol: "BTCUSDT",
}
2020-08-04 11:05:20 +00:00
_, err := stockManager.AddTrades(trades)
2020-08-04 07:25:09 +00:00
assert.NoError(t, err)
assert.Len(t, stockManager.Stocks, 1)
2020-08-04 09:03:57 +00:00
assert.Equal(t, StockSlice{
{
Symbol: "BTCUSDT",
Price: 9100.0,
Quantity: 0.03,
IsBuyer: true,
},
}, stockManager.Stocks)
2020-08-04 07:25:09 +00:00
assert.Len(t, stockManager.PendingSells, 0)
})
2020-08-04 09:03:57 +00:00
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 = &StockManager{
TradingFeeCurrency: "BNB",
Symbol: "BTCUSDT",
}
2020-08-04 11:05:20 +00:00
_, err := stockManager.AddTrades(trades)
2020-08-04 09:03:57 +00:00
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)
})
2020-08-04 06:14:19 +00:00
}