Lean  $LEAN_TAG$
PositionCollection.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 System.Collections;
19 using System.Collections.Generic;
20 
22 {
23  /// <summary>
24  /// Provides a collection type for <see cref="IPosition"/> aimed at providing indexing for
25  /// common operations required by the resolver implementations.
26  /// </summary>
27  public class PositionCollection : IEnumerable<IPosition>
28  {
29  private Dictionary<Symbol, IPosition> _positions;
30 
31  /// <summary>Gets the number of elements in the collection.</summary>
32  /// <returns>The number of elements in the collection. </returns>
33  public int Count => _positions.Count;
34 
35  /// <summary>
36  /// Initializes a new instance of the <see cref="PositionCollection"/> class
37  /// </summary>
38  /// <param name="positions">The positions to include in this collection</param>
39  public PositionCollection(Dictionary<Symbol, IPosition> positions)
40  {
41  _positions = positions;
42  }
43 
44  /// <summary>
45  /// Initializes a new instance of the <see cref="PositionCollection"/> class
46  /// </summary>
47  /// <param name="positions">The positions to include in this collection</param>
48  public PositionCollection(IEnumerable<IPosition> positions)
49  : this(positions.ToDictionary(p => p.Symbol))
50  {
51  }
52 
53  /// <summary>
54  /// Removes the quantities in the provided groups from this position collection.
55  /// This should be called following <see cref="IPositionGroupResolver"/> has resolved
56  /// position groups in order to update the collection of positions for the next resolver,
57  /// if one exists.
58  /// </summary>
59  /// <param name="groups">The resolved position groups</param>
60  /// <returns></returns>
61  public void Remove(IEnumerable<IPositionGroup> groups)
62  {
63  foreach (var group in groups)
64  {
65  foreach (var position in group.Positions)
66  {
67  IPosition existing;
68  if (!_positions.TryGetValue(position.Symbol, out existing))
69  {
70  throw new InvalidOperationException($"Position with symbol {position.Symbol} not found.");
71  }
72 
73  var resultingPosition = existing.Deduct(position.Quantity);
74  // directly remove positions hows quantity is 0
75  if(resultingPosition.Quantity == 0)
76  {
77  _positions.Remove(position.Symbol);
78  }
79  else
80  {
81  _positions[position.Symbol] = resultingPosition;
82  }
83  }
84  }
85  }
86 
87  /// <summary>
88  /// Clears this collection of all positions
89  /// </summary>
90  public void Clear()
91  {
92  _positions.Clear();
93  }
94 
95  /// <summary>
96  /// Attempts to retrieve the position with the specified symbol from this collection
97  /// </summary>
98  /// <param name="symbol">The symbol</param>
99  /// <param name="position">The position</param>
100  /// <returns>True if the position is found, otherwise false</returns>
101  public bool TryGetPosition(Symbol symbol, out IPosition position)
102  {
103  return _positions.TryGetValue(symbol, out position);
104  }
105 
106  /// <summary>Returns an enumerator that iterates through the collection.</summary>
107  /// <returns>An enumerator that can be used to iterate through the collection.</returns>
108  public IEnumerator<IPosition> GetEnumerator()
109  {
110  return _positions.Values.GetEnumerator();
111  }
112 
113  /// <summary>Returns an enumerator that iterates through a collection.</summary>
114  /// <returns>An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.</returns>
115  IEnumerator IEnumerable.GetEnumerator()
116  {
117  return GetEnumerator();
118  }
119  }
120 }