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

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

            return(new EmptyResult());
        }
        public ActionResult PlaySheets_Read([DataSourceRequest] DataSourceRequest request)
        {
            DataSourceResult result = new DataSourceResult();
            Query            query  = request.ToQuery();

            query.Includes.Add(WeeklyIncomePlaySheet.PropertyNames.ComboCountsInclude);
            query.Includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlansInclude);
            query.Includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.PairsInclude);
            query.Includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BullPutSpreadInclude);
            query.Includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BullPutSpread.SecurityInclude);
            query.Includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BullPutSpread.SellPutInclude);
            query.Includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BullPutSpread.BuyPutInclude);
            query.Includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BearCallSpreadInclude);
            query.Includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BearCallSpread.SecurityInclude);
            query.Includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BearCallSpread.SellCallInclude);
            query.Includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BearCallSpread.BuyCallInclude);

            result.Data  = mapper.Map <List <SimpleWeeklyIncomePlaySheetDto> >(WeeklyIncomePlaySheetService.GetCollection(query));
            result.Total = WeeklyIncomePlaySheetService.GetCount(query);

            return(new GuerillaLogisticsApiJsonResult(result));
        }
        public ActionResult PlaySheet(int id)
        {
            List <String> includes = new List <string>();

            includes.Add(WeeklyIncomePlaySheet.PropertyNames.ComboCountsInclude);
            includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlansInclude);
            includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.PairsInclude);
            includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BullPutSpreadInclude);
            includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BullPutSpread.SecurityInclude);
            includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BullPutSpread.SellPutInclude);
            includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BullPutSpread.BuyPutInclude);
            includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BearCallSpreadInclude);
            includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BearCallSpread.SecurityInclude);
            includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BearCallSpread.SellCallInclude);
            includes.Add(WeeklyIncomePlaySheet.PropertyNames.ActionPlans.Pairs.BearCallSpread.BuyCallInclude);

            WeeklyIncomePlaySheetDto model = this.mapper.Map <WeeklyIncomePlaySheetDto>(WeeklyIncomePlaySheetService.Get(id, includes));

            return(View(model));
        }
        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 void Purge()
        {
            Thread.Sleep(1500);

            SendPurgeMessage("WeeklyIncomeComboCount");
            foreach (WeeklyIncomeComboCount x in WeeklyIncomeComboCountService.GetCollection())
            {
                WeeklyIncomeComboCountService.Delete(x);
            }

            SendPurgeMessage("OptionStrike");
            foreach (OptionStrike x in OptionStrikeService.GetCollection())
            {
                OptionStrikeService.Delete(x);
            }

            SendPurgeMessage("OptionDate");
            foreach (OptionDate x in OptionDateService.GetCollection())
            {
                OptionDateService.Delete(x);
            }

            SendPurgeMessage("OptionChain");
            foreach (OptionChain x in OptionChainService.GetCollection())
            {
                OptionChainService.Delete(x);
            }

            SendPurgeMessage("BullPutSpread");
            foreach (BullPutSpread x in BullPutSpreadService.GetCollection())
            {
                BullPutSpreadService.Delete(x);
            }

            SendPurgeMessage("BearCallSpread");
            foreach (BearCallSpread x in BearCallSpreadService.GetCollection())
            {
                BearCallSpreadService.Delete(x);
            }

            SendPurgeMessage("PairCondor");
            foreach (PairCondor x in PairCondorService.GetCollection())
            {
                PairCondorService.Delete(x);
            }

            SendPurgeMessage("WeeklyIncomeActionPlan");
            foreach (WeeklyIncomeActionPlan x in WeeklyIncomeActionPlanService.GetCollection())
            {
                WeeklyIncomeActionPlanService.Delete(x);
            }

            SendPurgeMessage("Call");
            foreach (Call x in CallService.GetCollection())
            {
                CallService.Delete(x);
            }

            SendPurgeMessage("Put");
            foreach (Put x in PutService.GetCollection())
            {
                PutService.Delete(x);
            }

            SendPurgeMessage("WeeklyIncomePlaySheet");
            foreach (WeeklyIncomePlaySheet x in WeeklyIncomePlaySheetService.GetCollection())
            {
                WeeklyIncomePlaySheetService.Delete(x);
            }

            OnRedirectRaised("WeeklyIncome", "Index", 0, 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);
        }