@@ -49,6 +49,7 @@ type Strategy struct {
49
49
orderExecutor * bbgo.GeneralOrderExecutor
50
50
51
51
bbgo.QuantityOrAmount
52
+ Spread float64 `json:"spread"`
52
53
53
54
Interval int `json:"hftInterval"`
54
55
NR bool `json:"NR"`
@@ -365,13 +366,13 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
365
366
// s.Ma.LoadK((*klines)[0:])
366
367
//}
367
368
368
- s .rtNr = types .NewQueue (100 )
369
+ s .rtNr = types .NewQueue (s . Window )
369
370
370
371
s .rtMaFast = types .NewQueue (1 )
371
372
s .rtMaSlow = types .NewQueue (5 )
372
- s .rtMr = types .NewQueue (100 )
373
+ s .rtMr = types .NewQueue (s . Window )
373
374
374
- s .rtWeight = types .NewQueue (100 )
375
+ s .rtWeight = types .NewQueue (s . Window )
375
376
376
377
s .currentTradePrice = atomic .NewFloat64 (0 )
377
378
@@ -395,6 +396,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
395
396
select {
396
397
case <- intervalCloseTicker .C :
397
398
if s .currentTradePrice .Load () > 0 {
399
+ s .orderExecutor .CancelNoWait (context .Background ())
398
400
s .closePrice = s .currentTradePrice .Load ()
399
401
//log.Infof("Close Price: %f", s.closePrice)
400
402
// calculate real-time Negative Return
@@ -424,7 +426,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
424
426
s .rtWeight .Update (alpha )
425
427
log .Infof ("Alpha: %f/1.0" , s .rtWeight .Last ())
426
428
s .rebalancePosition (s .obBuyPrice .Load (), s .obSellPrice .Load (), s .rtWeight .Last ())
427
- s .orderExecutor .CancelNoWait (context .Background ())
428
429
}
429
430
case <- s .stopC :
430
431
log .Warnf ("%s goroutine stopped, due to the stop signal" , s .Symbol )
@@ -443,7 +444,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
443
444
for {
444
445
select {
445
446
case <- intervalOpenTicker .C :
446
- time .Sleep (200 * time .Microsecond )
447
+ time .Sleep (50 * time .Microsecond )
447
448
if s .currentTradePrice .Load () > 0 {
448
449
s .openPrice = s .currentTradePrice .Load ()
449
450
//log.Infof("Open Price: %f", s.openPrice)
@@ -487,44 +488,29 @@ func (s *Strategy) CalcAssetValue(price fixedpoint.Value) fixedpoint.Value {
487
488
}
488
489
489
490
func (s * Strategy ) rebalancePosition (bestBid , bestAsk float64 , w float64 ) {
490
- // alpha-weighted assets (inventory and capital)
491
- position := s .orderExecutor .Position ()
492
- p := fixedpoint .NewFromFloat ((bestBid + bestAsk ) / 2 )
493
-
494
- targetBase := s .QuantityOrAmount .CalculateQuantity (p ).Mul (fixedpoint .NewFromFloat (w ))
495
-
496
- // to buy/sell quantity
497
- diffQty := targetBase .Sub (position .Base )
498
- log .Infof ("Target Position Diff: %f" , diffQty .Float64 ())
499
-
500
- // ignore small changes
501
- if diffQty .Abs ().Float64 () < 0.0005 {
502
- return
503
- }
504
-
505
- if diffQty .Sign () > 0 {
506
- _ , err := s .orderExecutor .SubmitOrders (context .Background (), types.SubmitOrder {
491
+ if w < 0.5 {
492
+ _ , errB := s .orderExecutor .SubmitOrders (context .Background (), types.SubmitOrder {
507
493
Symbol : s .Symbol ,
508
494
Side : types .SideTypeBuy ,
509
- Quantity : diffQty . Abs () ,
510
- Type : types .OrderTypeLimit ,
511
- Price : fixedpoint .NewFromFloat (bestBid ),
512
- Tag : "irr re-balance : buy" ,
495
+ Quantity : s . Quantity ,
496
+ Type : types .OrderTypeLimitMaker ,
497
+ Price : fixedpoint .NewFromFloat (bestBid - s . Spread ),
498
+ Tag : "irr short : buy" ,
513
499
})
514
- if err != nil {
515
- log .WithError (err )
500
+ if errB != nil {
501
+ log .WithError (errB )
516
502
}
517
- } else if diffQty . Sign () < 0 {
518
- _ , err := s .orderExecutor .SubmitOrders (context .Background (), types.SubmitOrder {
503
+ } else if w > 0.5 {
504
+ _ , errA := s .orderExecutor .SubmitOrders (context .Background (), types.SubmitOrder {
519
505
Symbol : s .Symbol ,
520
506
Side : types .SideTypeSell ,
521
- Quantity : diffQty . Abs () ,
522
- Type : types .OrderTypeLimit ,
523
- Price : fixedpoint .NewFromFloat (bestAsk ),
524
- Tag : "irr re-balance: sell " ,
507
+ Quantity : s . Quantity ,
508
+ Type : types .OrderTypeLimitMaker ,
509
+ Price : fixedpoint .NewFromFloat (bestAsk + s . Spread ),
510
+ Tag : "irr long: buy " ,
525
511
})
526
- if err != nil {
527
- log .WithError (err )
512
+ if errA != nil {
513
+ log .WithError (errA )
528
514
}
529
515
}
530
516
}
0 commit comments