all: rename priceresolver to pricesolver

integrate pricesolver into xmaker
This commit is contained in:
c9s 2024-08-24 12:28:05 +08:00
parent 9f01dc28c8
commit 1c1959b8a8
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 21 additions and 13 deletions

View File

@ -1,4 +1,4 @@
package priceresolver package pricesolver
import ( import (
"sync" "sync"
@ -9,8 +9,8 @@ import (
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
// SimplePriceResolver implements a map-structure-based price index // SimplePriceSolver implements a map-structure-based price index
type SimplePriceResolver struct { type SimplePriceSolver struct {
// symbolPrices stores the latest trade price by mapping symbol to price // symbolPrices stores the latest trade price by mapping symbol to price
symbolPrices map[string]fixedpoint.Value symbolPrices map[string]fixedpoint.Value
markets types.MarketMap markets types.MarketMap
@ -28,8 +28,8 @@ type SimplePriceResolver struct {
mu sync.Mutex mu sync.Mutex
} }
func NewSimplePriceResolver(markets types.MarketMap) *SimplePriceResolver { func NewSimplePriceResolver(markets types.MarketMap) *SimplePriceSolver {
return &SimplePriceResolver{ return &SimplePriceSolver{
markets: markets, markets: markets,
symbolPrices: make(map[string]fixedpoint.Value), symbolPrices: make(map[string]fixedpoint.Value),
pricesByBase: make(map[string]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() m.mu.Lock()
defer m.mu.Unlock() defer m.mu.Unlock()
@ -65,11 +65,11 @@ func (m *SimplePriceResolver) Update(symbol string, price fixedpoint.Value) {
baseMap[market.BaseCurrency] = price baseMap[market.BaseCurrency] = price
} }
func (m *SimplePriceResolver) UpdateFromTrade(trade types.Trade) { func (m *SimplePriceSolver) UpdateFromTrade(trade types.Trade) {
m.Update(trade.Symbol, trade.Price) 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()) // log.Infof("inferencePrice %s = %f", asset, assetPrice.Float64())
quotePrices, ok := m.pricesByBase[asset] quotePrices, ok := m.pricesByBase[asset]
if ok { if ok {
@ -112,7 +112,7 @@ func (m *SimplePriceResolver) inferencePrice(asset string, assetPrice fixedpoint
return fixedpoint.Zero, false 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() m.mu.Lock()
defer m.mu.Unlock() defer m.mu.Unlock()
return m.inferencePrice(asset, fixedpoint.One, preferredFiats...) return m.inferencePrice(asset, fixedpoint.One, preferredFiats...)

View File

@ -1,4 +1,4 @@
package priceresolver package pricesolver
import ( import (
"testing" "testing"

View File

@ -14,7 +14,7 @@ import (
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/core" "github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/priceresolver" "github.com/c9s/bbgo/pkg/pricesolver"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
@ -57,7 +57,7 @@ type Strategy struct {
faultBalanceRecords map[string][]TimeBalance faultBalanceRecords map[string][]TimeBalance
priceResolver *priceresolver.SimplePriceResolver priceResolver *pricesolver.SimplePriceSolver
sessions map[string]*bbgo.ExchangeSession sessions map[string]*bbgo.ExchangeSession
orderBooks map[string]*bbgo.ActiveOrderBook orderBooks map[string]*bbgo.ActiveOrderBook
@ -372,7 +372,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
// session.Market(symbol) // session.Market(symbol)
} }
s.priceResolver = priceresolver.NewSimplePriceResolver(markets) s.priceResolver = pricesolver.NewSimplePriceResolver(markets)
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()

View File

@ -14,6 +14,7 @@ import (
"github.com/c9s/bbgo/pkg/core" "github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator" "github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/pricesolver"
"github.com/c9s/bbgo/pkg/risk/circuitbreaker" "github.com/c9s/bbgo/pkg/risk/circuitbreaker"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util" "github.com/c9s/bbgo/pkg/util"
@ -99,6 +100,7 @@ type Strategy struct {
state *State state *State
priceSolver *pricesolver.SimplePriceSolver
CircuitBreaker *circuitbreaker.BasicCircuitBreaker `json:"circuitBreaker"` CircuitBreaker *circuitbreaker.BasicCircuitBreaker `json:"circuitBreaker"`
// persistence fields // persistence fields
@ -214,6 +216,8 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
// use mid-price for the last price // use mid-price for the last price
s.lastPrice = bestBid.Price.Add(bestAsk.Price).Div(Two) s.lastPrice = bestBid.Price.Add(bestAsk.Price).Div(Two)
s.priceSolver.Update(s.Symbol, s.lastPrice)
bookLastUpdateTime := s.book.LastUpdateTime() bookLastUpdateTime := s.book.LastUpdateTime()
if _, err := s.bidPriceHeartBeat.Update(bestBid); err != nil { if _, err := s.bidPriceHeartBeat.Update(bestBid); err != nil {
@ -744,6 +748,10 @@ func (s *Strategy) CrossRun(
s.sourceSession = sourceSession s.sourceSession = sourceSession
// initialize the price resolver
sourceMarkets := s.sourceSession.Markets()
s.priceSolver = pricesolver.NewSimplePriceResolver(sourceMarkets)
makerSession, ok := sessions[s.MakerExchange] makerSession, ok := sessions[s.MakerExchange]
if !ok { if !ok {
return fmt.Errorf("maker exchange session %s is not defined", s.MakerExchange) return fmt.Errorf("maker exchange session %s is not defined", s.MakerExchange)