mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
Merge pull request #743 from c9s/improve/notify-api
strategy/bollmaker: refactor and clean up
This commit is contained in:
commit
d6daac1df6
|
@ -108,6 +108,7 @@ func (e *GeneralOrderExecutor) GracefulCancel(ctx context.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
e.tradeCollector.Process()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,3 +120,8 @@ func (e *GeneralOrderExecutor) ClosePosition(ctx context.Context, percentage fix
|
||||||
|
|
||||||
return e.SubmitOrders(ctx, *submitOrder)
|
return e.SubmitOrders(ctx, *submitOrder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (e *GeneralOrderExecutor) TradeCollector() *TradeCollector {
|
||||||
|
return e.tradeCollector
|
||||||
|
}
|
||||||
|
|
|
@ -25,7 +25,6 @@ const ID = "bollmaker"
|
||||||
|
|
||||||
const stateKey = "state-v1"
|
const stateKey = "state-v1"
|
||||||
|
|
||||||
var defaultFeeRate = fixedpoint.NewFromFloat(0.001)
|
|
||||||
var notionModifier = fixedpoint.NewFromFloat(1.1)
|
var notionModifier = fixedpoint.NewFromFloat(1.1)
|
||||||
var two = fixedpoint.NewFromInt(2)
|
var two = fixedpoint.NewFromInt(2)
|
||||||
|
|
||||||
|
@ -155,9 +154,7 @@ type Strategy struct {
|
||||||
Position *types.Position `json:"position,omitempty" persistence:"position"`
|
Position *types.Position `json:"position,omitempty" persistence:"position"`
|
||||||
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
|
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
|
||||||
|
|
||||||
activeMakerOrders *bbgo.ActiveOrderBook
|
orderExecutor *bbgo.GeneralOrderExecutor
|
||||||
orderStore *bbgo.OrderStore
|
|
||||||
tradeCollector *bbgo.TradeCollector
|
|
||||||
|
|
||||||
groupID uint32
|
groupID uint32
|
||||||
|
|
||||||
|
@ -244,13 +241,7 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu
|
||||||
|
|
||||||
bbgo.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder)
|
bbgo.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder)
|
||||||
|
|
||||||
createdOrders, err := s.session.Exchange.SubmitOrders(ctx, submitOrder)
|
err := s.orderExecutor.SubmitOrders(ctx, submitOrder)
|
||||||
if err != nil {
|
|
||||||
log.WithError(err).Errorf("can not place position close order")
|
|
||||||
}
|
|
||||||
|
|
||||||
s.orderStore.Add(createdOrders...)
|
|
||||||
s.activeMakerOrders.Add(createdOrders...)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -479,12 +470,7 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec
|
||||||
submitOrders[i] = adjustOrderQuantity(submitOrders[i], s.Market)
|
submitOrders[i] = adjustOrderQuantity(submitOrders[i], s.Market)
|
||||||
}
|
}
|
||||||
|
|
||||||
createdOrders, err := orderExecutor.SubmitOrders(ctx, submitOrders...)
|
_ = s.orderExecutor.SubmitOrders(ctx, submitOrders...)
|
||||||
if err != nil {
|
|
||||||
log.WithError(err).Errorf("can not place ping pong orders")
|
|
||||||
}
|
|
||||||
s.orderStore.Add(createdOrders...)
|
|
||||||
s.activeMakerOrders.Add(createdOrders...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) hasLongSet() bool {
|
func (s *Strategy) hasLongSet() bool {
|
||||||
|
@ -507,17 +493,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
|
|
||||||
s.OnSuspend(func() {
|
s.OnSuspend(func() {
|
||||||
s.Status = types.StrategyStatusStopped
|
s.Status = types.StrategyStatusStopped
|
||||||
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
||||||
// Cancel all order
|
|
||||||
if err := s.activeMakerOrders.GracefulCancel(ctx, s.session.Exchange); err != nil {
|
|
||||||
log.WithError(err).Errorf("graceful cancel order error")
|
|
||||||
bbgo.Notify("graceful cancel order error")
|
|
||||||
} else {
|
|
||||||
bbgo.Notify("All orders are cancelled.")
|
|
||||||
}
|
|
||||||
|
|
||||||
s.tradeCollector.Process()
|
|
||||||
|
|
||||||
_ = s.Persistence.Sync(s)
|
_ = s.Persistence.Sync(s)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -593,48 +569,21 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
s.Position.Strategy = ID
|
s.Position.Strategy = ID
|
||||||
s.Position.StrategyInstanceID = instanceID
|
s.Position.StrategyInstanceID = instanceID
|
||||||
|
|
||||||
|
s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position)
|
||||||
|
s.orderExecutor.BindEnvironment(s.Environment)
|
||||||
|
s.orderExecutor.BindProfitStats(s.ProfitStats)
|
||||||
|
s.orderExecutor.Bind()
|
||||||
|
|
||||||
s.stopC = make(chan struct{})
|
s.stopC = make(chan struct{})
|
||||||
|
|
||||||
s.activeMakerOrders = bbgo.NewActiveOrderBook(s.Symbol)
|
// TODO: migrate persistance to singleton
|
||||||
s.activeMakerOrders.BindStream(session.UserDataStream)
|
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
||||||
|
|
||||||
s.orderStore = bbgo.NewOrderStore(s.Symbol)
|
|
||||||
s.orderStore.BindStream(session.UserDataStream)
|
|
||||||
|
|
||||||
s.tradeCollector = bbgo.NewTradeCollector(s.Symbol, s.Position, s.orderStore)
|
|
||||||
|
|
||||||
s.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) {
|
|
||||||
bbgo.Notify(trade)
|
|
||||||
s.ProfitStats.AddTrade(trade)
|
|
||||||
|
|
||||||
if profit.Compare(fixedpoint.Zero) == 0 {
|
|
||||||
s.Environment.RecordPosition(s.Position, trade, nil)
|
|
||||||
} else {
|
|
||||||
log.Infof("%s generated profit: %v", s.Symbol, profit)
|
|
||||||
p := s.Position.NewProfit(trade, profit, netProfit)
|
|
||||||
p.Strategy = ID
|
|
||||||
p.StrategyInstanceID = instanceID
|
|
||||||
bbgo.Notify(&p)
|
|
||||||
|
|
||||||
s.ProfitStats.AddProfit(p)
|
|
||||||
bbgo.Notify(s.ProfitStats)
|
|
||||||
|
|
||||||
s.Environment.RecordPosition(s.Position, trade, &p)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
|
|
||||||
log.Infof("position changed: %s", s.Position)
|
|
||||||
bbgo.Notify(s.Position)
|
|
||||||
|
|
||||||
if err := s.Persistence.Sync(s); err != nil {
|
if err := s.Persistence.Sync(s); err != nil {
|
||||||
log.WithError(err).Errorf("can not sync state to persistence")
|
log.WithError(err).Errorf("can not sync state to persistence")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
s.tradeCollector.BindStream(session.UserDataStream)
|
s.SmartStops.RunStopControllers(ctx, session, s.orderExecutor.TradeCollector())
|
||||||
|
|
||||||
s.SmartStops.RunStopControllers(ctx, session, s.tradeCollector)
|
|
||||||
|
|
||||||
if s.Environment.IsBackTesting() {
|
if s.Environment.IsBackTesting() {
|
||||||
log.Warn("turning of useTickerPrice option in the back-testing environment...")
|
log.Warn("turning of useTickerPrice option in the back-testing environment...")
|
||||||
|
@ -682,12 +631,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.activeMakerOrders.GracefulCancel(ctx, s.session.Exchange); err != nil {
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
||||||
log.WithError(err).Errorf("graceful cancel order error")
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if there is a canceled order had partially filled.
|
|
||||||
s.tradeCollector.Process()
|
|
||||||
|
|
||||||
if s.UseTickerPrice {
|
if s.UseTickerPrice {
|
||||||
ticker, err := s.session.Exchange.QueryTicker(ctx, s.Symbol)
|
ticker, err := s.session.Exchange.QueryTicker(ctx, s.Symbol)
|
||||||
|
@ -710,11 +654,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
close(s.stopC)
|
close(s.stopC)
|
||||||
|
|
||||||
if err := s.activeMakerOrders.GracefulCancel(ctx, s.session.Exchange); err != nil {
|
_ = s.orderExecutor.GracefulCancel(ctx)
|
||||||
log.WithError(err).Errorf("graceful cancel order error")
|
|
||||||
}
|
|
||||||
|
|
||||||
s.tradeCollector.Process()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue
Block a user