add symbol and window log fields

This commit is contained in:
narumi 2024-05-20 14:08:46 +08:00
parent 970c1bb8c7
commit 0f045dccbb

View File

@ -35,15 +35,21 @@ type Strategy struct {
bbgo.QuantityOrAmount
// bbgo.OpenPositionOptions
logger *logrus.Entry
}
func (s *Strategy) Initialize() error {
if s.Strategy == nil {
s.Strategy = &common.Strategy{}
}
s.logger = log.WithFields(logrus.Fields{
"symbol": s.Symbol,
"window": s.Window,
})
return nil
}
func (s *Strategy) ID() string {
return ID
}
@ -65,7 +71,6 @@ func (s *Strategy) Defaults() error {
if s.Interval == "" {
s.Interval = types.Interval5m
}
return nil
}
@ -76,29 +81,29 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(k types.KLine) {
if err := s.Strategy.OrderExecutor.GracefulCancel(ctx); err != nil {
log.WithError(err).Error("unable to cancel open orders...")
s.logger.WithError(err).Error("unable to cancel open orders...")
return
}
account, err := session.UpdateAccount(ctx)
if err != nil {
log.WithError(err).Error("unable to update account")
s.logger.WithError(err).Error("unable to update account")
return
}
baseBalance, ok := account.Balance(s.Market.BaseCurrency)
if !ok {
log.Errorf("%s balance not found", s.Market.BaseCurrency)
s.logger.Errorf("%s balance not found", s.Market.BaseCurrency)
return
}
quoteBalance, ok := account.Balance(s.Market.QuoteCurrency)
if !ok {
log.Errorf("%s balance not found", s.Market.QuoteCurrency)
s.logger.Errorf("%s balance not found", s.Market.QuoteCurrency)
return
}
lastAtr := atr.Last(0)
log.Infof("atr: %f", lastAtr)
s.logger.Infof("atr: %f", lastAtr)
// protection
if lastAtr <= k.High.Sub(k.Low).Float64() {
@ -110,15 +115,15 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// if the atr is too small, apply the price range protection with 10%
// priceRange protection 10%
priceRange = fixedpoint.Max(priceRange, k.Close.Mul(s.MinPriceRange))
log.Infof("priceRange: %f", priceRange.Float64())
s.logger.Infof("priceRange: %f", priceRange.Float64())
ticker, err := session.Exchange.QueryTicker(ctx, s.Symbol)
if err != nil {
log.WithError(err).Error("unable to query ticker")
s.logger.WithError(err).Error("unable to query ticker")
return
}
log.Info(ticker.String())
s.logger.Info(ticker.String())
bidPrice := fixedpoint.Max(ticker.Buy.Sub(priceRange), s.Market.TickSize)
askPrice := ticker.Sell.Add(priceRange)
@ -129,7 +134,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
var orderForms []types.SubmitOrder
position := s.Strategy.OrderExecutor.Position()
log.Infof("position: %+v", position)
s.logger.Infof("position: %+v", position)
side := types.SideTypeBuy
takerPrice := ticker.Sell
@ -139,7 +144,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
}
if !position.IsDust(takerPrice) {
log.Infof("%s position is not dust", s.Symbol)
s.logger.Infof("%s position is not dust", s.Symbol)
orderForms = append(orderForms, types.SubmitOrder{
Symbol: s.Symbol,
@ -152,10 +157,10 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
Tag: "takeProfit",
})
log.Infof("SUBMIT TAKER ORDER: %+v", orderForms)
s.logger.Infof("SUBMIT TAKER ORDER: %+v", orderForms)
if _, err := s.Strategy.OrderExecutor.SubmitOrders(ctx, orderForms...); err != nil {
log.WithError(err).Errorf("unable to submit orders: %+v", orderForms)
s.logger.WithError(err).Errorf("unable to submit orders: %+v", orderForms)
}
return
@ -189,15 +194,15 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
}
if len(orderForms) == 0 {
log.Infof("no %s order to place", s.Symbol)
s.logger.Infof("no %s order to place", s.Symbol)
return
}
log.Infof("%s bid/ask: %f/%f", s.Symbol, bidPrice.Float64(), askPrice.Float64())
s.logger.Infof("%s bid/ask: %f/%f", s.Symbol, bidPrice.Float64(), askPrice.Float64())
log.Infof("submit orders: %+v", orderForms)
s.logger.Infof("submit orders: %+v", orderForms)
if _, err := s.Strategy.OrderExecutor.SubmitOrders(ctx, orderForms...); err != nil {
log.WithError(err).Errorf("unable to submit orders: %+v", orderForms)
s.logger.WithError(err).Errorf("unable to submit orders: %+v", orderForms)
}
}))
@ -205,7 +210,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
defer wg.Done()
if err := s.Strategy.OrderExecutor.GracefulCancel(ctx); err != nil {
log.WithError(err).Error("unable to cancel open orders...")
s.logger.WithError(err).Error("unable to cancel open orders...")
}
bbgo.Sync(ctx, s)