bbgo: call notify when opening new position

This commit is contained in:
c9s 2022-09-11 02:36:45 +08:00
parent 5c18bc4d41
commit 9474c2779c
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -143,27 +143,25 @@ type OpenPositionOptions struct {
Quantity fixedpoint.Value `json:"quantity,omitempty"` Quantity fixedpoint.Value `json:"quantity,omitempty"`
// MarketOrder set to true to open a position with a market order // MarketOrder set to true to open a position with a market order
MarketOrder bool MarketOrder bool `json:"marketOrder,omitempty"`
// LimitOrder set to true to open a position with a limit order // LimitOrder set to true to open a position with a limit order
LimitOrder bool LimitOrder bool `json:"limitOrder,omitempty"`
// LimitTakerRatio is used when LimitOrder = true, it adjusts the price of the limit order with a ratio. // LimitTakerRatio is used when LimitOrder = true, it adjusts the price of the limit order with a ratio.
// So you can ensure that the limit order can be a taker order. Higher the ratio, higher the chance it could be a taker order. // So you can ensure that the limit order can be a taker order. Higher the ratio, higher the chance it could be a taker order.
LimitTakerRatio fixedpoint.Value LimitTakerRatio fixedpoint.Value `json:"limitTakerRatio,omitempty"`
CurrentPrice fixedpoint.Value CurrentPrice fixedpoint.Value `json:"currentPrice,omitempty"`
Tag string Tags []string `json:"tags"`
} }
func (e *GeneralOrderExecutor) OpenPosition(ctx context.Context, options OpenPositionOptions) error { func (e *GeneralOrderExecutor) OpenPosition(ctx context.Context, options OpenPositionOptions) error {
log.Infof("opening %s position: %+v", e.position.Symbol, options)
price := options.CurrentPrice price := options.CurrentPrice
submitOrder := types.SubmitOrder{ submitOrder := types.SubmitOrder{
Symbol: e.position.Symbol, Symbol: e.position.Symbol,
Type: types.OrderTypeMarket, Type: types.OrderTypeMarket,
MarginSideEffect: types.SideEffectTypeMarginBuy, MarginSideEffect: types.SideEffectTypeMarginBuy,
Tag: options.Tag, Tag: strings.Join(options.Tags, ","),
} }
if !options.LimitTakerRatio.IsZero() { if !options.LimitTakerRatio.IsZero() {
@ -198,6 +196,7 @@ func (e *GeneralOrderExecutor) OpenPosition(ctx context.Context, options OpenPos
submitOrder.Side = types.SideTypeBuy submitOrder.Side = types.SideTypeBuy
submitOrder.Quantity = quantity submitOrder.Quantity = quantity
Notify("Opening %s long position with quantity %f at price %f", e.position.Symbol, quantity.Float64(), price.Float64())
createdOrder, err2 := e.SubmitOrders(ctx, submitOrder) createdOrder, err2 := e.SubmitOrders(ctx, submitOrder)
if err2 != nil { if err2 != nil {
return err2 return err2
@ -216,6 +215,7 @@ func (e *GeneralOrderExecutor) OpenPosition(ctx context.Context, options OpenPos
submitOrder.Side = types.SideTypeSell submitOrder.Side = types.SideTypeSell
submitOrder.Quantity = quantity submitOrder.Quantity = quantity
Notify("Opening %s short position with quantity %f at price %f", e.position.Symbol, quantity.Float64(), price.Float64())
createdOrder, err2 := e.SubmitOrders(ctx, submitOrder) createdOrder, err2 := e.SubmitOrders(ctx, submitOrder)
if err2 != nil { if err2 != nil {
return err2 return err2
@ -261,7 +261,7 @@ func (e *GeneralOrderExecutor) ClosePosition(ctx context.Context, percentage fix
submitOrder.Tag = tagStr submitOrder.Tag = tagStr
Notify("closing %s position %s with tags: %v", e.symbol, percentage.Percentage(), tagStr) Notify("closing %s position %s with tags: %v", e.symbol, percentage.Percentage(), tagStr)
_, err := e.SubmitOrders(ctx, *submitOrder) _, err := e.SubmitOrders(ctx, *submitOrder)
return err return err
} }