bbgo_origin/pkg/strategy/dca2/take_profit.go
chiahung.lin e3d51777d3 rename
2023-12-13 14:16:02 +08:00

44 lines
1.2 KiB
Go

package dca2
import (
"context"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func (s *Strategy) placeTakeProfitOrders(ctx context.Context) error {
s.logger.Info("[DCA] start placing take profit orders")
order := generateTakeProfitOrder(s.Market, s.Short, s.TakeProfitRatio, s.Position, s.OrderGroupID)
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
}
func generateTakeProfitOrder(market types.Market, short bool, takeProfitRatio fixedpoint.Value, position *types.Position, orderGroupID uint32) types.SubmitOrder {
side := types.SideTypeSell
if short {
takeProfitRatio = takeProfitRatio.Neg()
side = types.SideTypeBuy
}
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,
}
}