ktrade: error handling

ktrade: remove error handling

ktrade: remove error handling

ktrade: remove error handling

ktrade: error handling
This commit is contained in:
austin362667 2022-08-30 09:59:47 +08:00 committed by Austin Liu
parent 49f7c3de46
commit a18a06819e

View File

@ -31,11 +31,11 @@ type Minute struct {
bbgo.QuantityOrAmount bbgo.QuantityOrAmount
} }
func (s *Minute) updateOrder(ctx context.Context, symbol string) { func (s *Minute) updateQuote(ctx context.Context, symbol string) {
bestBid, bestAsk, _ := s.StreamBook.BestBidAndAsk() bestBid, bestAsk, _ := s.StreamBook.BestBidAndAsk()
s.midPrice = bestBid.Price.Add(bestAsk.Price).Div(fixedpoint.NewFromInt(2)) s.midPrice = bestBid.Price.Add(bestAsk.Price).Div(fixedpoint.NewFromInt(2))
log.Info(s.midPrice) //log.Info(s.midPrice)
} }
func (s *Minute) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) { func (s *Minute) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) {
@ -52,23 +52,30 @@ func (s *Minute) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.General
session.MarketDataStream.OnMarketTrade(func(trade types.Trade) { session.MarketDataStream.OnMarketTrade(func(trade types.Trade) {
log.Infof("%s trade @ %f", trade.Side, trade.Price.Float64()) log.Infof("%s trade @ %f", trade.Side, trade.Price.Float64())
bestAsk, _ := s.StreamBook.BestAsk() //bestAsk, _ := s.StreamBook.BestAsk()
bestBid, _ := s.StreamBook.BestBid() //bestBid, _ := s.StreamBook.BestBid()
if trade.Side == types.SideTypeBuy && trade.Price.Compare(s.midPrice) > 0 && trade.Price.Compare(bestAsk.Price) <= 0 { if trade.Side == types.SideTypeBuy && trade.Price.Compare(s.midPrice) > 0 {
_ = s.orderExecutor.GracefulCancel(context.Background()) _ = s.orderExecutor.GracefulCancel(context.Background())
// update ask price // update ask price
newAskPrice := s.midPrice.Mul(fixedpoint.NewFromFloat(0.25)).Add(trade.Price.Mul(fixedpoint.NewFromFloat(0.75))) newAskPrice := s.midPrice.Mul(fixedpoint.NewFromFloat(0.25)).Add(trade.Price.Mul(fixedpoint.NewFromFloat(0.75)))
log.Infof("short @ %f", newAskPrice.Float64()) log.Infof("short @ %f", newAskPrice.Float64())
s.placeOrder(context.Background(), types.SideTypeSell, s.Quantity, newAskPrice.Round(2, 1), symbol) if trade.Price.Compare(newAskPrice) > 0 {
s.placeOrder(context.Background(), types.SideTypeSell, s.Quantity, trade.Price.Mul(fixedpoint.NewFromFloat(1.001)), symbol)
} else {
s.placeOrder(context.Background(), types.SideTypeSell, s.Quantity, newAskPrice.Round(2, 1), symbol)
}
} else if trade.Side == types.SideTypeSell && trade.Price.Compare(s.midPrice) < 0 && trade.Price.Compare(bestBid.Price) >= 0 { } else if trade.Side == types.SideTypeSell && trade.Price.Compare(s.midPrice) < 0 {
_ = s.orderExecutor.GracefulCancel(context.Background()) _ = s.orderExecutor.GracefulCancel(context.Background())
// update bid price // update bid price
newBidPrice := s.midPrice.Mul(fixedpoint.NewFromFloat(0.25)).Add(trade.Price.Mul(fixedpoint.NewFromFloat(0.75))) newBidPrice := s.midPrice.Mul(fixedpoint.NewFromFloat(0.25)).Add(trade.Price.Mul(fixedpoint.NewFromFloat(0.75)))
log.Infof("long @ %f", newBidPrice.Float64()) log.Infof("long @ %f", newBidPrice.Float64())
s.placeOrder(context.Background(), types.SideTypeBuy, s.Quantity, newBidPrice.Round(2, 1), symbol) if trade.Price.Compare(newBidPrice) < 0 {
s.placeOrder(context.Background(), types.SideTypeBuy, s.Quantity, trade.Price.Mul(fixedpoint.NewFromFloat(0.999)), symbol)
} else {
s.placeOrder(context.Background(), types.SideTypeBuy, s.Quantity, newBidPrice.Round(2, 1), symbol)
}
} }
}) })
@ -86,7 +93,7 @@ func (s *Minute) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.General
for { for {
select { select {
case <-quoteTicker.C: case <-quoteTicker.C:
s.updateOrder(context.Background(), symbol) s.updateQuote(context.Background(), symbol)
} }
} }
}() }()