diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index da4563786..66dbece1b 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -36,6 +36,7 @@ func (s *Strategy) ID() string { type State struct { HedgePosition fixedpoint.Value `json:"hedgePosition"` + Position *bbgo.Position `json:"position,omitempty"` } type Strategy struct { @@ -342,8 +343,13 @@ func (s *Strategy) handleTradeUpdate(trade types.Trade) { } - log.Infof("identified trade %d with an existing order: %d", trade.ID, trade.OrderID) - s.Notify("identified %s trade %d with an existing order: %d", trade.Symbol, trade.ID, trade.OrderID) + s.Notify("Identified %s trade %d with an existing order: %d", trade.Symbol, trade.ID, trade.OrderID) + + if profit, madeProfit := s.state.Position.AddTrade(trade) ; madeProfit { + s.Notify("%s trade just made profit %f %s", profit, s.state.Position.QuoteCurrency) + } else { + s.Notify("%s trade modified the position average cost to %f %s", s.state.Position.AverageCost.Float64(), s.state.Position.QuoteCurrency) + } s.state.HedgePosition.AtomicAdd(q) @@ -351,7 +357,6 @@ func (s *Strategy) handleTradeUpdate(trade types.Trade) { log.Warnf("%s position changed: %f", s.Symbol, pos.Float64()) s.Notifiability.Notify("%s position is changed to %f", s.Symbol, pos.Float64()) - s.lastPrice = trade.Price } @@ -436,6 +441,15 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se s.Notify("position is restored => %f", s.state.HedgePosition.Float64()) } + // if position is nil, we need to allocate a new position for calculation + if s.state.Position == nil { + s.state.Position = &bbgo.Position{ + Symbol: s.Symbol, + BaseCurrency: s.makerMarket.BaseCurrency, + QuoteCurrency: s.makerMarket.QuoteCurrency, + } + } + s.book = types.NewStreamBook(s.Symbol) s.book.BindStream(s.sourceSession.Stream)