2020-11-05 03:00:51 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SyncService struct {
|
|
|
|
TradeService *TradeService
|
|
|
|
OrderService *OrderService
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SyncService) SyncOrders(ctx context.Context, exchange types.Exchange, symbol string, startTime time.Time) error {
|
2021-01-19 18:09:12 +00:00
|
|
|
isMargin := false
|
|
|
|
isIsolated := false
|
|
|
|
if marginExchange, ok := exchange.(types.MarginExchange); ok {
|
|
|
|
marginSettings := marginExchange.GetMarginSettings()
|
|
|
|
isMargin = marginSettings.IsMargin
|
|
|
|
isIsolated = marginSettings.IsIsolatedMargin
|
|
|
|
if marginSettings.IsIsolatedMargin {
|
|
|
|
symbol = marginSettings.IsolatedMarginSymbol
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastOrder, err := s.OrderService.QueryLast(exchange.Name(), symbol, isMargin, isIsolated)
|
2020-11-05 03:00:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastID uint64 = 0
|
|
|
|
if lastOrder != nil {
|
|
|
|
lastID = lastOrder.OrderID
|
2021-02-06 06:30:00 +00:00
|
|
|
startTime = lastOrder.CreationTime.Time()
|
2020-11-05 03:00:51 +00:00
|
|
|
|
|
|
|
logrus.Infof("found last order, start from lastID = %d since %s", lastID, startTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
batch := &types.ExchangeBatchProcessor{Exchange: exchange}
|
|
|
|
ordersC, errC := batch.BatchQueryClosedOrders(ctx, symbol, startTime, time.Now(), lastID)
|
|
|
|
for order := range ordersC {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
|
|
|
|
case err := <-errC:
|
2021-01-18 13:59:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-05 03:00:51 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.OrderService.Insert(order); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 16:49:17 +00:00
|
|
|
return <-errC
|
2020-11-05 03:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SyncService) SyncTrades(ctx context.Context, exchange types.Exchange, symbol string, startTime time.Time) error {
|
2021-01-19 18:09:12 +00:00
|
|
|
isMargin := false
|
|
|
|
isIsolated := false
|
|
|
|
if marginExchange, ok := exchange.(types.MarginExchange); ok {
|
|
|
|
marginSettings := marginExchange.GetMarginSettings()
|
|
|
|
isMargin = marginSettings.IsMargin
|
|
|
|
isIsolated = marginSettings.IsIsolatedMargin
|
|
|
|
if marginSettings.IsIsolatedMargin {
|
|
|
|
symbol = marginSettings.IsolatedMarginSymbol
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastTrade, err := s.TradeService.QueryLast(exchange.Name(), symbol, isMargin, isIsolated)
|
2020-11-05 03:00:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastID int64 = 0
|
|
|
|
if lastTrade != nil {
|
|
|
|
lastID = lastTrade.ID
|
2021-02-06 04:32:21 +00:00
|
|
|
startTime = time.Time(lastTrade.Time)
|
2020-11-05 03:00:51 +00:00
|
|
|
|
|
|
|
logrus.Infof("found last trade, start from lastID = %d since %s", lastID, startTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
batch := &types.ExchangeBatchProcessor{Exchange: exchange}
|
2020-11-05 05:35:04 +00:00
|
|
|
tradeC, errC := batch.BatchQueryTrades(ctx, symbol, &types.TradeQueryOptions{
|
2020-11-05 03:00:51 +00:00
|
|
|
StartTime: &startTime,
|
|
|
|
LastTradeID: lastID,
|
|
|
|
})
|
|
|
|
|
2020-11-05 05:35:04 +00:00
|
|
|
for trade := range tradeC {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
|
|
|
|
case err := <-errC:
|
2021-01-18 13:57:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-05 05:35:04 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2020-11-05 03:00:51 +00:00
|
|
|
if err := s.TradeService.Insert(trade); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-05 05:35:04 +00:00
|
|
|
|
2020-11-05 03:00:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 16:49:17 +00:00
|
|
|
return <-errC
|
2020-11-05 03:00:51 +00:00
|
|
|
}
|