mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
use existing interface
This commit is contained in:
parent
18a7520fa7
commit
49971a2e50
|
@ -13,14 +13,6 @@ import (
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"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 {
|
func (s *Strategy) recoverByScanningTrades(ctx context.Context, session *bbgo.ExchangeSession) error {
|
||||||
defer func() {
|
defer func() {
|
||||||
s.updateGridNumOfOrdersMetricsWithLock()
|
s.updateGridNumOfOrdersMetricsWithLock()
|
||||||
|
@ -115,7 +107,7 @@ func (s *Strategy) recoverByScanningTrades(ctx context.Context, session *bbgo.Ex
|
||||||
return nil
|
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
|
// set grid
|
||||||
grid := s.newGrid()
|
grid := s.newGrid()
|
||||||
s.setGrid(grid)
|
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
|
// 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
|
// 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)
|
twinOrdersFilled := make(TwinOrderMap)
|
||||||
|
|
||||||
// existedOrders is used to avoid re-query the same orders
|
// 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
|
// hard limit for recover
|
||||||
recoverSinceLimit := time.Date(2023, time.March, 10, 0, 0, 0, 0, time.UTC)
|
recoverSinceLimit := time.Date(2023, time.March, 10, 0, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
if s.RecoverWithin != 0 && until.Add(-1*s.RecoverWithin).After(recoverSinceLimit) {
|
if s.RecoverGridWithin != 0 && until.Add(-1*s.RecoverGridWithin).After(recoverSinceLimit) {
|
||||||
recoverSinceLimit = until.Add(-1 * s.RecoverWithin)
|
recoverSinceLimit = until.Add(-1 * s.RecoverGridWithin)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
@ -271,7 +263,7 @@ func (s *Strategy) buildFilledTwinOrderMapFromTrades(ctx context.Context, queryT
|
||||||
return twinOrdersFilled, nil
|
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 fromTradeID uint64 = 0
|
||||||
var limit int64 = 1000
|
var limit int64 = 1000
|
||||||
for {
|
for {
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/bbgo"
|
"github.com/c9s/bbgo/pkg/bbgo"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
|
@ -65,6 +66,16 @@ func (t *TestDataService) QueryOrder(ctx context.Context, q types.OrderQuery) (*
|
||||||
return &order, nil
|
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 {
|
func NewStrategy(t *TestData) *Strategy {
|
||||||
s := t.Strategy
|
s := t.Strategy
|
||||||
s.Debug = true
|
s.Debug = true
|
||||||
|
|
|
@ -167,7 +167,7 @@ type Strategy struct {
|
||||||
|
|
||||||
SkipSpreadCheck bool `json:"skipSpreadCheck"`
|
SkipSpreadCheck bool `json:"skipSpreadCheck"`
|
||||||
RecoverGridByScanningTrades bool `json:"recoverGridByScanningTrades"`
|
RecoverGridByScanningTrades bool `json:"recoverGridByScanningTrades"`
|
||||||
RecoverWithin time.Duration `json:"recoverWithin"`
|
RecoverGridWithin time.Duration `json:"recoverGridWithin"`
|
||||||
|
|
||||||
EnableProfitFixer bool `json:"enableProfitFixer"`
|
EnableProfitFixer bool `json:"enableProfitFixer"`
|
||||||
FixProfitSince *types.Time `json:"fixProfitSince"`
|
FixProfitSince *types.Time `json:"fixProfitSince"`
|
||||||
|
|
|
@ -99,7 +99,7 @@ func (m TwinOrderMap) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(pins, func(i, j int) bool {
|
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")
|
sb.WriteString("================== TWIN ORDER MAP ==================\n")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user