pull out types.Exchange interfaces to make it minimal

This commit is contained in:
c9s 2021-05-26 01:44:32 +08:00
parent c8cb75cabc
commit 4ded82c94e
4 changed files with 43 additions and 14 deletions

View File

@ -91,7 +91,14 @@ var listOrdersCmd = &cobra.Command{
return err return err
} }
case "closed": case "closed":
os, err = session.Exchange.QueryClosedOrders(ctx, symbol, time.Now().Add(-3*24*time.Hour), time.Now(), 0) tradeHistoryService, ok := session.Exchange.(types.ExchangeTradeHistoryService)
if !ok {
// skip exchanges that does not support trading history services
log.Warnf("exchange %s does not implement ExchangeTradeHistoryService, skip syncing closed orders", session.Exchange.Name())
return nil
}
os, err = tradeHistoryService.QueryClosedOrders(ctx, symbol, time.Now().Add(-3*24*time.Hour), time.Now(), 0)
if err != nil { if err != nil {
return err return err
} }

View File

@ -81,7 +81,15 @@ var tradesCmd = &cobra.Command{
until := time.Now() until := time.Now()
since := until.Add(-3 * 24 * time.Hour) since := until.Add(-3 * 24 * time.Hour)
trades, err := session.Exchange.QueryTrades(ctx, symbol, &types.TradeQueryOptions{
tradeHistoryService, ok := session.Exchange.(types.ExchangeTradeHistoryService)
if !ok {
// skip exchanges that does not support trading history services
log.Warnf("exchange %s does not implement ExchangeTradeHistoryService, skip syncing closed orders", session.Exchange.Name())
return nil
}
trades, err := tradeHistoryService.QueryTrades(ctx, symbol, &types.TradeQueryOptions{
StartTime: &since, StartTime: &since,
EndTime: &until, EndTime: &until,
Limit: limit, Limit: limit,

View File

@ -18,6 +18,13 @@ func (e ClosedOrderBatchQuery) Query(ctx context.Context, symbol string, startTi
c = make(chan types.Order, 500) c = make(chan types.Order, 500)
errC = make(chan error, 1) errC = make(chan error, 1)
tradeHistoryService, ok := e.Exchange.(types.ExchangeTradeHistoryService)
if !ok {
// skip exchanges that does not support trading history services
logrus.Warnf("exchange %s does not implement ExchangeTradeHistoryService, skip syncing closed orders", e.Exchange.Name())
return c, errC
}
go func() { go func() {
limiter := rate.NewLimiter(rate.Every(5*time.Second), 2) // from binance (original 1200, use 1000 for safety) limiter := rate.NewLimiter(rate.Every(5*time.Second), 2) // from binance (original 1200, use 1000 for safety)
@ -36,7 +43,7 @@ func (e ClosedOrderBatchQuery) Query(ctx context.Context, symbol string, startTi
logrus.Infof("batch querying %s closed orders %s <=> %s", symbol, startTime, endTime) logrus.Infof("batch querying %s closed orders %s <=> %s", symbol, startTime, endTime)
orders, err := e.QueryClosedOrders(ctx, symbol, startTime, endTime, lastOrderID) orders, err := tradeHistoryService.QueryClosedOrders(ctx, symbol, startTime, endTime, lastOrderID)
if err != nil { if err != nil {
errC <- err errC <- err
return return
@ -116,6 +123,13 @@ func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *type
c = make(chan types.Trade, 500) c = make(chan types.Trade, 500)
errC = make(chan error, 1) errC = make(chan error, 1)
tradeHistoryService, ok := e.Exchange.(types.ExchangeTradeHistoryService)
if !ok {
// skip exchanges that does not support trading history services
logrus.Warnf("exchange %s does not implement ExchangeTradeHistoryService, skip syncing closed orders", e.Exchange.Name())
return c, errC
}
var lastTradeID = options.LastTradeID var lastTradeID = options.LastTradeID
go func() { go func() {
@ -136,7 +150,7 @@ func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *type
var err error var err error
var trades []types.Trade var trades []types.Trade
trades, err = e.Exchange.QueryTrades(ctx, symbol, &types.TradeQueryOptions{ trades, err = tradeHistoryService.QueryTrades(ctx, symbol, &types.TradeQueryOptions{
Limit: options.Limit, Limit: options.Limit,
LastTradeID: lastTradeID, LastTradeID: lastTradeID,
}) })

View File

@ -40,9 +40,9 @@ func (n ExchangeName) String() string {
} }
const ( const (
ExchangeMax = ExchangeName("max") ExchangeMax = ExchangeName("max")
ExchangeBinance = ExchangeName("binance") ExchangeBinance = ExchangeName("binance")
ExchangeFTX = ExchangeName("ftx") ExchangeFTX = ExchangeName("ftx")
ExchangeBacktest = ExchangeName("backtest") ExchangeBacktest = ExchangeName("backtest")
) )
@ -64,28 +64,28 @@ type Exchange interface {
PlatformFeeCurrency() string PlatformFeeCurrency() string
// required implementation
ExchangeMarketDataService ExchangeMarketDataService
ExchangeTradingService ExchangeTradeService
} }
type ExchangeTradingService interface { type ExchangeTradeService interface {
QueryAccount(ctx context.Context) (*Account, error) QueryAccount(ctx context.Context) (*Account, error)
QueryAccountBalances(ctx context.Context) (BalanceMap, error) QueryAccountBalances(ctx context.Context) (BalanceMap, error)
QueryTrades(ctx context.Context, symbol string, options *TradeQueryOptions) ([]Trade, error)
SubmitOrders(ctx context.Context, orders ...SubmitOrder) (createdOrders OrderSlice, err error) SubmitOrders(ctx context.Context, orders ...SubmitOrder) (createdOrders OrderSlice, err error)
QueryOpenOrders(ctx context.Context, symbol string) (orders []Order, err error) QueryOpenOrders(ctx context.Context, symbol string) (orders []Order, err error)
QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []Order, err error)
CancelOrders(ctx context.Context, orders ...Order) error CancelOrders(ctx context.Context, orders ...Order) error
} }
type ExchangeTradeHistoryService interface {
QueryTrades(ctx context.Context, symbol string, options *TradeQueryOptions) ([]Trade, error)
QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []Order, err error)
}
type ExchangeMarketDataService interface { type ExchangeMarketDataService interface {
NewStream() Stream NewStream() Stream