2021-10-05 14:06:36 +00:00
|
|
|
/*
|
|
|
|
The backtest process
|
|
|
|
|
|
|
|
The backtest engine loads the klines from the database into a kline-channel,
|
|
|
|
there are multiple matching engine that matches the order sent from the strategy.
|
|
|
|
|
|
|
|
for each kline, the backtest engine:
|
|
|
|
|
|
|
|
1) load the kline, run matching logics to send out order update and trades to the user data stream.
|
|
|
|
2) once the matching process for the kline is done, the kline will be pushed to the market data stream.
|
|
|
|
3) go to 1 and load the next kline.
|
|
|
|
|
|
|
|
There are 2 ways that a strategy could work with backtest engine:
|
|
|
|
|
|
|
|
1. the strategy receives kline from the market data stream, and then it submits the order by the given market data to the backtest engine.
|
|
|
|
backtest engine receives the order and then pushes the trade and order updates to the user data stream.
|
|
|
|
|
|
|
|
the strategy receives the trade and update its position.
|
|
|
|
|
|
|
|
2. the strategy places the orders when it starts. (like grid) the strategy then receives the order updates and then submit a new order
|
|
|
|
by its order update message.
|
|
|
|
|
|
|
|
We need to ensure that:
|
|
|
|
|
|
|
|
1. if the strategy submits the order from the market data stream, since it's a separate goroutine, the strategy should block the backtest engine
|
|
|
|
to process the trades before the next kline is published.
|
|
|
|
*/
|
2020-11-06 18:57:50 +00:00
|
|
|
package backtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-09 08:34:35 +00:00
|
|
|
"fmt"
|
2022-06-26 08:10:10 +00:00
|
|
|
"strconv"
|
2021-11-30 02:40:28 +00:00
|
|
|
"sync"
|
2020-11-06 18:57:50 +00:00
|
|
|
"time"
|
|
|
|
|
2022-06-15 07:32:04 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-01-24 16:24:12 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/cache"
|
|
|
|
|
2020-11-06 18:57:50 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-11-06 19:18:05 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2020-11-06 18:57:50 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2022-06-15 07:32:04 +00:00
|
|
|
var log = logrus.WithField("cmd", "backtest")
|
|
|
|
|
2021-02-18 09:37:49 +00:00
|
|
|
var ErrUnimplemented = errors.New("unimplemented method")
|
|
|
|
|
2020-11-06 18:57:50 +00:00
|
|
|
type Exchange struct {
|
2022-07-03 18:34:46 +00:00
|
|
|
sourceName types.ExchangeName
|
|
|
|
publicExchange types.Exchange
|
|
|
|
srv *service.BacktestService
|
2022-07-03 18:38:42 +00:00
|
|
|
currentTime time.Time
|
2020-11-06 18:57:50 +00:00
|
|
|
|
2020-11-06 19:18:05 +00:00
|
|
|
account *types.Account
|
2020-11-07 08:08:20 +00:00
|
|
|
config *bbgo.Backtest
|
|
|
|
|
2022-07-03 18:27:29 +00:00
|
|
|
MarketDataStream types.StandardStreamEmitter
|
2020-11-07 11:57:36 +00:00
|
|
|
|
2021-12-08 09:26:25 +00:00
|
|
|
trades map[string][]types.Trade
|
2021-12-05 16:38:36 +00:00
|
|
|
tradesMutex sync.Mutex
|
|
|
|
|
2021-12-08 09:26:25 +00:00
|
|
|
closedOrders map[string][]types.Order
|
2021-12-05 16:38:36 +00:00
|
|
|
closedOrdersMutex sync.Mutex
|
|
|
|
|
2021-12-08 09:26:25 +00:00
|
|
|
matchingBooks map[string]*SimplePriceMatching
|
2021-11-30 02:40:28 +00:00
|
|
|
matchingBooksMutex sync.Mutex
|
|
|
|
|
2021-12-08 09:26:25 +00:00
|
|
|
markets types.MarketMap
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 15:24:42 +00:00
|
|
|
func NewExchange(sourceName types.ExchangeName, sourceExchange types.Exchange, srv *service.BacktestService, config *bbgo.Backtest) (*Exchange, error) {
|
|
|
|
ex := sourceExchange
|
2020-11-06 19:18:05 +00:00
|
|
|
|
2022-01-14 08:10:05 +00:00
|
|
|
markets, err := cache.LoadExchangeMarketsWithCache(context.Background(), ex)
|
2020-11-08 13:52:44 +00:00
|
|
|
if err != nil {
|
2021-12-08 15:30:58 +00:00
|
|
|
return nil, err
|
2020-11-08 13:52:44 +00:00
|
|
|
}
|
|
|
|
|
2022-07-03 18:34:46 +00:00
|
|
|
startTime := config.StartTime.Time()
|
2022-05-19 02:01:21 +00:00
|
|
|
configAccount := config.GetAccount(sourceName.String())
|
2022-03-01 13:48:48 +00:00
|
|
|
|
2020-11-06 19:18:05 +00:00
|
|
|
account := &types.Account{
|
2022-03-01 13:48:48 +00:00
|
|
|
MakerFeeRate: configAccount.MakerFeeRate,
|
|
|
|
TakerFeeRate: configAccount.TakerFeeRate,
|
2021-12-25 15:24:42 +00:00
|
|
|
AccountType: types.AccountTypeSpot,
|
2020-11-06 19:18:05 +00:00
|
|
|
}
|
2020-11-10 06:18:04 +00:00
|
|
|
|
2022-03-01 13:48:48 +00:00
|
|
|
balances := configAccount.Balances.BalanceMap()
|
2020-11-06 19:18:05 +00:00
|
|
|
account.UpdateBalances(balances)
|
|
|
|
|
2020-11-07 11:57:36 +00:00
|
|
|
e := &Exchange{
|
2020-11-08 04:13:34 +00:00
|
|
|
sourceName: sourceName,
|
2020-11-06 18:57:50 +00:00
|
|
|
publicExchange: ex,
|
2020-11-08 13:52:44 +00:00
|
|
|
markets: markets,
|
2020-11-06 18:57:50 +00:00
|
|
|
srv: srv,
|
2020-11-06 19:18:05 +00:00
|
|
|
config: config,
|
|
|
|
account: account,
|
2022-07-03 18:38:42 +00:00
|
|
|
currentTime: startTime,
|
2020-11-07 11:57:36 +00:00
|
|
|
closedOrders: make(map[string][]types.Order),
|
2020-11-07 12:34:34 +00:00
|
|
|
trades: make(map[string][]types.Trade),
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
2020-11-07 11:57:36 +00:00
|
|
|
|
2021-12-05 16:38:36 +00:00
|
|
|
e.resetMatchingBooks()
|
2021-12-08 15:30:58 +00:00
|
|
|
return e, nil
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 16:38:36 +00:00
|
|
|
func (e *Exchange) addTrade(trade types.Trade) {
|
|
|
|
e.tradesMutex.Lock()
|
|
|
|
e.trades[trade.Symbol] = append(e.trades[trade.Symbol], trade)
|
|
|
|
e.tradesMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) addClosedOrder(order types.Order) {
|
|
|
|
e.closedOrdersMutex.Lock()
|
|
|
|
e.closedOrders[order.Symbol] = append(e.closedOrders[order.Symbol], order)
|
|
|
|
e.closedOrdersMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) resetMatchingBooks() {
|
|
|
|
e.matchingBooksMutex.Lock()
|
|
|
|
e.matchingBooks = make(map[string]*SimplePriceMatching)
|
|
|
|
for symbol, market := range e.markets {
|
|
|
|
e._addMatchingBook(symbol, market)
|
|
|
|
}
|
|
|
|
e.matchingBooksMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) addMatchingBook(symbol string, market types.Market) {
|
|
|
|
e.matchingBooksMutex.Lock()
|
|
|
|
e._addMatchingBook(symbol, market)
|
|
|
|
e.matchingBooksMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) _addMatchingBook(symbol string, market types.Market) {
|
|
|
|
e.matchingBooks[symbol] = &SimplePriceMatching{
|
2022-07-03 18:38:42 +00:00
|
|
|
CurrentTime: e.currentTime,
|
2022-06-26 08:10:10 +00:00
|
|
|
Account: e.account,
|
|
|
|
Market: market,
|
|
|
|
closedOrders: make(map[uint64]types.Order),
|
2021-12-05 16:38:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 18:57:50 +00:00
|
|
|
func (e *Exchange) NewStream() types.Stream {
|
2022-06-15 07:32:04 +00:00
|
|
|
return &types.BacktestStream{
|
|
|
|
StandardStreamEmitter: &types.StandardStream{},
|
|
|
|
}
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-03 18:29:18 +00:00
|
|
|
func (e *Exchange) QueryOrder(ctx context.Context, q types.OrderQuery) (*types.Order, error) {
|
|
|
|
book := e.matchingBooks[q.Symbol]
|
|
|
|
oid, err := strconv.ParseUint(q.OrderID, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
order, ok := book.getOrder(oid)
|
|
|
|
if ok {
|
|
|
|
return &order, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error) {
|
2020-11-07 11:57:36 +00:00
|
|
|
for _, order := range orders {
|
|
|
|
symbol := order.Symbol
|
2021-11-30 02:40:28 +00:00
|
|
|
matching, ok := e.matchingBook(symbol)
|
2020-11-07 11:57:36 +00:00
|
|
|
if !ok {
|
2020-11-09 08:34:35 +00:00
|
|
|
return nil, fmt.Errorf("matching engine is not initialized for symbol %s", symbol)
|
2020-11-07 11:57:36 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
createdOrder, _, err := matching.PlaceOrder(order)
|
2020-11-07 11:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if createdOrder != nil {
|
|
|
|
createdOrders = append(createdOrders, *createdOrder)
|
|
|
|
|
|
|
|
// market order can be closed immediately.
|
|
|
|
switch createdOrder.Status {
|
|
|
|
case types.OrderStatusFilled, types.OrderStatusCanceled, types.OrderStatusRejected:
|
2021-12-05 16:38:36 +00:00
|
|
|
e.addClosedOrder(*createdOrder)
|
2020-11-07 11:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return createdOrders, nil
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
|
2021-11-30 02:40:28 +00:00
|
|
|
matching, ok := e.matchingBook(symbol)
|
2020-11-07 11:57:36 +00:00
|
|
|
if !ok {
|
2020-11-09 08:34:35 +00:00
|
|
|
return nil, fmt.Errorf("matching engine is not initialized for symbol %s", symbol)
|
2020-11-07 11:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return append(matching.bidOrders, matching.askOrders...), nil
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error) {
|
2020-11-07 11:57:36 +00:00
|
|
|
orders, ok := e.closedOrders[symbol]
|
|
|
|
if !ok {
|
2020-11-09 08:34:35 +00:00
|
|
|
return orders, fmt.Errorf("matching engine is not initialized for symbol %s", symbol)
|
2020-11-07 11:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return orders, nil
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error {
|
2020-11-07 11:57:36 +00:00
|
|
|
for _, order := range orders {
|
2021-11-30 02:40:28 +00:00
|
|
|
matching, ok := e.matchingBook(order.Symbol)
|
2020-11-07 11:57:36 +00:00
|
|
|
if !ok {
|
2020-11-09 08:34:35 +00:00
|
|
|
return fmt.Errorf("matching engine is not initialized for symbol %s", order.Symbol)
|
2020-11-07 11:57:36 +00:00
|
|
|
}
|
2022-07-03 18:20:50 +00:00
|
|
|
_, err := matching.CancelOrder(order)
|
2020-11-08 05:07:20 +00:00
|
|
|
if err != nil {
|
2020-11-07 11:57:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
|
2020-11-06 19:18:05 +00:00
|
|
|
return e.account, nil
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 19:18:05 +00:00
|
|
|
func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
|
|
|
|
return e.account.Balances(), nil
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
|
2020-11-08 04:13:34 +00:00
|
|
|
if options.EndTime != nil {
|
2021-05-07 16:57:12 +00:00
|
|
|
return e.srv.QueryKLinesBackward(e.sourceName, symbol, interval, *options.EndTime, 1000)
|
2020-11-08 04:13:34 +00:00
|
|
|
}
|
2021-05-07 16:45:24 +00:00
|
|
|
|
2020-11-08 04:13:34 +00:00
|
|
|
if options.StartTime != nil {
|
2021-05-07 16:57:12 +00:00
|
|
|
return e.srv.QueryKLinesForward(e.sourceName, symbol, interval, *options.StartTime, 1000)
|
2020-11-08 04:13:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("endTime or startTime can not be nil")
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
|
2020-11-06 18:57:50 +00:00
|
|
|
// we don't need query trades for backtest
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) {
|
2021-11-30 02:40:28 +00:00
|
|
|
matching, ok := e.matchingBook(symbol)
|
2021-03-15 18:13:52 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("matching engine is not initialized for symbol %s", symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
kline := matching.LastKLine
|
|
|
|
return &types.Ticker{
|
2021-12-15 05:04:01 +00:00
|
|
|
Time: kline.EndTime.Time(),
|
2021-03-15 18:13:52 +00:00
|
|
|
Volume: kline.Volume,
|
|
|
|
Last: kline.Close,
|
|
|
|
Open: kline.Open,
|
|
|
|
High: kline.High,
|
|
|
|
Low: kline.Low,
|
|
|
|
Buy: kline.Close,
|
|
|
|
Sell: kline.Close,
|
|
|
|
}, nil
|
2021-02-18 09:37:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error) {
|
2021-02-06 18:39:43 +00:00
|
|
|
// Not using Tickers in back test (yet)
|
2021-02-18 09:37:49 +00:00
|
|
|
return nil, ErrUnimplemented
|
2021-02-06 18:39:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) Name() types.ExchangeName {
|
2020-11-06 18:57:50 +00:00
|
|
|
return e.publicExchange.Name()
|
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) PlatformFeeCurrency() string {
|
2020-11-06 18:57:50 +00:00
|
|
|
return e.publicExchange.PlatformFeeCurrency()
|
|
|
|
}
|
|
|
|
|
2022-01-29 19:05:19 +00:00
|
|
|
func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
|
2021-12-23 15:02:07 +00:00
|
|
|
return e.markets, nil
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 13:55:43 +00:00
|
|
|
func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) {
|
2020-11-06 18:57:50 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:55:43 +00:00
|
|
|
func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error) {
|
2020-11-06 18:57:50 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-12-08 09:26:25 +00:00
|
|
|
func (e *Exchange) matchingBook(symbol string) (*SimplePriceMatching, bool) {
|
2021-11-30 02:40:28 +00:00
|
|
|
e.matchingBooksMutex.Lock()
|
|
|
|
m, ok := e.matchingBooks[symbol]
|
|
|
|
e.matchingBooksMutex.Unlock()
|
|
|
|
return m, ok
|
|
|
|
}
|
|
|
|
|
2022-07-03 18:27:29 +00:00
|
|
|
func (e *Exchange) BindUserData(userDataStream types.StandardStreamEmitter) {
|
|
|
|
userDataStream.OnTradeUpdate(func(trade types.Trade) {
|
2021-12-25 14:57:28 +00:00
|
|
|
e.addTrade(trade)
|
|
|
|
})
|
|
|
|
|
|
|
|
e.matchingBooksMutex.Lock()
|
|
|
|
for _, matching := range e.matchingBooks {
|
2022-07-03 18:27:29 +00:00
|
|
|
matching.OnTradeUpdate(userDataStream.EmitTradeUpdate)
|
|
|
|
matching.OnOrderUpdate(userDataStream.EmitOrderUpdate)
|
|
|
|
matching.OnBalanceUpdate(userDataStream.EmitBalanceUpdate)
|
2021-12-25 14:57:28 +00:00
|
|
|
}
|
|
|
|
e.matchingBooksMutex.Unlock()
|
2022-03-01 13:48:48 +00:00
|
|
|
}
|
|
|
|
|
2022-07-03 18:34:46 +00:00
|
|
|
func (e *Exchange) SubscribeMarketData(startTime, endTime time.Time, extraIntervals ...types.Interval) (chan types.KLine, error) {
|
2021-12-25 14:57:28 +00:00
|
|
|
log.Infof("collecting backtest configurations...")
|
|
|
|
|
|
|
|
loadedSymbols := map[string]struct{}{}
|
|
|
|
loadedIntervals := map[types.Interval]struct{}{
|
|
|
|
// 1m interval is required for the backtest matching engine
|
|
|
|
types.Interval1m: {},
|
|
|
|
}
|
2022-05-11 05:59:44 +00:00
|
|
|
|
|
|
|
for _, it := range extraIntervals {
|
|
|
|
loadedIntervals[it] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2022-06-09 07:50:06 +00:00
|
|
|
// collect subscriptions
|
2022-06-15 07:32:04 +00:00
|
|
|
for _, sub := range e.MarketDataStream.GetSubscriptions() {
|
2021-12-25 14:57:28 +00:00
|
|
|
loadedSymbols[sub.Symbol] = struct{}{}
|
|
|
|
|
|
|
|
switch sub.Channel {
|
|
|
|
case types.KLineChannel:
|
2022-06-09 07:50:06 +00:00
|
|
|
loadedIntervals[sub.Options.Interval] = struct{}{}
|
2021-12-25 14:57:28 +00:00
|
|
|
|
|
|
|
default:
|
2022-05-13 13:58:35 +00:00
|
|
|
// Since Environment is not yet been injected at this point, no hard error
|
|
|
|
log.Errorf("stream channel %s is not supported in backtest", sub.Channel)
|
2021-12-25 14:57:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var symbols []string
|
|
|
|
for symbol := range loadedSymbols {
|
|
|
|
symbols = append(symbols, symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
var intervals []types.Interval
|
|
|
|
for interval := range loadedIntervals {
|
|
|
|
intervals = append(intervals, interval)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("using symbols: %v and intervals: %v for back-testing", symbols, intervals)
|
|
|
|
log.Infof("querying klines from database...")
|
2022-07-03 18:34:46 +00:00
|
|
|
klineC, errC := e.srv.QueryKLinesCh(startTime, endTime, e, symbols, intervals)
|
2022-03-01 13:48:48 +00:00
|
|
|
go func() {
|
|
|
|
if err := <-errC; err != nil {
|
|
|
|
log.WithError(err).Error("backtest data feed error")
|
2021-12-25 14:57:28 +00:00
|
|
|
}
|
2022-03-01 13:48:48 +00:00
|
|
|
}()
|
|
|
|
return klineC, nil
|
|
|
|
}
|
2021-12-25 14:57:28 +00:00
|
|
|
|
2022-03-01 13:48:48 +00:00
|
|
|
func (e *Exchange) ConsumeKLine(k types.KLine) {
|
|
|
|
if k.Interval == types.Interval1m {
|
2022-07-03 18:38:42 +00:00
|
|
|
e.currentTime = k.EndTime.Time()
|
|
|
|
|
2022-03-01 13:48:48 +00:00
|
|
|
matching, ok := e.matchingBook(k.Symbol)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("matching book of %s is not initialized", k.Symbol)
|
|
|
|
return
|
|
|
|
}
|
2021-12-25 14:57:28 +00:00
|
|
|
|
2022-03-01 13:48:48 +00:00
|
|
|
// here we generate trades and order updates
|
|
|
|
matching.processKLine(k)
|
2021-12-25 14:57:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:32:04 +00:00
|
|
|
e.MarketDataStream.EmitKLineClosed(k)
|
2022-03-01 13:48:48 +00:00
|
|
|
}
|
2021-12-25 14:57:28 +00:00
|
|
|
|
2022-03-01 13:48:48 +00:00
|
|
|
func (e *Exchange) CloseMarketData() error {
|
2022-06-15 07:32:04 +00:00
|
|
|
if err := e.MarketDataStream.Close(); err != nil {
|
2021-12-25 14:57:28 +00:00
|
|
|
log.WithError(err).Error("stream close error")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|