From 1c1959b8a8fec1902141d777822ed0ac7ad87c0f Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 24 Aug 2024 12:28:05 +0800 Subject: [PATCH] all: rename priceresolver to pricesolver integrate pricesolver into xmaker --- pkg/{priceresolver => pricesolver}/simple.go | 18 +++++++++--------- .../simple_test.go | 2 +- pkg/strategy/xalign/strategy.go | 6 +++--- pkg/strategy/xmaker/strategy.go | 8 ++++++++ 4 files changed, 21 insertions(+), 13 deletions(-) rename pkg/{priceresolver => pricesolver}/simple.go (84%) rename pkg/{priceresolver => pricesolver}/simple_test.go (99%) diff --git a/pkg/priceresolver/simple.go b/pkg/pricesolver/simple.go similarity index 84% rename from pkg/priceresolver/simple.go rename to pkg/pricesolver/simple.go index 65b6ec51f..6e16700d2 100644 --- a/pkg/priceresolver/simple.go +++ b/pkg/pricesolver/simple.go @@ -1,4 +1,4 @@ -package priceresolver +package pricesolver import ( "sync" @@ -9,8 +9,8 @@ import ( "github.com/c9s/bbgo/pkg/types" ) -// SimplePriceResolver implements a map-structure-based price index -type SimplePriceResolver struct { +// SimplePriceSolver implements a map-structure-based price index +type SimplePriceSolver struct { // symbolPrices stores the latest trade price by mapping symbol to price symbolPrices map[string]fixedpoint.Value markets types.MarketMap @@ -28,8 +28,8 @@ type SimplePriceResolver struct { mu sync.Mutex } -func NewSimplePriceResolver(markets types.MarketMap) *SimplePriceResolver { - return &SimplePriceResolver{ +func NewSimplePriceResolver(markets types.MarketMap) *SimplePriceSolver { + return &SimplePriceSolver{ markets: markets, symbolPrices: make(map[string]fixedpoint.Value), pricesByBase: make(map[string]map[string]fixedpoint.Value), @@ -37,7 +37,7 @@ func NewSimplePriceResolver(markets types.MarketMap) *SimplePriceResolver { } } -func (m *SimplePriceResolver) Update(symbol string, price fixedpoint.Value) { +func (m *SimplePriceSolver) Update(symbol string, price fixedpoint.Value) { m.mu.Lock() defer m.mu.Unlock() @@ -65,11 +65,11 @@ func (m *SimplePriceResolver) Update(symbol string, price fixedpoint.Value) { baseMap[market.BaseCurrency] = price } -func (m *SimplePriceResolver) UpdateFromTrade(trade types.Trade) { +func (m *SimplePriceSolver) UpdateFromTrade(trade types.Trade) { m.Update(trade.Symbol, trade.Price) } -func (m *SimplePriceResolver) inferencePrice(asset string, assetPrice fixedpoint.Value, preferredFiats ...string) (fixedpoint.Value, bool) { +func (m *SimplePriceSolver) inferencePrice(asset string, assetPrice fixedpoint.Value, preferredFiats ...string) (fixedpoint.Value, bool) { // log.Infof("inferencePrice %s = %f", asset, assetPrice.Float64()) quotePrices, ok := m.pricesByBase[asset] if ok { @@ -112,7 +112,7 @@ func (m *SimplePriceResolver) inferencePrice(asset string, assetPrice fixedpoint return fixedpoint.Zero, false } -func (m *SimplePriceResolver) ResolvePrice(asset string, preferredFiats ...string) (fixedpoint.Value, bool) { +func (m *SimplePriceSolver) ResolvePrice(asset string, preferredFiats ...string) (fixedpoint.Value, bool) { m.mu.Lock() defer m.mu.Unlock() return m.inferencePrice(asset, fixedpoint.One, preferredFiats...) diff --git a/pkg/priceresolver/simple_test.go b/pkg/pricesolver/simple_test.go similarity index 99% rename from pkg/priceresolver/simple_test.go rename to pkg/pricesolver/simple_test.go index a5e9c68f2..1714b73ac 100644 --- a/pkg/priceresolver/simple_test.go +++ b/pkg/pricesolver/simple_test.go @@ -1,4 +1,4 @@ -package priceresolver +package pricesolver import ( "testing" diff --git a/pkg/strategy/xalign/strategy.go b/pkg/strategy/xalign/strategy.go index 40b0a6f01..2f5776622 100644 --- a/pkg/strategy/xalign/strategy.go +++ b/pkg/strategy/xalign/strategy.go @@ -14,7 +14,7 @@ import ( "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/core" "github.com/c9s/bbgo/pkg/fixedpoint" - "github.com/c9s/bbgo/pkg/priceresolver" + "github.com/c9s/bbgo/pkg/pricesolver" "github.com/c9s/bbgo/pkg/types" ) @@ -57,7 +57,7 @@ type Strategy struct { faultBalanceRecords map[string][]TimeBalance - priceResolver *priceresolver.SimplePriceResolver + priceResolver *pricesolver.SimplePriceSolver sessions map[string]*bbgo.ExchangeSession orderBooks map[string]*bbgo.ActiveOrderBook @@ -372,7 +372,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se // session.Market(symbol) } - s.priceResolver = priceresolver.NewSimplePriceResolver(markets) + s.priceResolver = pricesolver.NewSimplePriceResolver(markets) bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index 071d59600..50ce689c2 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -14,6 +14,7 @@ import ( "github.com/c9s/bbgo/pkg/core" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/indicator" + "github.com/c9s/bbgo/pkg/pricesolver" "github.com/c9s/bbgo/pkg/risk/circuitbreaker" "github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/util" @@ -99,6 +100,7 @@ type Strategy struct { state *State + priceSolver *pricesolver.SimplePriceSolver CircuitBreaker *circuitbreaker.BasicCircuitBreaker `json:"circuitBreaker"` // persistence fields @@ -214,6 +216,8 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or // use mid-price for the last price s.lastPrice = bestBid.Price.Add(bestAsk.Price).Div(Two) + s.priceSolver.Update(s.Symbol, s.lastPrice) + bookLastUpdateTime := s.book.LastUpdateTime() if _, err := s.bidPriceHeartBeat.Update(bestBid); err != nil { @@ -744,6 +748,10 @@ func (s *Strategy) CrossRun( s.sourceSession = sourceSession + // initialize the price resolver + sourceMarkets := s.sourceSession.Markets() + s.priceSolver = pricesolver.NewSimplePriceResolver(sourceMarkets) + makerSession, ok := sessions[s.MakerExchange] if !ok { return fmt.Errorf("maker exchange session %s is not defined", s.MakerExchange)