private SPY_Simulation_Result CalculateResult(SPY_Simulation_Result CurrentResult, byte[] Candidate, long ii, int TaskNumber, long CompletionDenom)
        {
            long profit = 0, surgePot = 0;

            foreach (SPY_Trading_Day day in this.SPY.Trading_Days)
            {
                byte nominator = (byte)(Candidate[(day.Fifty_Two_Week_Range - 1)]);

                profit = profit + CalculateProfit(day.Close, ref surgePot, nominator);
            }

            if (profit > CurrentResult.Profit)
            {
                CurrentResult.Profit        = profit;
                CurrentResult.IteratorIndex = ii;
                CurrentResult.Result        = Candidate;

                CurrentResult.Report = (DateTime.Now.ToString() + ": Task Number " + TaskNumber + " is " + ShowNumber(BuildCompletion((int)(ii * 100000 / CompletionDenom)), 4) + "% complete, it has found a new max profit: $" + ShowNumber(profit, 2));



                Console.WriteLine(CurrentResult.Report);
            }

            return(CurrentResult);
        }
        private SPY_Simulation_Result GrabMaxResult(ref List <SPY_Simulation_Result> SortingResults, ref long max, out int tii)
        {
            tii = 0;

            int ii = 0;

            SPY_Simulation_Result ret = new SPY_Simulation_Result(-1);

            foreach (SPY_Simulation_Result result in SortingResults)
            {
                if (result.Profit > max)
                {
                    max = result.Profit;
                    tii = ii;
                    ret = result;
                }

                ii++;
            }

            return(ret);
        }
        // SIMULATION METHODS

        private SPY_Simulation_Result Run_Simulation(int TaskNumber)
        {
            SPY_Simulation_Result result = new SPY_Simulation_Result(TaskNumber);

            long ii = progress + TaskNumber, tempCap = progress + TaskNumber;

            while (ii < this.countCap)
            {
                tempCap = tempCap + 1000;

                for (; ii < tempCap; ii = ii + this.numberoftasks)
                {
                    byte[] candidate = this.CE.buildRow(ii);

                    if (IsLogicalCandidate(candidate))
                    {
                        result = CalculateResult(result, candidate, ii, TaskNumber, tempCap);
                    }

                    if (this.progress < ii)
                    {
                        this.progress = ii;
                    }

                    if (this.run == false)
                    {
                        return(result);
                    }
                }

                ProgressReport(new SimulationReport(ii, "Simulation has run " + ii + " times.", true));
            }



            return(result);
        }