diff --git a/pkg/backtest/exchange.go b/pkg/backtest/exchange.go index 26d1c04c2..bdcc8c242 100644 --- a/pkg/backtest/exchange.go +++ b/pkg/backtest/exchange.go @@ -55,17 +55,17 @@ type Exchange struct { userDataStream *Stream - trades map[string][]types.Trade + trades map[string][]types.Trade tradesMutex sync.Mutex - closedOrders map[string][]types.Order + closedOrders map[string][]types.Order closedOrdersMutex sync.Mutex - matchingBooks map[string]*SimplePriceMatching + matchingBooks map[string]*SimplePriceMatching matchingBooksMutex sync.Mutex - markets types.MarketMap - doneC chan struct{} + markets types.MarketMap + doneC chan struct{} } 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{ MakerFeeRate: config.Account.MakerFeeRate, TakerFeeRate: config.Account.TakerFeeRate, - AccountType: "SPOT", // currently not used + AccountType: "SPOT", // currently not used } 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) { e.matchingBooks[symbol] = &SimplePriceMatching{ - CurrentTime: e.startTime, - Account: e.account, - Market: market, + CurrentTime: e.startTime, + Account: e.account, + Market: market, } } @@ -289,7 +289,7 @@ func (e Exchange) PlatformFeeCurrency() string { } 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) { @@ -300,7 +300,7 @@ func (e Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, return nil, nil } -func (e *Exchange) matchingBook(symbol string) (*SimplePriceMatching, bool){ +func (e *Exchange) matchingBook(symbol string) (*SimplePriceMatching, bool) { e.matchingBooksMutex.Lock() m, ok := e.matchingBooks[symbol] e.matchingBooksMutex.Unlock() diff --git a/pkg/bbgo/cache.go b/pkg/bbgo/cache.go index 09dad0d4e..9c2231e81 100644 --- a/pkg/bbgo/cache.go +++ b/pkg/bbgo/cache.go @@ -9,9 +9,8 @@ import ( "path" "reflect" - "github.com/pkg/errors" - "github.com/c9s/bbgo/pkg/types" + "github.com/pkg/errors" ) 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) { key := fmt.Sprintf("%s-markets", ex.Name()) - if futureExchange, implemented := ex.(types.FuturesExchange) ; implemented { + if futureExchange, implemented := ex.(types.FuturesExchange); implemented { settings := futureExchange.GetFuturesSettings() if settings.IsFutures { key = fmt.Sprintf("%s-futures-markets", ex.Name()) @@ -75,4 +74,3 @@ func LoadExchangeMarketsWithCache(ctx context.Context, ex types.Exchange) (marke }) return markets, err } - diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index 4cc5ccda1..3fbe4ebf3 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -8,13 +8,11 @@ import ( "github.com/c9s/bbgo/pkg/cmd/cmdutil" "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/service" "github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/util" + log "github.com/sirupsen/logrus" ) var ( @@ -265,26 +263,26 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment) var log = log.WithField("session", session.Name) - if !viper.GetBool("bbgo-markets-cache") { - markets, err := session.Exchange.QueryMarkets(ctx) - if err != nil { - return err - } - session.markets = markets + // load markets first + + var disableMarketsCache = false + var markets types.MarketMap + var err error + if util.SetEnvVarBool("DISABLE_MARKETS_CACHE", &disableMarketsCache); disableMarketsCache { + markets, err = session.Exchange.QueryMarkets(ctx) } else { - // load markets first - var markets, err = LoadExchangeMarketsWithCache(ctx, session.Exchange) + markets, err = LoadExchangeMarketsWithCache(ctx, session.Exchange) if err != nil { 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 log.Infof("querying balances from session %s...", session.Name) balances, err := session.Exchange.QueryAccountBalances(ctx)