Beispiel #1
0
        private void btSetVolumes_Click(object sender, EventArgs e)
        {
            if (Algo.SelectLogFile())
            {
                if (Algo.SelectSettingsFile())
                {
                    Parameters.isVolumeSet = true;
                    TradeInfoCollection trades;
                    string logFilename         = Algo.logFilename;
                    string currentSettingsFile = Algo.currentSettings;
                    string dir;
                    dir = Environment.CurrentDirectory;
                    Algo.CopyStepsAndCountDecimalDigits();
                    Filters OptimizationSettings = Filters.LoadOptimizationSettings("OptimizationSettings.xml");
                    trades = new TradeInfoCollection(logFilename, Parameters, Algo.Steps, OptimizationSettings.ParametersIndexes, OptimizationSettings);

                    int daysCount;
                    daysCount = trades.allDates.Count < Parameters.DaysForSetVolumes ? trades.allDates.Count : Parameters.DaysForSetVolumes;
                    VolumesSetter volume;
                    volume = new VolumesSetter(Parameters);
                    ConcurrentDictionary <string, ConcurrentDictionary <string, int> > Volumes;
                    Volumes = new ConcurrentDictionary <string, ConcurrentDictionary <string, int> >();
                    if (Parameters.Use322)
                    {
                        Volumes = volume.CalculateVolumes322(trades.DaysProfitExpectancy);
                    }
                    if (Parameters.UsePortfolio)
                    {
                        var averagePoints     = volume.CalculateAveragePointsPerDay(trades.DaysPoints, daysCount);
                        var CovariationMatrix = volume.GetCovariationMatrix(trades.DaysPoints, averagePoints, daysCount);
                        Volumes = volume.GetVolumes(CovariationMatrix, averagePoints);
                    }
                    volume.GetXMLwithVolumes(currentSettingsFile, Volumes);
                    MessageBox.Show("Volumes are calculated!");
                }
                else
                {
                    MessageBox.Show("Please select settings filename!");
                }
            }
            else
            {
                MessageBox.Show("Please select log filename!");
            }
        }
Beispiel #2
0
        private void btSetMaxDD_Click(object sender, EventArgs e)
        {
            if (Algo.SelectLogFile())
            {
                if (Algo.SelectSettingsFile())
                {
                    Parameters.isMaxDDSet = true;
                    TradeInfoCollection trades;
                    string logFilename         = Algo.logFilename;
                    string currentSettingsFile = Algo.currentSettings;
                    string dir;
                    dir = Environment.CurrentDirectory;
                    Algo.CopyStepsAndCountDecimalDigits();

                    Filters OptimizationSettings = Filters.LoadOptimizationSettings("OptimizationSettings.xml");
                    trades = new TradeInfoCollection(logFilename, Parameters, Algo.Steps, OptimizationSettings.ParametersIndexes, OptimizationSettings);
                    MaxDDSetter ddset = new MaxDDSetter(Parameters, trades.DealsPoints, currentSettingsFile, trades.DealsPointsGlobal, trades.DealsPointsPercent, trades.totalDealsCount);
                    if (ddset.GetXMLmaxDD())
                    {
                        MessageBox.Show("MaxDD and delays are calculated!");
                    }
                    else
                    {
                        MessageBox.Show("There are some problems with MaxDD and delays calculating!");
                    }
                }
                else
                {
                    MessageBox.Show("Please select settings filename!");
                }
            }
            else
            {
                MessageBox.Show("Please select log filename!");
            }
        }
        public void StartOptimization()
        {
            CopyStepsAndCountDecimalDigits();

            var dir = Environment.CurrentDirectory;

            // load/parse/filter trade data
            trades = new TradeInfoCollection(logFilename, Parameters, Steps, OptimizationSettings.ParametersIndexes, OptimizationSettings);

            var symbols   = new List <string>(trades.Trades.Keys);
            var exchanges = trades.GetArgExchanges();

            Directory.CreateDirectory(dir + "/Recomends");
            Directory.CreateDirectory(dir + "/Recomends/log");

            // число изменяемых параметров
            inflectiveNumber = CalcInflectiveNumber();
            // Calculate initial position
            initialPos = initialPositionOpt(inflectiveNumber);

            Queue <KeyValuePair <SymbolExchangeTradeCount, Thread> > taskQueueFat = new Queue <KeyValuePair <SymbolExchangeTradeCount, Thread> >();
            Queue <KeyValuePair <SymbolExchangeTradeCount, Thread> > taskQueueFit = new Queue <KeyValuePair <SymbolExchangeTradeCount, Thread> >();
            Dictionary <int, ThreadType> threadPool = new Dictionary <int, ThreadType>();

            PercentOfCompletedTasks = 0;

            List <SymbolExchangeTradeCount> symExCounts = new List <SymbolExchangeTradeCount>();

            foreach (KeyValuePair <string, ConcurrentDictionary <string, List <TradeInfo> > > sympair in trades.Trades)
            {
                foreach (KeyValuePair <string, List <TradeInfo> > expair in sympair.Value)
                {
                    symExCounts.Add(new SymbolExchangeTradeCount()
                    {
                        Symbol   = sympair.Key,
                        Symbol_  = sympair.Key.Replace('/', '_'),
                        Exchange = expair.Key,
                        Count    = expair.Value.Count
                    });
                }
            }

            var orderedFeedsForSplit = symExCounts.OrderByDescending(feed => feed.Count).ToList();
            int tradesTotal          = orderedFeedsForSplit.Sum(feed => feed.Count);

            if (Parameters.UseOpenCLDevice)
            {
                var pointer        = 0;
                var currentPercent = 0.0;

                for (int i = 0; i < orderedFeedsForSplit.Count; i++)
                {
                    currentPercent += (orderedFeedsForSplit[i].Count / (float)tradesTotal) * 100;
                    if (currentPercent >= 50.0F)
                    {
                        pointer = i;
                        break;
                    }
                }

                Parameters.MinTradesForUseOpenCL = orderedFeedsForSplit[pointer].Count;
            }

            var orderedFeedsFat = new List <SymbolExchangeTradeCount>();
            var orderedFeedsFit = new List <SymbolExchangeTradeCount>();

            if (Parameters.UseOpenCLDevice)
            {
                var fat = orderedFeedsForSplit.Where(feed => feed.Count >= Parameters.MinTradesForUseOpenCL).ToList();
                var fit = orderedFeedsForSplit.Where(feed => feed.Count < Parameters.MinTradesForUseOpenCL).OrderBy(feed => feed.Count).ToList();
                fat.ForEach(feed => { orderedFeedsFat.Add(feed); feed.IsFatFeed = true; });
                fit.ForEach(feed => { orderedFeedsFit.Add(feed); feed.IsFatFeed = false; });
            }
            else
            {
                orderedFeedsForSplit.ForEach(feed => orderedFeedsFat.Add(feed));
            }

            tradesCompleted = 0;

            foreach (var feed in orderedFeedsFat)
            {
                string symbol_ = feed.Symbol.Replace('/', '_');
                // check for skipping symbol/exchange pair
                if (!FilteringSettings[feed.Symbol, feed.Exchange])
                {
                    continue;
                }

                Thread t = new Thread(new ParameterizedThreadStart(OptimizerStarter));
                t.Priority = ThreadPriority.Highest;
                taskQueueFat.Enqueue(new KeyValuePair <SymbolExchangeTradeCount, Thread>(feed, t));
                taskCount++;
                tasksRest++;
            }

            foreach (var feed in orderedFeedsFit)
            {
                string symbol_ = feed.Symbol.Replace('/', '_');
                // check for skipping symbol/exchange pair
                if (!FilteringSettings[feed.Symbol, feed.Exchange])
                {
                    continue;
                }

                Thread t = new Thread(new ParameterizedThreadStart(OptimizerStarter));
                t.Priority = ThreadPriority.Highest;
                taskQueueFit.Enqueue(new KeyValuePair <SymbolExchangeTradeCount, Thread>(feed, t));
                taskCount++;
                tasksRest++;
            }

            try
            {
                if (Parameters.UseOpenCLDevice)
                {
                    OpenCLService.Device    = Parameters.OpenCLDevice;
                    OpenCLService.Platform  = OpenCLService.Device.Platform;
                    OpenCLService.IsRunning = false;
                    OpenCLService.InitOpenCLService();
                    OpenCLService.Start();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            var index = 0;

            for (int i = 0; i < (Parameters.UseOpenCLDevice ? Parameters.MaxConcurrency / 2 : Parameters.MaxConcurrency); i++)
            {
                if (taskQueueFat.Count > 0)
                {
                    KeyValuePair <SymbolExchangeTradeCount, Thread> pair = taskQueueFat.Dequeue();
                    threadPool.Add(index, new ThreadType()
                    {
                        Thread = pair.Value, Info = pair.Key
                    });
                    pair.Value.Start(pair.Key);
                    index++;
                }
                if (taskQueueFit.Count > 0)
                {
                    KeyValuePair <SymbolExchangeTradeCount, Thread> pair = taskQueueFit.Dequeue();
                    threadPool.Add(index, new ThreadType()
                    {
                        Thread = pair.Value, Info = pair.Key
                    });
                    pair.Value.Start(pair.Key);
                    index++;
                }
            }

            while (threadPool.Count > 0)
            {
                Thread.Sleep(2000);
                try
                {
                    var fatCount = 0;
                    var fitCount = 0;
                    foreach (var feed in threadPool)
                    {
                        fatCount += feed.Value.Info.IsFatFeed ? 1 : 0;
                        fitCount += feed.Value.Info.IsFatFeed ? 0 : 1;
                    }

                    var addFit = fatCount >= fitCount ? true : false;

                    for (int i = 0; i < Parameters.MaxConcurrency; i++)
                    {
                        if (i >= threadPool.Count)
                        {
                            break;
                        }
                        int    key       = threadPool.Keys.ToArray()[i];
                        Thread curThread = threadPool[key].Thread;
                        if (curThread == null)
                        {
                            continue;
                        }
                        if (curThread.ThreadState == System.Threading.ThreadState.Stopped)
                        {
                            MainForm.progressBar.Invoke(new Action(() =>
                            {
                                PercentOfCompletedTasks     = (int)(((double)(tradesCompleted) / (double)tradesTotal) * 100);
                                MainForm.progressBar.Value  = PercentOfCompletedTasks;
                                MainForm.EsitmatedTime      = new TimeSpan((long)(((double)MainForm.ElapsedTime.Ticks / (double)(tradesCompleted)) * (double)(tradesTotal - tradesCompleted)));
                                MainForm.ProjectedTotalTime = MainForm.EsitmatedTime + MainForm.ElapsedTime;
                                Application.DoEvents();
                            }));

                            Queue <KeyValuePair <SymbolExchangeTradeCount, Thread> > taskQueue;
                            taskQueue = addFit ? taskQueueFit : taskQueueFat;

                            if (taskQueue.Count > 0)
                            {
                                KeyValuePair <SymbolExchangeTradeCount, Thread> pair = taskQueue.Dequeue();
                                threadPool[key] = new ThreadType()
                                {
                                    Thread = pair.Value, Info = pair.Key
                                };
                                threadPool[key].Thread.Start(pair.Key);
                            }
                            else if (taskQueueFat.Count == 0 && taskQueueFit.Count == 0)
                            {
                                threadPool.Remove(key);
                                break;
                            }
                            else
                            {
                                taskQueue = addFit ? taskQueueFat : taskQueueFit;
                                KeyValuePair <SymbolExchangeTradeCount, Thread> pair = taskQueue.Dequeue();
                                threadPool[key] = new ThreadType()
                                {
                                    Thread = pair.Value, Info = pair.Key
                                };
                                threadPool[key].Thread.Start(pair.Key);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            if (Parameters.UseOpenCLDevice)
            {
                OpenCLService.Stop();
            }

            try
            {
                XMLcopyDefTrivial(symbols, exchanges, currentSettings);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            MainForm.Invoke(new Action(() =>
            {
                MainForm.UnlockControls();
                MainForm.timer.Stop();
                MainForm.lbEstimatedTimeValue.Text = "00:00:00";
                MainForm.progressBar.Value         = 100;
                MessageBox.Show("Optimization completed!");
            }));
        }