Lean  $LEAN_TAG$
Brokerage.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Linq;
18 using Newtonsoft.Json;
19 using System.Threading;
20 using QuantConnect.Data;
21 using QuantConnect.Orders;
22 using QuantConnect.Logging;
23 using System.Threading.Tasks;
27 using System.Collections.Generic;
28 using System.Collections.Concurrent;
30 
32 {
33  /// <summary>
34  /// Represents the base Brokerage implementation. This provides logging on brokerage events.
35  /// </summary>
36  public abstract class Brokerage : IBrokerage
37  {
38  // 7:45 AM (New York time zone)
39  private static readonly TimeSpan LiveBrokerageCashSyncTime = new TimeSpan(7, 45, 0);
40 
41  private readonly object _performCashSyncReentranceGuard = new object();
42  private bool _syncedLiveBrokerageCashToday = true;
43  private long _lastSyncTimeTicks = DateTime.UtcNow.Ticks;
44 
45  /// <summary>
46  /// Event that fires each time the brokerage order id changes
47  /// </summary>
48  public event EventHandler<BrokerageOrderIdChangedEvent> OrderIdChanged;
49 
50  /// <summary>
51  /// Event that fires each time the status for a list of orders change
52  /// </summary>
53  public event EventHandler<List<OrderEvent>> OrdersStatusChanged;
54 
55  /// <summary>
56  /// Event that fires each time an order is updated in the brokerage side
57  /// </summary>
58  /// <remarks>
59  /// These are not status changes but mainly price changes, like the stop price of a trailing stop order
60  /// </remarks>
61  public event EventHandler<OrderUpdateEvent> OrderUpdated;
62 
63  /// <summary>
64  /// Event that fires each time a short option position is assigned
65  /// </summary>
66  public event EventHandler<OrderEvent> OptionPositionAssigned;
67 
68  /// <summary>
69  /// Event that fires each time an option position has changed
70  /// </summary>
71  public event EventHandler<OptionNotificationEventArgs> OptionNotification;
72 
73  /// <summary>
74  /// Event that fires each time there's a brokerage side generated order
75  /// </summary>
76  public event EventHandler<NewBrokerageOrderNotificationEventArgs> NewBrokerageOrderNotification;
77 
78  /// <summary>
79  /// Event that fires each time a delisting occurs
80  /// </summary>
81  public event EventHandler<DelistingNotificationEventArgs> DelistingNotification;
82 
83  /// <summary>
84  /// Event that fires each time a user's brokerage account is changed
85  /// </summary>
86  public event EventHandler<AccountEvent> AccountChanged;
87 
88  /// <summary>
89  /// Event that fires when an error is encountered in the brokerage
90  /// </summary>
91  public event EventHandler<BrokerageMessageEvent> Message;
92 
93  /// <summary>
94  /// Gets the name of the brokerage
95  /// </summary>
96  public string Name { get; }
97 
98  /// <summary>
99  /// Returns true if we're currently connected to the broker
100  /// </summary>
101  public abstract bool IsConnected { get; }
102 
103  /// <summary>
104  /// Creates a new Brokerage instance with the specified name
105  /// </summary>
106  /// <param name="name">The name of the brokerage</param>
107  protected Brokerage(string name)
108  {
109  Name = name;
110  }
111 
112  /// <summary>
113  /// Places a new order and assigns a new broker ID to the order
114  /// </summary>
115  /// <param name="order">The order to be placed</param>
116  /// <returns>True if the request for a new order has been placed, false otherwise</returns>
117  public abstract bool PlaceOrder(Order order);
118 
119  /// <summary>
120  /// Updates the order with the same id
121  /// </summary>
122  /// <param name="order">The new order information</param>
123  /// <returns>True if the request was made for the order to be updated, false otherwise</returns>
124  public abstract bool UpdateOrder(Order order);
125 
126  /// <summary>
127  /// Cancels the order with the specified ID
128  /// </summary>
129  /// <param name="order">The order to cancel</param>
130  /// <returns>True if the request was made for the order to be canceled, false otherwise</returns>
131  public abstract bool CancelOrder(Order order);
132 
133  /// <summary>
134  /// Connects the client to the broker's remote servers
135  /// </summary>
136  public abstract void Connect();
137 
138  /// <summary>
139  /// Disconnects the client from the broker's remote servers
140  /// </summary>
141  public abstract void Disconnect();
142 
143  /// <summary>
144  /// Dispose of the brokerage instance
145  /// </summary>
146  public virtual void Dispose()
147  {
148  // NOP
149  }
150 
151  /// <summary>
152  /// Event invocator for the OrderFilled event
153  /// </summary>
154  /// <param name="orderEvents">The list of order events</param>
155  protected virtual void OnOrderEvents(List<OrderEvent> orderEvents)
156  {
157  try
158  {
159  OrdersStatusChanged?.Invoke(this, orderEvents);
160  }
161  catch (Exception err)
162  {
163  Log.Error(err);
164  }
165  }
166 
167  /// <summary>
168  /// Event invocator for the OrderFilled event
169  /// </summary>
170  /// <param name="e">The order event</param>
171  protected virtual void OnOrderEvent(OrderEvent e)
172  {
173  OnOrderEvents(new List<OrderEvent> { e });
174  }
175 
176  /// <summary>
177  /// Event invocator for the OrderUpdated event
178  /// </summary>
179  /// <param name="e">The update event</param>
180  protected virtual void OnOrderUpdated(OrderUpdateEvent e)
181  {
182  try
183  {
184  OrderUpdated?.Invoke(this, e);
185  }
186  catch (Exception err)
187  {
188  Log.Error(err);
189  }
190  }
191 
192  /// <summary>
193  /// Event invocator for the OrderIdChanged event
194  /// </summary>
195  /// <param name="e">The BrokerageOrderIdChangedEvent</param>
197  {
198  try
199  {
200  OrderIdChanged?.Invoke(this, e);
201  }
202  catch (Exception err)
203  {
204  Log.Error(err);
205  }
206  }
207 
208  /// <summary>
209  /// Event invocator for the OptionPositionAssigned event
210  /// </summary>
211  /// <param name="e">The OrderEvent</param>
212  protected virtual void OnOptionPositionAssigned(OrderEvent e)
213  {
214  try
215  {
216  Log.Debug("Brokerage.OptionPositionAssigned(): " + e);
217 
218  OptionPositionAssigned?.Invoke(this, e);
219  }
220  catch (Exception err)
221  {
222  Log.Error(err);
223  }
224  }
225 
226  /// <summary>
227  /// Event invocator for the OptionNotification event
228  /// </summary>
229  /// <param name="e">The OptionNotification event arguments</param>
231  {
232  try
233  {
234  Log.Debug("Brokerage.OnOptionNotification(): " + e);
235 
236  OptionNotification?.Invoke(this, e);
237  }
238  catch (Exception err)
239  {
240  Log.Error(err);
241  }
242  }
243 
244  /// <summary>
245  /// Event invocator for the NewBrokerageOrderNotification event
246  /// </summary>
247  /// <param name="e">The NewBrokerageOrderNotification event arguments</param>
249  {
250  try
251  {
252  Log.Debug("Brokerage.OnNewBrokerageOrderNotification(): " + e);
253 
254  NewBrokerageOrderNotification?.Invoke(this, e);
255  }
256  catch (Exception err)
257  {
258  Log.Error(err);
259  }
260  }
261 
262  /// <summary>
263  /// Event invocator for the DelistingNotification event
264  /// </summary>
265  /// <param name="e">The DelistingNotification event arguments</param>
267  {
268  try
269  {
270  Log.Debug("Brokerage.OnDelistingNotification(): " + e);
271 
272  DelistingNotification?.Invoke(this, e);
273  }
274  catch (Exception err)
275  {
276  Log.Error(err);
277  }
278  }
279 
280  /// <summary>
281  /// Event invocator for the AccountChanged event
282  /// </summary>
283  /// <param name="e">The AccountEvent</param>
284  protected virtual void OnAccountChanged(AccountEvent e)
285  {
286  try
287  {
288  Log.Trace($"Brokerage.OnAccountChanged(): {e}");
289 
290  AccountChanged?.Invoke(this, e);
291  }
292  catch (Exception err)
293  {
294  Log.Error(err);
295  }
296  }
297 
298  /// <summary>
299  /// Event invocator for the Message event
300  /// </summary>
301  /// <param name="e">The error</param>
302  protected virtual void OnMessage(BrokerageMessageEvent e)
303  {
304  try
305  {
306  if (e.Type == BrokerageMessageType.Error)
307  {
308  Log.Error("Brokerage.OnMessage(): " + e);
309  }
310  else
311  {
312  Log.Trace("Brokerage.OnMessage(): " + e);
313  }
314 
315  Message?.Invoke(this, e);
316  }
317  catch (Exception err)
318  {
319  Log.Error(err);
320  }
321  }
322 
323  /// <summary>
324  /// Helper method that will try to get the live holdings from the provided brokerage data collection else will default to the algorithm state
325  /// </summary>
326  /// <remarks>Holdings will removed from the provided collection on the first call, since this method is expected to be called only
327  /// once on initialize, after which the algorithm should use Lean accounting</remarks>
328  protected virtual List<Holding> GetAccountHoldings(Dictionary<string, string> brokerageData, IEnumerable<Security> securities)
329  {
330  if (Log.DebuggingEnabled)
331  {
332  Log.Debug("Brokerage.GetAccountHoldings(): starting...");
333  }
334 
335  if (brokerageData != null && brokerageData.Remove("live-holdings", out var value) && !string.IsNullOrEmpty(value))
336  {
337  // remove the key, we really only want to return the cached value on the first request
338  var result = JsonConvert.DeserializeObject<List<Holding>>(value);
339  if (result == null)
340  {
341  return new List<Holding>();
342  }
343  Log.Trace($"Brokerage.GetAccountHoldings(): sourcing holdings from provided brokerage data, found {result.Count} entries");
344  return result;
345  }
346 
347  return securities?.Where(security => security.Holdings.AbsoluteQuantity > 0)
348  .OrderBy(security => security.Symbol)
349  .Select(security => new Holding(security)).ToList() ?? new List<Holding>();
350  }
351 
352  /// <summary>
353  /// Helper method that will try to get the live cash balance from the provided brokerage data collection else will default to the algorithm state
354  /// </summary>
355  /// <remarks>Cash balance will removed from the provided collection on the first call, since this method is expected to be called only
356  /// once on initialize, after which the algorithm should use Lean accounting</remarks>
357  protected virtual List<CashAmount> GetCashBalance(Dictionary<string, string> brokerageData, CashBook cashBook)
358  {
359  if (Log.DebuggingEnabled)
360  {
361  Log.Debug("Brokerage.GetCashBalance(): starting...");
362  }
363 
364  if (brokerageData != null && brokerageData.Remove("live-cash-balance", out var value) && !string.IsNullOrEmpty(value))
365  {
366  // remove the key, we really only want to return the cached value on the first request
367  var result = JsonConvert.DeserializeObject<List<CashAmount>>(value);
368  if (result == null)
369  {
370  return new List<CashAmount>();
371  }
372  Log.Trace($"Brokerage.GetCashBalance(): sourcing cash balance from provided brokerage data, found {result.Count} entries");
373  return result;
374  }
375 
376  return cashBook?.Select(x => new CashAmount(x.Value.Amount, x.Value.Symbol)).ToList() ?? new List<CashAmount>();
377  }
378 
379  /// <summary>
380  /// Gets all open orders on the account.
381  /// NOTE: The order objects returned do not have QC order IDs.
382  /// </summary>
383  /// <returns>The open orders returned from IB</returns>
384  public abstract List<Order> GetOpenOrders();
385 
386  /// <summary>
387  /// Gets all holdings for the account
388  /// </summary>
389  /// <returns>The current holdings from the account</returns>
390  public abstract List<Holding> GetAccountHoldings();
391 
392  /// <summary>
393  /// Gets the current cash balance for each currency held in the brokerage account
394  /// </summary>
395  /// <returns>The current cash balance for each currency available for trading</returns>
396  public abstract List<CashAmount> GetCashBalance();
397 
398  /// <summary>
399  /// Specifies whether the brokerage will instantly update account balances
400  /// </summary>
401  public virtual bool AccountInstantlyUpdated => false;
402 
403  /// <summary>
404  /// Returns the brokerage account's base currency
405  /// </summary>
406  public virtual string AccountBaseCurrency { get; protected set; }
407 
408  /// <summary>
409  /// Gets the history for the requested security
410  /// </summary>
411  /// <param name="request">The historical data request</param>
412  /// <returns>An enumerable of bars covering the span specified in the request</returns>
413  public virtual IEnumerable<BaseData> GetHistory(HistoryRequest request)
414  {
415  return Enumerable.Empty<BaseData>();
416  }
417 
418  /// <summary>
419  /// Gets the position that might result given the specified order direction and the current holdings quantity.
420  /// This is useful for brokerages that require more specific direction information than provided by the OrderDirection enum
421  /// (e.g. Tradier differentiates Buy/Sell and BuyToOpen/BuyToCover/SellShort/SellToClose)
422  /// </summary>
423  /// <param name="orderDirection">The order direction</param>
424  /// <param name="holdingsQuantity">The current holdings quantity</param>
425  /// <returns>The order position</returns>
426  protected static OrderPosition GetOrderPosition(OrderDirection orderDirection, decimal holdingsQuantity)
427  {
428  return orderDirection switch
429  {
430  OrderDirection.Buy => holdingsQuantity >= 0 ? OrderPosition.BuyToOpen : OrderPosition.BuyToClose,
431  OrderDirection.Sell => holdingsQuantity <= 0 ? OrderPosition.SellToOpen : OrderPosition.SellToClose,
432  _ => throw new ArgumentOutOfRangeException(nameof(orderDirection), orderDirection, "Invalid order direction")
433  };
434  }
435 
436  #region IBrokerageCashSynchronizer implementation
437 
438  /// <summary>
439  /// Gets the date of the last sync (New York time zone)
440  /// </summary>
441  protected DateTime LastSyncDate => LastSyncDateTimeUtc.ConvertFromUtc(TimeZones.NewYork).Date;
442 
443  /// <summary>
444  /// Gets the datetime of the last sync (UTC)
445  /// </summary>
446  public DateTime LastSyncDateTimeUtc => new DateTime(Interlocked.Read(ref _lastSyncTimeTicks));
447 
448  /// <summary>
449  /// Returns whether the brokerage should perform the cash synchronization
450  /// </summary>
451  /// <param name="currentTimeUtc">The current time (UTC)</param>
452  /// <returns>True if the cash sync should be performed</returns>
453  public virtual bool ShouldPerformCashSync(DateTime currentTimeUtc)
454  {
455  // every morning flip this switch back
456  var currentTimeNewYork = currentTimeUtc.ConvertFromUtc(TimeZones.NewYork);
457  if (_syncedLiveBrokerageCashToday && currentTimeNewYork.Date != LastSyncDate)
458  {
459  _syncedLiveBrokerageCashToday = false;
460  }
461 
462  return !_syncedLiveBrokerageCashToday && currentTimeNewYork.TimeOfDay >= LiveBrokerageCashSyncTime;
463  }
464 
465  /// <summary>
466  /// Synchronizes the cashbook with the brokerage account
467  /// </summary>
468  /// <param name="algorithm">The algorithm instance</param>
469  /// <param name="currentTimeUtc">The current time (UTC)</param>
470  /// <param name="getTimeSinceLastFill">A function which returns the time elapsed since the last fill</param>
471  /// <returns>True if the cash sync was performed successfully</returns>
472  public virtual bool PerformCashSync(IAlgorithm algorithm, DateTime currentTimeUtc, Func<TimeSpan> getTimeSinceLastFill)
473  {
474  try
475  {
476  // prevent reentrance in this method
477  if (!Monitor.TryEnter(_performCashSyncReentranceGuard))
478  {
479  Log.Trace("Brokerage.PerformCashSync(): Reentrant call, cash sync not performed");
480  return false;
481  }
482 
483  Log.Trace("Brokerage.PerformCashSync(): Sync cash balance");
484 
485  List<CashAmount> balances = null;
486  try
487  {
488  balances = GetCashBalance();
489  }
490  catch (Exception err)
491  {
492  Log.Error(err, "Error in GetCashBalance:");
493  }
494 
495  // empty cash balance is valid, if there was No error/exception
496  if (balances == null)
497  {
498  Log.Trace("Brokerage.PerformCashSync(): No cash balances available, cash sync not performed");
499  return false;
500  }
501 
502  // Adds currency to the cashbook that the user might have deposited
503  foreach (var balance in balances)
504  {
505  if (!algorithm.Portfolio.CashBook.ContainsKey(balance.Currency))
506  {
507  Log.Trace($"Brokerage.PerformCashSync(): Unexpected cash found {balance.Currency} {balance.Amount}", true);
508  algorithm.Portfolio.SetCash(balance.Currency, balance.Amount, 0);
509  }
510  }
511 
512  var totalPorfolioValueThreshold = algorithm.Portfolio.TotalPortfolioValue * 0.02m;
513  // if we were returned our balances, update everything and flip our flag as having performed sync today
514  foreach (var kvp in algorithm.Portfolio.CashBook)
515  {
516  var cash = kvp.Value;
517 
518  //update the cash if the entry if found in the balances
519  var balanceCash = balances.Find(balance => balance.Currency == cash.Symbol);
520  if (balanceCash != default(CashAmount))
521  {
522  // compare in account currency
523  var delta = cash.Amount - balanceCash.Amount;
524  if (Math.Abs(algorithm.Portfolio.CashBook.ConvertToAccountCurrency(delta, cash.Symbol)) > totalPorfolioValueThreshold)
525  {
526  // log the delta between
527  Log.Trace($"Brokerage.PerformCashSync(): {balanceCash.Currency} Delta: {delta:0.00}", true);
528  }
529  algorithm.Portfolio.CashBook[cash.Symbol].SetAmount(balanceCash.Amount);
530  }
531  else
532  {
533  //Set the cash amount to zero if cash entry not found in the balances
534  Log.Trace($"Brokerage.PerformCashSync(): {cash.Symbol} was not found in brokerage cash balance, setting the amount to 0", true);
535  algorithm.Portfolio.CashBook[cash.Symbol].SetAmount(0);
536  }
537  }
538  _syncedLiveBrokerageCashToday = true;
539  _lastSyncTimeTicks = currentTimeUtc.Ticks;
540  }
541  finally
542  {
543  Monitor.Exit(_performCashSyncReentranceGuard);
544  }
545 
546  // fire off this task to check if we've had recent fills, if we have then we'll invalidate the cash sync
547  // and do it again until we're confident in it
548  Task.Delay(TimeSpan.FromSeconds(10)).ContinueWith(_ =>
549  {
550  // we want to make sure this is a good value, so check for any recent fills
551  if (getTimeSinceLastFill() <= TimeSpan.FromSeconds(20))
552  {
553  // this will cause us to come back in and reset cash again until we
554  // haven't processed a fill for +- 10 seconds of the set cash time
555  _syncedLiveBrokerageCashToday = false;
556  //_failedCashSyncAttempts = 0;
557  Log.Trace("Brokerage.PerformCashSync(): Unverified cash sync - resync required.");
558  }
559  else
560  {
561  Log.Trace("Brokerage.PerformCashSync(): Verified cash sync.");
562 
563  algorithm.Portfolio.LogMarginInformation();
564  }
565  });
566 
567  return true;
568  }
569 
570  #endregion
571 
572  #region CrossZeroOrder implementation
573 
574  /// <summary>
575  /// A dictionary to store the relationship between brokerage crossing orders and Lean orer id.
576  /// </summary>
577  private readonly ConcurrentDictionary<int, CrossZeroSecondOrderRequest> _leanOrderByBrokerageCrossingOrders = new();
578 
579  /// <summary>
580  /// An object used to lock the critical section in the <see cref="TryGetOrRemoveCrossZeroOrder"/> method,
581  /// ensuring thread safety when accessing the order collection.
582  /// </summary>
583  private object _lockCrossZeroObject = new();
584 
585  /// <summary>
586  /// A thread-safe dictionary that maps brokerage order IDs to their corresponding Order objects.
587  /// </summary>
588  /// <remarks>
589  /// This ConcurrentDictionary is used to maintain a mapping between Zero Cross brokerage order IDs and Lean Order objects.
590  /// The dictionary is protected and read-only, ensuring that it can only be modified by the class that declares it and cannot
591  /// be assigned a new instance after initialization.
592  /// </remarks>
593  protected ConcurrentDictionary<string, Order> LeanOrderByZeroCrossBrokerageOrderId { get; } = new();
594 
595  /// <summary>
596  /// Places an order that crosses zero (transitions from a short position to a long position or vice versa) and returns the response.
597  /// This method should be overridden in a derived class to implement brokerage-specific logic for placing such orders.
598  /// </summary>
599  /// <param name="crossZeroOrderRequest">The request object containing details of the cross zero order to be placed.</param>
600  /// <param name="isPlaceOrderWithLeanEvent">
601  /// A boolean indicating whether the order should be placed with triggering a Lean event.
602  /// Default is <c>true</c>, meaning Lean events will be triggered.
603  /// </param>
604  /// <returns>
605  /// A <see cref="CrossZeroOrderResponse"/> object indicating the result of the order placement.
606  /// </returns>
607  /// <exception cref="NotImplementedException">
608  /// Thrown if the method is not overridden in a derived class.
609  /// </exception>
610  protected virtual CrossZeroOrderResponse PlaceCrossZeroOrder(CrossZeroFirstOrderRequest crossZeroOrderRequest, bool isPlaceOrderWithLeanEvent = true)
611  {
612  throw new NotImplementedException($"{nameof(PlaceCrossZeroOrder)} method should be overridden in the derived class to handle brokerage-specific logic.");
613  }
614 
615  /// <summary>
616  /// Attempts to place an order that may cross the zero position.
617  /// If the order needs to be split into two parts due to crossing zero,
618  /// this method handles the split and placement accordingly.
619  /// </summary>
620  /// <param name="order">The order to be placed. Must not be <c>null</c>.</param>
621  /// <param name="holdingQuantity">The current holding quantity of the order's symbol.</param>
622  /// <returns>
623  /// <para><c>true</c> if the order crosses zero and the first part was successfully placed;</para>
624  /// <para><c>false</c> if the first part of the order could not be placed;</para>
625  /// <para><c>null</c> if the order does not cross zero.</para>
626  /// </returns>
627  /// <exception cref="ArgumentNullException">
628  /// Thrown if <paramref name="order"/> is <c>null</c>.
629  /// </exception>
630  protected bool? TryCrossZeroPositionOrder(Order order, decimal holdingQuantity)
631  {
632  if (order == null)
633  {
634  throw new ArgumentNullException(nameof(order), "The order parameter cannot be null.");
635  }
636 
637  // do we need to split the order into two pieces?
638  var crossesZero = BrokerageExtensions.OrderCrossesZero(holdingQuantity, order.Quantity);
639  if (crossesZero)
640  {
641  // first we need an order to close out the current position
642  var (firstOrderQuantity, secondOrderQuantity) = GetQuantityOnCrossPosition(holdingQuantity, order.Quantity);
643 
644  // Note: original quantity - already sell
645  var firstOrderPartRequest = new CrossZeroFirstOrderRequest(order, order.Type, firstOrderQuantity, holdingQuantity,
646  GetOrderPosition(order.Direction, holdingQuantity));
647 
648  // we actually can't place this order until the closingOrder is filled
649  // create another order for the rest, but we'll convert the order type to not be a stop
650  // but a market or a limit order
651  var secondOrderPartRequest = new CrossZeroSecondOrderRequest(order, order.Type, secondOrderQuantity, 0m,
652  GetOrderPosition(order.Direction, 0m), firstOrderPartRequest);
653 
654  _leanOrderByBrokerageCrossingOrders.AddOrUpdate(order.Id, secondOrderPartRequest);
655 
656  CrossZeroOrderResponse response;
657  lock (_lockCrossZeroObject)
658  {
659  // issue the first order to close the position
660  response = PlaceCrossZeroOrder(firstOrderPartRequest);
661  if (response.IsOrderPlacedSuccessfully)
662  {
663  var orderId = response.BrokerageOrderId;
664  if (!order.BrokerId.Contains(orderId))
665  {
666  order.BrokerId.Add(orderId);
667  }
668  }
669  }
670 
671  if (!response.IsOrderPlacedSuccessfully)
672  {
673  OnOrderEvent(new OrderEvent(order, DateTime.UtcNow, OrderFee.Zero, $"{nameof(Brokerage)}: {response.Message}")
674  {
675  Status = OrderStatus.Invalid
676  });
677  // remove the contingent order if we weren't successful in placing the first
678  //ContingentOrderQueue contingent;
679  _leanOrderByBrokerageCrossingOrders.TryRemove(order.Id, out _);
680  return false;
681  }
682  return true;
683  }
684 
685  return null;
686  }
687 
688  /// <summary>
689  /// Determines whether the given Lean order crosses zero quantity based on the initial order quantity.
690  /// </summary>
691  /// <param name="leanOrder">The Lean order to check.</param>
692  /// <param name="quantity">The quantity to be updated based on whether the order crosses zero.</param>
693  /// <returns>
694  /// <c>true</c> if the Lean order does not cross zero quantity; otherwise, <c>false</c>.
695  /// </returns>
696  /// <exception cref="ArgumentNullException">Thrown when the <paramref name="leanOrder"/> is null.</exception>
697  protected bool TryGetUpdateCrossZeroOrderQuantity(Order leanOrder, out decimal quantity)
698  {
699  if (leanOrder == null)
700  {
701  throw new ArgumentNullException(nameof(leanOrder), "The provided leanOrder cannot be null.");
702  }
703 
704  // Check if the order is a CrossZeroOrder.
705  if (_leanOrderByBrokerageCrossingOrders.TryGetValue(leanOrder.Id, out var crossZeroOrderRequest))
706  {
707  // If it is a CrossZeroOrder, use the first part of the quantity for the update.
708  quantity = crossZeroOrderRequest.FirstPartCrossZeroOrder.OrderQuantity;
709  // If the quantities of the LeanOrder do not match, return false. Don't support.
710  if (crossZeroOrderRequest.LeanOrder.Quantity != leanOrder.Quantity)
711  {
712  return false;
713  }
714  }
715  else
716  {
717  // If it is not a CrossZeroOrder, use the original order quantity.
718  quantity = leanOrder.Quantity;
719  }
720  return true;
721  }
722 
723  /// <summary>
724  /// Attempts to retrieve or remove a cross-zero order based on the brokerage order ID and its filled status.
725  /// </summary>
726  /// <param name="brokerageOrderId">The unique identifier of the brokerage order.</param>
727  /// <param name="leanOrderStatus">The updated status of the order received from the brokerage</param>
728  /// <param name="leanOrder">
729  /// When this method returns, contains the <see cref="Order"/> object associated with the given brokerage order ID,
730  /// if the operation was successful; otherwise, null.
731  /// This parameter is passed uninitialized.
732  /// </param>
733  /// <returns>
734  /// <c>true</c> if the method successfully retrieves or removes the order; otherwise, <c>false</c>.
735  /// </returns>
736  /// <remarks>
737  /// The method locks on a private object to ensure thread safety while accessing the collection of orders.
738  /// If the order is filled, it is removed from the collection. If the order is partially filled,
739  /// it is retrieved but not removed. If the order is not found, the method returns <c>false</c>.
740  /// </remarks>
741  protected bool TryGetOrRemoveCrossZeroOrder(string brokerageOrderId, OrderStatus leanOrderStatus, out Order leanOrder)
742  {
743  lock (_lockCrossZeroObject)
744  {
745  if (LeanOrderByZeroCrossBrokerageOrderId.TryGetValue(brokerageOrderId, out leanOrder))
746  {
747  switch (leanOrderStatus)
748  {
749  case OrderStatus.Filled:
750  case OrderStatus.Canceled:
751  case OrderStatus.Invalid:
752  LeanOrderByZeroCrossBrokerageOrderId.TryRemove(brokerageOrderId, out var _);
753  break;
754  };
755  return true;
756  }
757  // Return false if the brokerage order ID does not correspond to a cross-zero order
758  return false;
759  }
760  }
761 
762  /// <summary>
763  /// Attempts to handle any remaining orders that cross the zero boundary.
764  /// </summary>
765  /// <param name="leanOrder">The order object that needs to be processed.</param>
766  /// <param name="orderEvent">The event object containing order event details.</param>
767  protected bool TryHandleRemainingCrossZeroOrder(Order leanOrder, OrderEvent orderEvent)
768  {
769  if (leanOrder != null && orderEvent != null && _leanOrderByBrokerageCrossingOrders.TryGetValue(leanOrder.Id, out var brokerageOrder))
770  {
771  switch (orderEvent.Status)
772  {
773  case OrderStatus.Filled:
774  // if we have a contingent that needs to be submitted then we can't respect the 'Filled' state from the order
775  // because the Lean order hasn't been technically filled yet, so mark it as 'PartiallyFilled'
776  orderEvent.Status = OrderStatus.PartiallyFilled;
777  _leanOrderByBrokerageCrossingOrders.Remove(leanOrder.Id, out var _);
778  break;
779  case OrderStatus.Canceled:
780  case OrderStatus.Invalid:
781  _leanOrderByBrokerageCrossingOrders.Remove(leanOrder.Id, out var _);
782  return false;
783  default:
784  return false;
785  };
786 
787  OnOrderEvent(orderEvent);
788 
789  Task.Run(() =>
790  {
791 #pragma warning disable CA1031 // Do not catch general exception types
792  try
793  {
794  var response = default(CrossZeroOrderResponse);
795  lock (_lockCrossZeroObject)
796  {
797  Log.Trace($"{nameof(Brokerage)}.{nameof(TryHandleRemainingCrossZeroOrder)}: Submit the second part of cross order by Id:{leanOrder.Id}");
798  response = PlaceCrossZeroOrder(brokerageOrder, false);
799 
800  if (response.IsOrderPlacedSuccessfully)
801  {
802  // add the new brokerage id for retrieval later
803  var orderId = response.BrokerageOrderId;
804  if (!leanOrder.BrokerId.Contains(orderId))
805  {
806  leanOrder.BrokerId.Add(orderId);
807  }
808 
809  // leanOrder is a clone, here we can add the new brokerage order Id for the second part of the cross zero
810  OnOrderIdChangedEvent(new BrokerageOrderIdChangedEvent { OrderId = leanOrder.Id, BrokerId = leanOrder.BrokerId });
811  LeanOrderByZeroCrossBrokerageOrderId.AddOrUpdate(orderId, leanOrder);
812  }
813  }
814 
815  if (!response.IsOrderPlacedSuccessfully)
816  {
817  // if we failed to place this order I don't know what to do, we've filled the first part
818  // and failed to place the second... strange. Should we invalidate the rest of the order??
819  Log.Error($"{nameof(Brokerage)}.{nameof(TryHandleRemainingCrossZeroOrder)}: Failed to submit contingent order.");
820  var message = $"{leanOrder.Symbol} Failed submitting the second part of cross order for " +
821  $"LeanOrderId: {leanOrder.Id.ToStringInvariant()} Filled - BrokerageOrderId: {response.BrokerageOrderId}. " +
822  $"{response.Message}";
823  OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Warning, "CrossZeroFailed", message));
824  OnOrderEvent(new OrderEvent(leanOrder, DateTime.UtcNow, OrderFee.Zero) { Status = OrderStatus.Canceled });
825  }
826  }
827  catch (Exception err)
828  {
829  Log.Error(err);
830  OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Warning, "CrossZeroOrderError", "Error occurred submitting cross zero order: " + err.Message));
831  OnOrderEvent(new OrderEvent(leanOrder, DateTime.UtcNow, OrderFee.Zero) { Status = OrderStatus.Canceled });
832  }
833 #pragma warning restore CA1031 // Do not catch general exception types
834  });
835  return true;
836  }
837  return false;
838  }
839 
840  /// <summary>
841  /// Calculates the quantities needed to close the current position and establish a new position based on the provided order.
842  /// </summary>
843  /// <param name="holdingQuantity">The quantity currently held in the position that needs to be closed.</param>
844  /// <param name="orderQuantity">The quantity defined in the new order to be established.</param>
845  /// <returns>
846  /// A tuple containing:
847  /// <list type="bullet">
848  /// <item>
849  /// <description>The quantity needed to close the current position (negative value).</description>
850  /// </item>
851  /// <item>
852  /// <description>The quantity needed to establish the new position.</description>
853  /// </item>
854  /// </list>
855  /// </returns>
856  private static (decimal closePostionQunatity, decimal newPositionQuantity) GetQuantityOnCrossPosition(decimal holdingQuantity, decimal orderQuantity)
857  {
858  // first we need an order to close out the current position
859  var firstOrderQuantity = -holdingQuantity;
860  var secondOrderQuantity = orderQuantity - firstOrderQuantity;
861 
862  return (firstOrderQuantity, secondOrderQuantity);
863  }
864 
865  #endregion
866  }
867 }