/// <summary> /// Initializes a new instance of the <see cref="PositionGroup"/> class /// </summary> /// <param name="key">The deterministic key for this group</param> /// <param name="positions">The positions comprising this group</param> public PositionGroup(PositionGroupKey key, Dictionary <Symbol, IPosition> positions) { Key = key; _positions = positions; var firstPosition = positions.First(); Quantity = firstPosition.Value.Quantity / firstPosition.Value.UnitQuantity; }
/// <summary> /// Gets the <see cref="IPositionGroup"/> matching the specified key. If one does not exist, then an empty /// group is returned matching the unit quantities defined in the <paramref name="key"/> /// </summary> /// <param name="key">The position group key to search for</param> /// <returns>The position group matching the specified key, or a new empty group if no matching group is found.</returns> public IPositionGroup this[PositionGroupKey key] { get { IPositionGroup group; if (!TryGetGroup(key, out group)) { return(new PositionGroup(key, key.CreateEmptyPositions())); } return(group); } }
/// <summary> /// Attempts to group the specified positions into a new <see cref="IPositionGroup"/> using an /// appropriate <see cref="IPositionGroupBuyingPowerModel"/> for position groups created via this /// resolver. /// </summary> /// <param name="newPositions">The positions to be grouped</param> /// <param name="currentPositions">The currently grouped positions</param> /// <param name="group">The grouped positions when this resolver is able to, otherwise null</param> /// <returns>True if this resolver can group the specified positions, otherwise false</returns> public bool TryGroup(IReadOnlyCollection <IPosition> newPositions, PositionGroupCollection currentPositions, out IPositionGroup group) { // we can only create default groupings containing a single security if (newPositions.Count != 1) { group = null; return(false); } var key = new PositionGroupKey(_buyingPowerModel, newPositions); group = new PositionGroup(key, newPositions.ToDictionary(p => p.Symbol)); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="PositionGroup"/> class /// </summary> /// <param name="key">The deterministic key for this group</param> /// <param name="positions">The positions comprising this group</param> public PositionGroup(PositionGroupKey key, Dictionary <Symbol, IPosition> positions) { Key = key; _positions = positions; var firstPosition = positions.First(); var quantity = firstPosition.Value.Quantity / firstPosition.Value.UnitQuantity; #if DEBUG if (quantity != Math.Truncate(quantity)) { throw new InvalidOperationException("PositionGroup.Quantity must be a whole number."); } #endif Quantity = quantity; }
/// <summary> /// Gets the <see cref="IPositionGroup"/> matching the specified <paramref name="key"/>. If one is not found, /// then a new empty position group is returned. /// </summary> public IPositionGroup this[PositionGroupKey key] => Groups[key];
/// <summary> /// Attempts to retrieve the group with the specified key /// </summary> /// <param name="key">The group key to search for</param> /// <param name="group">The position group</param> /// <returns>True if group with key found, otherwise false</returns> public bool TryGetGroup(PositionGroupKey key, out IPositionGroup group) { return(_groups.TryGetValue(key, out group)); }
/// <summary> /// Determines whether or not a group with the specified key exists in this collection /// </summary> /// <param name="key">The group key to search for</param> /// <returns>True if a group with the specified key was found, false otherwise</returns> public bool Contains(PositionGroupKey key) { return(_groups.ContainsKey(key)); }
/// <summary> /// Initializes a new instance of the <see cref="PositionGroup"/> class /// </summary> /// <param name="key">The deterministic key for this group</param> /// <param name="positions">The positions comprising this group</param> public PositionGroup(PositionGroupKey key, params IPosition[] positions) : this(key, positions.ToDictionary(p => p.Symbol)) { }