Merge pull request #480 from zenixls2/fix/flashcrash

fix: submit order on userDataStream == nil
This commit is contained in:
Yo-An Lin 2022-03-15 21:55:52 +08:00 committed by GitHub
commit 2aa3e4d51c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -151,6 +151,9 @@ func (e *Exchange) NewStream() types.Stream {
} }
func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error) { func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error) {
if e.userDataStream == nil {
return createdOrders, fmt.Errorf("SubmitOrders should be called after userDataStream been initialized")
}
for _, order := range orders { for _, order := range orders {
symbol := order.Symbol symbol := order.Symbol
matching, ok := e.matchingBook(symbol) matching, ok := e.matchingBook(symbol)
@ -198,6 +201,9 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since,
} }
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error { func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error {
if e.userDataStream == nil {
return fmt.Errorf("CancelOrders should be called after userDataStream been initialized")
}
for _, order := range orders { for _, order := range orders {
matching, ok := e.matchingBook(order.Symbol) matching, ok := e.matchingBook(order.Symbol)
if !ok { if !ok {

View File

@ -129,11 +129,13 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
Window: 25, Window: 25,
}) })
session.UserDataStream.OnStart(func() {
s.updateOrders(orderExecutor, session)
})
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) { session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
s.updateOrders(orderExecutor, session) s.updateOrders(orderExecutor, session)
}) })
// TODO: move this to the stream onConnect handler
s.updateOrders(orderExecutor, session)
return nil return nil
} }