Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMPROVE: [xalign] improve logs #1801

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/strategy/common/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ type Strategy struct {
RiskController
}

func (s *Strategy) Initialize(ctx context.Context, environ *bbgo.Environment, session *bbgo.ExchangeSession, market types.Market, strategyID, instanceID string) {
func (s *Strategy) Initialize(
ctx context.Context, environ *bbgo.Environment, session *bbgo.ExchangeSession, market types.Market, strategyID, instanceID string,
) {
s.parent = ctx
s.ctx, s.cancel = context.WithCancel(ctx)

Expand Down Expand Up @@ -92,6 +94,7 @@ func (s *Strategy) IsHalted(t time.Time) bool {
if s.circuitBreakRiskControl == nil {
return false
}

_, isHalted := s.circuitBreakRiskControl.IsHalted(t)
return isHalted
}
4 changes: 3 additions & 1 deletion pkg/strategy/liquiditymaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ func (s *Strategy) liquidityWorker(ctx context.Context, interval types.Interval)
return

case <-metricsTicker.C:
s.updateMarketMetrics(ctx)
if err := s.updateMarketMetrics(ctx); err != nil {
s.logger.WithError(err).Errorf("unable to update market metrics")
}

case <-ticker.C:
s.placeLiquidityOrders(ctx)
Expand Down
40 changes: 31 additions & 9 deletions pkg/strategy/xalign/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
return nil
}

func (s *Strategy) resetFaultBalanceRecords(currency string) {
s.faultBalanceRecords[currency] = nil
}

func (s *Strategy) recordBalance(totalBalances types.BalanceMap) {
now := time.Now()
for currency, expectedBalance := range s.ExpectedBalances {
Expand All @@ -450,7 +454,7 @@ func (s *Strategy) recordBalance(totalBalances types.BalanceMap) {
})
} else {
// reset counter
s.faultBalanceRecords[currency] = nil
s.resetFaultBalanceRecords(currency)
}
}
}
Expand All @@ -473,22 +477,35 @@ func (s *Strategy) align(ctx context.Context, sessions map[string]*bbgo.Exchange
if err != nil {
log.WithError(err).Errorf("unable to check active transfers (withdraw)")
} else if pendingWithdraw != nil {
log.Warnf("found active transfer (withdraw), skip balance align check")
log.Warnf("found active transfer (%f %s withdraw), skip balance align check",
pendingWithdraw.Amount.Float64(),
pendingWithdraw.Asset)

s.resetFaultBalanceRecords(pendingWithdraw.Asset)

if activeTransferNotificationLimiter.Allow() {
bbgo.Notify("Found active withdraw, skip balance align", pendingWithdraw)
bbgo.Notify("Found active %s withdraw, skip balance align",
pendingWithdraw.Asset,
pendingWithdraw)
}

return
}

pendingDeposit, err := s.detectActiveDeposit(ctx, sessions)
if err != nil {
log.WithError(err).Errorf("unable to check active transfers (deposit)")
} else if pendingDeposit != nil {
log.Warnf("found active transfer (deposit), skip balance align check")
log.Warnf("found active transfer (%f %s deposit), skip balance align check",
pendingDeposit.Amount.Float64(),
pendingDeposit.Asset)

s.resetFaultBalanceRecords(pendingDeposit.Asset)

if activeTransferNotificationLimiter.Allow() {
bbgo.Notify("Found active deposit, skip balance align", pendingDeposit)
bbgo.Notify("Found active %s deposit, skip balance align",
pendingDeposit.Asset,
pendingDeposit)
}
return
}
Expand All @@ -502,7 +519,7 @@ func (s *Strategy) align(ctx context.Context, sessions map[string]*bbgo.Exchange
q := s.calculateRefillQuantity(totalBalances, currency, expectedBalance)

if s.Duration > 0 {
log.Infof("checking fault balance records...")
log.Infof("checking %s fault balance records...", currency)
if faultBalance, ok := s.faultBalanceRecords[currency]; ok && len(faultBalance) > 0 {
if time.Since(faultBalance[0].Time) < s.Duration.Duration() {
log.Infof("%s fault record since: %s < persistence period %s", currency, faultBalance[0].Time, s.Duration.Duration())
Expand All @@ -513,17 +530,21 @@ func (s *Strategy) align(ctx context.Context, sessions map[string]*bbgo.Exchange

selectedSession, submitOrder := s.selectSessionForCurrency(ctx, sessions, currency, q)
if selectedSession != nil && submitOrder != nil {
log.Infof("placing order on %s: %+v", selectedSession.Name, submitOrder)
log.Infof("placing %s order on %s: %+v", submitOrder.Symbol, selectedSession.Name, submitOrder)

bbgo.Notify("Aligning position on exchange session %s, delta: %f", selectedSession.Name, q.Float64(), submitOrder)
bbgo.Notify("Aligning %s position on exchange session %s, delta: %f %s, expected balance: %f %s",
currency, selectedSession.Name,
q.Float64(), currency,
expectedBalance.Float64(), currency,
submitOrder)

if s.DryRun {
return
}

createdOrder, err := selectedSession.Exchange.SubmitOrder(ctx, *submitOrder)
if err != nil {
log.WithError(err).Errorf("can not place order")
log.WithError(err).Errorf("can not place order: %+v", submitOrder)
return
}

Expand All @@ -533,6 +554,7 @@ func (s *Strategy) align(ctx context.Context, sessions map[string]*bbgo.Exchange
} else {
log.Errorf("orderbook %s not found", selectedSession.Name)
}

s.orderBooks[selectedSession.Name].Add(*createdOrder)
}
}
Expand Down
Loading