From cb4c8799425f3d9f88609ae99942cc872abfc8a3 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 6 Jun 2022 06:12:54 +0800 Subject: [PATCH] backtest: copy the order object for updating status --- .gitignore | 1 + pkg/backtest/matching.go | 18 +++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 34051dcda..1b3b385e6 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ otp*png /.deploy *.swp +/pkg/backtest/assets.go diff --git a/pkg/backtest/matching.go b/pkg/backtest/matching.go index 54bf9c86c..e31112245 100644 --- a/pkg/backtest/matching.go +++ b/pkg/backtest/matching.go @@ -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