backtest: should panic if last price is zero

This commit is contained in:
c9s 2022-01-30 02:41:00 +08:00
parent 6566db1624
commit e595b9acb2

View File

@ -109,10 +109,15 @@ func (m *SimplePriceMatching) CancelOrder(o types.Order) (types.Order, error) {
} }
func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (closedOrders *types.Order, trades *types.Trade, err error) { func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (closedOrders *types.Order, trades *types.Trade, err error) {
// price for checking account balance // price for checking account balance, default price
price := o.Price price := o.Price
switch o.Type { switch o.Type {
case types.OrderTypeMarket: case types.OrderTypeMarket:
if m.LastPrice == 0 {
panic("unexpected: last price can not be zero")
}
price = m.LastPrice.Float64() price = m.LastPrice.Float64()
case types.OrderTypeLimit, types.OrderTypeLimitMaker: case types.OrderTypeLimit, types.OrderTypeLimitMaker:
price = o.Price price = o.Price
@ -156,12 +161,13 @@ func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (closedOrders *typ
order.Status = types.OrderStatusFilled order.Status = types.OrderStatusFilled
order.ExecutedQuantity = order.Quantity order.ExecutedQuantity = order.Quantity
order.Price = price order.Price = price
order.IsWorking = false
m.EmitOrderUpdate(order) m.EmitOrderUpdate(order)
m.EmitBalanceUpdate(m.Account.Balances())
return &order, &trade, nil return &order, &trade, nil
} }
// for limit maker orders // for limit maker orders
// TODO: handle limit taker order
switch o.Side { switch o.Side {
case types.SideTypeBuy: case types.SideTypeBuy:
@ -219,6 +225,10 @@ func (m *SimplePriceMatching) newTradeFromOrder(order types.Order, isMaker bool)
price := order.Price price := order.Price
switch order.Type { switch order.Type {
case types.OrderTypeMarket, types.OrderTypeStopMarket: case types.OrderTypeMarket, types.OrderTypeStopMarket:
if m.LastPrice == 0 {
panic("unexpected: last price can not be zero")
}
price = m.LastPrice.Float64() price = m.LastPrice.Float64()
} }
@ -238,7 +248,6 @@ func (m *SimplePriceMatching) newTradeFromOrder(order types.Order, isMaker bool)
} }
var id = incTradeID() var id = incTradeID()
return types.Trade{ return types.Trade{
ID: id, ID: id,