Skip to content

Commit

Permalink
fix markets info cache
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 8, 2021
1 parent e030b87 commit d52edce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
22 changes: 11 additions & 11 deletions pkg/backtest/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions pkg/bbgo/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -75,4 +74,3 @@ func LoadExchangeMarketsWithCache(ctx context.Context, ex types.Exchange) (marke
})
return markets, err
}

30 changes: 14 additions & 16 deletions pkg/bbgo/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d52edce

Please sign in to comment.