backtest: add order quantity check

This commit is contained in:
c9s 2022-10-06 15:08:44 +08:00
parent c374a56110
commit a515fff053
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -48,6 +48,8 @@ import (
var log = logrus.WithField("cmd", "backtest")
var ErrUnimplemented = errors.New("unimplemented method")
var ErrNegativeQuantity = errors.New("order quantity can not be negative")
var ErrZeroQuantity = errors.New("order quantity can not be zero")
type Exchange struct {
sourceName types.ExchangeName
@ -176,6 +178,14 @@ func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (cr
return nil, fmt.Errorf("matching engine is not initialized for symbol %s", symbol)
}
if order.Quantity.Sign() < 0 {
return nil, ErrNegativeQuantity
}
if order.Quantity.IsZero() {
return nil, ErrZeroQuantity
}
createdOrder, _, err = matching.PlaceOrder(order)
if createdOrder != nil {
// market order can be closed immediately.