mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-27 17:25:16 +00:00
strategy: refactor strategies using position interface
This commit is contained in:
parent
ae17e0ddbe
commit
a3ca8326f2
|
@ -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)
|
log.Infof("position changed: %s", position)
|
||||||
s.Notify(s.Position)
|
s.Notify(s.Position)
|
||||||
})
|
})
|
||||||
|
|
|
@ -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.Notifiability.Notify(position)
|
||||||
})
|
})
|
||||||
s.tradeCollector.BindStream(session.UserDataStream)
|
s.tradeCollector.BindStream(session.UserDataStream)
|
||||||
|
|
|
@ -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)
|
log.Infof("position changed: %s", s.Position)
|
||||||
s.Notify(s.Position)
|
s.Notify(s.Position)
|
||||||
})
|
})
|
||||||
|
|
|
@ -499,13 +499,13 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
|
|
||||||
if !s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() {
|
if !s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() {
|
||||||
// Update trailing stop when the position changes
|
// Update trailing stop when the position changes
|
||||||
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
|
s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) {
|
||||||
// StrategyController
|
// StrategyController
|
||||||
if s.Status != types.StrategyStatusRunning {
|
if s.Status != types.StrategyStatusRunning {
|
||||||
return
|
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
|
// Cancel the original order
|
||||||
if err := s.cancelOrder(s.trailingStopControl.OrderID, ctx, orderExecutor); err != nil {
|
if err := s.cancelOrder(s.trailingStopControl.OrderID, ctx, orderExecutor); err != nil {
|
||||||
log.WithError(err).Errorf("Can not cancel the original trailing stop order!")
|
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
|
// Calculate minimum target price
|
||||||
var minTargetPrice = fixedpoint.Zero
|
var minTargetPrice = fixedpoint.Zero
|
||||||
if s.trailingStopControl.minimumProfitPercentage.Sign() > 0 {
|
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
|
// Place new order if the target price is higher than the minimum target price
|
||||||
if s.trailingStopControl.IsHigherThanMin(minTargetPrice) {
|
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)
|
orders, err := s.submitOrders(ctx, orderExecutor, orderForm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Error("submit profit trailing stop order error")
|
log.WithError(err).Error("submit profit trailing stop order error")
|
||||||
|
|
|
@ -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)
|
log.Infof("position changed: %s", s.Position)
|
||||||
s.Notify(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")
|
log.WithError(err).Errorf("can not place order")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if err := s.activeAdjustmentOrders.GracefulCancel(ctx, s.session.Exchange); err != nil {
|
if err := s.activeAdjustmentOrders.GracefulCancel(ctx, s.session.Exchange); err != nil {
|
||||||
log.WithError(err).Errorf("graceful cancel order error")
|
log.WithError(err).Errorf("graceful cancel order error")
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.Notifiability.Notify(position)
|
||||||
})
|
})
|
||||||
s.tradeCollector.OnRecover(func(trade types.Trade) {
|
s.tradeCollector.OnRecover(func(trade types.Trade) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user