grid2: add order filled handler

This commit is contained in:
c9s 2022-12-03 14:46:05 +08:00
parent 3d77a319fc
commit 54ffc8cbcc
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -126,7 +126,43 @@ func (s *Strategy) InstanceID() string {
} }
func (s *Strategy) handleOrderFilled(o types.Order) { func (s *Strategy) handleOrderFilled(o types.Order) {
// check order fee
newSide := types.SideTypeSell
newPrice := o.Price
newQuantity := o.Quantity
// quantityReduction := fixedpoint.Zero
switch o.Side {
case types.SideTypeSell:
newSide = types.SideTypeBuy
if pin, ok := s.grid.NextLowerPin(newPrice); ok {
newPrice = fixedpoint.Value(pin)
}
case types.SideTypeBuy:
newSide = types.SideTypeSell
if pin, ok := s.grid.NextHigherPin(newPrice); ok {
newPrice = fixedpoint.Value(pin)
}
}
orderForm := types.SubmitOrder{
Symbol: s.Symbol,
Market: s.Market,
Type: types.OrderTypeLimit,
Price: newPrice,
Side: newSide,
TimeInForce: types.TimeInForceGTC,
Tag: "grid",
Quantity: newQuantity,
}
if createdOrders, err := s.orderExecutor.SubmitOrders(context.Background(), orderForm); err != nil {
s.logger.WithError(err).Errorf("can not submit arbitrage order")
} else {
s.logger.Infof("order created: %+v", createdOrders)
}
} }
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
@ -155,6 +191,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
bbgo.Sync(ctx, s) bbgo.Sync(ctx, s)
}) })
s.orderExecutor.ActiveMakerOrders().OnFilled(s.handleOrderFilled)
s.grid = NewGrid(s.LowerPrice, s.UpperPrice, fixedpoint.NewFromInt(s.GridNum), s.Market.TickSize) s.grid = NewGrid(s.LowerPrice, s.UpperPrice, fixedpoint.NewFromInt(s.GridNum), s.Market.TickSize)
s.grid.CalculateArithmeticPins() s.grid.CalculateArithmeticPins()
@ -502,7 +539,7 @@ func (s *Strategy) setupGridOrders(ctx context.Context, session *bbgo.ExchangeSe
if usedBase.Add(quantity).Compare(totalBase) < 0 { if usedBase.Add(quantity).Compare(totalBase) < 0 {
submitOrders = append(submitOrders, types.SubmitOrder{ submitOrders = append(submitOrders, types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
Type: types.OrderTypeLimitMaker, Type: types.OrderTypeLimit,
Side: types.SideTypeSell, Side: types.SideTypeSell,
Price: price, Price: price,
Quantity: quantity, Quantity: quantity,
@ -517,7 +554,7 @@ func (s *Strategy) setupGridOrders(ctx context.Context, session *bbgo.ExchangeSe
nextPrice := fixedpoint.Value(nextPin) nextPrice := fixedpoint.Value(nextPin)
submitOrders = append(submitOrders, types.SubmitOrder{ submitOrders = append(submitOrders, types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
Type: types.OrderTypeLimitMaker, Type: types.OrderTypeLimit,
Side: types.SideTypeBuy, Side: types.SideTypeBuy,
Price: nextPrice, Price: nextPrice,
Quantity: quantity, Quantity: quantity,
@ -536,7 +573,7 @@ func (s *Strategy) setupGridOrders(ctx context.Context, session *bbgo.ExchangeSe
submitOrders = append(submitOrders, types.SubmitOrder{ submitOrders = append(submitOrders, types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
Type: types.OrderTypeLimitMaker, Type: types.OrderTypeLimit,
Side: types.SideTypeBuy, Side: types.SideTypeBuy,
Price: price, Price: price,
Quantity: quantity, Quantity: quantity,