mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
44 lines
1.2 KiB
Go
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,
|
|
}
|
|
}
|