Example #1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("# of CPU cores=" + System.Environment.ProcessorCount.ToString());

            var key = "";

            while (true)
            {
                Console.WriteLine("\"ga\" : island GA");
                Console.WriteLine("\"sim\" : read sim");
                Console.WriteLine("\"mul ga\" : multi strategy ga");
                Console.WriteLine("\"mul sim\" : multi strategy sim");
                Console.WriteLine("\"conti\" : do ga / sim continuously");
                Console.WriteLine("\"win ga\" : do win ga");
                Console.WriteLine("\"write\" : write MarketData");
                Console.WriteLine("\"test\" : test");
                key = Console.ReadLine();
                if (key == "ga" || key == "sim" || key == "mul ga" || key == "mul sim" || key == "win ga" || key == "conti" || key == "win ga" || key == "write" || key == "test")
                {
                    break;
                }
            }

            if (key == "test")
            {
                RandomSeed.initialize();
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(RandomSeed.rnd.Next());
                }
                Environment.Exit(0);
            }


            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            Console.WriteLine("started program.");
            List <int> terms = new List <int>();

            for (int i = 10; i < 1000; i = i + 100)
            {
                terms.Add(i);
            }
            MarketData.initializer(terms);

            var    from           = 1000;
            var    to             = 501000;//MarketData.Close.Count-1;
            int    max_amount     = 1;
            var    index          = new int[] { 0, 0, 0, 1, 1, 0, 0 };
            double nn_threshold   = 0.3;
            int    best_island_id = 4;
            bool   display_chart  = true;
            var    sim_type       = 1; //0:limit, 1:market/limit

            //read weight sim
            if (key == "sim")
            {
                var ac = doSim(from, to, max_amount, sim_type, best_island_id, display_chart, nn_threshold);
            }
            //island ga
            else if (key == "ga")
            {
                int num_island         = 2;
                int num_chromos        = 4;
                int num_generations    = 20;
                int banned_move_period = 2;
                var units         = new int[] { 67, 5, 5, 5, 5 };
                var mutation_rate = 0.5;
                var move_ratio    = 0.2;
                best_island_id = doGA(from, to, max_amount, num_island, num_chromos, num_generations, banned_move_period, units, mutation_rate, move_ratio, index, display_chart, nn_threshold);
                doSim(from, to, max_amount, sim_type, best_island_id, display_chart, nn_threshold);
                doSim(to, MarketData.Close.Count - 1, max_amount, sim_type, best_island_id, display_chart, nn_threshold);
            }
            //multi strategy combination sim
            else if (key == "mul ga")
            {
                var index_list = new List <int[]> {
                    new int[] { 0, 0, 0, 1, 1, 0, 0 }, new int[] { 0, 0, 0, 0, 1, 1, 1 }
                };
                var units_list = new List <int[]> {
                    new int[] { 77, 5, 5, 5, 5 }, new int[] { 77, 5, 5, 5, 5 }
                };
                var best_pl_list       = new List <List <double> >();
                var best_ac_list       = new List <SimAccount>();
                int num_island         = 2;
                int num_chromos        = 4;
                int num_generations    = 3;
                int banned_move_period = 2;
                var mutation_rate      = 0.5;
                var move_ratio         = 0.2;
                var id_list            = new List <int>();
                var nn_threshold_list  = new List <double>();
                for (int i = 0; i < index_list.Count; i++)
                {
                    best_island_id = doGA(from, to, max_amount, num_island, num_chromos, num_generations, banned_move_period, units_list[i], mutation_rate, move_ratio, index_list[i], display_chart, nn_threshold);
                    var ac = doSim(from, to, max_amount, sim_type, best_island_id, display_chart, nn_threshold);
                    best_pl_list.Add(ac.total_pl_ratio_list);
                    best_ac_list.Add(ac);
                    if (File.Exists(@"./log_best_weight_ID-" + i.ToString() + ".csv"))
                    {
                        File.Delete(@"./log_best_weight_ID-" + i.ToString() + ".csv");
                    }
                    File.Copy(@"./best_weight_ID-" + best_island_id.ToString() + ".csv", @"./log_best_weight_ID-" + i.ToString() + ".csv");
                    id_list.Add(i);
                    nn_threshold_list.Add(nn_threshold);
                }
                doMultiSim(from, to, max_amount, id_list, true, nn_threshold_list);
                doMultiSim(to, MarketData.Close.Count - 1, max_amount, id_list, true, nn_threshold_list);
            }
            else if (key == "mul sim")
            {
                var num_best_chromo   = 4;
                var id_list           = new List <int>();
                var nn_threshold_list = new List <double>();
                for (int i = 0; i < num_best_chromo; i++)
                {
                    id_list.Add(i);
                    nn_threshold_list.Add(nn_threshold);
                }
                doMultiSim(from, to, max_amount, id_list, true, nn_threshold_list);
            }
            else if (key == "win ga")
            {
                int num_island         = 2;
                int num_chromos        = 4;
                int num_generations    = 20;
                int banned_move_period = 2;
                int num_random_windows = 10;
                index = new int[] { 0, 0, 0, 1, 0, 0, 0 };
                var units         = new int[] { 10, 30, 5, 3 };
                var mutation_rate = 0.5;
                var move_ratio    = 0.2;

                best_island_id = doWinGA(from, to, num_random_windows, num_island, num_chromos, num_generations, banned_move_period, units, mutation_rate, move_ratio, index, display_chart, nn_threshold);
                doWinSim(from, to, best_island_id, true, nn_threshold);
                doWinSim(to, MarketData.Close.Count - 1, best_island_id, true, nn_threshold);
            }
            else if (key == "conti")
            {
                int num_island         = 2;
                int num_chromos        = 4;
                int num_generations    = 20;
                int banned_move_period = 2;
                var units         = new int[] { 67, 5, 5, 5, 5 };
                var mutation_rate = 0.5;
                var move_ratio    = 0.2;
                var sim_period    = 5000;
                var ga_period     = 10000;
                var conti_from    = from;
                var ac_list       = new List <SimAccount>();
                var all_pl_list   = new List <double>();
                all_pl_list.Add(0);
                var all_num_trade = 0;

                while (to > sim_period + ga_period + conti_from)
                {
                    best_island_id = doGA(conti_from, conti_from + ga_period, max_amount, num_island, num_chromos, num_generations, banned_move_period, units, mutation_rate, move_ratio, index, display_chart, nn_threshold);
                    ac_list.Add(doSim(conti_from + ga_period, conti_from + ga_period + sim_period, max_amount, sim_type, best_island_id, true, nn_threshold));
                    foreach (var p in ac_list.Last().total_pl_list)
                    {
                        all_pl_list.Add(all_pl_list.Last() + p);
                    }
                    all_num_trade += ac_list.Last().performance_data.num_trade;
                    conti_from    += sim_period;
                }
                Console.WriteLine("Total pl =" + all_pl_list.Last().ToString() + ", num trade=" + all_num_trade.ToString());
                LineChart.DisplayLineChart(all_pl_list, "from=" + (from + ga_period).ToString() + ", to=" + (conti_from + ga_period + sim_period).ToString() + ", Total pl =" + all_pl_list.Last().ToString() + ", num trade=" + all_num_trade.ToString());
            }
            else if (key == "write")
            {
                MarketData.writeData();
            }

            stopWatch.Stop();
            Console.WriteLine("Completed all processes.");
            Console.WriteLine("Time Elapsed (sec)=" + stopWatch.Elapsed.TotalSeconds.ToString());
        }