Пример #1
0
        public StrategyViewModel GetEditStrategy(int strategyId)
        {
            Strategy strategy = _strategyService.GetById(strategyId);

            if (strategy == null)
            {
                return(null);
            }
            StrategyViewModel viewModel = Mapper.Map <Strategy, StrategyViewModel>(strategy);

            viewModel.PairStrategyOptions = GetPairStrategyOptions(strategyId);
            return(viewModel);
        }
        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);
        }