Exemple #1
0
        public static double TrainNetworks(SupportVectorMachine network, MarketMLDataSet training)
        {
            // train the neural network
            SVMTrain trainMain = new SVMTrain(network, training);

            StopTrainingStrategy stop = new StopTrainingStrategy(0.0001, 200);

            trainMain.AddStrategy(stop);

            var sw = new Stopwatch();

            sw.Start();
            while (!stop.ShouldStop())
            {
                trainMain.PreIteration();

                trainMain.Iteration();
                trainMain.PostIteration();

                Console.WriteLine(@"Iteration #:" + trainMain.IterationNumber + @" Error:" + trainMain.Error);
            }
            sw.Stop();
            Console.WriteLine("SVM Trained in :" + sw.ElapsedMilliseconds + "For error:" + trainMain.Error + " Iterated:" + trainMain.IterationNumber);
            return(trainMain.Error);
        }
Exemple #2
0
        public static MarketMLDataSet GrabData(string newfileLoad)
        {
            IMarketLoader loader = new CSVFileLoader();//CSVLoader();

            loader.GetFile(newfileLoad);

            var result = new MarketMLDataSet(loader,
                                             Config.INPUT_WINDOW, Config.PREDICT_WINDOW);
            //  var desc = new MarketDataDescription(Config.TICKER,
            //   MarketDataType.Close, true, true);

            var desc = new MarketDataDescription(Config.TICKER,
                                                 MarketDataType.Close, TemporalDataDescription.Type.PercentChange, true, true);

            result.AddDescription(desc);

            var begin = DateTime.ParseExact("29.05.2005", "dd.MM.yyyy", CultureInfo.CurrentCulture); // begin 30 days ago
            var end   = DateTime.ParseExact("22.07.2005", "dd.MM.yyyy", CultureInfo.CurrentCulture); // begin 30 days ago

            begin = begin.AddDays(Config.DAYS_OFFSET).AddDays(Config.TEST_OFFSET);
            end   = end.AddDays(Config.DAYS_OFFSET).AddDays(Config.TEST_OFFSET).AddDays(Config.TEST_STRATCH);

            result.Load(begin, end);
            result.Generate();

            return(result);
        }
Exemple #3
0
        public static MarketMLDataSet GrabData(string newfileLoad)
        {
            IMarketLoader loader = new CSVFinal();

            loader.GetFile(newfileLoad);

            var result = new MarketMLDataSet(loader,
                                             CONFIG.INPUT_WINDOW, CONFIG.PREDICT_WINDOW);
            //  var desc = new MarketDataDescription(Config.TICKER,
            //   MarketDataType.Close, true, true);

            var desc = new MarketDataDescription(CONFIG.TICKER,
                                                 MarketDataType.Close, true, true);

            result.AddDescription(desc);

            var end   = DateTime.Now;            // end today
            var begin = new DateTime(end.Ticks); // begin 30 days ago

            begin = begin.AddDays(-950);

            result.Load(begin, end);
            result.Generate();

            return(result);
        }
Exemple #4
0
        public static void Generate(FileInfo dataDir)
        {
            IMarketLoader loader = new YahooFinanceLoader();
            var           market = new MarketMLDataSet(loader,
                                                       Config.INPUT_WINDOW, Config.PREDICT_WINDOW);
            var desc = new MarketDataDescription(
                Config.TICKER, MarketDataType.AdjustedClose, true, true);

            market.AddDescription(desc);

            var end   = DateTime.Now;            // end today
            var begin = new DateTime(end.Ticks); // begin 30 days ago

            // Gather training data for the last 2 years, stopping 60 days short of today.
            // The 60 days will be used to evaluate prediction.
            begin = begin.AddDays(-60);
            end   = end.AddDays(-60);
            begin = begin.AddYears(-2);

            market.Load(begin, end);
            market.Generate();
            EncogUtility.SaveEGB(FileUtil.CombinePath(dataDir, Config.TRAINING_FILE), market);

            // create a network
            BasicNetwork network = EncogUtility.SimpleFeedForward(
                market.InputSize,
                Config.HIDDEN1_COUNT,
                Config.HIDDEN2_COUNT,
                market.IdealSize,
                true);

            // save the network and the training
            EncogDirectoryPersistence.SaveObject(FileUtil.CombinePath(dataDir, Config.NETWORK_FILE), network);
        }
Exemple #5
0
        public static double Evaluate(FileInfo dataDir, string filename)
        {
            FileInfo file = FileUtil.CombinePath(dataDir, Config.NETWORK_FILE);

            if (!file.Exists)
            {
                Console.WriteLine(@"Can't read file: " + file);
                return(0);
            }

            var network = (BasicNetwork)EncogDirectoryPersistence.LoadObject(file);

            MarketMLDataSet data = GrabData(filename);

            int count   = 0;
            int correct = 0;

            foreach (IMLDataPair pair in data)
            {
                IMLData input       = pair.Input;
                IMLData actualData  = pair.Ideal;
                IMLData predictData = network.Compute(input);

                double actual  = actualData[0];
                double predict = predictData[0];
                double diff    = Math.Abs(predict - actual);

                Direction actualDirection  = DetermineDirection(actual);
                Direction predictDirection = DetermineDirection(predict);

                if (actualDirection == predictDirection)
                {
                    correct++;
                }

                count++;


                Console.WriteLine(@"Day " + count + @":actual="
                                  + Format.FormatDouble(actual, 4) + @"(" + actualDirection + @")"
                                  + @",predict=" + Format.FormatDouble(predict, 4) + @"("
                                  + predictDirection + @")" + @",diff=" + diff);
            }
            double percent = correct / (double)count;

            Console.WriteLine(@"Direction correct:" + correct + @"/" + count);
            Console.WriteLine(@"Directional Accuracy:"
                              + Format.FormatPercent(percent));
            return(percent);
        }
Exemple #6
0
        public static void Generate(string fileName)
        {
            FileInfo      dataDir = new FileInfo(@Environment.CurrentDirectory);
            IMarketLoader loader  = new CSVFinal();
            var           market  = new MarketMLDataSet(loader, CONFIG.INPUT_WINDOW, CONFIG.PREDICT_WINDOW);
            //  var desc = new MarketDataDescription(Config.TICKER, MarketDataType.Close, true, true);

            var desc = new MarketDataDescription(CONFIG.TICKER, MarketDataType.Close, true, true);

            market.AddDescription(desc);
            string currentDirectory = @"c:\";

            loader.GetFile(fileName);

            var end   = DateTime.Now;            // end today
            var begin = new DateTime(end.Ticks); // begin 30 days ago

            // Gather training data for the last 2 years, stopping 60 days short of today.
            // The 60 days will be used to evaluate prediction.
            begin = begin.AddDays(-600);
            end   = begin.AddDays(200);

            Console.WriteLine("You are loading date from:" + begin.ToShortDateString() + " To :" + end.ToShortDateString());

            market.Load(begin, end);
            market.Generate();
            EncogUtility.SaveEGB(FileUtil.CombinePath(dataDir, CONFIG.SVMTRAINING_FILE), market);

            // create a network
            //BasicNetwork network = EncogUtility.SimpleFeedForward(
            //    market.InputSize,
            //    CONFIG.HIDDEN1_COUNT,
            //    CONFIG.HIDDEN2_COUNT,
            //    market.IdealSize,
            //    true);


            SupportVectorMachine network = new SupportVectorMachine(CONFIG.INPUT_WINDOW, true);

            TrainNetworks(network, market);
            // save the network and the training
            EncogDirectoryPersistence.SaveObject(FileUtil.CombinePath(dataDir, CONFIG.SVMTRAINING_FILE), network);
        }
        public static void Generate(string fileName)
        {
            FileInfo dataDir = new FileInfo(@Environment.CurrentDirectory);
            //Lets use the CSVFinal..(and not the CSV Form loader).
            IMarketLoader loader = new CSVFinal();

            loader.GetFile(fileName);
            var market = new MarketMLDataSet(loader, Config.INPUT_WINDOW, Config.PREDICT_WINDOW);
            //  var desc = new MarketDataDescription(Config.TICKER, MarketDataType.Close, true, true);

            var desc = new MarketDataDescription(Config.TICKER, MarketDataType.Close, TemporalDataDescription.Type.PercentChange, true, true);

            market.AddDescription(desc);
            loader.GetFile(fileName);

            var begin = DateTime.ParseExact("01.01.2000", "dd.MM.yyyy", CultureInfo.CurrentCulture); // begin 30 days ago
            var end   = DateTime.ParseExact("22.06.2005", "dd.MM.yyyy", CultureInfo.CurrentCulture); // begin 30 days ago

            begin = begin.AddDays(Config.DAYS_OFFSET);
            end   = end.AddDays(Config.DAYS_OFFSET);

            // Gather training data for the last 2 years, stopping 60 days short of today.
            // The 60 days will be used to evaluate prediction.
            //begin = begin.AddDays(-200);
            //end = begin.AddDays(1);

            Console.WriteLine("You are loading date from:" + begin.ToShortDateString() + " To :" + end.ToShortDateString());

            market.Load(begin, end);
            market.Generate();
            EncogUtility.SaveEGB(FileUtil.CombinePath(dataDir, Config.TRAINING_FILE), market);

            // create a network
            BasicNetwork network = EncogUtility.SimpleFeedForward(
                market.InputSize,
                Config.HIDDEN1_COUNT,
                Config.HIDDEN2_COUNT,
                market.IdealSize,
                true);

            // save the network and the training
            EncogDirectoryPersistence.SaveObject(FileUtil.CombinePath(dataDir, Config.NETWORK_FILE), network);
        }
        public static MarketMLDataSet GrabData()
        {
            IMarketLoader loader = new YahooFinanceLoader();
            var           result = new MarketMLDataSet(loader,
                                                       Config.INPUT_WINDOW, Config.PREDICT_WINDOW);
            var desc = new MarketDataDescription(Config.TICKER,
                                                 MarketDataType.AdjustedClose, true, true);

            result.AddDescription(desc);

            var end   = DateTime.Now;            // end today
            var begin = new DateTime(end.Ticks); // begin 30 days ago

            begin = begin.AddDays(-60);

            result.Load(begin, end);
            result.Generate();

            return(result);
        }