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