Skip to content

Commit

Permalink
Fix brokerage transaction handler null object access bug (QuantConnec…
Browse files Browse the repository at this point in the history
…t#7586)

* Fix using brokerage transaction handler algo befor set

Brokerage open orders are being set before algorithm is set to the transaction handler, so SetPriceAdjustmentMode needs to get the algo to use

* Update unit tests
  • Loading branch information
jhonabreul authored Nov 20, 2023
1 parent 6cc2bc6 commit a761c76
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions Engine/TransactionHandlers/BrokerageTransactionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ public void AddOpenOrder(Order order, IAlgorithm algorithm)

var orderTicket = order.ToOrderTicket(algorithm.Transactions);

SetPriceAdjustmentMode(order);
SetPriceAdjustmentMode(order, algorithm);

_openOrders.AddOrUpdate(order.Id, order, (i, o) => order);
_completeOrders.AddOrUpdate(order.Id, order, (i, o) => order);
Expand Down Expand Up @@ -796,7 +796,7 @@ private OrderResponse HandleSubmitOrderRequest(SubmitOrderRequest request)
order.OrderSubmissionData = new OrderSubmissionData(security.BidPrice, security.AskPrice, security.Close);

// Set order price adjustment mode
SetPriceAdjustmentMode(order);
SetPriceAdjustmentMode(order, _algorithm);

// update the ticket's internal storage with this new order reference
ticket.SetOrder(order);
Expand Down Expand Up @@ -1293,9 +1293,9 @@ private void HandleOrderUpdated(OrderUpdateEvent e)
/// <summary>
/// Gets the price adjustment mode for the specified symbol from its subscription configurations
/// </summary>
private void SetPriceAdjustmentMode(Order order)
private void SetPriceAdjustmentMode(Order order, IAlgorithm algorithm)
{
if (_algorithm.LiveMode)
if (algorithm.LiveMode)
{
// live trading always uses raw prices
order.PriceAdjustmentMode = DataNormalizationMode.Raw;
Expand All @@ -1304,7 +1304,7 @@ private void SetPriceAdjustmentMode(Order order)

if (!_priceAdjustmentModes.TryGetValue(order.Symbol, out var mode))
{
var configs = _algorithm.SubscriptionManager.SubscriptionDataConfigService
var configs = algorithm.SubscriptionManager.SubscriptionDataConfigService
.GetSubscriptionDataConfigs(order.Symbol, includeInternalConfigs: true);
if (configs.Count == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2067,9 +2067,9 @@ public void OrderPriceAdjustmentModeIsSetWhenAddingOpenOrder(DataNormalizationMo
{
_algorithm.SetLiveMode(liveMode);

//Initializes the transaction handler
// The engine might fetch brokerage open orders before even initializing the transaction handler,
// so let's not initialize it here to simulate that scenario
var transactionHandler = new TestBrokerageTransactionHandler();
transactionHandler.Initialize(_algorithm, new BacktestingBrokerage(_algorithm), new BacktestingResultHandler());

// Add the security
var security = _algorithm.AddSecurity(SecurityType.Forex, "CADUSD", dataNormalizationMode: dataNormalizationMode);
Expand Down

0 comments on commit a761c76

Please sign in to comment.