Skip to content

Commit 26ba6a5

Browse files
committed
log with fields
1 parent 970c1bb commit 26ba6a5

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

pkg/strategy/atrpin/strategy.go

+24-19
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,21 @@ type Strategy struct {
3535

3636
bbgo.QuantityOrAmount
3737
// bbgo.OpenPositionOptions
38+
39+
logger *logrus.Entry
3840
}
3941

4042
func (s *Strategy) Initialize() error {
4143
if s.Strategy == nil {
4244
s.Strategy = &common.Strategy{}
4345
}
46+
47+
s.logger = log.WithFields(logrus.Fields{
48+
"symbol": s.Symbol,
49+
"window": s.Window,
50+
})
4451
return nil
4552
}
46-
4753
func (s *Strategy) ID() string {
4854
return ID
4955
}
@@ -65,7 +71,6 @@ func (s *Strategy) Defaults() error {
6571
if s.Interval == "" {
6672
s.Interval = types.Interval5m
6773
}
68-
6974
return nil
7075
}
7176

@@ -76,29 +81,29 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
7681

7782
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(k types.KLine) {
7883
if err := s.Strategy.OrderExecutor.GracefulCancel(ctx); err != nil {
79-
log.WithError(err).Error("unable to cancel open orders...")
84+
s.logger.WithError(err).Error("unable to cancel open orders...")
8085
return
8186
}
8287

8388
account, err := session.UpdateAccount(ctx)
8489
if err != nil {
85-
log.WithError(err).Error("unable to update account")
90+
s.logger.WithError(err).Error("unable to update account")
8691
return
8792
}
8893

8994
baseBalance, ok := account.Balance(s.Market.BaseCurrency)
9095
if !ok {
91-
log.Errorf("%s balance not found", s.Market.BaseCurrency)
96+
s.logger.Errorf("%s balance not found", s.Market.BaseCurrency)
9297
return
9398
}
9499
quoteBalance, ok := account.Balance(s.Market.QuoteCurrency)
95100
if !ok {
96-
log.Errorf("%s balance not found", s.Market.QuoteCurrency)
101+
s.logger.Errorf("%s balance not found", s.Market.QuoteCurrency)
97102
return
98103
}
99104

100105
lastAtr := atr.Last(0)
101-
log.Infof("atr: %f", lastAtr)
106+
s.logger.Infof("atr: %f", lastAtr)
102107

103108
// protection
104109
if lastAtr <= k.High.Sub(k.Low).Float64() {
@@ -110,15 +115,15 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
110115
// if the atr is too small, apply the price range protection with 10%
111116
// priceRange protection 10%
112117
priceRange = fixedpoint.Max(priceRange, k.Close.Mul(s.MinPriceRange))
113-
log.Infof("priceRange: %f", priceRange.Float64())
118+
s.logger.Infof("priceRange: %f", priceRange.Float64())
114119

115120
ticker, err := session.Exchange.QueryTicker(ctx, s.Symbol)
116121
if err != nil {
117-
log.WithError(err).Error("unable to query ticker")
122+
s.logger.WithError(err).Error("unable to query ticker")
118123
return
119124
}
120125

121-
log.Info(ticker.String())
126+
s.logger.Info(ticker.String())
122127

123128
bidPrice := fixedpoint.Max(ticker.Buy.Sub(priceRange), s.Market.TickSize)
124129
askPrice := ticker.Sell.Add(priceRange)
@@ -129,7 +134,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
129134
var orderForms []types.SubmitOrder
130135

131136
position := s.Strategy.OrderExecutor.Position()
132-
log.Infof("position: %+v", position)
137+
s.logger.Infof("position: %+v", position)
133138

134139
side := types.SideTypeBuy
135140
takerPrice := ticker.Sell
@@ -139,7 +144,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
139144
}
140145

141146
if !position.IsDust(takerPrice) {
142-
log.Infof("%s position is not dust", s.Symbol)
147+
s.logger.Infof("%s position is not dust", s.Symbol)
143148

144149
orderForms = append(orderForms, types.SubmitOrder{
145150
Symbol: s.Symbol,
@@ -152,10 +157,10 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
152157
Tag: "takeProfit",
153158
})
154159

155-
log.Infof("SUBMIT TAKER ORDER: %+v", orderForms)
160+
s.logger.Infof("SUBMIT TAKER ORDER: %+v", orderForms)
156161

157162
if _, err := s.Strategy.OrderExecutor.SubmitOrders(ctx, orderForms...); err != nil {
158-
log.WithError(err).Errorf("unable to submit orders: %+v", orderForms)
163+
s.logger.WithError(err).Errorf("unable to submit orders: %+v", orderForms)
159164
}
160165

161166
return
@@ -189,23 +194,23 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
189194
}
190195

191196
if len(orderForms) == 0 {
192-
log.Infof("no %s order to place", s.Symbol)
197+
s.logger.Infof("no %s order to place", s.Symbol)
193198
return
194199
}
195200

196-
log.Infof("%s bid/ask: %f/%f", s.Symbol, bidPrice.Float64(), askPrice.Float64())
201+
s.logger.Infof("%s bid/ask: %f/%f", s.Symbol, bidPrice.Float64(), askPrice.Float64())
197202

198-
log.Infof("submit orders: %+v", orderForms)
203+
s.logger.Infof("submit orders: %+v", orderForms)
199204
if _, err := s.Strategy.OrderExecutor.SubmitOrders(ctx, orderForms...); err != nil {
200-
log.WithError(err).Errorf("unable to submit orders: %+v", orderForms)
205+
s.logger.WithError(err).Errorf("unable to submit orders: %+v", orderForms)
201206
}
202207
}))
203208

204209
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
205210
defer wg.Done()
206211

207212
if err := s.Strategy.OrderExecutor.GracefulCancel(ctx); err != nil {
208-
log.WithError(err).Error("unable to cancel open orders...")
213+
s.logger.WithError(err).Error("unable to cancel open orders...")
209214
}
210215

211216
bbgo.Sync(ctx, s)

0 commit comments

Comments
 (0)