@@ -35,15 +35,21 @@ type Strategy struct {
35
35
36
36
bbgo.QuantityOrAmount
37
37
// bbgo.OpenPositionOptions
38
+
39
+ logger * logrus.Entry
38
40
}
39
41
40
42
func (s * Strategy ) Initialize () error {
41
43
if s .Strategy == nil {
42
44
s .Strategy = & common.Strategy {}
43
45
}
46
+
47
+ s .logger = log .WithFields (logrus.Fields {
48
+ "symbol" : s .Symbol ,
49
+ "window" : s .Window ,
50
+ })
44
51
return nil
45
52
}
46
-
47
53
func (s * Strategy ) ID () string {
48
54
return ID
49
55
}
@@ -65,7 +71,6 @@ func (s *Strategy) Defaults() error {
65
71
if s .Interval == "" {
66
72
s .Interval = types .Interval5m
67
73
}
68
-
69
74
return nil
70
75
}
71
76
@@ -76,29 +81,29 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
76
81
77
82
session .MarketDataStream .OnKLineClosed (types .KLineWith (s .Symbol , s .Interval , func (k types.KLine ) {
78
83
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..." )
80
85
return
81
86
}
82
87
83
88
account , err := session .UpdateAccount (ctx )
84
89
if err != nil {
85
- log .WithError (err ).Error ("unable to update account" )
90
+ s . logger .WithError (err ).Error ("unable to update account" )
86
91
return
87
92
}
88
93
89
94
baseBalance , ok := account .Balance (s .Market .BaseCurrency )
90
95
if ! ok {
91
- log .Errorf ("%s balance not found" , s .Market .BaseCurrency )
96
+ s . logger .Errorf ("%s balance not found" , s .Market .BaseCurrency )
92
97
return
93
98
}
94
99
quoteBalance , ok := account .Balance (s .Market .QuoteCurrency )
95
100
if ! ok {
96
- log .Errorf ("%s balance not found" , s .Market .QuoteCurrency )
101
+ s . logger .Errorf ("%s balance not found" , s .Market .QuoteCurrency )
97
102
return
98
103
}
99
104
100
105
lastAtr := atr .Last (0 )
101
- log .Infof ("atr: %f" , lastAtr )
106
+ s . logger .Infof ("atr: %f" , lastAtr )
102
107
103
108
// protection
104
109
if lastAtr <= k .High .Sub (k .Low ).Float64 () {
@@ -110,15 +115,15 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
110
115
// if the atr is too small, apply the price range protection with 10%
111
116
// priceRange protection 10%
112
117
priceRange = fixedpoint .Max (priceRange , k .Close .Mul (s .MinPriceRange ))
113
- log .Infof ("priceRange: %f" , priceRange .Float64 ())
118
+ s . logger .Infof ("priceRange: %f" , priceRange .Float64 ())
114
119
115
120
ticker , err := session .Exchange .QueryTicker (ctx , s .Symbol )
116
121
if err != nil {
117
- log .WithError (err ).Error ("unable to query ticker" )
122
+ s . logger .WithError (err ).Error ("unable to query ticker" )
118
123
return
119
124
}
120
125
121
- log .Info (ticker .String ())
126
+ s . logger .Info (ticker .String ())
122
127
123
128
bidPrice := fixedpoint .Max (ticker .Buy .Sub (priceRange ), s .Market .TickSize )
124
129
askPrice := ticker .Sell .Add (priceRange )
@@ -129,7 +134,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
129
134
var orderForms []types.SubmitOrder
130
135
131
136
position := s .Strategy .OrderExecutor .Position ()
132
- log .Infof ("position: %+v" , position )
137
+ s . logger .Infof ("position: %+v" , position )
133
138
134
139
side := types .SideTypeBuy
135
140
takerPrice := ticker .Sell
@@ -139,7 +144,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
139
144
}
140
145
141
146
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 )
143
148
144
149
orderForms = append (orderForms , types.SubmitOrder {
145
150
Symbol : s .Symbol ,
@@ -152,10 +157,10 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
152
157
Tag : "takeProfit" ,
153
158
})
154
159
155
- log .Infof ("SUBMIT TAKER ORDER: %+v" , orderForms )
160
+ s . logger .Infof ("SUBMIT TAKER ORDER: %+v" , orderForms )
156
161
157
162
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 )
159
164
}
160
165
161
166
return
@@ -189,23 +194,23 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
189
194
}
190
195
191
196
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 )
193
198
return
194
199
}
195
200
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 ())
197
202
198
- log .Infof ("submit orders: %+v" , orderForms )
203
+ s . logger .Infof ("submit orders: %+v" , orderForms )
199
204
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 )
201
206
}
202
207
}))
203
208
204
209
bbgo .OnShutdown (ctx , func (ctx context.Context , wg * sync.WaitGroup ) {
205
210
defer wg .Done ()
206
211
207
212
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..." )
209
214
}
210
215
211
216
bbgo .Sync (ctx , s )
0 commit comments