2020-11-05 03:00:51 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 08:39:48 +00:00
|
|
|
"errors"
|
2020-11-05 03:00:51 +00:00
|
|
|
"time"
|
|
|
|
|
2022-01-23 07:14:29 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/cache"
|
2024-03-26 03:55:28 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2022-01-23 07:14:29 +00:00
|
|
|
|
2021-12-31 06:12:41 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-11-05 03:00:51 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2021-03-13 12:49:51 +00:00
|
|
|
var ErrNotImplemented = errors.New("not implemented")
|
|
|
|
var ErrExchangeRewardServiceNotImplemented = errors.New("exchange does not implement ExchangeRewardService interface")
|
2021-02-23 08:39:48 +00:00
|
|
|
|
2020-11-05 03:00:51 +00:00
|
|
|
type SyncService struct {
|
2021-03-14 03:04:56 +00:00
|
|
|
TradeService *TradeService
|
|
|
|
OrderService *OrderService
|
|
|
|
RewardService *RewardService
|
|
|
|
WithdrawService *WithdrawService
|
|
|
|
DepositService *DepositService
|
2022-06-01 07:54:50 +00:00
|
|
|
MarginService *MarginService
|
2022-03-17 12:06:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 02:26:13 +00:00
|
|
|
// SyncSessionSymbols syncs the trades from the given exchange session
|
2023-11-14 08:09:29 +00:00
|
|
|
func (s *SyncService) SyncSessionSymbols(
|
2024-06-19 06:18:21 +00:00
|
|
|
ctx context.Context, exchange types.Exchange,
|
|
|
|
startTime, endTime time.Time,
|
|
|
|
symbols ...string,
|
2023-11-14 08:09:29 +00:00
|
|
|
) error {
|
2022-01-23 07:14:29 +00:00
|
|
|
markets, err := cache.LoadExchangeMarketsWithCache(ctx, exchange)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-13 11:32:38 +00:00
|
|
|
|
2022-01-23 07:14:29 +00:00
|
|
|
for _, symbol := range symbols {
|
2023-11-14 08:09:29 +00:00
|
|
|
// skip symbols do not exist in the market info
|
|
|
|
if _, ok := markets[symbol]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("syncing %s %s trades from %s...", exchange.Name(), symbol, startTime)
|
2024-06-19 06:18:21 +00:00
|
|
|
if err := s.TradeService.Sync(ctx, exchange, symbol, startTime, endTime); err != nil {
|
2023-11-14 08:09:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("syncing %s %s orders from %s...", exchange.Name(), symbol, startTime)
|
2024-06-19 06:18:21 +00:00
|
|
|
if err := s.OrderService.Sync(ctx, exchange, symbol, startTime, endTime); err != nil {
|
2023-11-14 08:09:29 +00:00
|
|
|
return err
|
2021-02-19 02:26:13 +00:00
|
|
|
}
|
2021-03-14 03:04:56 +00:00
|
|
|
}
|
2021-02-23 08:39:48 +00:00
|
|
|
|
2022-06-01 07:54:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-14 08:09:29 +00:00
|
|
|
func (s *SyncService) SyncMarginHistory(
|
|
|
|
ctx context.Context, exchange types.Exchange, startTime time.Time, assets ...string,
|
|
|
|
) error {
|
2023-03-25 16:53:43 +00:00
|
|
|
if _, implemented := exchange.(types.MarginHistoryService); !implemented {
|
|
|
|
log.Debugf("exchange %T does not support types.MarginHistoryService", exchange)
|
2022-06-01 07:54:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if marginExchange, implemented := exchange.(types.MarginExchange); !implemented {
|
|
|
|
log.Debugf("exchange %T does not implement types.MarginExchange", exchange)
|
2022-03-17 12:15:25 +00:00
|
|
|
return nil
|
2022-06-01 07:54:50 +00:00
|
|
|
} else {
|
|
|
|
marginSettings := marginExchange.GetMarginSettings()
|
|
|
|
if !marginSettings.IsMargin {
|
|
|
|
log.Debugf("exchange %T is not using margin", exchange)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("syncing %s margin history: %v...", exchange.Name(), assets)
|
|
|
|
for _, asset := range assets {
|
|
|
|
if err := s.MarginService.Sync(ctx, exchange, asset, startTime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-17 12:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:18:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-17 17:42:33 +00:00
|
|
|
func (s *SyncService) SyncRewardHistory(ctx context.Context, exchange types.Exchange, startTime time.Time) error {
|
2022-06-01 07:54:50 +00:00
|
|
|
if _, implemented := exchange.(types.ExchangeRewardService); !implemented {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 09:18:42 +00:00
|
|
|
log.Infof("syncing %s reward records...", exchange.Name())
|
2024-03-26 03:55:28 +00:00
|
|
|
if util.IsPaperTrade() {
|
|
|
|
log.Info("reward is not supported in paper trading")
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-17 17:42:33 +00:00
|
|
|
if err := s.RewardService.Sync(ctx, exchange, startTime); err != nil {
|
2022-06-01 07:54:50 +00:00
|
|
|
return err
|
2022-04-25 09:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-08 09:32:42 +00:00
|
|
|
func (s *SyncService) SyncDepositHistory(ctx context.Context, exchange types.Exchange, startTime time.Time) error {
|
2022-03-17 12:15:25 +00:00
|
|
|
log.Infof("syncing %s deposit records...", exchange.Name())
|
2024-03-26 03:55:28 +00:00
|
|
|
if util.IsPaperTrade() {
|
|
|
|
log.Info("deposit is not supported in paper trading")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-08 09:32:42 +00:00
|
|
|
if err := s.DepositService.Sync(ctx, exchange, startTime); err != nil {
|
2022-03-17 12:15:25 +00:00
|
|
|
if err != ErrNotImplemented {
|
2022-04-25 09:18:42 +00:00
|
|
|
log.Warnf("%s deposit service is not supported", exchange.Name())
|
2022-03-17 12:15:25 +00:00
|
|
|
return err
|
2021-03-14 02:44:02 +00:00
|
|
|
}
|
2022-03-17 12:15:25 +00:00
|
|
|
}
|
2021-03-14 02:44:02 +00:00
|
|
|
|
2022-04-25 09:18:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-02 05:38:54 +00:00
|
|
|
func (s *SyncService) SyncWithdrawHistory(ctx context.Context, exchange types.Exchange, startTime time.Time) error {
|
2022-03-17 12:15:25 +00:00
|
|
|
log.Infof("syncing %s withdraw records...", exchange.Name())
|
2024-03-26 03:55:28 +00:00
|
|
|
if util.IsPaperTrade() {
|
|
|
|
log.Info("withdraw is not supported in paper trading")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-02 05:38:54 +00:00
|
|
|
if err := s.WithdrawService.Sync(ctx, exchange, startTime); err != nil {
|
2022-03-17 12:15:25 +00:00
|
|
|
if err != ErrNotImplemented {
|
2022-04-25 09:18:42 +00:00
|
|
|
log.Warnf("%s withdraw service is not supported", exchange.Name())
|
2022-03-17 12:15:25 +00:00
|
|
|
return err
|
2021-02-23 08:39:48 +00:00
|
|
|
}
|
2021-02-19 02:26:13 +00:00
|
|
|
}
|
2022-03-17 12:15:25 +00:00
|
|
|
|
2021-02-19 02:26:13 +00:00
|
|
|
return nil
|
|
|
|
}
|