Exemplo n.º 1
0
        public void Calculate()
        {
            var fName = string.Format("{0}_{1}", typeof(T).Name, Settings.Default.ResultsFileName);

            if (File.Exists(fName))
            {
                File.Delete(fName);
            }
            MemoryCache <FinamCandleProvider> cache;

            using (var provider = new FinamCandleProvider(Settings.Default.InsName, Settings.Default.TimePeriod,
                                                          Settings.Default.MarketCode, Settings.Default.InsCode, Settings.Default.DateFrom, Settings.Default.DateTo))
            {
                cache = new MemoryCache <FinamCandleProvider>(provider);
            }
            File.AppendAllLines(fName, new string[] { "Parameters;Populations total;Sharp Index;Profit;Mean profit per deal;Mean +profit per deal;Mean -profit per deal;Success %;" });
            for (var i = Settings.Default.ParamsCountFrom; i < Settings.Default.ParamsCountTo; i = i + 2)
            {
                _num          = i;
                Console.Title = string.Format("Length {0}", _num);
                Console.WriteLine();
                Console.WriteLine(" Length {0}:", i);
                Console.WriteLine(" ----------------------------------------------------------------");
                Console.WriteLine(" | iter | profit | sharp | mean p | success | mean p+ | mean p- |");
                Console.WriteLine(" ----------------------------------------------------------------");
                var genSelector = new GeneticsSelector <FinamCandleProvider, T>(cache, 3, 100, i, generationSize: Settings.Default.GenerationSize, threadsNum: Settings.Default.ThreadsCount, crossPercent: Settings.Default.CrossPercent);
                genSelector.PopulationCompleted += GenSelector_PopulationCompleted;
                genSelector.Select(Settings.Default.PopulationsCount);
                genSelector.Wait();
                var result = genSelector.GetBestResults().First();
                File.AppendAllLines(fName,
                                    new string[]
                {
                    string.Format("{0};{1};{2};{3};{4};{5};{6};{7};",
                                  string.Join(",", result.Parameters),
                                  genSelector.PopulationIndex,
                                  result.SharpIndex,
                                  result.Balance,
                                  result.MeanProfit,
                                  result.MeanPositiveProfit,
                                  result.MeanNegativeProfit,
                                  result.SuccessRatio)
                });
                // generate result file for best result
                var algDataFileName = string.Format("{0}_{1}params_{2}",
                                                    typeof(T).Name, i, Settings.Default.ResultsFileName);
                AlgDataExport.ExportToCSV(result, algDataFileName, typeof(T).Name);
            }

            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Calculation completed.");
            Console.ReadLine();
            _num = 0;
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            List <Type> algTypes   = new List <Type>();
            var         searchPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            foreach (var file in Directory.GetFiles(searchPath, "*.dll"))
            {
                algTypes.AddRange(Assembly.LoadFrom(file).GetTypes()
                                  .Where(o =>
                                         o.BaseType == typeof(AlgorithmBase) &&
                                         o.IsDefined(typeof(AlgorithmAttribute))));
            }

            Console.WriteLine("=== Found {0} algorithms ===", algTypes.Count());
            for (var i = 1; i <= algTypes.Count(); i++)
            {
                Console.WriteLine(" {0}) {1}", i, algTypes[i - 1].Name);
            }
            Console.WriteLine();
            Console.Write(" Select algorithm: ");
            int num;

            int.TryParse(Console.ReadLine(), out num);

            if (num < 1 || num > algTypes.Count())
            {
                Console.WriteLine("Incorrect choice.");
                Console.ReadLine();
                return;
            }

            if (!args.Any() || (args[0] == "--genetics"))
            {
                var calc = (IRealTimeCalculator)Activator.CreateInstance(typeof(RealTimeCalculator <>)
                                                                         .MakeGenericType(algTypes[num - 1]));
                calc.Calculate();
            }
            if (args[0] == "--calculate")
            {
                if (args.Length <= 1)
                {
                    return;
                }
                using (var provider = new FinamCandleProvider(Settings.Default.InsName, Settings.Default.TimePeriod,
                                                              Settings.Default.MarketCode, Settings.Default.InsCode, Settings.Default.DateFrom, Settings.Default.DateTo))
                {
                    var calc = (ICalculator <FinamCandleProvider>)Activator.CreateInstance(typeof(Calculator <,>)
                                                                                           .MakeGenericType(typeof(FinamCandleProvider), algTypes[num - 1]));
                    calc.MemCache = new MemoryCache <FinamCandleProvider>(provider);

                    foreach (var arg in args.Skip(1))
                    {
                        if (!arg.StartsWith("--p="))
                        {
                            return;
                        }
                        var splits = arg.Split('=').Last().Split(',');
                        var prms   = new float[splits.Length];
                        for (var i = 0; i < splits.Length; i++)
                        {
                            float p;
                            if (!float.TryParse(splits[i], out p))
                            {
                                return;
                            }
                            prms[i] = p;
                        }
                        calc.AddParamsForCalculation(prms);
                    }
                    calc.CalculateAsync();
                    calc.Wait();
                    var n = 0;
                    foreach (var r in calc.Results)
                    {
                        n++;
                        AlgDataExport.ExportToCSV(r, string.Format("{0}_{1}",
                                                                   n, Settings.Default.ResultsFileName), algTypes[num - 1].Name);
                    }
                }
            }
        }