Merge pull request #1155 from andycheng123/improve/supertrend

This commit is contained in:
Yo-An Lin 2023-04-20 18:48:39 +08:00 committed by GitHub
commit cd69232156
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -621,18 +621,25 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// The default value of side is an empty string. Unless side is set by the checks above, the result of the following condition is false
if side == types.SideTypeSell || side == types.SideTypeBuy {
bbgo.Notify("open %s position for signal %v", s.Symbol, side)
// Close opposite position if any
if !s.Position.IsDust(closePrice) {
if (side == types.SideTypeSell && s.Position.IsLong()) || (side == types.SideTypeBuy && s.Position.IsShort()) {
bbgo.Notify("close existing %s position before open a new position", s.Symbol)
amount := s.calculateQuantity(ctx, closePrice, side)
// Add opposite position amount if any
if (side == types.SideTypeSell && s.Position.IsLong()) || (side == types.SideTypeBuy && s.Position.IsShort()) {
if bbgo.IsBackTesting {
_ = s.ClosePosition(ctx, fixedpoint.One)
bbgo.Notify("close existing %s position before open a new position", s.Symbol)
amount = s.calculateQuantity(ctx, closePrice, side)
} else {
bbgo.Notify("existing %s position has the same direction with the signal", s.Symbol)
return
bbgo.Notify("add existing opposite position amount %f of %s to the amount %f of open new position order", s.Position.GetQuantity().Float64(), s.Symbol, amount.Float64())
amount = amount.Add(s.Position.GetQuantity())
}
} else if !s.Position.IsDust(closePrice) {
bbgo.Notify("existing %s position has the same direction as the signal", s.Symbol)
return
}
orderForm := s.generateOrderForm(side, s.calculateQuantity(ctx, closePrice, side), types.SideEffectTypeMarginBuy)
orderForm := s.generateOrderForm(side, amount, types.SideEffectTypeMarginBuy)
log.Infof("submit open position order %v", orderForm)
_, err := s.orderExecutor.SubmitOrders(ctx, orderForm)
if err != nil {