2023-12-07 06:38:13 +00:00
|
|
|
package dca2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Strategy) placeTakeProfitOrders(ctx context.Context) error {
|
2024-03-12 06:29:36 +00:00
|
|
|
s.logger.Info("start placing take profit orders")
|
2023-12-22 07:50:48 +00:00
|
|
|
order := generateTakeProfitOrder(s.Market, s.TakeProfitRatio, s.Position, s.OrderGroupID)
|
2023-12-07 06:38:13 +00:00
|
|
|
createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, order)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, createdOrder := range createdOrders {
|
|
|
|
s.logger.Info("SUBMIT TAKE PROFIT ORDER ", createdOrder.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-22 07:50:48 +00:00
|
|
|
func generateTakeProfitOrder(market types.Market, takeProfitRatio fixedpoint.Value, position *types.Position, orderGroupID uint32) types.SubmitOrder {
|
2023-12-07 06:38:13 +00:00
|
|
|
side := types.SideTypeSell
|
|
|
|
takeProfitPrice := market.TruncatePrice(position.AverageCost.Mul(fixedpoint.One.Add(takeProfitRatio)))
|
|
|
|
return types.SubmitOrder{
|
|
|
|
Symbol: market.Symbol,
|
|
|
|
Market: market,
|
|
|
|
Type: types.OrderTypeLimit,
|
|
|
|
Price: takeProfitPrice,
|
|
|
|
Side: side,
|
|
|
|
TimeInForce: types.TimeInForceGTC,
|
|
|
|
Quantity: position.GetBase().Abs(),
|
|
|
|
Tag: orderTag,
|
|
|
|
GroupID: orderGroupID,
|
|
|
|
}
|
|
|
|
}
|