use existing interface

This commit is contained in:
chiahung 2023-05-24 14:58:19 +08:00
parent 18a7520fa7
commit 49971a2e50
4 changed files with 18 additions and 15 deletions

View File

@ -13,14 +13,6 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
type QueryTradesService interface {
QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error)
}
type QueryOrderService interface {
QueryOrder(ctx context.Context, q types.OrderQuery) (*types.Order, error)
}
func (s *Strategy) recoverByScanningTrades(ctx context.Context, session *bbgo.ExchangeSession) error {
defer func() {
s.updateGridNumOfOrdersMetricsWithLock()
@ -115,7 +107,7 @@ func (s *Strategy) recoverByScanningTrades(ctx context.Context, session *bbgo.Ex
return nil
}
func (s *Strategy) getFilledOrdersByScanningTrades(ctx context.Context, queryTradesService QueryTradesService, queryOrderService QueryOrderService, openOrdersOnGrid []types.Order) ([]types.Order, error) {
func (s *Strategy) getFilledOrdersByScanningTrades(ctx context.Context, queryTradesService types.ExchangeTradeHistoryService, queryOrderService types.ExchangeOrderQueryService, openOrdersOnGrid []types.Order) ([]types.Order, error) {
// set grid
grid := s.newGrid()
s.setGrid(grid)
@ -227,7 +219,7 @@ func (s *Strategy) buildTwinOrderMap(pins []Pin, openOrders []types.Order) (Twin
// buildFilledTwinOrderMapFromTrades will query the trades from last 24 hour and use them to build a pin order map
// It will skip the orders on pins at which open orders are already
func (s *Strategy) buildFilledTwinOrderMapFromTrades(ctx context.Context, queryTradesService QueryTradesService, queryOrderService QueryOrderService, twinOrdersOpen TwinOrderMap, expectedFillNum int) (TwinOrderMap, error) {
func (s *Strategy) buildFilledTwinOrderMapFromTrades(ctx context.Context, queryTradesService types.ExchangeTradeHistoryService, queryOrderService types.ExchangeOrderQueryService, twinOrdersOpen TwinOrderMap, expectedFillNum int) (TwinOrderMap, error) {
twinOrdersFilled := make(TwinOrderMap)
// existedOrders is used to avoid re-query the same orders
@ -240,8 +232,8 @@ func (s *Strategy) buildFilledTwinOrderMapFromTrades(ctx context.Context, queryT
// hard limit for recover
recoverSinceLimit := time.Date(2023, time.March, 10, 0, 0, 0, 0, time.UTC)
if s.RecoverWithin != 0 && until.Add(-1*s.RecoverWithin).After(recoverSinceLimit) {
recoverSinceLimit = until.Add(-1 * s.RecoverWithin)
if s.RecoverGridWithin != 0 && until.Add(-1*s.RecoverGridWithin).After(recoverSinceLimit) {
recoverSinceLimit = until.Add(-1 * s.RecoverGridWithin)
}
for {
@ -271,7 +263,7 @@ func (s *Strategy) buildFilledTwinOrderMapFromTrades(ctx context.Context, queryT
return twinOrdersFilled, nil
}
func (s *Strategy) queryTradesToUpdateTwinOrdersMap(ctx context.Context, queryTradesService QueryTradesService, queryOrderService QueryOrderService, twinOrdersOpen, twinOrdersFilled TwinOrderMap, existedOrders *types.SyncOrderMap, since, until time.Time) error {
func (s *Strategy) queryTradesToUpdateTwinOrdersMap(ctx context.Context, queryTradesService types.ExchangeTradeHistoryService, queryOrderService types.ExchangeOrderQueryService, twinOrdersOpen, twinOrdersFilled TwinOrderMap, existedOrders *types.SyncOrderMap, since, until time.Time) error {
var fromTradeID uint64 = 0
var limit int64 = 1000
for {

View File

@ -11,6 +11,7 @@ import (
"sort"
"strconv"
"testing"
"time"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
@ -65,6 +66,16 @@ func (t *TestDataService) QueryOrder(ctx context.Context, q types.OrderQuery) (*
return &order, nil
}
// dummy method for interface
func (t *TestDataService) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error) {
return nil, nil
}
// dummy method for interface
func (t *TestDataService) QueryOrderTrades(ctx context.Context, q types.OrderQuery) ([]types.Trade, error) {
return nil, nil
}
func NewStrategy(t *TestData) *Strategy {
s := t.Strategy
s.Debug = true

View File

@ -167,7 +167,7 @@ type Strategy struct {
SkipSpreadCheck bool `json:"skipSpreadCheck"`
RecoverGridByScanningTrades bool `json:"recoverGridByScanningTrades"`
RecoverWithin time.Duration `json:"recoverWithin"`
RecoverGridWithin time.Duration `json:"recoverGridWithin"`
EnableProfitFixer bool `json:"enableProfitFixer"`
FixProfitSince *types.Time `json:"fixProfitSince"`

View File

@ -99,7 +99,7 @@ func (m TwinOrderMap) String() string {
}
sort.Slice(pins, func(i, j int) bool {
return pins[j] < pins[i]
return pins[j].Compare(pins[i]) < 0
})
sb.WriteString("================== TWIN ORDER MAP ==================\n")