-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.go
606 lines (493 loc) · 18.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
package grid
import (
"context"
"fmt"
"sync"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/exchange/max"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/types"
)
const ID = "grid"
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{})
}
// State is the grid snapshot
type State struct {
Orders []types.SubmitOrder `json:"orders,omitempty"`
FilledBuyGrids map[fixedpoint.Value]struct{} `json:"filledBuyGrids"`
FilledSellGrids map[fixedpoint.Value]struct{} `json:"filledSellGrids"`
Position *bbgo.Position `json:"position,omitempty"`
AccumulativeArbitrageProfit fixedpoint.Value `json:"accumulativeArbitrageProfit"`
// any created orders for tracking trades
// [source Order ID] -> arbitrage order
ArbitrageOrders map[uint64]types.Order `json:"arbitrageOrders"`
}
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 `json:"-" yaml:"-"`
*bbgo.Graceful `json:"-" yaml:"-"`
*bbgo.Persistence
// OrderExecutor is an interface for submitting order.
// This field will be injected automatically since it's a single exchange strategy.
bbgo.OrderExecutor `json:"-" yaml:"-"`
// 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 `json:"-" yaml:"-"`
TradeService *service.TradeService `json:"-" yaml:"-"`
// These fields will be filled from the config file (it translates YAML to JSON)
Symbol string `json:"symbol" yaml:"symbol"`
// ProfitSpread is the fixed profit spread you want to submit the sell order
ProfitSpread fixedpoint.Value `json:"profitSpread" yaml:"profitSpread"`
// GridNum is the grid number, how many orders you want to post on the orderbook.
GridNum int `json:"gridNumber" yaml:"gridNumber"`
UpperPrice fixedpoint.Value `json:"upperPrice" yaml:"upperPrice"`
LowerPrice fixedpoint.Value `json:"lowerPrice" yaml:"lowerPrice"`
// Quantity is the quantity you want to submit for each order.
Quantity fixedpoint.Value `json:"quantity,omitempty"`
// QuantityScale helps user to define the quantity by price scale or volume scale
QuantityScale *bbgo.PriceVolumeScale `json:"quantityScale,omitempty"`
// FixedAmount is used for fixed amount (dynamic quantity) if you don't want to use fixed quantity.
FixedAmount fixedpoint.Value `json:"amount,omitempty" yaml:"amount"`
// Side is the initial maker orders side. defaults to "both"
Side types.SideType `json:"side" yaml:"side"`
// CatchUp let the maker grid catch up with the price change.
CatchUp bool `json:"catchUp" yaml:"catchUp"`
// Long means you want to hold more base asset than the quote asset.
Long bool `json:"long,omitempty" yaml:"long,omitempty"`
state *State
// orderStore is used to store all the created orders, so that we can filter the trades.
orderStore *bbgo.OrderStore
// activeOrders is the locally maintained active order book of the maker orders.
activeOrders *bbgo.LocalActiveOrderBook
// groupID is the group ID used for the strategy instance for canceling orders
groupID uint32
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Validate() error {
if s.UpperPrice == 0 {
return errors.New("upperPrice can not be zero, you forgot to set?")
}
if s.LowerPrice == 0 {
return errors.New("lowerPrice can not be zero, you forgot to set?")
}
if s.UpperPrice <= s.LowerPrice {
return fmt.Errorf("upperPrice (%f) should not be less than or equal to lowerPrice (%f)", s.UpperPrice.Float64(), s.LowerPrice.Float64())
}
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 && s.QuantityScale == nil {
return fmt.Errorf("quantity or scaleQuantity can not be zero")
}
return nil
}
func (s *Strategy) generateGridSellOrders(session *bbgo.ExchangeSession) ([]types.SubmitOrder, error) {
currentPriceFloat, ok := session.LastPrice(s.Symbol)
if !ok {
return nil, fmt.Errorf("can not generate sell orders, %s last price not found", s.Symbol)
}
currentPrice := fixedpoint.NewFromFloat(currentPriceFloat)
if currentPrice > s.UpperPrice {
return nil, fmt.Errorf("can not generate sell orders, the current price %f is higher than upper price %f", currentPrice.Float64(), s.UpperPrice.Float64())
}
priceRange := s.UpperPrice - s.LowerPrice
numGrids := fixedpoint.NewFromInt(s.GridNum)
gridSpread := priceRange.Div(numGrids)
// find the nearest grid price from the current price
startPrice := fixedpoint.Max(
s.LowerPrice,
s.UpperPrice-(s.UpperPrice-currentPrice).Div(gridSpread).Floor().Mul(gridSpread))
if startPrice > s.UpperPrice {
return nil, fmt.Errorf("current price %f exceeded the upper price boundary %f",
currentPrice.Float64(),
s.UpperPrice.Float64())
}
balances := session.Account.Balances()
baseBalance, ok := balances[s.Market.BaseCurrency]
if !ok {
return nil, fmt.Errorf("base balance %s not found", s.Market.BaseCurrency)
}
if baseBalance.Available == 0 {
return nil, fmt.Errorf("base balance %s is zero: %+v", s.Market.BaseCurrency, baseBalance)
}
log.Infof("placing grid sell orders from %f ~ %f, grid spread %f",
startPrice.Float64(),
s.UpperPrice.Float64(),
gridSpread.Float64())
var orders []types.SubmitOrder
for price := startPrice; price <= s.UpperPrice; price += gridSpread {
var quantity fixedpoint.Value
if s.Quantity > 0 {
quantity = s.Quantity
} else if s.QuantityScale != nil {
qf, err := s.QuantityScale.Scale(price.Float64(), 0)
if err != nil {
return nil, err
}
quantity = fixedpoint.NewFromFloat(qf)
} else if s.FixedAmount > 0 {
quantity = s.FixedAmount.Div(price)
}
// quoteQuantity := price.Mul(quantity)
if baseBalance.Available < quantity {
return orders, fmt.Errorf("base balance %s %f is not enough, stop generating sell orders",
baseBalance.Currency,
baseBalance.Available.Float64())
}
if _, filled := s.state.FilledSellGrids[price]; filled {
log.Debugf("sell grid at price %f is already filled, skipping", price.Float64())
continue
}
orders = append(orders, types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeSell,
Type: types.OrderTypeLimit,
Market: s.Market,
Quantity: quantity.Float64(),
Price: price.Float64(),
TimeInForce: "GTC",
GroupID: s.groupID,
})
baseBalance.Available -= quantity
s.state.FilledSellGrids[price] = struct{}{}
}
return orders, nil
}
func (s *Strategy) generateGridBuyOrders(session *bbgo.ExchangeSession) ([]types.SubmitOrder, error) {
// session.Exchange.QueryTicker()
currentPriceFloat, ok := session.LastPrice(s.Symbol)
if !ok {
return nil, fmt.Errorf("%s last price not found, skipping", s.Symbol)
}
currentPrice := fixedpoint.NewFromFloat(currentPriceFloat)
if currentPrice < s.LowerPrice {
return nil, fmt.Errorf("current price %f is lower than the lower price %f", currentPrice.Float64(), s.LowerPrice.Float64())
}
priceRange := s.UpperPrice - s.LowerPrice
numGrids := fixedpoint.NewFromInt(s.GridNum)
gridSpread := priceRange.Div(numGrids)
// Find the nearest grid price for placing buy orders:
// buyRange = currentPrice - lowerPrice
// numOfBuyGrids = Floor(buyRange / gridSpread)
// startPrice = lowerPrice + numOfBuyGrids * gridSpread
// priceOfBuyOrder1 = startPrice
// priceOfBuyOrder2 = startPrice - gridSpread
// priceOfBuyOrder3 = startPrice - gridSpread * 2
startPrice := fixedpoint.Min(
s.UpperPrice,
s.LowerPrice+(currentPrice-s.LowerPrice).Div(gridSpread).Floor().Mul(gridSpread))
if startPrice < s.LowerPrice {
return nil, fmt.Errorf("current price %f exceeded the lower price boundary %f",
currentPrice.Float64(),
s.UpperPrice.Float64())
}
balances := session.Account.Balances()
balance, ok := balances[s.Market.QuoteCurrency]
if !ok {
return nil, fmt.Errorf("quote balance %s not found", s.Market.QuoteCurrency)
}
if balance.Available == 0 {
return nil, fmt.Errorf("quote balance %s is zero: %+v", s.Market.QuoteCurrency, balance)
}
log.Infof("placing grid buy orders from %f to %f, grid spread %f",
startPrice.Float64(),
s.LowerPrice.Float64(),
gridSpread.Float64())
var orders []types.SubmitOrder
for price := startPrice; s.LowerPrice <= price; price -= gridSpread {
var quantity fixedpoint.Value
if s.Quantity > 0 {
quantity = s.Quantity
} else if s.QuantityScale != nil {
qf, err := s.QuantityScale.Scale(price.Float64(), 0)
if err != nil {
return nil, err
}
quantity = fixedpoint.NewFromFloat(qf)
} else if s.FixedAmount > 0 {
quantity = s.FixedAmount.Div(price)
}
quoteQuantity := price.Mul(quantity)
if balance.Available < quoteQuantity {
return orders, fmt.Errorf("quote balance %s %f is not enough for %f, stop generating buy orders",
balance.Currency,
balance.Available.Float64(),
quoteQuantity.Float64())
}
if _, filled := s.state.FilledBuyGrids[price]; filled {
log.Debugf("buy grid at price %f is already filled, skipping", price.Float64())
continue
}
orders = append(orders, types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Market: s.Market,
Quantity: quantity.Float64(),
Price: price.Float64(),
TimeInForce: "GTC",
GroupID: s.groupID,
})
balance.Available -= quoteQuantity
s.state.FilledBuyGrids[price] = struct{}{}
}
return orders, nil
}
func (s *Strategy) placeGridSellOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
orderForms, err := s.generateGridSellOrders(session)
if len(orderForms) == 0 {
if err != nil {
return err
}
return errors.New("none of sell order is generated")
}
log.Infof("submitting %d sell orders...", len(orderForms))
createdOrders, err := orderExecutor.SubmitOrders(context.Background(), orderForms...)
s.activeOrders.Add(createdOrders...)
return err
}
func (s *Strategy) placeGridBuyOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
orderForms, err := s.generateGridBuyOrders(session)
if len(orderForms) == 0 {
if err != nil {
return err
}
return errors.New("none of buy order is generated")
}
log.Infof("submitting %d buy orders...", len(orderForms))
createdOrders, err := orderExecutor.SubmitOrders(context.Background(), orderForms...)
s.activeOrders.Add(createdOrders...)
return err
}
func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
log.Infof("placing grid orders on side %s...", s.Side)
switch s.Side {
case types.SideTypeBuy:
if err := s.placeGridBuyOrders(orderExecutor, session); err != nil {
log.Warn(err.Error())
}
case types.SideTypeSell:
if err := s.placeGridSellOrders(orderExecutor, session); err != nil {
log.Warn(err.Error())
}
case types.SideTypeBoth:
if err := s.placeGridSellOrders(orderExecutor, session); err != nil {
log.Warn(err.Error())
}
if err := s.placeGridBuyOrders(orderExecutor, session); err != nil {
log.Warn(err.Error())
}
default:
log.Errorf("invalid side %s", s.Side)
}
}
func (s *Strategy) tradeUpdateHandler(trade types.Trade) {
if trade.Symbol != s.Symbol {
return
}
if s.orderStore.Exists(trade.OrderID) {
log.Infof("received trade update of order %d: %+v", trade.OrderID, trade)
if s.TradeService != nil {
if err := s.TradeService.Mark(context.Background(), trade.ID, ID); err != nil {
log.WithError(err).Error("trade mark error")
}
}
if trade.Side == types.SideTypeSelf {
return
}
profit, netProfit, madeProfit := s.state.Position.AddTrade(trade)
if madeProfit {
s.Notify("%s average cost profit: %f, net profit =~ %f", s.Symbol, profit.Float64(), netProfit.Float64())
}
}
}
func (s *Strategy) handleFilledOrder(filledOrder types.Order) {
// generate arbitrage order
var side = filledOrder.Side.Reverse()
var price = filledOrder.Price
var quantity = filledOrder.Quantity
switch side {
case types.SideTypeSell:
price += s.ProfitSpread.Float64()
case types.SideTypeBuy:
price -= s.ProfitSpread.Float64()
}
if s.FixedAmount > 0 {
quantity = s.FixedAmount.Float64() / price
} else if s.Long {
// long = use the same amount to buy more quantity back
// the original amount
var amount = filledOrder.Price * filledOrder.Quantity
quantity = amount / price
}
submitOrder := types.SubmitOrder{
Symbol: s.Symbol,
Side: side,
Type: types.OrderTypeLimit,
Quantity: quantity,
Price: price,
TimeInForce: "GTC",
GroupID: s.groupID,
}
log.Infof("submitting arbitrage order: %s against filled order %s", submitOrder.String(), filledOrder.String())
createdOrders, err := s.OrderExecutor.SubmitOrders(context.Background(), submitOrder)
// create one-way link from the newly created orders
for _, o := range createdOrders {
s.state.ArbitrageOrders[o.OrderID] = filledOrder
}
s.orderStore.Add(createdOrders...)
s.activeOrders.Add(createdOrders...)
if err != nil {
log.WithError(err).Errorf("can not place orders")
return
}
// calculate arbitrage profit
// TODO: apply fee rate here
if s.Long {
switch filledOrder.Side {
case types.SideTypeSell:
if buyOrder, ok := s.state.ArbitrageOrders[filledOrder.OrderID]; ok {
// use base asset quantity here
baseProfit := buyOrder.Quantity - filledOrder.Quantity
s.state.AccumulativeArbitrageProfit += fixedpoint.NewFromFloat(baseProfit)
s.Notify("%s grid arbitrage profit %f %s, accumulative arbitrage profit %f %s", s.Symbol,
baseProfit, s.Market.BaseCurrency,
s.state.AccumulativeArbitrageProfit.Float64(), s.Market.BaseCurrency,
)
}
case types.SideTypeBuy:
if sellOrder, ok := s.state.ArbitrageOrders[filledOrder.OrderID]; ok {
// use base asset quantity here
baseProfit := filledOrder.Quantity - sellOrder.Quantity
s.state.AccumulativeArbitrageProfit += fixedpoint.NewFromFloat(baseProfit)
s.Notify("%s grid arbitrage profit %f %s, accumulative arbitrage profit %f %s", s.Symbol,
baseProfit, s.Market.BaseCurrency,
s.state.AccumulativeArbitrageProfit.Float64(), s.Market.BaseCurrency,
)
}
}
} else if !s.Long && s.Quantity > 0 {
switch filledOrder.Side {
case types.SideTypeSell:
if buyOrder, ok := s.state.ArbitrageOrders[filledOrder.OrderID]; ok {
// use base asset quantity here
quoteProfit := (filledOrder.Quantity * filledOrder.Price) - (buyOrder.Quantity * buyOrder.Price)
s.state.AccumulativeArbitrageProfit += fixedpoint.NewFromFloat(quoteProfit)
s.Notify("%s grid arbitrage profit %f %s, accumulative arbitrage profit %f %s", s.Symbol,
quoteProfit, s.Market.QuoteCurrency,
s.state.AccumulativeArbitrageProfit.Float64(), s.Market.QuoteCurrency,
)
}
case types.SideTypeBuy:
if sellOrder, ok := s.state.ArbitrageOrders[filledOrder.OrderID]; ok {
// use base asset quantity here
quoteProfit := (sellOrder.Quantity * sellOrder.Price) - (filledOrder.Quantity * filledOrder.Price)
s.state.AccumulativeArbitrageProfit += fixedpoint.NewFromFloat(quoteProfit)
s.Notify("%s grid arbitrage profit %f %s, accumulative arbitrage profit %f %s", s.Symbol,
quoteProfit, s.Market.QuoteCurrency,
s.state.AccumulativeArbitrageProfit.Float64(), s.Market.QuoteCurrency,
)
}
}
}
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"})
}
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
// do some basic validation
if s.GridNum == 0 {
s.GridNum = 10
}
if s.Side == "" {
s.Side = types.SideTypeBoth
}
instanceID := fmt.Sprintf("grid-%s-%d-%d-%d", s.Symbol, s.GridNum, s.UpperPrice, s.LowerPrice)
s.groupID = max.GenerateGroupID(instanceID)
log.Infof("using group id %d from fnv(%s)", s.groupID, instanceID)
var stateLoaded = false
if s.Persistence != nil {
var state State
if err := s.Persistence.Load(&state, ID, instanceID); err != nil {
if err != service.ErrPersistenceNotExists {
return errors.Wrapf(err, "state load error")
}
} else {
log.Infof("grid state loaded")
stateLoaded = true
s.state = &state
}
}
if s.state == nil {
position, ok := session.Position(s.Symbol)
if !ok {
return fmt.Errorf("position not found")
}
s.state = &State{
FilledBuyGrids: make(map[fixedpoint.Value]struct{}),
FilledSellGrids: make(map[fixedpoint.Value]struct{}),
ArbitrageOrders: make(map[uint64]types.Order),
Position: position,
}
}
if s.state.ArbitrageOrders == nil {
s.state.ArbitrageOrders = make(map[uint64]types.Order)
}
s.Notify("current position %+v", s.state.Position)
s.orderStore = bbgo.NewOrderStore(s.Symbol)
s.orderStore.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(s.handleFilledOrder)
s.activeOrders.BindStream(session.UserDataStream)
s.Graceful.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
if s.Persistence != nil {
log.Infof("backing up grid state...")
submitOrders := s.activeOrders.Backup()
s.state.Orders = submitOrders
if err := s.Persistence.Save(s.state, ID, instanceID); err != nil {
log.WithError(err).Error("can not save active order backups")
} else {
log.Infof("active order snapshot saved")
}
}
log.Infof("canceling active orders...")
if err := session.Exchange.CancelOrders(ctx, s.activeOrders.Orders()...); err != nil {
log.WithError(err).Errorf("cancel order error")
}
})
session.UserDataStream.OnTradeUpdate(s.tradeUpdateHandler)
session.UserDataStream.OnStart(func() {
if stateLoaded && len(s.state.Orders) > 0 {
createdOrders, err := orderExecutor.SubmitOrders(ctx, s.state.Orders...)
if err != nil {
log.WithError(err).Error("active orders restore error")
}
s.activeOrders.Add(createdOrders...)
s.orderStore.Add(createdOrders...)
} else {
s.placeGridOrders(orderExecutor, session)
}
})
if s.CatchUp {
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
log.Infof("catchUp mode is enabled, updating grid orders...")
// update grid
s.placeGridOrders(orderExecutor, session)
})
}
return nil
}