/// <summary>
        /// Metoda testująca znalezioną najlepszą sieć neuronową.
        /// </summary>
        public void TestNet()
        {
            StringReader reader;

            try
            {
                reader = new StringReader(File.ReadAllText(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\networks\\LowestMSENetwork.xml"));
            }
            catch
            {
                return;
            }
            XmlSerializer serializer = new XmlSerializer(typeof(NetworkMSE));
            NetworkMSE lowesMSE = (NetworkMSE)serializer.Deserialize(reader);
            NeuralNet net = new NeuralNet();
            if (!net.CreateFromFile("networks\\" + lowesMSE.NetworkFileName))
            {
                return;
            }
            uint numberOfInputs = net.GetNumInput();
            uint numberOfPeriods = (numberOfInputs + 1) / 2;
            DateTime date = new DateTime(2009, 12, 13);
            DateTime iterationDate = date;
            date = DataProvider.Instance.GetNextExchangeQuotationDate(date, 0);
            DateTime endDate = new DateTime(2011, 12 , 13);
            Trend up = new Trend(), down = new Trend(), side = new Trend();
            int predictionDownCount = 0;
            int predictionSidewaysCount = 0;
            int predictionUpCount = 0;
            int predictionDownTrue = 0;
            int predictionSidewaysTrue = 0;
            int predictionUpTrue = 0;
            while (date <= endDate)
            {
                List<ExchangePeriod> periods = DataProvider.Instance.GetExchangePeriodsMergedByMovementDirectoryFromEndDate((int)numberOfPeriods, date, this.firstExchangeQuotationDate, 1);
                List<ExchangePeriod> testPeriods = DataProvider.Instance.GetExchangePeriodsMergedByMovementDirectoryFromStartDate((int)numberOfPeriods, DataProvider.Instance.GetNextExchangeQuotationDate(DateTime.Now, 0), date, 1);
                TrendDirection truTrend = GetTrendDirection(testPeriods, 0);
                double[] inputs = new double[numberOfInputs];
                for (int i = 0; i < numberOfPeriods; i += 2)
                {
                    inputs[i] = periods[i].PercentageChange;
                    if (i + 1 < periods.Count)
                    {
                        inputs[i + 1] = (periods[i + 1].PublicTrading - periods[i].PublicTrading) / periods[i].PublicTrading;
                    }
                }
                double[] result = net.Run(inputs);
                double sum = 0;
                foreach (double r in result)
                {
                    if (r > 0)
                    {
                        sum += r;
                    }
                }

                for (int i = 0; i < result.Length; i++)
                {
                    TrendDirectionWithPropability td = new TrendDirectionWithPropability();
                    if (result[i] > 0)
                    {
                        result[i] = result[i] * 100 / sum;
                    }
                    else
                    {
                        result[i] = 0;
                    }
                }

                switch (result.ToList().IndexOf(result.Max()))
                {
                    case 0:
                        predictionDownCount++;
                        if (truTrend == TrendDirection.Down)
                        {
                            predictionDownTrue++;
                        }
                        break;
                    case 1:
                        predictionSidewaysCount++;
                        if (truTrend == TrendDirection.Sideways)
                        {
                            predictionSidewaysTrue++;
                        }
                        break;
                    case 2:
                        predictionUpCount++;
                        if (truTrend == TrendDirection.Up)
                        {
                            predictionUpTrue++;
                        }
                        break;
                }

                switch (truTrend)
                {
                    case TrendDirection.Down:
                        down.count++;
                        down.avgDown += result[0];
                        down.avgSide += result[1];
                        down.avgUp += result[2];
                        break;
                    case TrendDirection.Sideways:
                        side.count++;
                        side.avgDown += result[0];
                        side.avgSide += result[1];
                        side.avgUp += result[2];
                        break;
                    case TrendDirection.Up:
                        up.count++;
                        up.avgDown += result[0];
                        up.avgSide += result[1];
                        up.avgUp += result[2];
                        break;
                }

              //  iterationDate = iterationDate.AddDays(7);
              //  date = DataProvider.Instance.GetNextExchangeQuotationDate(iterationDate, 0);
                date = DataProvider.Instance.GetNextExchangeQuotationDate(date, 1);
            }
            down.avgUp = down.avgUp/(double) down.count;
            down.avgDown = down.avgDown/(double) down.count;
            down.avgSide = down.avgSide / (double)down.count;
            up.avgUp = up.avgUp / (double)up.count;
            up.avgDown = up.avgDown / (double)up.count;
            up.avgSide = up.avgSide / (double)up.count;
            side.avgUp = side.avgUp / (double)side.count;
            side.avgDown = side.avgDown / (double)side.count;
            side.avgSide = side.avgSide / (double)side.count;
            List<string> fileContent = new List<string>();
            fileContent.Add("Trend Name \t Sample Count \t Up \t Side \t Down ");
            fileContent.Add(TrendDirection.Up.ToString() + " \t " + up.count.ToString() + " \t " + up.avgUp.ToString() + " \t " + up.avgSide.ToString() + " \t " + up.avgDown.ToString());
            fileContent.Add(TrendDirection.Sideways.ToString() + " \t " + side.count.ToString() + " \t " + side.avgUp.ToString() + " \t " + side.avgSide.ToString() + " \t " + side.avgDown.ToString());
            fileContent.Add(TrendDirection.Down.ToString() + " \t " + down.count.ToString() + " \t " + down.avgUp.ToString() + " \t " + down.avgSide.ToString() + " \t " + down.avgDown.ToString());
            fileContent.Add("");
            fileContent.Add(TrendDirection.Up.ToString() + " \t " + (predictionUpTrue/(double)predictionUpCount).ToString());
            fileContent.Add(TrendDirection.Sideways.ToString() + " \t " + (predictionSidewaysTrue / (double)predictionSidewaysCount).ToString());
            fileContent.Add(TrendDirection.Down.ToString() + " \t " + (predictionDownTrue / (double)predictionDownCount).ToString());
            fileContent.Add("");
            File.AppendAllLines(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\test result.txt", fileContent);
        }
        /// <summary>
        /// Metoda przewidująca kierunek trendu na podstawie najlepszej znalezionej sieci neuronowej.
        /// </summary>
        /// <param name="date">Data dla której ma się odbyć przewidywanie.</param>
        /// <param name="path">Ścieżka do folderu zawierającego sieci neuronowe.</param>
        /// <returns>Wyniki przewidywania.</returns>
        public PredictionResult PredictTrendDirection(DateTime date, string path)
        {
            path = Path.GetFileName(path);
            StringReader reader;
            try
            {
                reader = new StringReader(File.ReadAllText(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + path + "\\LowestMSENetwork.xml"));
            }
            catch
            {
                return null;
            }
            XmlSerializer serializer = new XmlSerializer(typeof(NetworkMSE));
            NetworkMSE lowesMSE = (NetworkMSE)serializer.Deserialize(reader);
            NeuralNet net = new NeuralNet();
            if (!net.CreateFromFile(path + "\\" + lowesMSE.NetworkFileName))
            {
                return null;
            }
            uint numberOfInputs = net.GetNumInput();
            uint numberOfPeriods = (numberOfInputs + 1) / 2;
            List<ExchangePeriod> periods = DataProvider.Instance.GetExchangePeriodsMergedByMovementDirectoryFromEndDate((int)numberOfPeriods, date, this.firstExchangeQuotationDate, 1);
            double[] inputs = new double[numberOfInputs];
            for (int i = 0; i < numberOfPeriods; i += 2)
            {
                inputs[i] = periods[i].PercentageChange;
                if (i + 1 < periods.Count)
                {
                    inputs[i + 1] = (periods[i + 1].PublicTrading - periods[i].PublicTrading) / periods[i].PublicTrading;
                }
            }
            double[] result = net.Run(inputs);
            PredictionResult results = new PredictionResult();
            results.Trends = new List<TrendDirectionWithPropability>();
            double sum = 0;
            foreach (double r in result)
            {
                if (r > 0)
                {
                    sum += r;
                }
            }

            for (int i = 0; i < result.Length; i++)
            {
                TrendDirectionWithPropability td = new TrendDirectionWithPropability();
                if (result[i] > 0)
                {
                    td.Propability = result[i] * 100 / sum;
                }
                else
                {
                    td.Propability = 0;
                }

                switch (i)
                {
                    case 0:
                        td.Direction = TrendDirection.Down;
                        break;
                    case 1:
                        td.Direction = TrendDirection.Sideways;
                        break;
                    case 2:
                        td.Direction = TrendDirection.Up;
                        break;
                }
                results.Trends.Add(td);
            }

            int[] resultParam = DataProvider.Instance.GetNetParametersFromFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + path + "\\" + lowesMSE.NetworkFileName);
            if (resultParam != null)
            {
                results.PeriodNo = resultParam[0];
                results.PatternsNo = resultParam[1];
                results.NeuronNo = resultParam[2];
                results.EpochsNo = resultParam[3];
            }
            results.MSE = lowesMSE.MSE;
            return results;
        }