-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.go
398 lines (325 loc) · 12.7 KB
/
strategy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package bollgrid
import (
"context"
"fmt"
"math"
"sync"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
)
const ID = "bollgrid"
var log = logrus.WithField("strategy", ID)
func init() {
// Register the pointer of the strategy struct,
// so that bbgo knows what struct to be used to unmarshal the configs (YAML or JSON)
// Note: built-in strategies need to imported manually in the bbgo cmd package.
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
// The notification system will be injected into the strategy automatically.
// This field will be injected automatically since it's a single exchange strategy.
*bbgo.Notifiability
// OrderExecutor is an interface for submitting order.
// This field will be injected automatically since it's a single exchange strategy.
bbgo.OrderExecutor
// if Symbol string field is defined, bbgo will know it's a symbol-based strategy
// The following embedded fields will be injected with the corresponding instances.
// MarketDataStore is a pointer only injection field. public trades, k-lines (candlestick)
// and order book updates are maintained in the market data store.
// This field will be injected automatically since we defined the Symbol field.
*bbgo.MarketDataStore
// StandardIndicatorSet contains the standard indicators of a market (symbol)
// This field will be injected automatically since we defined the Symbol field.
*bbgo.StandardIndicatorSet
// Graceful let you define the graceful shutdown handler
*bbgo.Graceful
// Market stores the configuration of the market, for example, VolumePrecision, PricePrecision, MinLotSize... etc
// This field will be injected automatically since we defined the Symbol field.
types.Market
// These fields will be filled from the config file (it translates YAML to JSON)
Symbol string `json:"symbol"`
// Interval is the interval used by the BOLLINGER indicator (which uses K-Line as its source price)
Interval types.Interval `json:"interval"`
// RepostInterval is the interval for re-posting maker orders
RepostInterval types.Interval `json:"repostInterval"`
// GridPips is the pips of grid
// e.g., 0.001, so that your orders will be submitted at price like 0.127, 0.128, 0.129, 0.130
GridPips fixedpoint.Value `json:"gridPips"`
ProfitSpread fixedpoint.Value `json:"profitSpread"`
// GridNum is the grid number, how many orders you want to post on the orderbook.
GridNum int `json:"gridNumber"`
// Quantity is the quantity you want to submit for each order.
Quantity float64 `json:"quantity"`
// activeOrders is the locally maintained active order book of the maker orders.
activeOrders *bbgo.LocalActiveOrderBook
profitOrders *bbgo.LocalActiveOrderBook
orders *bbgo.OrderStore
// boll is the BOLLINGER indicator we used for predicting the price.
boll *indicator.BOLL
CancelProfitOrdersOnShutdown bool `json: "shutdownCancelProfitOrders"`
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Validate() error {
if s.ProfitSpread <= 0 {
// If profitSpread is empty or its value is negative
return fmt.Errorf("profit spread should bigger than 0")
}
if s.Quantity <= 0 {
// If quantity is empty or its value is negative
return fmt.Errorf("quantity should bigger than 0")
}
return nil
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
if s.Interval == "" {
panic("bollgrid interval can not be empty")
}
// currently we need the 1m kline to update the last close price and indicators
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()})
if len(s.RepostInterval) > 0 && s.Interval != s.RepostInterval {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.RepostInterval.String()})
}
}
func (s *Strategy) generateGridBuyOrders(session *bbgo.ExchangeSession) ([]types.SubmitOrder, error) {
balances := session.Account.Balances()
quoteBalance := balances[s.Market.QuoteCurrency].Available
if quoteBalance <= 0 {
return nil, fmt.Errorf("quote balance %s is zero: %f", s.Market.QuoteCurrency, quoteBalance.Float64())
}
upBand, downBand := s.boll.LastUpBand(), s.boll.LastDownBand()
if upBand <= 0.0 {
return nil, fmt.Errorf("up band == 0")
}
if downBand <= 0.0 {
return nil, fmt.Errorf("down band == 0")
}
currentPrice, ok := session.LastPrice(s.Symbol)
if !ok {
return nil, fmt.Errorf("last price not found")
}
if currentPrice > upBand || currentPrice < downBand {
return nil, fmt.Errorf("current price %f exceed the bollinger band %f <> %f", currentPrice, upBand, downBand)
}
ema99 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 99})
ema25 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 25})
ema7 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 7})
if ema7.Last() > ema25.Last()*1.001 && ema25.Last() > ema99.Last()*1.0005 {
log.Infof("all ema lines trend up, skip buy")
return nil, nil
}
priceRange := upBand - downBand
gridSize := priceRange / float64(s.GridNum)
var orders []types.SubmitOrder
for price := upBand; price >= downBand; price -= gridSize {
if price >= currentPrice {
continue
}
// adjust buy quantity using current quote balance
quantity := bbgo.AdjustFloatQuantityByMaxAmount(s.Quantity, price, quoteBalance.Float64())
order := types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Market: s.Market,
Quantity: quantity,
Price: price,
TimeInForce: "GTC",
}
quoteQuantity := fixedpoint.NewFromFloat(order.Quantity).MulFloat64(price)
if quantity < s.MinQuantity {
// don't submit this order if buy quantity is too small
log.Infof("quote balance %f is not enough, stop generating buy orders", quoteBalance.Float64())
break
}
quoteBalance = quoteBalance.Sub(quoteQuantity)
log.Infof("submitting order: %s", order.String())
orders = append(orders, order)
}
return orders, nil
}
func (s *Strategy) generateGridSellOrders(session *bbgo.ExchangeSession) ([]types.SubmitOrder, error) {
balances := session.Account.Balances()
baseBalance := balances[s.Market.BaseCurrency].Available
if baseBalance <= 0 {
return nil, fmt.Errorf("base balance %s is zero: %+v", s.Market.BaseCurrency, baseBalance.Float64())
}
upBand, downBand := s.boll.LastUpBand(), s.boll.LastDownBand()
if upBand <= 0.0 {
return nil, fmt.Errorf("up band == 0")
}
if downBand <= 0.0 {
return nil, fmt.Errorf("down band == 0")
}
currentPrice, ok := session.LastPrice(s.Symbol)
if !ok {
return nil, fmt.Errorf("last price not found")
}
if currentPrice > upBand || currentPrice < downBand {
return nil, fmt.Errorf("current price exceed the bollinger band")
}
ema99 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 99})
ema25 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 25})
ema7 := s.StandardIndicatorSet.EWMA(types.IntervalWindow{Interval: s.Interval, Window: 7})
if ema7.Last() < ema25.Last()*(1-0.004) && ema25.Last() < ema99.Last()*(1-0.0005) {
log.Infof("all ema lines trend down, skip sell")
return nil, nil
}
priceRange := upBand - downBand
gridSize := priceRange / float64(s.GridNum)
var orders []types.SubmitOrder
for price := downBand; price <= upBand; price += gridSize {
if price <= currentPrice {
continue
}
// adjust sell quantity using current base balance
quantity := math.Min(s.Quantity, baseBalance.Float64())
order := types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeSell,
Type: types.OrderTypeLimit,
Market: s.Market,
Quantity: quantity,
Price: price,
TimeInForce: "GTC",
}
baseQuantity := fixedpoint.NewFromFloat(order.Quantity)
if quantity < s.MinQuantity {
// don't submit this order if sell quantity is too small
log.Infof("base balance %f is not enough, stop generating sell orders", baseBalance.Float64())
break
}
baseBalance = baseBalance.Sub(baseQuantity)
log.Infof("submitting order: %s", order.String())
orders = append(orders, order)
}
return orders, nil
}
func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
sellOrders, err := s.generateGridSellOrders(session)
if err != nil {
log.Warn(err.Error())
}
createdSellOrders, err := orderExecutor.SubmitOrders(context.Background(), sellOrders...)
if err != nil {
log.WithError(err).Errorf("can not place sell orders")
}
buyOrders, err := s.generateGridBuyOrders(session)
if err != nil {
log.Warn(err.Error())
}
createdBuyOrders, err := orderExecutor.SubmitOrders(context.Background(), buyOrders...)
if err != nil {
log.WithError(err).Errorf("can not place buy orders")
}
createdOrders := append(createdSellOrders, createdBuyOrders...)
s.activeOrders.Add(createdOrders...)
s.orders.Add(createdOrders...)
}
func (s *Strategy) updateOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
if err := session.Exchange.CancelOrders(context.Background(), s.activeOrders.Orders()...); err != nil {
log.WithError(err).Errorf("cancel order error")
}
// skip order updates if up-band - down-band < min profit spread
if (s.boll.LastUpBand() - s.boll.LastDownBand()) <= s.ProfitSpread.Float64() {
log.Infof("boll: down band price == up band price, skipping...")
return
}
s.placeGridOrders(orderExecutor, session)
s.activeOrders.Print()
}
func (s *Strategy) submitReverseOrder(order types.Order, session *bbgo.ExchangeSession) {
balances := session.Account.Balances()
var side = order.Side.Reverse()
var price = order.Price
var quantity = order.Quantity
switch side {
case types.SideTypeSell:
price += s.ProfitSpread.Float64()
maxQuantity := balances[s.Market.BaseCurrency].Available.Float64()
quantity = math.Min(quantity, maxQuantity)
case types.SideTypeBuy:
price -= s.ProfitSpread.Float64()
maxQuantity := balances[s.Market.QuoteCurrency].Available.Float64() / price
quantity = math.Min(quantity, maxQuantity)
}
submitOrder := types.SubmitOrder{
Symbol: s.Symbol,
Side: side,
Type: types.OrderTypeLimit,
Quantity: quantity,
Price: price,
TimeInForce: "GTC",
}
log.Infof("submitting reverse order: %s against %s", submitOrder.String(), order.String())
createdOrders, err := s.OrderExecutor.SubmitOrders(context.Background(), submitOrder)
if err != nil {
log.WithError(err).Errorf("can not place orders")
return
}
s.profitOrders.Add(createdOrders...)
s.orders.Add(createdOrders...)
}
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
if s.GridNum == 0 {
s.GridNum = 2
}
s.boll = s.StandardIndicatorSet.BOLL(types.IntervalWindow{
Interval: s.Interval,
Window: 21,
}, 2.0)
s.orders = bbgo.NewOrderStore(s.Symbol)
s.orders.BindStream(session.UserDataStream)
// we don't persist orders so that we can not clear the previous orders for now. just need time to support this.
s.activeOrders = bbgo.NewLocalActiveOrderBook()
s.activeOrders.OnFilled(func(o types.Order) {
s.submitReverseOrder(o, session)
})
s.activeOrders.BindStream(session.UserDataStream)
s.profitOrders = bbgo.NewLocalActiveOrderBook()
s.profitOrders.OnFilled(func(o types.Order) {
// we made profit here!
})
s.profitOrders.BindStream(session.UserDataStream)
// setup graceful shutting down handler
s.Graceful.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
// call Done to notify the main process.
defer wg.Done()
log.Infof("canceling active orders...")
if err := session.Exchange.CancelOrders(ctx, s.activeOrders.Orders()...); err != nil {
log.WithError(err).Errorf("cancel order error")
}
if s.CancelProfitOrdersOnShutdown {
log.Infof("canceling profit orders...")
err := session.Exchange.CancelOrders(ctx, s.profitOrders.Orders()...)
if err != nil {
log.WithError(err).Errorf("cancel profit order error")
}
}
})
session.UserDataStream.OnStart(func() {
log.Infof("connected, submitting the first round of the orders")
s.updateOrders(orderExecutor, session)
})
// avoid using time ticker since we will need back testing here
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
// skip kline events that does not belong to this symbol
if kline.Symbol != s.Symbol {
log.Infof("%s != %s", kline.Symbol, s.Symbol)
return
}
if s.RepostInterval != "" {
// see if we have enough balances and then we create limit orders on the up band and the down band.
if s.RepostInterval == kline.Interval {
s.updateOrders(orderExecutor, session)
}
} else if s.Interval == kline.Interval {
s.updateOrders(orderExecutor, session)
}
})
return nil
}