backtest: copy the order object for updating status

This commit is contained in:
c9s 2022-06-06 06:12:54 +08:00
parent f65b343ea6
commit cb4c879942
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 12 additions and 7 deletions

1
.gitignore vendored
View File

@ -43,3 +43,4 @@ otp*png
/.deploy
*.swp
/pkg/backtest/assets.go

View File

@ -148,17 +148,21 @@ func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (closedOrders *typ
if o.Type == types.OrderTypeMarket {
m.EmitOrderUpdate(order)
// copy the order object to avoid side effect (for different callbacks)
var order2 = order
// emit trade before we publish order
trade := m.newTradeFromOrder(&order, false)
trade := m.newTradeFromOrder(&order2, false)
m.executeTrade(trade)
// update the order status
order.Status = types.OrderStatusFilled
order.ExecutedQuantity = order.Quantity
order.Price = price
order.IsWorking = false
m.EmitOrderUpdate(order)
return &order, &trade, nil
order2.Status = types.OrderStatusFilled
order2.ExecutedQuantity = order2.Quantity
order2.Price = price
order2.IsWorking = false
m.EmitOrderUpdate(order2)
return &order2, &trade, nil
}
// for limit maker orders