2022-11-25 10:07:02 +00:00
|
|
|
//go:build !dnum
|
|
|
|
|
2022-11-15 07:29:30 +00:00
|
|
|
package grid2
|
|
|
|
|
|
|
|
import (
|
2022-12-05 18:30:18 +00:00
|
|
|
"context"
|
2022-11-15 07:29:30 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-12-03 03:31:44 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-11-15 07:29:30 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-11-16 05:29:55 +00:00
|
|
|
|
2022-12-05 18:30:18 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/backtest"
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/exchange"
|
2022-12-05 03:21:07 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2022-12-05 18:30:18 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
2022-11-16 05:29:55 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2022-11-15 07:29:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStrategy_checkRequiredInvestmentByQuantity(t *testing.T) {
|
2022-11-16 05:29:55 +00:00
|
|
|
s := &Strategy{
|
2022-12-03 03:31:44 +00:00
|
|
|
logger: logrus.NewEntry(logrus.New()),
|
|
|
|
|
2022-11-16 05:29:55 +00:00
|
|
|
Market: types.Market{
|
|
|
|
BaseCurrency: "BTC",
|
|
|
|
QuoteCurrency: "USDT",
|
|
|
|
},
|
|
|
|
}
|
2022-11-15 07:29:30 +00:00
|
|
|
|
2022-11-16 04:09:55 +00:00
|
|
|
t.Run("quote to base balance conversion check", func(t *testing.T) {
|
2022-11-17 09:40:59 +00:00
|
|
|
_, requiredQuote, err := s.checkRequiredInvestmentByQuantity(number(0.0), number(10_000.0), number(0.1), number(13_500.0), []Pin{
|
|
|
|
Pin(number(10_000.0)), // 0.1 * 10_000 = 1000 USD (buy)
|
|
|
|
Pin(number(11_000.0)), // 0.1 * 11_000 = 1100 USD (buy)
|
|
|
|
Pin(number(12_000.0)), // 0.1 * 12_000 = 1200 USD (buy)
|
|
|
|
Pin(number(13_000.0)), // 0.1 * 13_000 = 1300 USD (buy)
|
|
|
|
Pin(number(14_000.0)), // 0.1 * 14_000 = 1400 USD (buy)
|
|
|
|
Pin(number(15_000.0)), // 0.1 * 15_000 = 1500 USD
|
|
|
|
})
|
2022-11-16 04:09:55 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, number(6000.0), requiredQuote)
|
|
|
|
})
|
2022-11-16 05:29:55 +00:00
|
|
|
|
|
|
|
t.Run("quote to base balance conversion not enough", func(t *testing.T) {
|
2022-11-17 09:40:59 +00:00
|
|
|
_, requiredQuote, err := s.checkRequiredInvestmentByQuantity(number(0.0), number(5_000.0), number(0.1), number(13_500.0), []Pin{
|
|
|
|
Pin(number(10_000.0)), // 0.1 * 10_000 = 1000 USD (buy)
|
|
|
|
Pin(number(11_000.0)), // 0.1 * 11_000 = 1100 USD (buy)
|
|
|
|
Pin(number(12_000.0)), // 0.1 * 12_000 = 1200 USD (buy)
|
|
|
|
Pin(number(13_000.0)), // 0.1 * 13_000 = 1300 USD (buy)
|
|
|
|
Pin(number(14_000.0)), // 0.1 * 14_000 = 1400 USD (buy)
|
|
|
|
Pin(number(15_000.0)), // 0.1 * 15_000 = 1500 USD
|
|
|
|
})
|
2022-11-16 05:29:55 +00:00
|
|
|
assert.EqualError(t, err, "quote balance (5000.000000 USDT) is not enough, required = quote 6000.000000")
|
|
|
|
assert.Equal(t, number(6000.0), requiredQuote)
|
|
|
|
})
|
2022-11-16 07:11:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 03:21:07 +00:00
|
|
|
type PriceSideAssert struct {
|
|
|
|
Price fixedpoint.Value
|
|
|
|
Side types.SideType
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertPriceSide(t *testing.T, priceSideAsserts []PriceSideAssert, orders []types.SubmitOrder) {
|
|
|
|
for i, a := range priceSideAsserts {
|
2022-12-05 17:17:29 +00:00
|
|
|
assert.Equalf(t, a.Price, orders[i].Price, "order #%d price should be %f", i+1, a.Price.Float64())
|
|
|
|
assert.Equalf(t, a.Side, orders[i].Side, "order at price %f should be %s", a.Price.Float64(), a.Side)
|
2022-12-05 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStrategy_generateGridOrders(t *testing.T) {
|
|
|
|
t.Run("quote only", func(t *testing.T) {
|
|
|
|
s := newTestStrategy()
|
|
|
|
s.grid = NewGrid(s.LowerPrice, s.UpperPrice, fixedpoint.NewFromInt(s.GridNum), s.Market.TickSize)
|
|
|
|
s.grid.CalculateArithmeticPins()
|
|
|
|
s.QuantityOrAmount.Quantity = number(0.01)
|
|
|
|
|
|
|
|
lastPrice := number(15300)
|
|
|
|
orders, err := s.generateGridOrders(number(10000.0), number(0), lastPrice)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, 10, len(orders)) {
|
|
|
|
for _, o := range orders {
|
|
|
|
t.Logf("- %s %s", o.Price.String(), o.Side)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertPriceSide(t, []PriceSideAssert{
|
|
|
|
{number(19000.0), types.SideTypeBuy},
|
|
|
|
{number(18000.0), types.SideTypeBuy},
|
|
|
|
{number(17000.0), types.SideTypeBuy},
|
|
|
|
{number(16000.0), types.SideTypeBuy},
|
|
|
|
{number(15000.0), types.SideTypeBuy},
|
|
|
|
{number(14000.0), types.SideTypeBuy},
|
|
|
|
{number(13000.0), types.SideTypeBuy},
|
|
|
|
{number(12000.0), types.SideTypeBuy},
|
|
|
|
{number(11000.0), types.SideTypeBuy},
|
|
|
|
{number(10000.0), types.SideTypeBuy},
|
|
|
|
}, orders)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("base + quote", func(t *testing.T) {
|
|
|
|
s := newTestStrategy()
|
|
|
|
s.grid = NewGrid(s.LowerPrice, s.UpperPrice, fixedpoint.NewFromInt(s.GridNum), s.Market.TickSize)
|
|
|
|
s.grid.CalculateArithmeticPins()
|
|
|
|
s.QuantityOrAmount.Quantity = number(0.01)
|
|
|
|
|
|
|
|
lastPrice := number(15300)
|
|
|
|
orders, err := s.generateGridOrders(number(10000.0), number(0.021), lastPrice)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, 10, len(orders)) {
|
|
|
|
for _, o := range orders {
|
|
|
|
t.Logf("- %s %s", o.Price.String(), o.Side)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertPriceSide(t, []PriceSideAssert{
|
|
|
|
{number(20000.0), types.SideTypeSell},
|
|
|
|
{number(19000.0), types.SideTypeSell},
|
|
|
|
{number(17000.0), types.SideTypeBuy},
|
|
|
|
{number(16000.0), types.SideTypeBuy},
|
|
|
|
{number(15000.0), types.SideTypeBuy},
|
|
|
|
{number(14000.0), types.SideTypeBuy},
|
|
|
|
{number(13000.0), types.SideTypeBuy},
|
|
|
|
{number(12000.0), types.SideTypeBuy},
|
|
|
|
{number(11000.0), types.SideTypeBuy},
|
|
|
|
{number(10000.0), types.SideTypeBuy},
|
|
|
|
}, orders)
|
|
|
|
})
|
2022-12-05 03:23:21 +00:00
|
|
|
|
|
|
|
t.Run("enough base + quote", func(t *testing.T) {
|
|
|
|
s := newTestStrategy()
|
|
|
|
s.grid = NewGrid(s.LowerPrice, s.UpperPrice, fixedpoint.NewFromInt(s.GridNum), s.Market.TickSize)
|
|
|
|
s.grid.CalculateArithmeticPins()
|
|
|
|
s.QuantityOrAmount.Quantity = number(0.01)
|
|
|
|
|
|
|
|
lastPrice := number(15300)
|
|
|
|
orders, err := s.generateGridOrders(number(10000.0), number(1.0), lastPrice)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, 10, len(orders)) {
|
|
|
|
for _, o := range orders {
|
|
|
|
t.Logf("- %s %s", o.Price.String(), o.Side)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertPriceSide(t, []PriceSideAssert{
|
|
|
|
{number(20000.0), types.SideTypeSell},
|
|
|
|
{number(19000.0), types.SideTypeSell},
|
|
|
|
{number(18000.0), types.SideTypeSell},
|
|
|
|
{number(17000.0), types.SideTypeSell},
|
|
|
|
{number(16000.0), types.SideTypeSell},
|
|
|
|
{number(14000.0), types.SideTypeBuy},
|
|
|
|
{number(13000.0), types.SideTypeBuy},
|
|
|
|
{number(12000.0), types.SideTypeBuy},
|
|
|
|
{number(11000.0), types.SideTypeBuy},
|
|
|
|
{number(10000.0), types.SideTypeBuy},
|
|
|
|
}, orders)
|
|
|
|
})
|
|
|
|
|
2022-12-05 17:17:29 +00:00
|
|
|
t.Run("enough base + quote + profitSpread", func(t *testing.T) {
|
|
|
|
s := newTestStrategy()
|
|
|
|
s.ProfitSpread = number(1_000)
|
|
|
|
s.grid = NewGrid(s.LowerPrice, s.UpperPrice, fixedpoint.NewFromInt(s.GridNum), s.Market.TickSize)
|
|
|
|
s.grid.CalculateArithmeticPins()
|
|
|
|
s.QuantityOrAmount.Quantity = number(0.01)
|
|
|
|
|
|
|
|
lastPrice := number(15300)
|
|
|
|
orders, err := s.generateGridOrders(number(10000.0), number(1.0), lastPrice)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, 11, len(orders)) {
|
|
|
|
for _, o := range orders {
|
|
|
|
t.Logf("- %s %s", o.Price.String(), o.Side)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertPriceSide(t, []PriceSideAssert{
|
|
|
|
{number(21000.0), types.SideTypeSell},
|
|
|
|
{number(20000.0), types.SideTypeSell},
|
|
|
|
{number(19000.0), types.SideTypeSell},
|
|
|
|
{number(18000.0), types.SideTypeSell},
|
|
|
|
{number(17000.0), types.SideTypeSell},
|
|
|
|
|
|
|
|
{number(15000.0), types.SideTypeBuy},
|
|
|
|
{number(14000.0), types.SideTypeBuy},
|
|
|
|
{number(13000.0), types.SideTypeBuy},
|
|
|
|
{number(12000.0), types.SideTypeBuy},
|
|
|
|
{number(11000.0), types.SideTypeBuy},
|
|
|
|
{number(10000.0), types.SideTypeBuy},
|
|
|
|
}, orders)
|
|
|
|
})
|
|
|
|
|
2022-12-05 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 07:11:42 +00:00
|
|
|
func TestStrategy_checkRequiredInvestmentByAmount(t *testing.T) {
|
|
|
|
s := &Strategy{
|
2022-12-03 03:31:44 +00:00
|
|
|
logger: logrus.NewEntry(logrus.New()),
|
2022-11-16 07:11:42 +00:00
|
|
|
Market: types.Market{
|
|
|
|
BaseCurrency: "BTC",
|
|
|
|
QuoteCurrency: "USDT",
|
|
|
|
},
|
|
|
|
}
|
2022-11-16 05:29:55 +00:00
|
|
|
|
2022-11-16 07:11:42 +00:00
|
|
|
t.Run("quote to base balance conversion", func(t *testing.T) {
|
2022-11-25 07:42:45 +00:00
|
|
|
_, requiredQuote, err := s.checkRequiredInvestmentByAmount(
|
2022-11-16 07:11:42 +00:00
|
|
|
number(0.0), number(3_000.0),
|
|
|
|
number(1000.0),
|
|
|
|
number(13_500.0), []Pin{
|
|
|
|
Pin(number(10_000.0)),
|
|
|
|
Pin(number(11_000.0)),
|
|
|
|
Pin(number(12_000.0)),
|
|
|
|
Pin(number(13_000.0)),
|
|
|
|
Pin(number(14_000.0)),
|
|
|
|
Pin(number(15_000.0)),
|
|
|
|
})
|
2022-12-05 03:21:07 +00:00
|
|
|
assert.EqualError(t, err, "quote balance (3000.000000 USDT) is not enough, required = quote 4999.999890")
|
|
|
|
assert.InDelta(t, 4999.999890, requiredQuote.Float64(), number(0.001).Float64())
|
2022-11-16 07:11:42 +00:00
|
|
|
})
|
2022-11-15 07:29:30 +00:00
|
|
|
}
|
2022-11-27 11:19:54 +00:00
|
|
|
|
|
|
|
func TestStrategy_calculateQuoteInvestmentQuantity(t *testing.T) {
|
2022-12-05 17:51:50 +00:00
|
|
|
t.Run("quote quantity", func(t *testing.T) {
|
2022-11-27 11:19:54 +00:00
|
|
|
// quoteInvestment = (10,000 + 11,000 + 12,000 + 13,000 + 14,000) * q
|
|
|
|
// q = quoteInvestment / (10,000 + 11,000 + 12,000 + 13,000 + 14,000)
|
|
|
|
// q = 12_000 / (10,000 + 11,000 + 12,000 + 13,000 + 14,000)
|
|
|
|
// q = 0.2
|
2022-12-05 17:51:50 +00:00
|
|
|
s := newTestStrategy()
|
|
|
|
lastPrice := number(13_500.0)
|
|
|
|
quantity, err := s.calculateQuoteInvestmentQuantity(number(12_000.0), lastPrice, []Pin{
|
2022-11-27 11:19:54 +00:00
|
|
|
Pin(number(10_000.0)), // buy
|
|
|
|
Pin(number(11_000.0)), // buy
|
|
|
|
Pin(number(12_000.0)), // buy
|
|
|
|
Pin(number(13_000.0)), // buy
|
|
|
|
Pin(number(14_000.0)), // buy
|
|
|
|
Pin(number(15_000.0)),
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, number(0.2).String(), quantity.String())
|
|
|
|
})
|
2022-12-05 17:51:50 +00:00
|
|
|
|
|
|
|
t.Run("profit spread", func(t *testing.T) {
|
|
|
|
// quoteInvestment = (10,000 + 11,000 + 12,000 + 13,000 + 14,000 + 15,000) * q
|
|
|
|
// q = quoteInvestment / (10,000 + 11,000 + 12,000 + 13,000 + 14,000 + 15,000)
|
|
|
|
// q = 7500 / (10,000 + 11,000 + 12,000 + 13,000 + 14,000 + 15,000)
|
|
|
|
// q = 0.1
|
|
|
|
s := newTestStrategy()
|
|
|
|
s.ProfitSpread = number(2000.0)
|
|
|
|
lastPrice := number(13_500.0)
|
|
|
|
quantity, err := s.calculateQuoteInvestmentQuantity(number(7500.0), lastPrice, []Pin{
|
|
|
|
Pin(number(10_000.0)), // sell order @ 12_000
|
|
|
|
Pin(number(11_000.0)), // sell order @ 13_000
|
|
|
|
Pin(number(12_000.0)), // sell order @ 14_000
|
|
|
|
Pin(number(13_000.0)), // sell order @ 15_000
|
|
|
|
Pin(number(14_000.0)), // sell order @ 16_000
|
|
|
|
Pin(number(15_000.0)), // sell order @ 17_000
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, number(0.1).String(), quantity.String())
|
|
|
|
})
|
|
|
|
|
2022-11-27 11:19:54 +00:00
|
|
|
}
|
2022-12-04 09:35:35 +00:00
|
|
|
|
|
|
|
func newTestStrategy() *Strategy {
|
|
|
|
market := types.Market{
|
2022-12-05 18:37:33 +00:00
|
|
|
BaseCurrency: "BTC",
|
|
|
|
QuoteCurrency: "USDT",
|
|
|
|
TickSize: number(0.01),
|
|
|
|
PricePrecision: 2,
|
|
|
|
VolumePrecision: 8,
|
2022-12-04 09:35:35 +00:00
|
|
|
}
|
2022-12-04 09:36:35 +00:00
|
|
|
|
2022-12-04 09:35:35 +00:00
|
|
|
s := &Strategy{
|
|
|
|
logger: logrus.NewEntry(logrus.New()),
|
|
|
|
Market: market,
|
|
|
|
GridProfitStats: newGridProfitStats(market),
|
2022-12-04 09:36:35 +00:00
|
|
|
UpperPrice: number(20_000),
|
|
|
|
LowerPrice: number(10_000),
|
|
|
|
GridNum: 10,
|
2022-12-05 18:37:33 +00:00
|
|
|
|
|
|
|
// QuoteInvestment: number(9000.0),
|
2022-12-04 09:35:35 +00:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStrategy_calculateProfit(t *testing.T) {
|
|
|
|
t.Run("earn quote without compound", func(t *testing.T) {
|
|
|
|
s := newTestStrategy()
|
|
|
|
profit := s.calculateProfit(types.Order{
|
|
|
|
SubmitOrder: types.SubmitOrder{
|
|
|
|
Price: number(13_000),
|
|
|
|
Quantity: number(1.0),
|
|
|
|
},
|
|
|
|
}, number(12_000), number(1.0))
|
|
|
|
assert.NotNil(t, profit)
|
|
|
|
assert.Equal(t, "USDT", profit.Currency)
|
|
|
|
assert.InDelta(t, 1000.0, profit.Profit.Float64(), 0.1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("earn quote with compound", func(t *testing.T) {
|
|
|
|
s := newTestStrategy()
|
|
|
|
s.Compound = true
|
|
|
|
|
|
|
|
profit := s.calculateProfit(types.Order{
|
|
|
|
SubmitOrder: types.SubmitOrder{
|
|
|
|
Price: number(13_000),
|
|
|
|
Quantity: number(1.0),
|
|
|
|
},
|
|
|
|
}, number(12_000), number(1.0))
|
|
|
|
assert.NotNil(t, profit)
|
|
|
|
assert.Equal(t, "USDT", profit.Currency)
|
|
|
|
assert.InDelta(t, 1000.0, profit.Profit.Float64(), 0.1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("earn base without compound", func(t *testing.T) {
|
|
|
|
s := newTestStrategy()
|
|
|
|
s.EarnBase = true
|
|
|
|
s.Compound = false
|
|
|
|
|
|
|
|
quoteQuantity := number(12_000).Mul(number(1.0))
|
|
|
|
sellQuantity := quoteQuantity.Div(number(13_000.0))
|
|
|
|
|
|
|
|
buyOrder := types.SubmitOrder{
|
|
|
|
Price: number(12_000.0),
|
|
|
|
Quantity: number(1.0),
|
|
|
|
}
|
|
|
|
|
|
|
|
profit := s.calculateProfit(types.Order{
|
|
|
|
SubmitOrder: types.SubmitOrder{
|
|
|
|
Price: number(13_000.0),
|
|
|
|
Quantity: sellQuantity,
|
|
|
|
},
|
|
|
|
}, buyOrder.Price, buyOrder.Quantity)
|
|
|
|
assert.NotNil(t, profit)
|
|
|
|
assert.Equal(t, "BTC", profit.Currency)
|
|
|
|
assert.InDelta(t, sellQuantity.Float64()-buyOrder.Quantity.Float64(), profit.Profit.Float64(), 0.001)
|
|
|
|
})
|
|
|
|
}
|
2022-12-05 18:30:18 +00:00
|
|
|
|
|
|
|
func TestBacktestStrategy(t *testing.T) {
|
2022-12-05 18:37:33 +00:00
|
|
|
market := types.Market{
|
|
|
|
BaseCurrency: "BTC",
|
|
|
|
QuoteCurrency: "USDT",
|
|
|
|
TickSize: number(0.01),
|
|
|
|
PricePrecision: 2,
|
|
|
|
VolumePrecision: 8,
|
|
|
|
}
|
|
|
|
strategy := &Strategy{
|
|
|
|
logger: logrus.NewEntry(logrus.New()),
|
|
|
|
Symbol: "BTCUSDT",
|
|
|
|
Market: market,
|
|
|
|
GridProfitStats: newGridProfitStats(market),
|
|
|
|
UpperPrice: number(60_000),
|
|
|
|
LowerPrice: number(28_000),
|
|
|
|
GridNum: 100,
|
|
|
|
QuoteInvestment: number(9000.0),
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEMPLATE {{{ start backtest
|
2022-12-05 18:30:18 +00:00
|
|
|
startTime, err := types.ParseLooseFormatTime("2021-06-01")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
endTime, err := types.ParseLooseFormatTime("2021-06-30")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
backtestConfig := &bbgo.Backtest{
|
|
|
|
StartTime: startTime,
|
|
|
|
EndTime: &endTime,
|
|
|
|
RecordTrades: false,
|
|
|
|
FeeMode: bbgo.BacktestFeeModeToken,
|
|
|
|
Accounts: map[string]bbgo.BacktestAccount{
|
|
|
|
"binance": {
|
|
|
|
MakerFeeRate: number(0.075 * 0.01),
|
|
|
|
TakerFeeRate: number(0.075 * 0.01),
|
|
|
|
Balances: bbgo.BacktestAccountBalanceMap{
|
|
|
|
"USDT": number(10_000.0),
|
|
|
|
"BTC": number(1.0),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Symbols: []string{"BTCUSDT"},
|
|
|
|
Sessions: []string{"binance"},
|
|
|
|
SyncSecKLines: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("backtestConfig: %+v", backtestConfig)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
environ := bbgo.NewEnvironment()
|
2022-12-05 18:37:33 +00:00
|
|
|
environ.SetStartTime(startTime.Time())
|
|
|
|
|
2022-12-05 18:30:18 +00:00
|
|
|
err = environ.ConfigureDatabaseDriver(ctx, "sqlite3", "../../../data/bbgo_test.sqlite3")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
|
|
|
|
defer func() {
|
|
|
|
err := environ.DatabaseService.DB.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
environ.BacktestService = backtestService
|
|
|
|
bbgo.SetBackTesting(backtestService)
|
|
|
|
defer bbgo.SetBackTesting(nil)
|
|
|
|
|
|
|
|
exName, err := types.ValidExchangeName("binance")
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
publicExchange, err := exchange.NewPublic(exName)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
backtestExchange, err := backtest.NewExchange(publicExchange.Name(), publicExchange, backtestService, backtestConfig)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
session := environ.AddExchange(exName.String(), backtestExchange)
|
|
|
|
assert.NotNil(t, session)
|
|
|
|
|
|
|
|
err = environ.Init(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
for _, ses := range environ.Sessions() {
|
|
|
|
userDataStream := ses.UserDataStream.(types.StandardStreamEmitter)
|
|
|
|
backtestEx := ses.Exchange.(*backtest.Exchange)
|
|
|
|
backtestEx.MarketDataStream = ses.MarketDataStream.(types.StandardStreamEmitter)
|
|
|
|
backtestEx.BindUserData(userDataStream)
|
|
|
|
}
|
|
|
|
|
|
|
|
trader := bbgo.NewTrader(environ)
|
|
|
|
if assert.NotNil(t, trader) {
|
|
|
|
trader.DisableLogging()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add grid2 to the user config and run backtest
|
2022-12-05 18:37:33 +00:00
|
|
|
userConfig := &bbgo.Config{
|
|
|
|
ExchangeStrategies: []bbgo.ExchangeStrategyMount{
|
|
|
|
{
|
|
|
|
Mounts: []string{"binance"},
|
|
|
|
Strategy: strategy,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = trader.Configure(userConfig)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = trader.Run(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// }}}
|
2022-12-05 18:30:18 +00:00
|
|
|
}
|