-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.go
622 lines (510 loc) · 19 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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package support
import (
"context"
"fmt"
"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 = "support"
var log = logrus.WithField("strategy", ID)
var zeroiw = types.IntervalWindow{}
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
type State struct {
Position *types.Position `json:"position,omitempty"`
CurrentHighestPrice *fixedpoint.Value `json:"currentHighestPrice,omitempty"`
}
type Target struct {
ProfitPercentage fixedpoint.Value `json:"profitPercentage"`
QuantityPercentage fixedpoint.Value `json:"quantityPercentage"`
MarginOrderSideEffect types.MarginOrderSideEffectType `json:"marginOrderSideEffect"`
}
// PercentageTargetStop is a kind of stop order by setting fixed percentage target
type PercentageTargetStop struct {
Targets []Target `json:"targets"`
}
// GenerateOrders generates the orders from the given targets
func (stop *PercentageTargetStop) GenerateOrders(market types.Market, pos *types.Position) []types.SubmitOrder {
var price = pos.AverageCost
var quantity = pos.GetBase()
// submit target orders
var targetOrders []types.SubmitOrder
for _, target := range stop.Targets {
targetPrice := price.Mul(fixedpoint.One.Add(target.ProfitPercentage))
targetQuantity := quantity.Mul(target.QuantityPercentage)
targetQuoteQuantity := targetPrice.Mul(targetQuantity)
if targetQuoteQuantity.Compare(market.MinNotional) <= 0 {
continue
}
if targetQuantity.Compare(market.MinQuantity) <= 0 {
continue
}
targetOrders = append(targetOrders, types.SubmitOrder{
Symbol: market.Symbol,
Market: market,
Type: types.OrderTypeLimit,
Side: types.SideTypeSell,
Price: targetPrice,
Quantity: targetQuantity,
MarginSideEffect: target.MarginOrderSideEffect,
TimeInForce: types.TimeInForceGTC,
})
}
return targetOrders
}
type TrailingStopTarget struct {
TrailingStopCallbackRatio fixedpoint.Value `json:"callbackRatio"`
MinimumProfitPercentage fixedpoint.Value `json:"minimumProfitPercentage"`
}
type TrailingStopControl struct {
symbol string
market types.Market
marginSideEffect types.MarginOrderSideEffectType
trailingStopCallbackRatio fixedpoint.Value
minimumProfitPercentage fixedpoint.Value
CurrentHighestPrice fixedpoint.Value
StopOrder *types.Order
}
func (control *TrailingStopControl) UpdateCurrentHighestPrice(p fixedpoint.Value) bool {
orig := control.CurrentHighestPrice
control.CurrentHighestPrice = fixedpoint.Max(control.CurrentHighestPrice, p)
return orig.Compare(control.CurrentHighestPrice) == 0
}
func (control *TrailingStopControl) IsHigherThanMin(minTargetPrice fixedpoint.Value) bool {
targetPrice := control.CurrentHighestPrice.Mul(fixedpoint.One.Sub(control.trailingStopCallbackRatio))
return targetPrice.Compare(minTargetPrice) >= 0
}
func (control *TrailingStopControl) GenerateStopOrder(quantity fixedpoint.Value) types.SubmitOrder {
targetPrice := control.CurrentHighestPrice.Mul(fixedpoint.One.Sub(control.trailingStopCallbackRatio))
orderForm := types.SubmitOrder{
Symbol: control.symbol,
Market: control.market,
Side: types.SideTypeSell,
Type: types.OrderTypeStopLimit,
Quantity: quantity,
MarginSideEffect: control.marginSideEffect,
TimeInForce: types.TimeInForceGTC,
Price: targetPrice,
StopPrice: targetPrice,
}
return orderForm
}
// Not implemented yet
// ResistanceStop is a kind of stop order by detecting resistance
// type ResistanceStop struct {
// Interval types.Interval `json:"interval"`
// sensitivity fixedpoint.Value `json:"sensitivity"`
// MinVolume fixedpoint.Value `json:"minVolume"`
// TakerBuyRatio fixedpoint.Value `json:"takerBuyRatio"`
// }
type Strategy struct {
*bbgo.Environment `json:"-"`
session *bbgo.ExchangeSession
Symbol string `json:"symbol"`
Market types.Market `json:"-"`
// Interval for checking support
Interval types.Interval `json:"interval"`
// moving average window for checking support (support should be under the moving average line)
TriggerMovingAverage types.IntervalWindow `json:"triggerMovingAverage"`
// LongTermMovingAverage is the second moving average line for checking support position
LongTermMovingAverage types.IntervalWindow `json:"longTermMovingAverage"`
Quantity fixedpoint.Value `json:"quantity"`
MinVolume fixedpoint.Value `json:"minVolume"`
Sensitivity fixedpoint.Value `json:"sensitivity"`
TakerBuyRatio fixedpoint.Value `json:"takerBuyRatio"`
MarginOrderSideEffect types.MarginOrderSideEffectType `json:"marginOrderSideEffect"`
Targets []Target `json:"targets"`
// Not implemented yet
// ResistanceStop *ResistanceStop `json:"resistanceStop"`
//
// ResistanceTakerBuyRatio fixedpoint.Value `json:"resistanceTakerBuyRatio"`
// Min BaseAsset balance to keep
MinBaseAssetBalance fixedpoint.Value `json:"minBaseAssetBalance"`
// Max BaseAsset balance to buy
MaxBaseAssetBalance fixedpoint.Value `json:"maxBaseAssetBalance"`
MinQuoteAssetBalance fixedpoint.Value `json:"minQuoteAssetBalance"`
ScaleQuantity *bbgo.PriceVolumeScale `json:"scaleQuantity"`
orderExecutor *bbgo.GeneralOrderExecutor
Position *types.Position `persistence:"position"`
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
TradeStats *types.TradeStats `persistence:"trade_stats"`
CurrentHighestPrice fixedpoint.Value `persistence:"current_highest_price"`
triggerEMA *indicator.EWMA
longTermEMA *indicator.EWMA
// Trailing stop
TrailingStopTarget TrailingStopTarget `json:"trailingStopTarget"`
trailingStopControl *TrailingStopControl
// StrategyController
bbgo.StrategyController
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s:%s", ID, s.Symbol)
}
func (s *Strategy) Validate() error {
if s.Quantity.IsZero() && s.ScaleQuantity == nil {
return fmt.Errorf("quantity or scaleQuantity can not be zero")
}
if s.MinVolume.IsZero() && s.Sensitivity.IsZero() {
return fmt.Errorf("either minVolume nor sensitivity can not be zero")
}
return nil
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
if s.TriggerMovingAverage != zeroiw {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.TriggerMovingAverage.Interval})
}
if s.LongTermMovingAverage != zeroiw {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.LongTermMovingAverage.Interval})
}
}
func (s *Strategy) CurrentPosition() *types.Position {
return s.Position
}
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
base := s.Position.GetBase()
if base.IsZero() {
return fmt.Errorf("no opened %s position", s.Position.Symbol)
}
// make it negative
quantity := base.Mul(percentage).Abs()
side := types.SideTypeBuy
if base.Sign() > 0 {
side = types.SideTypeSell
}
if quantity.Compare(s.Market.MinQuantity) < 0 {
return fmt.Errorf("order quantity %v is too small, less than %v", quantity, s.Market.MinQuantity)
}
submitOrder := types.SubmitOrder{
Symbol: s.Symbol,
Side: side,
Type: types.OrderTypeMarket,
Quantity: quantity,
Market: s.Market,
}
bbgo.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder)
_, err := s.orderExecutor.SubmitOrders(ctx, submitOrder)
return err
}
func (s *Strategy) submitOrders(ctx context.Context, orderExecutor bbgo.OrderExecutor, orderForms ...types.SubmitOrder) (types.OrderSlice, error) {
return s.orderExecutor.SubmitOrders(ctx, orderForms...)
}
var slippageModifier = fixedpoint.NewFromFloat(1.003)
func (s *Strategy) calculateQuantity(session *bbgo.ExchangeSession, side types.SideType, closePrice fixedpoint.Value, volume fixedpoint.Value) (fixedpoint.Value, error) {
var quantity fixedpoint.Value
if s.Quantity.Sign() > 0 {
quantity = s.Quantity
} else if s.ScaleQuantity != nil {
q, err := s.ScaleQuantity.Scale(closePrice.Float64(), volume.Float64())
if err != nil {
return fixedpoint.Zero, err
}
quantity = fixedpoint.NewFromFloat(q)
}
baseBalance, _ := session.GetAccount().Balance(s.Market.BaseCurrency)
if side == types.SideTypeSell {
// quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quota)
if s.MinBaseAssetBalance.Sign() > 0 &&
baseBalance.Total().Sub(quantity).Compare(s.MinBaseAssetBalance) < 0 {
quota := baseBalance.Available.Sub(s.MinBaseAssetBalance)
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quota)
}
} else if side == types.SideTypeBuy {
if s.MaxBaseAssetBalance.Sign() > 0 &&
baseBalance.Total().Add(quantity).Compare(s.MaxBaseAssetBalance) > 0 {
quota := s.MaxBaseAssetBalance.Sub(baseBalance.Total())
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quota)
}
quoteBalance, ok := session.GetAccount().Balance(s.Market.QuoteCurrency)
if !ok {
return fixedpoint.Zero, fmt.Errorf("quote balance %s not found", s.Market.QuoteCurrency)
}
// for spot, we need to modify the quantity according to the quote balance
if !session.Margin {
// add 0.3% for price slippage
notional := closePrice.Mul(quantity).Mul(slippageModifier)
if s.MinQuoteAssetBalance.Sign() > 0 &&
quoteBalance.Available.Sub(notional).Compare(s.MinQuoteAssetBalance) < 0 {
log.Warnf("modifying quantity %v according to the min quote asset balance %v %s",
quantity,
quoteBalance.Available,
s.Market.QuoteCurrency)
quota := quoteBalance.Available.Sub(s.MinQuoteAssetBalance)
quantity = bbgo.AdjustQuantityByMinAmount(quantity, closePrice, quota)
} else if notional.Compare(quoteBalance.Available) > 0 {
log.Warnf("modifying quantity %v according to the quote asset balance %v %s",
quantity,
quoteBalance.Available,
s.Market.QuoteCurrency)
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, closePrice, quoteBalance.Available)
}
}
}
return quantity, nil
}
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
s.session = session
instanceID := s.InstanceID()
if s.Position == nil {
s.Position = types.NewPositionFromMarket(s.Market)
}
if s.ProfitStats == nil {
s.ProfitStats = types.NewProfitStats(s.Market)
}
// trade stats
if s.TradeStats == nil {
s.TradeStats = types.NewTradeStats(s.Symbol)
}
s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position)
s.orderExecutor.BindEnvironment(s.Environment)
s.orderExecutor.BindProfitStats(s.ProfitStats)
s.orderExecutor.BindTradeStats(s.TradeStats)
s.orderExecutor.Bind()
// StrategyController
s.Status = types.StrategyStatusRunning
s.OnSuspend(func() {
// Cancel all order
_ = s.orderExecutor.GracefulCancel(ctx)
bbgo.Sync(ctx, s)
})
s.OnEmergencyStop(func() {
// Close 100% position
percentage := fixedpoint.NewFromFloat(1.0)
if err := s.ClosePosition(context.Background(), percentage); err != nil {
errMsg := "failed to close position"
log.WithError(err).Errorf(errMsg)
bbgo.Notify(errMsg)
}
if err := s.Suspend(); err != nil {
errMsg := "failed to suspend strategy"
log.WithError(err).Errorf(errMsg)
bbgo.Notify(errMsg)
}
})
// set default values
if s.Interval == "" {
s.Interval = types.Interval5m
}
if s.Sensitivity.Sign() > 0 {
volRange, err := s.ScaleQuantity.ByVolumeRule.Range()
if err != nil {
return err
}
scaleUp := fixedpoint.NewFromFloat(volRange[1])
scaleLow := fixedpoint.NewFromFloat(volRange[0])
s.MinVolume = scaleUp.Sub(scaleLow).
Mul(fixedpoint.One.Sub(s.Sensitivity)).
Add(scaleLow)
log.Infof("adjusted minimal support volume to %s according to sensitivity %s", s.MinVolume.String(), s.Sensitivity.String())
}
standardIndicatorSet := session.StandardIndicatorSet(s.Symbol)
if s.TriggerMovingAverage != zeroiw {
s.triggerEMA = standardIndicatorSet.EWMA(s.TriggerMovingAverage)
} else {
s.triggerEMA = standardIndicatorSet.EWMA(types.IntervalWindow{
Interval: s.Interval,
Window: 99, // default window
})
}
if s.LongTermMovingAverage != zeroiw {
s.longTermEMA = standardIndicatorSet.EWMA(s.LongTermMovingAverage)
}
if !s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() {
s.trailingStopControl = &TrailingStopControl{
symbol: s.Symbol,
market: s.Market,
marginSideEffect: s.MarginOrderSideEffect,
trailingStopCallbackRatio: s.TrailingStopTarget.TrailingStopCallbackRatio,
minimumProfitPercentage: s.TrailingStopTarget.MinimumProfitPercentage,
CurrentHighestPrice: s.CurrentHighestPrice,
}
}
if !s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() {
// Update trailing stop when the position changes
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
// StrategyController
if s.Status != types.StrategyStatusRunning {
return
}
if !position.IsLong() || position.IsDust(position.AverageCost) {
return
}
s.updateStopOrder(ctx)
})
}
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
// StrategyController
if s.Status != types.StrategyStatusRunning {
return
}
// skip k-lines from other symbols
if kline.Symbol != s.Symbol {
return
}
if kline.Interval != s.Interval {
return
}
closePrice := kline.GetClose()
highPrice := kline.GetHigh()
// check our trailing stop
if s.TrailingStopTarget.TrailingStopCallbackRatio.Sign() > 0 {
if s.Position.IsLong() && !s.Position.IsDust(closePrice) {
changed := s.trailingStopControl.UpdateCurrentHighestPrice(highPrice)
if changed {
// Cancel the original order
s.updateStopOrder(ctx)
}
}
}
// check support volume
if kline.Volume.Compare(s.MinVolume) < 0 {
return
}
// check taker buy ratio, we need strong buy taker
if s.TakerBuyRatio.Sign() > 0 {
takerBuyRatio := kline.TakerBuyBaseAssetVolume.Div(kline.Volume)
takerBuyBaseVolumeThreshold := kline.Volume.Mul(s.TakerBuyRatio)
if takerBuyRatio.Compare(s.TakerBuyRatio) < 0 {
bbgo.Notify("%s: taker buy base volume %s (volume ratio %s) is less than %s (volume ratio %s)",
s.Symbol,
kline.TakerBuyBaseAssetVolume.String(),
takerBuyRatio.String(),
takerBuyBaseVolumeThreshold.String(),
kline.Volume.String(),
s.TakerBuyRatio.String(),
kline,
)
return
}
}
if s.longTermEMA != nil && closePrice.Float64() < s.longTermEMA.Last() {
bbgo.Notify("%s: closed price is below the long term moving average line %f, skipping this support",
s.Symbol,
s.longTermEMA.Last(),
kline,
)
return
}
if s.triggerEMA != nil && closePrice.Float64() > s.triggerEMA.Last() {
bbgo.Notify("%s: closed price is above the trigger moving average line %f, skipping this support",
s.Symbol,
s.triggerEMA.Last(),
kline,
)
return
}
if s.triggerEMA != nil && s.longTermEMA != nil {
bbgo.Notify("Found %s support: the close price %s is below trigger EMA %f and above long term EMA %f and volume %s > minimum volume %s",
s.Symbol,
closePrice.String(),
s.triggerEMA.Last(),
s.longTermEMA.Last(),
kline.Volume.String(),
s.MinVolume.String(),
kline)
} else {
bbgo.Notify("Found %s support: the close price %s and volume %s > minimum volume %s",
s.Symbol,
closePrice.String(),
kline.Volume.String(),
s.MinVolume.String(),
kline)
}
quantity, err := s.calculateQuantity(session, types.SideTypeBuy, closePrice, kline.Volume)
if err != nil {
log.WithError(err).Errorf("%s quantity calculation error", s.Symbol)
return
}
orderForm := types.SubmitOrder{
Symbol: s.Symbol,
Market: s.Market,
Side: types.SideTypeBuy,
Type: types.OrderTypeMarket,
Quantity: quantity,
MarginSideEffect: s.MarginOrderSideEffect,
}
bbgo.Notify("Submitting %s market order buy with quantity %s according to the base volume %s, taker buy base volume %s",
s.Symbol,
quantity.String(),
kline.Volume.String(),
kline.TakerBuyBaseAssetVolume.String(),
orderForm)
if _, err := s.submitOrders(ctx, orderExecutor, orderForm); err != nil {
log.WithError(err).Error("submit order error")
return
}
if s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() { // submit fixed target orders
var targetOrders []types.SubmitOrder
for _, target := range s.Targets {
targetPrice := closePrice.Mul(fixedpoint.One.Add(target.ProfitPercentage))
targetQuantity := quantity.Mul(target.QuantityPercentage)
targetQuoteQuantity := targetPrice.Mul(targetQuantity)
if targetQuoteQuantity.Compare(s.Market.MinNotional) <= 0 {
continue
}
if targetQuantity.Compare(s.Market.MinQuantity) <= 0 {
continue
}
targetOrders = append(targetOrders, types.SubmitOrder{
Symbol: kline.Symbol,
Market: s.Market,
Type: types.OrderTypeLimit,
Side: types.SideTypeSell,
Price: targetPrice,
Quantity: targetQuantity,
MarginSideEffect: target.MarginOrderSideEffect,
TimeInForce: types.TimeInForceGTC,
})
}
_, err = s.orderExecutor.SubmitOrders(ctx, targetOrders...)
if err != nil {
bbgo.Notify("submit %s profit trailing stop order error: %s", s.Symbol, err.Error())
}
}
})
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
// Cancel trailing stop order
if s.TrailingStopTarget.TrailingStopCallbackRatio.Sign() > 0 {
_ = s.orderExecutor.GracefulCancel(ctx)
}
})
return nil
}
func (s *Strategy) updateStopOrder(ctx context.Context) {
// cancel the original stop order
if s.trailingStopControl.StopOrder != nil {
if err := s.session.Exchange.CancelOrders(ctx, *s.trailingStopControl.StopOrder); err != nil {
log.WithError(err).Error("cancel order error")
}
s.trailingStopControl.StopOrder = nil
s.orderExecutor.TradeCollector().Process()
}
// Calculate minimum target price
var minTargetPrice = fixedpoint.Zero
if s.trailingStopControl.minimumProfitPercentage.Sign() > 0 {
minTargetPrice = s.Position.AverageCost.Mul(fixedpoint.One.Add(s.trailingStopControl.minimumProfitPercentage))
}
// Place new order if the target price is higher than the minimum target price
if s.trailingStopControl.IsHigherThanMin(minTargetPrice) {
orderForm := s.trailingStopControl.GenerateStopOrder(s.Position.Base)
orders, err := s.orderExecutor.SubmitOrders(ctx, orderForm)
if err != nil {
bbgo.Notify("failed to submit the trailing stop order on %s", s.Symbol)
log.WithError(err).Error("submit profit trailing stop order error")
}
if len(orders) == 0 {
log.Error("unexpected error: len(createdOrders) = 0")
return
}
s.trailingStopControl.StopOrder = &orders[0]
}
}