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 /.deploy
*.swp *.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 { if o.Type == types.OrderTypeMarket {
m.EmitOrderUpdate(order) m.EmitOrderUpdate(order)
// copy the order object to avoid side effect (for different callbacks)
var order2 = order
// emit trade before we publish order // emit trade before we publish order
trade := m.newTradeFromOrder(&order, false) trade := m.newTradeFromOrder(&order2, false)
m.executeTrade(trade) m.executeTrade(trade)
// update the order status // update the order status
order.Status = types.OrderStatusFilled order2.Status = types.OrderStatusFilled
order.ExecutedQuantity = order.Quantity order2.ExecutedQuantity = order2.Quantity
order.Price = price order2.Price = price
order.IsWorking = false order2.IsWorking = false
m.EmitOrderUpdate(order) m.EmitOrderUpdate(order2)
return &order, &trade, nil
return &order2, &trade, nil
} }
// for limit maker orders // for limit maker orders