strategy: refactor strategies using position interface

This commit is contained in:
austin362667 2022-05-30 15:18:50 +08:00
parent ae17e0ddbe
commit a3ca8326f2
6 changed files with 9 additions and 10 deletions

View File

@ -834,7 +834,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
}
})
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) {
log.Infof("position changed: %s", position)
s.Notify(s.Position)
})

View File

@ -620,7 +620,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
}
*/
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) {
s.Notifiability.Notify(position)
})
s.tradeCollector.BindStream(session.UserDataStream)

View File

@ -179,7 +179,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
}
})
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) {
log.Infof("position changed: %s", s.Position)
s.Notify(s.Position)
})

View File

@ -499,13 +499,13 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
if !s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() {
// Update trailing stop when the position changes
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) {
// StrategyController
if s.Status != types.StrategyStatusRunning {
return
}
if position.Base.Compare(s.Market.MinQuantity) > 0 { // Update order if we have a position
if position.(*types.Position).Base.Compare(s.Market.MinQuantity) > 0 { // Update order if we have a position
// Cancel the original order
if err := s.cancelOrder(s.trailingStopControl.OrderID, ctx, orderExecutor); err != nil {
log.WithError(err).Errorf("Can not cancel the original trailing stop order!")
@ -515,12 +515,12 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// Calculate minimum target price
var minTargetPrice = fixedpoint.Zero
if s.trailingStopControl.minimumProfitPercentage.Sign() > 0 {
minTargetPrice = position.AverageCost.Mul(fixedpoint.One.Add(s.trailingStopControl.minimumProfitPercentage))
minTargetPrice = position.(*types.Position).AverageCost.Mul(fixedpoint.One.Add(s.trailingStopControl.minimumProfitPercentage))
}
// Place new order if the target price is higher than the minimum target price
if s.trailingStopControl.IsHigherThanMin(minTargetPrice) {
orderForm := s.trailingStopControl.GenerateStopOrder(position.Base)
orderForm := s.trailingStopControl.GenerateStopOrder(position.(*types.Position).Base)
orders, err := s.submitOrders(ctx, orderExecutor, orderForm)
if err != nil {
log.WithError(err).Error("submit profit trailing stop order error")

View File

@ -302,7 +302,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
}
})
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) {
log.Infof("position changed: %s", s.Position)
s.Notify(s.Position)
})
@ -340,7 +340,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
log.WithError(err).Errorf("can not place order")
}
if err := s.activeAdjustmentOrders.GracefulCancel(ctx, s.session.Exchange); err != nil {
log.WithError(err).Errorf("graceful cancel order error")
}

View File

@ -781,7 +781,7 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
}
})
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) {
s.Notifiability.Notify(position)
})
s.tradeCollector.OnRecover(func(trade types.Trade) {