private IEnumerable <IPositionGroup> GetPositionGroups(IEnumerable <IPosition> positions) { foreach (var positionsByUnderlying in positions .Where(position => position.Symbol.SecurityType.HasOptions() || position.Symbol.SecurityType.IsOption()) .GroupBy(position => position.Symbol.HasUnderlying? position.Symbol.Underlying : position.Symbol)) { var optionPosition = positionsByUnderlying.FirstOrDefault(position => position.Symbol.SecurityType.IsOption()); if (optionPosition == null) { // if there isn't any option position we aren't really interested, can't create any option strategy! continue; } var contractMultiplier = (_securities[optionPosition.Symbol].SymbolProperties as OptionSymbolProperties)?.ContractUnitOfTrade ?? 100; var optionPositionCollection = OptionPositionCollection.FromPositions(positionsByUnderlying, contractMultiplier); var matches = _strategyMatcher.MatchOnce(optionPositionCollection); if (matches.Strategies.Count == 0) { continue; } foreach (var matchedStrategy in matches.Strategies) { var positionsToGroup = matchedStrategy.OptionLegs .Select(optionLeg => (IPosition) new Position(optionLeg.Symbol, optionLeg.Quantity, 1)) .Concat(matchedStrategy.UnderlyingLegs.Select(underlyingLeg => new Position(underlyingLeg.Symbol, underlyingLeg.Quantity * contractMultiplier, 1))) .ToArray(); yield return(new PositionGroup(new OptionStrategyPositionGroupBuyingPowerModel(matchedStrategy), positionsToGroup)); } } }
public void Setup() { _holdings = new[] { CreateHolding(1), CreateHolding(2), CreateHolding(3), CreateHolding(4), CreateHolding(Symbols.SPY, ContractMultiplier * UnderlyingLots) }; _positions = OptionPositionCollection.Create(Symbols.SPY, ContractMultiplier, _holdings); }