mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 23:05:15 +00:00
improving logs
This commit is contained in:
parent
f69cbe9c31
commit
dc040bb82b
|
@ -25,9 +25,10 @@ type TwapExecution struct {
|
||||||
market types.Market
|
market types.Market
|
||||||
marketDataStream types.Stream
|
marketDataStream types.Stream
|
||||||
userDataStream types.Stream
|
userDataStream types.Stream
|
||||||
orderBook *types.StreamOrderBook
|
|
||||||
currentPrice fixedpoint.Value
|
orderBook *types.StreamOrderBook
|
||||||
activePosition fixedpoint.Value
|
currentPrice fixedpoint.Value
|
||||||
|
activePosition fixedpoint.Value
|
||||||
|
|
||||||
activeMakerOrders *LocalActiveOrderBook
|
activeMakerOrders *LocalActiveOrderBook
|
||||||
orderStore *OrderStore
|
orderStore *OrderStore
|
||||||
|
@ -91,6 +92,7 @@ func (e *TwapExecution) newBestPriceMakerOrder() (orderForm types.SubmitOrder, e
|
||||||
|
|
||||||
tickSize := fixedpoint.NewFromFloat(e.market.TickSize)
|
tickSize := fixedpoint.NewFromFloat(e.market.TickSize)
|
||||||
if spread > tickSize {
|
if spread > tickSize {
|
||||||
|
log.Infof("spread %f is greater than the tick size %f, adding 1 tick to the price...", spread.Float64(), tickSize.Float64())
|
||||||
switch e.Side {
|
switch e.Side {
|
||||||
case types.SideTypeSell:
|
case types.SideTypeSell:
|
||||||
newPrice -= fixedpoint.NewFromFloat(e.market.TickSize)
|
newPrice -= fixedpoint.NewFromFloat(e.market.TickSize)
|
||||||
|
@ -113,11 +115,6 @@ func (e *TwapExecution) newBestPriceMakerOrder() (orderForm types.SubmitOrder, e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *TwapExecution) updateOrder(ctx context.Context) error {
|
func (e *TwapExecution) updateOrder(ctx context.Context) error {
|
||||||
book := e.orderBook.Get()
|
|
||||||
bestBid, _ := book.BestBid()
|
|
||||||
bestAsk, _ := book.BestAsk()
|
|
||||||
log.Infof("best bid %f, best ask %f", bestBid.Price.Float64(), bestAsk.Price.Float64())
|
|
||||||
|
|
||||||
sideBook, err := e.getSideBook()
|
sideBook, err := e.getSideBook()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -154,22 +151,20 @@ func (e *TwapExecution) updateOrder(ctx context.Context) error {
|
||||||
// if there is no gap between the first price entry and the second price entry
|
// if there is no gap between the first price entry and the second price entry
|
||||||
second, ok := sideBook.Second()
|
second, ok := sideBook.Second()
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("there is no secoond price on the %s order book %s", e.Symbol, e.Side)
|
return fmt.Errorf("there is no secoond price on the %s order book %s, can not update", e.Symbol, e.Side)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("checking second price %f - %f")
|
|
||||||
// if there is no gap
|
// if there is no gap
|
||||||
if fixedpoint.Abs(first.Price-second.Price) == tickSize {
|
if fixedpoint.Abs(first.Price-second.Price) == tickSize {
|
||||||
|
log.Infof("there is no gap between the second price %f and the first price %f (tick size = %f), skip updating",
|
||||||
|
first.Price.Float64(),
|
||||||
|
second.Price.Float64(),
|
||||||
|
tickSize.Float64())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("canceling orders...")
|
e.cancelActiveOrders(ctx)
|
||||||
if err := e.Session.Exchange.CancelOrders(ctx, orders...); err != nil {
|
|
||||||
log.WithError(err).Errorf("can not cancel %s orders", e.Symbol)
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
orderForm, err := e.newBestPriceMakerOrder()
|
orderForm, err := e.newBestPriceMakerOrder()
|
||||||
|
@ -187,10 +182,33 @@ func (e *TwapExecution) updateOrder(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *TwapExecution) cancelActiveOrders(ctx context.Context) {
|
||||||
|
didCancel := false
|
||||||
|
for e.activeMakerOrders.NumOfOrders() > 0 {
|
||||||
|
didCancel = true
|
||||||
|
|
||||||
|
log.Infof("canceling open orders...")
|
||||||
|
orders := e.activeMakerOrders.Orders()
|
||||||
|
if err := e.Session.Exchange.CancelOrders(ctx, orders...); err != nil {
|
||||||
|
log.WithError(err).Errorf("can not cancel %s orders", e.Symbol)
|
||||||
|
}
|
||||||
|
time.Sleep(3 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
if didCancel {
|
||||||
|
log.Infof("orders are canceled successfully")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (e *TwapExecution) orderUpdater(ctx context.Context) {
|
func (e *TwapExecution) orderUpdater(ctx context.Context) {
|
||||||
rateLimiter := rate.NewLimiter(rate.Every(time.Minute), 15)
|
rateLimiter := rate.NewLimiter(rate.Every(time.Minute), 15)
|
||||||
ticker := time.NewTimer(5 * time.Second)
|
ticker := time.NewTimer(5 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
e.cancelActiveOrders(context.Background())
|
||||||
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
@ -218,7 +236,7 @@ func (e *TwapExecution) orderUpdater(ctx context.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e TwapExecution) tradeUpdateHandler(trade types.Trade) {
|
func (e *TwapExecution) handleTradeUpdate(trade types.Trade) {
|
||||||
// ignore trades that are not in the symbol we interested
|
// ignore trades that are not in the symbol we interested
|
||||||
if trade.Symbol != e.Symbol {
|
if trade.Symbol != e.Symbol {
|
||||||
return
|
return
|
||||||
|
@ -236,6 +254,10 @@ func (e TwapExecution) tradeUpdateHandler(trade types.Trade) {
|
||||||
log.Infof("position updated: %+v", e.position)
|
log.Infof("position updated: %+v", e.position)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *TwapExecution) handleFilledOrder(order types.Order) {
|
||||||
|
log.Infof("order is filled: %s", order.String())
|
||||||
|
}
|
||||||
|
|
||||||
func (e *TwapExecution) Run(ctx context.Context) error {
|
func (e *TwapExecution) Run(ctx context.Context) error {
|
||||||
var ok bool
|
var ok bool
|
||||||
e.market, ok = e.Session.Market(e.Symbol)
|
e.market, ok = e.Session.Market(e.Symbol)
|
||||||
|
@ -252,7 +274,7 @@ func (e *TwapExecution) Run(ctx context.Context) error {
|
||||||
go e.connectMarketData(ctx)
|
go e.connectMarketData(ctx)
|
||||||
|
|
||||||
e.userDataStream = e.Session.Exchange.NewStream()
|
e.userDataStream = e.Session.Exchange.NewStream()
|
||||||
e.userDataStream.OnTradeUpdate(e.tradeUpdateHandler)
|
e.userDataStream.OnTradeUpdate(e.handleTradeUpdate)
|
||||||
e.position = &Position{
|
e.position = &Position{
|
||||||
Symbol: e.Symbol,
|
Symbol: e.Symbol,
|
||||||
BaseCurrency: e.market.BaseCurrency,
|
BaseCurrency: e.market.BaseCurrency,
|
||||||
|
@ -262,6 +284,7 @@ func (e *TwapExecution) Run(ctx context.Context) error {
|
||||||
e.orderStore = NewOrderStore(e.Symbol)
|
e.orderStore = NewOrderStore(e.Symbol)
|
||||||
e.orderStore.BindStream(e.userDataStream)
|
e.orderStore.BindStream(e.userDataStream)
|
||||||
e.activeMakerOrders = NewLocalActiveOrderBook()
|
e.activeMakerOrders = NewLocalActiveOrderBook()
|
||||||
|
e.activeMakerOrders.OnFilled(e.handleFilledOrder)
|
||||||
e.activeMakerOrders.BindStream(e.userDataStream)
|
e.activeMakerOrders.BindStream(e.userDataStream)
|
||||||
|
|
||||||
go e.connectUserData(ctx)
|
go e.connectUserData(ctx)
|
||||||
|
|
|
@ -140,7 +140,7 @@ func (b *OrderBook) Spread() (fixedpoint.Value, bool) {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
bestAsk, ok := b.BestBid()
|
bestAsk, ok := b.BestAsk()
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user