Пример #1
0
        //複数chromを使ったsimを行い、それらの結果の総合したパフォーマンスを表示する。
        public SimAccount sim_ga_multi_chromo(int from, int to, int max_amount, List <Gene2> chromo, string title, bool chart, List <double> nn_threshold)
        {
            var ac_list = new List <SimAccount>();

            for (int i = 0; i < chromo.Count; i++)
            {
                var sim = new Sim();
                var ac  = new SimAccount();
                ac = sim.sim_ga_market_limit(from, to, max_amount, chromo[i], ac, nn_threshold[i], false);
                ac_list.Add(ac);
                Console.WriteLine("Chromo-" + i.ToString() + ":");
                Console.WriteLine("pl=" + ac.performance_data.total_pl);
                Console.WriteLine("num trade=" + ac.performance_data.num_trade);
                Console.WriteLine("num market order=" + ac.performance_data.num_maker_order);
                Console.WriteLine("win rate=" + ac.performance_data.win_rate);
                Console.WriteLine("sharp_ratio=" + ac.performance_data.sharp_ratio);
                Console.WriteLine("num_buy=" + ac.performance_data.num_buy);
                Console.WriteLine("num_sell=" + ac.performance_data.num_sell);
                Console.WriteLine("buy_pl=" + ac.performance_data.buy_pl_list.Sum());
                Console.WriteLine("sell_pl=" + ac.performance_data.sell_pl_list.Sum());
            }
            //各chrom sim結果を平均する。
            var ac_master   = new SimAccount();
            var buy_pl_sum  = 0.0;
            var sell_pl_sum = 0.0;

            for (int i = 0; i < ac_list[0].total_pl_list.Count; i++)
            {
                ac_master.total_pl_list.Add(ac_list[0].total_pl_list[i]);
                ac_master.total_pl_ratio_list.Add(ac_list[0].total_pl_ratio_list[i]);
            }
            for (int i = 0; i < chromo.Count; i++)
            {
                ac_master.performance_data.num_trade      += ac_list[i].performance_data.num_trade;
                ac_master.performance_data.num_buy        += ac_list[i].performance_data.num_buy;
                ac_master.performance_data.num_sell       += ac_list[i].performance_data.num_sell;
                ac_master.performance_data.num_win        += ac_list[i].performance_data.num_win;
                ac_master.performance_data.total_pl       += ac_list[i].performance_data.total_pl;
                ac_master.performance_data.total_pl_ratio += ac_list[i].performance_data.total_pl_ratio;
                buy_pl_sum  += ac_list[i].performance_data.buy_pl_list.Sum();
                sell_pl_sum += ac_list[i].performance_data.sell_pl_list.Sum();
                for (int j = 0; j < ac_list[i].total_pl_list.Count; j++)
                {
                    if (i > 0)
                    {
                        ac_master.total_pl_list[j]       += ac_list[i].total_pl_list[j];
                        ac_master.total_pl_ratio_list[j] += ac_list[i].total_pl_ratio_list[j];
                    }
                }
            }
            for (int i = 0; i < ac_list[0].total_pl_list.Count; i++)
            {
                ac_master.total_pl_list[i]       = ac_master.total_pl_list[i] / Convert.ToDouble(ac_list.Count);
                ac_master.total_pl_ratio_list[i] = ac_master.total_pl_ratio_list[i] / Convert.ToDouble(ac_list.Count);
            }
            Console.WriteLine("");
            Console.WriteLine("Master Results:");
            Console.WriteLine("pl=" + ac_master.performance_data.total_pl / Convert.ToDouble(ac_list.Count));
            Console.WriteLine("num trade=" + ac_master.performance_data.num_trade / Convert.ToDouble(ac_list.Count));
            Console.WriteLine("num market order=" + ac_master.performance_data.num_maker_order);
            if (ac_master.performance_data.num_trade > 0)
            {
                Console.WriteLine("win rate=" + ac_master.performance_data.num_win / ac_master.performance_data.num_trade);
            }
            else
            {
                Console.WriteLine("win rate=" + "0");
            }
            Console.WriteLine("num_buy=" + ac_master.performance_data.num_buy);
            Console.WriteLine("num_sell=" + ac_master.performance_data.num_sell);
            Console.WriteLine("buy_pl=" + buy_pl_sum);
            Console.WriteLine("sell_pl=" + sell_pl_sum);
            if (chart)
            {
                LineChart.DisplayLineChart(ac_master.total_pl_ratio_list, title);
            }
            return(ac_master);
        }
Пример #2
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("\"win sim\" : do win sim");
                Console.WriteLine("\"sa1\" : statistics analysis1");
                Console.WriteLine("\"sa2\" : statistics analysis2");
                Console.WriteLine("\"sa3\" : statistics analysis3");
                Console.WriteLine("\"ptlc sim\" : ptlc periodical entry sim");
                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 == "win sim" || key == "write" || key == "test" || key == "sa1" || key == "sa2" || key == "sa3" || key == "ptlc sim")
                {
                    break;
                }
            }


            Stopwatch stopWatch = new Stopwatch();

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

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

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


            if (key == "test")
            {
                var ac = doSim(from, to, max_amount, sim_type, best_island_id, display_chart, nn_threshold);
                LineChart.DisplayLineChart2(ac.total_pl_list, ac.log_data.close_log, ac.log_data.buy_points.Values.ToList(), ac.log_data.sell_points.Values.ToList(), "test");
                System.Diagnostics.Process.Start(@"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", @"./line_chart.html");
            }


            //read weight sim
            if (key == "sim")
            {
                var ac = doSim(from, to, max_amount, sim_type, best_island_id, display_chart, nn_threshold);
                LineChart.DisplayLineChart2(ac.total_pl_list, ac.log_data.close_log, ac.log_data.buy_points.Values.ToList(), ac.log_data.sell_points.Values.ToList(), "test");
                System.Diagnostics.Process.Start(@"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", @"./line_chart.html");
            }
            //island ga
            else if (key == "ga")
            {
                int num_island         = 1;
                int num_chromos        = 4;
                int num_generations    = 100;
                int banned_move_period = 5;
                var units         = new int[] { 42, 90, 5 };
                var mutation_rate = 0.7;
                var move_ratio    = 0.5;
                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[] { 1, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 1, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 1, 0, 0, 0, 0 }
                };
                var units_list = new List <int[]> {
                    new int[] { 34, 10, 5 }, new int[] { 34, 10, 5 }, new int[] { 34, 10, 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    = 30;
                int banned_move_period = 3;
                var mutation_rate      = 0.7;
                var move_ratio         = 0.5;
                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   = 3;
                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    = 200;
                int banned_move_period = 5;
                int num_random_windows = 10;
                index = new int[] { 0, 0, 0, 1, 0, 0, 0 };
                var units         = new int[] { 10, 30, 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 == "win sim")
            {
                doWinSim(from, to, best_island_id, true, nn_threshold);
            }
            else if (key == "sa1")
            {
                var sa = new StatisticsAnalysis();
                var data_entry_points = Enumerable.Range(0, MarketData.Close.Count).Where(i => i % 1000 == 0).ToList();
                //var entry_num = Enumerable.Range(1, 100).Where(i => i % 5 == 0).ToList();
                var entry_num = new List <int>()
                {
                    1, 5, 50, 100
                };
                //var entry_interval = Enumerable.Range(1, 100).Where(i => i % 5 == 0).ToList();
                var entry_interval = new List <int>()
                {
                    1, 5, 50, 100
                };
                //var ptlc_ratio = Enumerable.Range(2, 100).ToList().ConvertAll(x => Math.Round(x * 0.1,1)).ToList();
                var ptlc_ratio = new List <double>()
                {
                    0.2, 1, 5, 15
                };
                sa.startAnalysis1(data_entry_points, entry_num, entry_interval, entry_interval, ptlc_ratio);
            }
            else if (key == "sa2")
            {
                var sa = new StatisticsAnalysis();

                /*var leverages = new List<double>() { 0.5,1,1.5,2,2.5,3,5};
                 * var entry_num = new List<int>() { 1, 5, 50, 100 };
                 * var entry_interval = new List<int>() { 1, 5, 50, 100 };
                 * var pt_ratio = new List<double>() { 0.05, 0.07, 0.1, 0.15, 0.2, 0.25 };
                 * var lc_ratio = new List<double>() { -0.05, -0.07, -0.1, -0.15, -0.2, -0.25 };*/
                var leverages = new List <double>()
                {
                    0.5, 5
                };
                var entry_num = new List <int>()
                {
                    1, 5
                };
                var entry_interval = new List <int>()
                {
                    1, 5
                };
                var pt_ratio = new List <double>()
                {
                    0.05, 0.15
                };
                var lc_ratio = new List <double>()
                {
                    -0.05, -0.15
                };
                sa.startAnalysis2(pt_ratio, lc_ratio, leverages, entry_interval, entry_num);
            }
            else if (key == "sa3")
            {
                var sa = new StatisticsAnalysis();
                var buy_price_change_minutes = new List <int>()
                {
                    10, 30, 60, 90, 300, 600
                };
                var buy_price_change_ratio = new List <double>()
                {
                    -0.01, -0.05, -0.07, -0.1, -0.15, -0.25
                };
                var sell_price_change_minutes = new List <int>()
                {
                    10, 30, 60, 90, 300, 600
                };
                var sell_price_change_ratio = new List <double>()
                {
                    0.01, 0.05, 0.07, 0.1, 0.15, 0.25
                };
                var param_data = new List <Dictionary <string, double> >();
                var param1     = new Dictionary <string, double>()
                {
                    { "leverage", 3.0 }, { "entry_num", 100 }, { "entry_interval", 5 }, { "pt", 0.25 }, { "lc", -0.2 }
                };
                var param2 = new Dictionary <string, double>()
                {
                    { "leverage", 2.0 }, { "entry_num", 50 }, { "entry_interval", 5 }, { "pt", 0.07 }, { "lc", -0.2 }
                };
                var param3 = new Dictionary <string, double>()
                {
                    { "leverage", 2.0 }, { "entry_num", 5 }, { "entry_interval", 5 }, { "pt", 0.15 }, { "lc", -0.2 }
                };
                param_data.Add(param1);
                param_data.Add(param2);
                param_data.Add(param3);
                sa.startAnalysis3(param_data, buy_price_change_minutes, buy_price_change_ratio, sell_price_change_minutes, sell_price_change_ratio);
            }
            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 == "ptlc sim")
            {
                var pt       = 0.07;
                var lc       = -0.2;
                var leverage = 2;
                var side     = "buy";
                var entry_interval_minutes = 5;
                var entry_num = 50;
                var ac        = new SimAccount();
                var sim       = new Sim();
                ac = sim.sim_entry_timing_ptlc(from, to, ac, side, leverage, entry_interval_minutes, entry_num, pt, lc);
                displaySimResult(ac, "ptlc sim from:" + from.ToString() + " to:" + to.ToString() + ", pl=" + ac.performance_data.total_pl.ToString() + ", num=" + ac.performance_data.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());
        }
Пример #3
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());
        }