Merge pull request #1109 from c9s/narumi/rebalance/fix-order-executor-not-found

fix: rebalance: fix positions and profit stats map
This commit is contained in:
なるみ 2023-03-15 12:25:19 +08:00 committed by GitHub
commit 7d91fd01d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 13 deletions

View File

@ -33,10 +33,9 @@ exchangeStrategies:
targetWeights: targetWeights:
BTC: 50% BTC: 50%
ETH: 25% ETH: 25%
MAX: 15% USDT: 25%
USDT: 10%
threshold: 1% threshold: 1%
maxAmount: 1_000 # max amount to buy or sell per order maxAmount: 1_000 # max amount to buy or sell per order
orderType: LIMIT_MAKER # LIMIT, LIMIT_MAKER or MARKET orderType: LIMIT_MAKER # LIMIT, LIMIT_MAKER or MARKET
dryRun: false dryRun: false
onStart: false onStart: true

View File

@ -14,6 +14,7 @@ func NewGeneralOrderExecutorMap(session *bbgo.ExchangeSession, positionMap Posit
m := make(GeneralOrderExecutorMap) m := make(GeneralOrderExecutorMap)
for symbol, position := range positionMap { for symbol, position := range positionMap {
log.Infof("creating order executor for symbol %s", symbol)
orderExecutor := bbgo.NewGeneralOrderExecutor(session, symbol, ID, instanceID(symbol), position) orderExecutor := bbgo.NewGeneralOrderExecutor(session, symbol, ID, instanceID(symbol), position)
m[symbol] = orderExecutor m[symbol] = orderExecutor
} }
@ -29,6 +30,7 @@ func (m GeneralOrderExecutorMap) BindEnvironment(environ *bbgo.Environment) {
func (m GeneralOrderExecutorMap) BindProfitStats(profitStatsMap ProfitStatsMap) { func (m GeneralOrderExecutorMap) BindProfitStats(profitStatsMap ProfitStatsMap) {
for symbol, orderExecutor := range m { for symbol, orderExecutor := range m {
log.Infof("binding profit stats for symbol %s", symbol)
orderExecutor.BindProfitStats(profitStatsMap[symbol]) orderExecutor.BindProfitStats(profitStatsMap[symbol])
} }
} }
@ -50,6 +52,7 @@ func (m GeneralOrderExecutorMap) Sync(ctx context.Context, obj interface{}) {
func (m GeneralOrderExecutorMap) SubmitOrders(ctx context.Context, submitOrders ...types.SubmitOrder) (types.OrderSlice, error) { func (m GeneralOrderExecutorMap) SubmitOrders(ctx context.Context, submitOrders ...types.SubmitOrder) (types.OrderSlice, error) {
var allCreatedOrders types.OrderSlice var allCreatedOrders types.OrderSlice
for _, submitOrder := range submitOrders { for _, submitOrder := range submitOrders {
log.Infof("submitting order: %+v", submitOrder)
orderExecutor, ok := m[submitOrder.Symbol] orderExecutor, ok := m[submitOrder.Symbol]
if !ok { if !ok {
return nil, fmt.Errorf("order executor not found for symbol %s", submitOrder.Symbol) return nil, fmt.Errorf("order executor not found for symbol %s", submitOrder.Symbol)

View File

@ -6,15 +6,17 @@ import (
type PositionMap map[string]*types.Position type PositionMap map[string]*types.Position
func NewPositionMap(markets []types.Market) PositionMap { func (m PositionMap) createPositions(markets []types.Market) PositionMap {
m := make(PositionMap)
for _, market := range markets { for _, market := range markets {
if _, ok := m[market.Symbol]; ok {
continue
}
log.Infof("creating position for symbol %s", market.Symbol)
position := types.NewPositionFromMarket(market) position := types.NewPositionFromMarket(market)
position.Strategy = ID position.Strategy = ID
position.StrategyInstanceID = instanceID(market.Symbol) position.StrategyInstanceID = instanceID(market.Symbol)
m[market.Symbol] = position m[market.Symbol] = position
} }
return m return m
} }

View File

@ -4,12 +4,14 @@ import "github.com/c9s/bbgo/pkg/types"
type ProfitStatsMap map[string]*types.ProfitStats type ProfitStatsMap map[string]*types.ProfitStats
func NewProfitStatsMap(markets []types.Market) ProfitStatsMap { func (m ProfitStatsMap) createProfitStats(markets []types.Market) ProfitStatsMap {
m := make(ProfitStatsMap)
for _, market := range markets { for _, market := range markets {
if _, ok := m[market.Symbol]; ok {
continue
}
log.Infof("creating profit stats for symbol %s", market.Symbol)
m[market.Symbol] = types.NewProfitStats(market) m[market.Symbol] = types.NewProfitStats(market)
} }
return m return m
} }

View File

@ -99,12 +99,14 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
} }
if s.PositionMap == nil { if s.PositionMap == nil {
s.PositionMap = NewPositionMap(markets) s.PositionMap = make(PositionMap)
} }
s.PositionMap.createPositions(markets)
if s.ProfitStatsMap == nil { if s.ProfitStatsMap == nil {
s.ProfitStatsMap = NewProfitStatsMap(markets) s.ProfitStatsMap = make(ProfitStatsMap)
} }
s.ProfitStatsMap.createProfitStats(markets)
s.orderExecutorMap = NewGeneralOrderExecutorMap(session, s.PositionMap) s.orderExecutorMap = NewGeneralOrderExecutorMap(session, s.PositionMap)
s.orderExecutorMap.BindEnvironment(s.Environment) s.orderExecutorMap.BindEnvironment(s.Environment)