xmaker: merge all markets into one

This commit is contained in:
c9s 2024-10-05 14:36:43 +08:00
parent e3a2a857cd
commit 51aa34dec7
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 26 additions and 4 deletions

View File

@ -150,6 +150,8 @@ type Strategy struct {
makerMarket, sourceMarket types.Market
allMarkets types.MarketMap
// boll is the BOLLINGER indicator we used for predicting the price.
boll *indicatorv2.BOLLStream
@ -1393,9 +1395,6 @@ func (s *Strategy) CrossRun(
s.sourceSession = sourceSession
// initialize the price resolver
sourceMarkets := s.sourceSession.Markets()
makerSession, ok := sessions[s.MakerExchange]
if !ok {
return fmt.Errorf("maker exchange session %s is not defined", s.MakerExchange)
@ -1413,6 +1412,10 @@ func (s *Strategy) CrossRun(
return fmt.Errorf("maker session market %s is not defined", s.Symbol)
}
sourceMarkets := s.sourceSession.Markets()
makerMarkets := s.makerSession.Markets()
s.allMarkets = sourceMarkets.Merge(makerMarkets)
indicators := s.sourceSession.Indicators(s.Symbol)
s.boll = indicators.BOLL(types.IntervalWindow{
@ -1469,7 +1472,7 @@ func (s *Strategy) CrossRun(
})
}
s.priceSolver = pricesolver.NewSimplePriceResolver(sourceMarkets)
s.priceSolver = pricesolver.NewSimplePriceResolver(s.allMarkets)
s.priceSolver.BindStream(s.sourceSession.MarketDataStream)
s.accountValueCalculator = bbgo.NewAccountValueCalculator(s.sourceSession, s.priceSolver, s.sourceMarket.QuoteCurrency)

View File

@ -270,3 +270,22 @@ func (m MarketMap) Has(symbol string) bool {
_, ok := m[symbol]
return ok
}
// Merge merges the given market map into the current market map
// if the market already exists, it will be skipped
func (m MarketMap) Merge(b MarketMap) MarketMap {
var a = MarketMap{}
for k, v := range m {
a[k] = v
}
for k, v := range b {
if _, exists := a[k]; exists {
continue
}
a[k] = v
}
return a
}