Merge pull request #1063 from c9s/fix/grid2/logging

grid2: improve logging
This commit is contained in:
Yo-An Lin 2023-02-22 16:12:52 +08:00 committed by GitHub
commit 4b4af5a09c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 17 deletions

View File

@ -14,6 +14,10 @@ func BootstrapEnvironmentLightweight(ctx context.Context, environ *Environment,
return errors.Wrap(err, "exchange session configure error")
}
if userConfig.Logging != nil {
environ.SetLogging(userConfig.Logging)
}
if userConfig.Persistence != nil {
if err := ConfigurePersistence(ctx, userConfig.Persistence); err != nil {
return errors.Wrap(err, "persistence configure error")
@ -32,6 +36,10 @@ func BootstrapEnvironment(ctx context.Context, environ *Environment, userConfig
return errors.Wrap(err, "exchange session configure error")
}
if userConfig.Logging != nil {
environ.SetLogging(userConfig.Logging)
}
if userConfig.Persistence != nil {
if err := ConfigurePersistence(ctx, userConfig.Persistence); err != nil {
return errors.Wrap(err, "persistence configure error")

View File

@ -89,6 +89,11 @@ type NotificationConfig struct {
Switches *NotificationSwitches `json:"switches" yaml:"switches"`
}
type LoggingConfig struct {
Trade bool `json:"trade,omitempty"`
Order bool `json:"order,omitempty"`
}
type Session struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
ExchangeName string `json:"exchange" yaml:"exchange"`
@ -326,6 +331,8 @@ type Config struct {
RiskControls *RiskControls `json:"riskControls,omitempty" yaml:"riskControls,omitempty"`
Logging *LoggingConfig `json:"logging,omitempty"`
ExchangeStrategies []ExchangeStrategyMount `json:"-" yaml:"-"`
CrossExchangeStrategies []CrossExchangeStrategy `json:"-" yaml:"-"`

View File

@ -102,6 +102,8 @@ type Environment struct {
syncStatus SyncStatus
syncConfig *SyncConfig
loggingConfig *LoggingConfig
sessions map[string]*ExchangeSession
}
@ -127,6 +129,10 @@ func (environ *Environment) Sessions() map[string]*ExchangeSession {
return environ.sessions
}
func (environ *Environment) SetLogging(config *LoggingConfig) {
environ.loggingConfig = config
}
func (environ *Environment) SelectSessions(names ...string) map[string]*ExchangeSession {
if len(names) == 0 {
return environ.sessions

View File

@ -261,10 +261,25 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
}
}
// add trade logger
session.UserDataStream.OnTradeUpdate(func(trade types.Trade) {
log.Info(trade.String())
})
if environ.loggingConfig != nil {
if environ.loggingConfig.Trade {
session.UserDataStream.OnTradeUpdate(func(trade types.Trade) {
log.Info(trade.String())
})
}
if environ.loggingConfig.Order {
session.UserDataStream.OnOrderUpdate(func(order types.Order) {
log.Info(order.String())
})
}
} else {
// if logging config is nil, then apply default logging setup
// add trade logger
session.UserDataStream.OnTradeUpdate(func(trade types.Trade) {
log.Info(trade.String())
})
}
if viper.GetBool("debug-kline") {
session.MarketDataStream.OnKLine(func(kline types.KLine) {

View File

@ -2,14 +2,19 @@ package grid2
import (
"fmt"
"strings"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func debugGrid(grid *Grid, book *bbgo.ActiveOrderBook) {
fmt.Println("================== GRID ORDERS ==================")
func debugGrid(logger logrus.FieldLogger, grid *Grid, book *bbgo.ActiveOrderBook) {
var sb strings.Builder
sb.WriteString("================== GRID ORDERS ==================\n")
pins := grid.Pins
missingPins := scanMissingPinPrices(book, pins)
@ -19,30 +24,33 @@ func debugGrid(grid *Grid, book *bbgo.ActiveOrderBook) {
pin := pins[i]
price := fixedpoint.Value(pin)
fmt.Printf("%s -> ", price.String())
sb.WriteString(fmt.Sprintf("%s -> ", price.String()))
existingOrder := book.Lookup(func(o types.Order) bool {
return o.Price.Eq(price)
})
if existingOrder != nil {
fmt.Printf("%s", existingOrder.String())
sb.WriteString(existingOrder.String())
switch existingOrder.Status {
case types.OrderStatusFilled:
fmt.Printf(" | 🔧")
sb.WriteString(" | 🔧")
case types.OrderStatusCanceled:
fmt.Printf(" | 🔄")
sb.WriteString(" | 🔄")
default:
fmt.Printf(" | ✅")
sb.WriteString(" | ✅")
}
} else {
fmt.Printf("ORDER MISSING ⚠️ ")
sb.WriteString("ORDER MISSING ⚠️ ")
if missing == 1 {
fmt.Printf(" COULD BE EMPTY SLOT")
sb.WriteString(" COULD BE EMPTY SLOT")
}
}
fmt.Printf("\n")
sb.WriteString("\n")
}
fmt.Println("================== END OF GRID ORDERS ===================")
sb.WriteString("================== END OF GRID ORDERS ===================")
logger.Infoln(sb.String())
}

View File

@ -1223,7 +1223,7 @@ func (s *Strategy) recoverGridWithOpenOrders(ctx context.Context, historyService
}
}
debugGrid(grid, orderBook)
debugGrid(s.logger, grid, orderBook)
tmpOrders := orderBook.Orders()
@ -1272,7 +1272,7 @@ func (s *Strategy) recoverGridWithOpenOrders(ctx context.Context, historyService
s.logger.Infof("GRID RECOVER COMPLETE")
debugGrid(grid, s.orderExecutor.ActiveMakerOrders())
debugGrid(s.logger, grid, s.orderExecutor.ActiveMakerOrders())
return nil
}