private Option GetOptionMatchToStrategyLeg(OptionChain chain, SuggestedStrategyLeg strategyLeg) { if (!strategyLeg.StrikePrice.HasValue || strategyLeg.LegType == LegType.Security) { throw new InvalidOperationException(); } Option targetOptionChain = chain[strategyLeg.ExpirationDate.FutureDate, strategyLeg.StrikePrice.Value, strategyLeg.LegType == LegType.Put ? LegType.Put : LegType.Call]; return(targetOptionChain); }
private bool MeetMinBidResctictions(OptionChain chain, SuggestedStrategyLeg strategyLeg) { Option targetOption = GetOptionMatchToStrategyLeg(chain, strategyLeg); if (targetOption == null) { return(false); } double minimalBidPrice = AppConfigManager.MinimalBidForTradingStrategies; double currentBid = targetOption.Bid == 0 ? targetOption.PreviousSettlementPrice : targetOption.Bid; return(currentBid > minimalBidPrice); }
public SuggestedStrategy FullfillStrategy(OptionChain chain, Strategy strategyTemplate, bool opposite = false) { bool invertLegs = false; if (opposite) { if (strategyTemplate.PairStrategyId == null) { // if there is no pair strategy defined we just invert legs for current strategy } else { strategyTemplate = _strategyService.GetById(strategyTemplate.PairStrategyId.Value); } } int zeroExpiryIndex = 0; if (chain != null) { DateAndNumberOfDaysUntil sigma1Date, sigma2Date; // expiry date that is closest to 45 (by default) days from today chain.GetClosestExpiryDatesToDaysInFeature(DefaultDaysInFutureForExpiry, out sigma1Date, out sigma2Date); // this should be threated as base index for expiryDate. Use ExpirationDates[one + strategy.Expiry] to get current one. zeroExpiryIndex = chain.ExpirationDates.ToList().IndexOf(sigma1Date) - 1; } SuggestedStrategy model = new SuggestedStrategy(strategyTemplate.Name, BuyOrSell.Buy); List <SuggestedStrategyLeg> legs = new List <SuggestedStrategyLeg>(); foreach (StrategyLeg leg in strategyTemplate.Legs) { BuyOrSell buyOrSellForLeg = !invertLegs ? leg.BuyOrSell.Value : leg.BuyOrSell == BuyOrSell.Buy ? BuyOrSell.Sell : BuyOrSell.Buy; SuggestedStrategyLeg legModel = new SuggestedStrategyLeg(leg.LegType.Value, buyOrSellForLeg, leg.Quantity); if (legModel.LegType != LegType.Security) { if (chain == null) { return(model); } Debug.Assert(leg.Expiry != null, "leg.Expiry != null"); legModel.ExpirationDate = chain.ExpirationDates[zeroExpiryIndex + leg.Expiry.Value]; legModel.StrikePrice = GetStrikePriceForLegOld(chain, leg, legModel.ExpirationDate); if (!legModel.StrikePrice.HasValue || !MeetMinBidResctictions(chain, legModel)) { return(model); } } legs.Add(legModel); } if (legs.Count == 2 && legs[0].BuyOrSell != legs[1].BuyOrSell && legs[0].StrikePrice == legs[1].StrikePrice && legs[0].ExpirationDate == legs[1].ExpirationDate) { // don't compose strategy if both legs have the same expiry and strike price return(model); } model.Legs = legs; return(model); }