Lean  $LEAN_TAG$
BrokerageExtensions.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 
18 {
19  /// <summary>
20  /// Provides extension methods for handling brokerage operations.
21  /// </summary>
22  public static class BrokerageExtensions
23  {
24  /// <summary>
25  /// Determines if executing the specified order will cross the zero holdings threshold.
26  /// </summary>
27  /// <param name="holdingQuantity">The current quantity of holdings.</param>
28  /// <param name="orderQuantity">The quantity of the order to be evaluated.</param>
29  /// <returns>
30  /// <c>true</c> if the order will change the holdings from positive to negative or vice versa; otherwise, <c>false</c>.
31  /// </returns>
32  /// <remarks>
33  /// This method checks if the order will result in a position change from positive to negative holdings or from negative to positive holdings.
34  /// </remarks>
35  public static bool OrderCrossesZero(decimal holdingQuantity, decimal orderQuantity)
36  {
37  //We're reducing position or flipping:
38  if (holdingQuantity > 0 && orderQuantity < 0)
39  {
40  if ((holdingQuantity + orderQuantity) < 0)
41  {
42  //We don't have enough holdings so will cross through zero:
43  return true;
44  }
45  }
46  else if (holdingQuantity < 0 && orderQuantity > 0)
47  {
48  if ((holdingQuantity + orderQuantity) > 0)
49  {
50  //Crossed zero: need to split into 2 orders:
51  return true;
52  }
53  }
54  return false;
55  }
56  }
57 }