Example #1
0
        public float GetAverage(StockNNES stockNNES)
        {
            float sum = 0;

            foreach (var performance in stockNNES.Performances)
            {
                sum += performance.Value;
            }

            return(sum / stockNNES.Performances.Count);
        }
Example #2
0
        static void PrintPerformance(StockNNES stockNNES, EvolutionalEntity <Action> bestTrainingPerformer = null)
        {
            float sum = 0;

            foreach (KeyValuePair <EvolutionalEntity <Action>, float> performance in stockNNES.Performances.OrderBy(p => p.Value).Reverse())
            {
                float percentage = 100 * (performance.Value - stockNNES.DefaultState.Cash) / stockNNES.DefaultState.Cash;
                if (bestTrainingPerformer != null && performance.Key == bestTrainingPerformer)
                {
                    Console.WriteLine("{0}->{1}\t{2}% <---- Best performer from training", stockNNES.DefaultState.Cash, performance.Value, percentage);
                }
                Console.WriteLine("{0}->{1}\t{2}%", stockNNES.DefaultState.Cash, performance.Value, percentage);
                sum += percentage;
            }
            Console.WriteLine("Average: {0}%", sum / stockNNES.Performances.Count);
        }
Example #3
0
        static void Main(string[] args)
        {
            const int TICKS_TRAIN = 120;
            const int TICKS_TEST  = 120;
            const int INPUT_HOURS = 24;

            const int GENERATION_TO_STOP_TRAINING = 50;
            const int POPULATION_SIZE             = 100;

            StockEnvironment stockEnvironment;

            //build model
            SequentialNN model = new SequentialNN();

            model.AddLayer(new Layer(5));
            model.AddLayer(new Layer(5));

            StockNNES stockNNES       = new StockNNES(model, POPULATION_SIZE);
            StockNNES stockNNESRandom = new StockNNES(model, POPULATION_SIZE);

            //train
            for (int i = 0; i < GENERATION_TO_STOP_TRAINING; i++)
            {
                Console.Clear();
                Console.WriteLine("Training Generation: {0}", stockNNES.Generation);

                //create new environment
                stockEnvironment      = new StockEnvironment("XVG", INPUT_HOURS, TICKS_TRAIN, TICKS_TEST + INPUT_HOURS - 1);
                stockNNES.Environment = stockEnvironment;

                for (int j = 0; j < TICKS_TRAIN; j++)
                {
                    stockNNES.RunTick();
                }

                if (i != GENERATION_TO_STOP_TRAINING - 1)
                {
                    stockNNES.CreateNextGeneration();
                }
            }

            //print performances
            Console.WriteLine();
            Console.WriteLine("Performance of trained population");
            Console.WriteLine("----------------");
            PrintPerformance(stockNNES);

            EvolutionalEntity <Action> bestTrainingPerformer = stockNNES.Performances.OrderBy(p => p.Value).Last().Key;

            //reset performance
            stockNNES.ResetState();

            //test trained
            Console.WriteLine("Testing Trained");

            stockEnvironment      = new StockEnvironment("BTC", INPUT_HOURS, TICKS_TEST);
            stockNNES.Environment = stockEnvironment;
            for (int i = 0; i < TICKS_TEST; i++)
            {
                stockNNES.RunTick();
            }

            //test random
            Console.WriteLine("Testing Random");

            stockEnvironment            = new StockEnvironment("BTC", INPUT_HOURS, TICKS_TEST);
            stockNNESRandom.Environment = stockEnvironment;
            for (int i = 0; i < TICKS_TEST; i++)
            {
                stockNNESRandom.RunTick();
            }

            //print performances of random population
            Console.WriteLine();
            Console.WriteLine("Performance of random population");
            Console.WriteLine("----------------");
            PrintPerformance(stockNNESRandom);

            //print performances
            Console.WriteLine();
            Console.WriteLine("Performance of trained population");
            Console.WriteLine("----------------");
            PrintPerformance(stockNNES, bestTrainingPerformer);
        }
Example #4
0
        public TrainReturn Train(TrainParameters trainParameters)
        {
            TrainReturn trainReturn = new TrainReturn();
            List <NNStockStatistics> trainStatistics = new List <NNStockStatistics>();
            List <NNStockStatistics> testStatistics  = new List <NNStockStatistics>();

            List <NNStockStatistics> trainAverage = new List <NNStockStatistics>();
            List <NNStockStatistics> testAverage  = new List <NNStockStatistics>();

            SequentialNN model = new SequentialNN();

            model.AddLayer(new Layer(5));
            model.AddLayer(new Layer(5));

            StockNNES stockNNES = new StockNNES(model, 100);

            for (int i = 0; i < trainParameters.generations; i++)
            {
                //train
                stockNNES.Environment = new StockEnvironment(trainParameters.stockName, trainParameters.inputSize, trainParameters.ticksTrain, trainParameters.ticksTest + trainParameters.inputSize - 1);

                for (int j = 0; j < trainParameters.ticksTrain; j++)
                {
                    stockNNES.RunTick();
                }
                KeyValuePair <EvolutionalEntity <Action>, float> bestPerformerTrain = stockNNES.Performances.OrderBy(p => p.Value).Last();
                trainStatistics.Add(new NNStockStatistics {
                    EvolutionalEntity = bestPerformerTrain.Key, Performance = bestPerformerTrain.Value, Generation = i + 1
                });

                trainAverage.Add(new NNStockStatistics {
                    EvolutionalEntity = null, Performance = GetAverage(stockNNES), Generation = i + 1
                });

                Dictionary <EvolutionalEntity <Action>, float> performance = new Dictionary <EvolutionalEntity <Action>, float>(stockNNES.Performances);

                //test
                stockNNES.Environment = new StockEnvironment(trainParameters.stockName, trainParameters.inputSize, trainParameters.ticksTest);
                for (int j = 0; j < trainParameters.ticksTest; j++)
                {
                    stockNNES.RunTick();
                }

                KeyValuePair <EvolutionalEntity <Action>, float> bestPerformerTest = stockNNES.Performances.OrderBy(p => p.Value).Last();
                testStatistics.Add(new NNStockStatistics {
                    EvolutionalEntity = bestPerformerTest.Key, Performance = bestPerformerTest.Value, Generation = i + 1
                });

                testAverage.Add(new NNStockStatistics {
                    EvolutionalEntity = null, Performance = GetAverage(stockNNES), Generation = i + 1
                });

                GetAverage(stockNNES);

                //reset performance
                stockNNES.Performances = performance;

                if (i != trainParameters.generations - 1)
                {
                    stockNNES.CreateNextGeneration();
                }
            }

            trainReturn.trainStatistics = trainStatistics;
            trainReturn.testStatistics  = testStatistics;

            trainReturn.trainAverage = trainAverage;
            trainReturn.testAverage  = testAverage;

            return(trainReturn);
        }