mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
Merge pull request #1475 from c9s/narumi/rebalance/fix-instance-id
REFACTOR: [rebalance] refactor MultiMarketStrategy.Initialize
This commit is contained in:
commit
9f8bdeb3e9
|
@ -7,6 +7,9 @@ import (
|
|||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
type PositionMap map[string]*types.Position
|
||||
type ProfitStatsMap map[string]*types.ProfitStats
|
||||
|
||||
type MultiMarketStrategy struct {
|
||||
Environ *bbgo.Environment
|
||||
Session *bbgo.ExchangeSession
|
||||
|
@ -19,26 +22,47 @@ type MultiMarketStrategy struct {
|
|||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (s *MultiMarketStrategy) Initialize(ctx context.Context, environ *bbgo.Environment, session *bbgo.ExchangeSession, markets map[string]types.Market, strategyID string) {
|
||||
func (s *MultiMarketStrategy) Initialize(ctx context.Context, environ *bbgo.Environment, session *bbgo.ExchangeSession, markets map[string]types.Market, strategyID string, instanceID string) {
|
||||
s.parent = ctx
|
||||
s.ctx, s.cancel = context.WithCancel(ctx)
|
||||
|
||||
s.Environ = environ
|
||||
s.Session = session
|
||||
|
||||
// initialize position map
|
||||
if s.PositionMap == nil {
|
||||
log.Infof("creating position map")
|
||||
s.PositionMap = make(PositionMap)
|
||||
}
|
||||
s.PositionMap.CreatePositions(markets)
|
||||
for symbol, market := range markets {
|
||||
if _, ok := s.PositionMap[symbol]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Infof("creating position for symbol %s", symbol)
|
||||
position := types.NewPositionFromMarket(market)
|
||||
position.Strategy = ID
|
||||
position.StrategyInstanceID = instanceID
|
||||
s.PositionMap[symbol] = position
|
||||
}
|
||||
|
||||
// initialize profit stats map
|
||||
if s.ProfitStatsMap == nil {
|
||||
log.Infof("creating profit stats map")
|
||||
s.ProfitStatsMap = make(ProfitStatsMap)
|
||||
}
|
||||
s.ProfitStatsMap.CreateProfitStats(markets)
|
||||
for symbol, market := range markets {
|
||||
if _, ok := s.ProfitStatsMap[symbol]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
s.OrderExecutorMap = NewGeneralOrderExecutorMap(session, s.PositionMap)
|
||||
log.Infof("creating profit stats for symbol %s", symbol)
|
||||
s.ProfitStatsMap[symbol] = types.NewProfitStats(market)
|
||||
}
|
||||
|
||||
// initialize order executor map
|
||||
s.OrderExecutorMap = NewGeneralOrderExecutorMap(session, strategyID, instanceID, s.PositionMap)
|
||||
s.OrderExecutorMap.BindEnvironment(environ)
|
||||
s.OrderExecutorMap.BindProfitStats(s.ProfitStatsMap)
|
||||
s.OrderExecutorMap.Sync(ctx, s)
|
||||
s.OrderExecutorMap.Bind()
|
||||
}
|
||||
|
|
|
@ -10,12 +10,12 @@ import (
|
|||
|
||||
type GeneralOrderExecutorMap map[string]*bbgo.GeneralOrderExecutor
|
||||
|
||||
func NewGeneralOrderExecutorMap(session *bbgo.ExchangeSession, positionMap PositionMap) GeneralOrderExecutorMap {
|
||||
func NewGeneralOrderExecutorMap(session *bbgo.ExchangeSession, strategyID string, instanceID string, positionMap PositionMap) GeneralOrderExecutorMap {
|
||||
m := make(GeneralOrderExecutorMap)
|
||||
|
||||
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, strategyID, instanceID, position)
|
||||
m[symbol] = orderExecutor
|
||||
}
|
||||
|
||||
|
@ -41,14 +41,6 @@ func (m GeneralOrderExecutorMap) Bind() {
|
|||
}
|
||||
}
|
||||
|
||||
func (m GeneralOrderExecutorMap) Sync(ctx context.Context, obj interface{}) {
|
||||
for _, orderExecutor := range m {
|
||||
orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
||||
bbgo.Sync(ctx, obj)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (m GeneralOrderExecutorMap) SubmitOrders(ctx context.Context, submitOrders ...types.SubmitOrder) (types.OrderSlice, error) {
|
||||
var allCreatedOrders types.OrderSlice
|
||||
for _, submitOrder := range submitOrders {
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
package rebalance
|
||||
|
||||
import (
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
type PositionMap map[string]*types.Position
|
||||
|
||||
func (m PositionMap) CreatePositions(markets map[string]types.Market) PositionMap {
|
||||
for symbol, market := range markets {
|
||||
if _, ok := m[symbol]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Infof("creating position for symbol %s", symbol)
|
||||
position := types.NewPositionFromMarket(market)
|
||||
position.Strategy = ID
|
||||
position.StrategyInstanceID = instanceID(symbol)
|
||||
m[symbol] = position
|
||||
}
|
||||
return m
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package rebalance
|
||||
|
||||
import (
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
type ProfitStatsMap map[string]*types.ProfitStats
|
||||
|
||||
func (m ProfitStatsMap) CreateProfitStats(markets map[string]types.Market) ProfitStatsMap {
|
||||
for symbol, market := range markets {
|
||||
if _, ok := m[symbol]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Infof("creating profit stats for symbol %s", symbol)
|
||||
m[symbol] = types.NewProfitStats(market)
|
||||
}
|
||||
return m
|
||||
}
|
|
@ -22,10 +22,6 @@ func init() {
|
|||
bbgo.RegisterStrategy(ID, &Strategy{})
|
||||
}
|
||||
|
||||
func instanceID(symbol string) string {
|
||||
return fmt.Sprintf("%s:%s", ID, symbol)
|
||||
}
|
||||
|
||||
type Strategy struct {
|
||||
*MultiMarketStrategy
|
||||
|
||||
|
@ -72,6 +68,10 @@ func (s *Strategy) ID() string {
|
|||
return ID
|
||||
}
|
||||
|
||||
func (s *Strategy) InstanceID() string {
|
||||
return ID
|
||||
}
|
||||
|
||||
func (s *Strategy) Validate() error {
|
||||
if len(s.TargetWeights) == 0 {
|
||||
return fmt.Errorf("targetWeights should not be empty")
|
||||
|
@ -109,7 +109,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
|
|||
s.markets[symbol] = market
|
||||
}
|
||||
|
||||
s.MultiMarketStrategy.Initialize(ctx, s.Environment, session, s.markets, ID)
|
||||
s.MultiMarketStrategy.Initialize(ctx, s.Environment, session, s.markets, ID, s.InstanceID())
|
||||
|
||||
s.activeOrderBook = bbgo.NewActiveOrderBook("")
|
||||
s.activeOrderBook.BindStream(session.UserDataStream)
|
||||
|
|
Loading…
Reference in New Issue
Block a user