public ActionResult SetUsed(int identifier, bool used)
        {
            WeeklyIncomePlaySheet playSheet = WeeklyIncomePlaySheetService.Get(identifier);

            playSheet.Used = used;
            WeeklyIncomePlaySheetService.Save(playSheet);

            return(new EmptyResult());
        }
        public async Task BuildPcPlan(int slots, Decimal minStrikeDiff, Decimal maxRisk, int strikePadding)
        {
            Thread.Sleep(1000);

            Query query = new Query();

            query.SortPropertyName = Security.PropertyNames.Symbol;
            query.QuerySingleFilters.Add(new QuerySingleFilter()
            {
                PropertyName  = Security.PropertyNames.PairEligible,
                Parameter     = "true",
                QueryOperator = QueryOperators.Equals,
                IsAndFilter   = true
            });

            query.QuerySingleFilters.Add(new QuerySingleFilter()
            {
                PropertyName  = Security.PropertyNames.Ignore,
                Parameter     = "false",
                QueryOperator = QueryOperators.Equals,
                IsAndFilter   = true
            });

            query.Includes.Add(Security.PropertyNames.OptionChainsInclude);
            query.Includes.Add(Security.PropertyNames.OptionChains.DatesInclude);
            query.Includes.Add(Security.PropertyNames.OptionChains.Dates.StrikesInclude);
            query.Includes.Add(Security.PropertyNames.OptionChains.Dates.Strikes.CallInclude);
            query.Includes.Add(Security.PropertyNames.OptionChains.Dates.Strikes.PutInclude);

            List <Security> securities = SecurityService.GetCollection(query);

            securities.FilterForImportantDates();

            //securities = securities.Where(x => x.SectorEnum == Enums.Sectors.Index).ToList();

            OnProgressMessageRaised(new HtmlTag("li").Class("list-group-item").Class("active")
                                    .Append(HtmlTags.B.Append(securities.Count.ToString()))
                                    .Append(HtmlTags.Span.Append("&nbsp;optionable securities retrieved"))
                                    .ToHtml().ToString(), this.JobId);

            foreach (Security security in securities)
            {
                security.MinStrikeDiff = minStrikeDiff;
                security.MaxRisk       = maxRisk;
                security.StrikePadding = strikePadding;

                OnProgressMessageRaised(new HtmlTag("li").Class("list-group-item")
                                        .Append(HtmlTags.B.Append($"{security.Symbol}"))
                                        .Append(HtmlTags.Span.Append("&nbsp;expected move:&nbsp;"))
                                        .Append(HtmlTags.B.Append($"{security.ExpectedMove:C} ({(security.ExpectedMove / security.CurrentPrice):P2})"))
                                        .Append(HtmlTags.Span.Append("&nbsp;credit spreads:&nbsp;"))
                                        .Append(HtmlTags.B.Append($"{security.PcBearCallSpread}, {security.PcBullPutSpread}"))
                                        //.Append(HtmlTags.Span.Append($",&nbsp;price range:&nbsp;"))
                                        //.Append(HtmlTags.B.Append($"{security.LowerBoundStrike:C} : {security.ExpectedLowerMove:C} : {security.CurrentPrice:C} : {security.ExpectedUpperMove:C} : {security.UpperBoundStrike:C}"))
                                        //.Append(HtmlTags.Span.Append($",&nbsp;step/qty:&nbsp;"))
                                        //.Append(HtmlTags.B.Append($"{security.StrikeStep:C} / {security.ContractQuantity}"))
                                        .ToHtml().ToString(), this.JobId);
            }

            WeeklyIncomePlaySheet playSheet = new WeeklyIncomePlaySheet();

            playSheet.TimeStamp = DateTime.Now;
            playSheet.Expiry    = securities[0].LatestOptionDate.ExpiryDate;

            //var results = from p in persons
            //              group p.car by p.PersonId into g
            //              select new { PersonId = g.Key, Cars = g.ToList() };

            var sectorGroups = from s in securities
                               group s by s.SectorEnum into g
                               select new { Sector = g.Key, Securities = g.ToList() };

            List <PairCondor> pairCondorCandidates = new List <PairCondor>();

            foreach (var sectorGroup in sectorGroups)
            {
                OnProgressMessageRaised(new HtmlTag("li").Class("list-group-item")
                                        .Append(HtmlTags.Span.Append("Sector group:&nbsp;"))
                                        .Append(HtmlTags.B.Append($"{sectorGroup.Sector.GetStringValue()}"))
                                        .ToHtml().ToString(), this.JobId);

                if (sectorGroup.Securities.Count > 1)
                {
                    for (int i = 0; i < sectorGroup.Securities.Count - 1; i++)
                    {
                        for (int j = i + 1; j < sectorGroup.Securities.Count; j++)
                        {
                            Security iSecurity = sectorGroup.Securities[i];
                            Security jSecurity = sectorGroup.Securities[j];

                            if (!iSecurity.IsBearish && !jSecurity.IsBullish)
                            {
                                PairCondor ijPairCondor = new PairCondor();
                                ijPairCondor.SectorEnum     = sectorGroup.Sector;
                                ijPairCondor.Strategy       = StrategyTypes.PairCondor;
                                ijPairCondor.BullPutSpread  = iSecurity.PcBullPutSpread;
                                ijPairCondor.BearCallSpread = jSecurity.PcBearCallSpread;
                                pairCondorCandidates.Add(ijPairCondor);
                            }

                            if (!jSecurity.IsBearish && !iSecurity.IsBullish)
                            {
                                PairCondor jiPairCondor = new PairCondor();
                                jiPairCondor.SectorEnum     = sectorGroup.Sector;
                                jiPairCondor.Strategy       = StrategyTypes.PairCondor;
                                jiPairCondor.BullPutSpread  = jSecurity.PcBullPutSpread;
                                jiPairCondor.BearCallSpread = iSecurity.PcBearCallSpread;
                                pairCondorCandidates.Add(jiPairCondor);
                            }
                        }
                    }
                }
            }

            List <WeeklyIncomeComboCount> comboCounts = (from x in pairCondorCandidates
                                                         group x by x.SectorEnum into g
                                                         select new WeeklyIncomeComboCount()
            {
                SectorEnum = g.Key,
                Count = g.Count()
            }).OrderByDescending(x => x.Count).ToList();

            playSheet.ComboCounts = comboCounts;
            playSheet.Strategy    = StrategyTypes.PairCondor;

            int playSheetId = WeeklyIncomePlaySheetService.Save(playSheet);

            pairCondorCandidates = pairCondorCandidates.OrderByDescending(x => x.Credit).Select(x => new PairCondor(x)).ToList();
            WeeklyIncomeActionPlan bruteForceActionPlan = PcBruteForceActionPlanGenerator(pairCondorCandidates, slots);

            bruteForceActionPlan.PlaySheetIdentifier = playSheetId;
            WeeklyIncomeActionPlanService.Save(bruteForceActionPlan);

            pairCondorCandidates = pairCondorCandidates.OrderByDescending(x => x.Credit).Select(x => new PairCondor(x)).ToList();
            WeeklyIncomeActionPlan randomSearchActionPlan = PcRandomSearchActionPlanGenerator(pairCondorCandidates, slots);

            randomSearchActionPlan.PlaySheetIdentifier = playSheetId;
            WeeklyIncomeActionPlanService.Save(randomSearchActionPlan);

            pairCondorCandidates = pairCondorCandidates.OrderByDescending(x => x.Credit).Select(x => new PairCondor(x)).ToList();
            WeeklyIncomeActionPlan geneticOptimizationActionPlan = PcGeneticOptimizationActionPlanGenerator(pairCondorCandidates, slots);

            geneticOptimizationActionPlan.PlaySheetIdentifier = playSheetId;
            WeeklyIncomeActionPlanService.Save(geneticOptimizationActionPlan);

            Thread.Sleep(1000);
            OnRedirectRaised("WeeklyIncome", "PlaySheet", playSheetId, this.JobId);
        }
        public async Task BuildIcPlan(int slots, Decimal minStrikeDiff, Decimal maxRisk, int strikePadding)
        {
            Thread.Sleep(1000);

            Query query = new Query();

            query.SortPropertyName = Security.PropertyNames.Symbol;
            query.QuerySingleFilters.Add(new QuerySingleFilter()
            {
                PropertyName  = Security.PropertyNames.IronCondorEligible,
                Parameter     = "true",
                QueryOperator = QueryOperators.Equals,
                IsAndFilter   = true
            });

            query.QuerySingleFilters.Add(new QuerySingleFilter()
            {
                PropertyName  = Security.PropertyNames.Ignore,
                Parameter     = "false",
                QueryOperator = QueryOperators.Equals,
                IsAndFilter   = true
            });

            query.Includes.Add(Security.PropertyNames.OptionChainsInclude);
            query.Includes.Add(Security.PropertyNames.OptionChains.DatesInclude);
            query.Includes.Add(Security.PropertyNames.OptionChains.Dates.StrikesInclude);
            query.Includes.Add(Security.PropertyNames.OptionChains.Dates.Strikes.CallInclude);
            query.Includes.Add(Security.PropertyNames.OptionChains.Dates.Strikes.PutInclude);

            List <Security> securities = SecurityService.GetCollection(query);

            securities.FilterForImportantDates();

            //securities = securities.Where(x => x.SectorEnum == Enums.Sectors.Index).ToList();

            OnProgressMessageRaised(new HtmlTag("li").Class("list-group-item").Class("active")
                                    .Append(HtmlTags.B.Append(securities.Count.ToString()))
                                    .Append(HtmlTags.Span.Append("&nbsp;optionable securities retrieved"))
                                    .ToHtml().ToString(), this.JobId);


            WeeklyIncomePlaySheet playSheet = new WeeklyIncomePlaySheet();

            playSheet.TimeStamp = DateTime.Now;
            playSheet.Expiry    = securities[0].LatestOptionDate.ExpiryDate;

            List <PairCondor> ironCondorCandidates = new List <PairCondor>();

            foreach (Security security in securities)
            {
                security.MinStrikeDiff = minStrikeDiff;
                security.MaxRisk       = maxRisk;
                security.StrikePadding = strikePadding;
            }

            foreach (Security security in securities.Where(x => x.IcOptionsExist))
            {
                PairCondor ironCondor = new PairCondor();
                ironCondor.SectorEnum     = security.SectorEnum;
                ironCondor.Strategy       = StrategyTypes.IronCondor;
                ironCondor.BullPutSpread  = security.IcBullPutSpread;
                ironCondor.BearCallSpread = security.IcBearCallSpread;
                ironCondorCandidates.Add(ironCondor);
            }

            playSheet.Strategy = StrategyTypes.IronCondor;

            int playSheetId = WeeklyIncomePlaySheetService.Save(playSheet);

            ironCondorCandidates = ironCondorCandidates.OrderByDescending(x => x.Credit).Select(x => new PairCondor(x)).ToList();
            WeeklyIncomeActionPlan bruteForceActionPlan = IcBruteForceActionPlanGenerator(ironCondorCandidates, slots);

            bruteForceActionPlan.PlaySheetIdentifier = playSheetId;
            WeeklyIncomeActionPlanService.Save(bruteForceActionPlan);

            Thread.Sleep(1000);
            OnRedirectRaised("WeeklyIncome", "PlaySheet", playSheetId, this.JobId);
        }