fix markets info cache

This commit is contained in:
c9s 2021-12-08 17:26:25 +08:00
parent e030b87e4e
commit d52edce40b
3 changed files with 28 additions and 32 deletions

View File

@ -55,17 +55,17 @@ type Exchange struct {
userDataStream *Stream userDataStream *Stream
trades map[string][]types.Trade trades map[string][]types.Trade
tradesMutex sync.Mutex tradesMutex sync.Mutex
closedOrders map[string][]types.Order closedOrders map[string][]types.Order
closedOrdersMutex sync.Mutex closedOrdersMutex sync.Mutex
matchingBooks map[string]*SimplePriceMatching matchingBooks map[string]*SimplePriceMatching
matchingBooksMutex sync.Mutex matchingBooksMutex sync.Mutex
markets types.MarketMap markets types.MarketMap
doneC chan struct{} doneC chan struct{}
} }
func NewExchange(sourceName types.ExchangeName, srv *service.BacktestService, config *bbgo.Backtest) *Exchange { func NewExchange(sourceName types.ExchangeName, srv *service.BacktestService, config *bbgo.Backtest) *Exchange {
@ -96,7 +96,7 @@ func NewExchange(sourceName types.ExchangeName, srv *service.BacktestService, co
account := &types.Account{ account := &types.Account{
MakerFeeRate: config.Account.MakerFeeRate, MakerFeeRate: config.Account.MakerFeeRate,
TakerFeeRate: config.Account.TakerFeeRate, TakerFeeRate: config.Account.TakerFeeRate,
AccountType: "SPOT", // currently not used AccountType: "SPOT", // currently not used
} }
balances := config.Account.Balances.BalanceMap() balances := config.Account.Balances.BalanceMap()
@ -149,9 +149,9 @@ func (e *Exchange) addMatchingBook(symbol string, market types.Market) {
func (e *Exchange) _addMatchingBook(symbol string, market types.Market) { func (e *Exchange) _addMatchingBook(symbol string, market types.Market) {
e.matchingBooks[symbol] = &SimplePriceMatching{ e.matchingBooks[symbol] = &SimplePriceMatching{
CurrentTime: e.startTime, CurrentTime: e.startTime,
Account: e.account, Account: e.account,
Market: market, Market: market,
} }
} }
@ -289,7 +289,7 @@ func (e Exchange) PlatformFeeCurrency() string {
} }
func (e Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) { func (e Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
return e.publicExchange.QueryMarkets(ctx) return bbgo.LoadExchangeMarketsWithCache(ctx, e.publicExchange)
} }
func (e Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) { func (e Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) {
@ -300,7 +300,7 @@ func (e Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since,
return nil, nil return nil, nil
} }
func (e *Exchange) matchingBook(symbol string) (*SimplePriceMatching, bool){ func (e *Exchange) matchingBook(symbol string) (*SimplePriceMatching, bool) {
e.matchingBooksMutex.Lock() e.matchingBooksMutex.Lock()
m, ok := e.matchingBooks[symbol] m, ok := e.matchingBooks[symbol]
e.matchingBooksMutex.Unlock() e.matchingBooksMutex.Unlock()

View File

@ -9,9 +9,8 @@ import (
"path" "path"
"reflect" "reflect"
"github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/pkg/errors"
) )
type DataFetcher func() (interface{}, error) type DataFetcher func() (interface{}, error)
@ -63,7 +62,7 @@ func WithCache(key string, obj interface{}, fetcher DataFetcher) error {
func LoadExchangeMarketsWithCache(ctx context.Context, ex types.Exchange) (markets types.MarketMap, err error) { func LoadExchangeMarketsWithCache(ctx context.Context, ex types.Exchange) (markets types.MarketMap, err error) {
key := fmt.Sprintf("%s-markets", ex.Name()) key := fmt.Sprintf("%s-markets", ex.Name())
if futureExchange, implemented := ex.(types.FuturesExchange) ; implemented { if futureExchange, implemented := ex.(types.FuturesExchange); implemented {
settings := futureExchange.GetFuturesSettings() settings := futureExchange.GetFuturesSettings()
if settings.IsFutures { if settings.IsFutures {
key = fmt.Sprintf("%s-futures-markets", ex.Name()) key = fmt.Sprintf("%s-futures-markets", ex.Name())
@ -75,4 +74,3 @@ func LoadExchangeMarketsWithCache(ctx context.Context, ex types.Exchange) (marke
}) })
return markets, err return markets, err
} }

View File

@ -8,13 +8,11 @@ import (
"github.com/c9s/bbgo/pkg/cmd/cmdutil" "github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/c9s/bbgo/pkg/indicator" "github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/service" "github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util" "github.com/c9s/bbgo/pkg/util"
log "github.com/sirupsen/logrus"
) )
var ( var (
@ -265,26 +263,26 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
var log = log.WithField("session", session.Name) var log = log.WithField("session", session.Name)
if !viper.GetBool("bbgo-markets-cache") { // load markets first
markets, err := session.Exchange.QueryMarkets(ctx)
if err != nil { var disableMarketsCache = false
return err var markets types.MarketMap
} var err error
session.markets = markets if util.SetEnvVarBool("DISABLE_MARKETS_CACHE", &disableMarketsCache); disableMarketsCache {
markets, err = session.Exchange.QueryMarkets(ctx)
} else { } else {
// load markets first markets, err = LoadExchangeMarketsWithCache(ctx, session.Exchange)
var markets, err = LoadExchangeMarketsWithCache(ctx, session.Exchange)
if err != nil { if err != nil {
return err return err
} }
if len(markets) == 0 {
return fmt.Errorf("market config should not be empty")
}
session.markets = markets
} }
if len(markets) == 0 {
return fmt.Errorf("market config should not be empty")
}
session.markets = markets
// query and initialize the balances // query and initialize the balances
log.Infof("querying balances from session %s...", session.Name) log.Infof("querying balances from session %s...", session.Name)
balances, err := session.Exchange.QueryAccountBalances(ctx) balances, err := session.Exchange.QueryAccountBalances(ctx)