Skip to content

Commit

Permalink
Merge pull request #1637 from c9s/narumi/atrpin-log-with-fields
Browse files Browse the repository at this point in the history
CHORE: [atrpin] add symbol and window log fields
  • Loading branch information
narumiruna authored May 25, 2024
2 parents 01fac1f + 0f045dc commit 7bde48a
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions pkg/strategy/atrpin/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -65,7 +71,6 @@ func (s *Strategy) Defaults() error {
if s.Interval == "" {
s.Interval = types.Interval5m
}

return nil
}

Expand All @@ -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() {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -189,23 +194,23 @@ 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)
}
}))

bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
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)
Expand Down

0 comments on commit 7bde48a

Please sign in to comment.