Ejemplo n.º 1
0
        public static void CopyFiles(Hyperparameters h, string source, string destination)
        {
            if (destination[destination.Length - 1] != '\\')
            {
                destination += destination + '\\';
            }
            Directory.CreateDirectory(destination);
            if (source.Replace("\\\\", "\\") != destination.Replace("\\\\", "\\"))
            {
                foreach (string file in Directory.GetFiles(source))
                {
                    if (Path.GetFileName(file) != "h.json")
                    {
                        File.Copy(file, destination + Path.GetFileName(file), true);
                    }
                }
            }

            //указание пути сохранения в параметрах
            h.setValueByName("save_folder", destination);
            string jsonFilePath = destination + "h.json";

            h.setValueByName("json_file_path", jsonFilePath);
            string predictionsFilePath = destination + "predictions.txt";

            h.setValueByName("predictions_file_path", predictionsFilePath);
            File.WriteAllText(jsonFilePath, h.toJSON(0), System.Text.Encoding.Default);
        }
 double searchInExplored(Hyperparameters h)
 {
     for (int tfp = 0; tfp < explored.Count; tfp++)
     {
         bool coincidence = true;
         for (int i = 0; i < h.nodes.Count; i++)
         {
             if (h.nodes[i].getAttributeValue("variable") == "numerical")
             {
                 var a = explored[tfp].h.nodes[i].getValue();
                 var b = h.nodes[i].getValue();
                 if (explored[tfp].h.nodes[i].getValue() != h.nodes[i].getValue())
                 {
                     coincidence = false;
                     break;
                 }
             }
         }
         if (coincidence)
         {
             return(explored[tfp].Q);
         }
     }
     return(-1);
 }
Ejemplo n.º 3
0
        public Hyperparameters Copy(Hyperparameters individ, string source, string destination)
        {
            Hyperparameters H = individ.Clone();

            var algorithmBranches = H.getNodesByparentID(H.getNodeByName("committee")[0].ID);

            List <Hyperparameters> hs = new List <Hyperparameters>();

            for (int j = 0; j < algorithmBranches.Count; j++)
            {
                var h = new Hyperparameters(H.toJSON(algorithmBranches[j].ID), form1);
                var new_save_folder = destination + "\\" + h.getValueByName("model_name") + "\\";
                //новые пути прописываются в h.json автоматически, если передать объект Hyperparameters по ссылке, а не по значению
                Algorithm.CopyFiles(h, h.getValueByName("save_folder"), new_save_folder);
                hs.Add(h);
            }

            //удаление старых записей
            for (int i = 0; i < algorithmBranches.Count; i++)
            {
                H.deleteBranch(algorithmBranches[i].ID);
            }

            //приращение новых записей к узлу  "committee"
            for (int i = 0; i < algorithmBranches.Count; i++)
            {
                H.addBranch(hs[i], hs[i].nodes[0].name(), H.getNodeByName("committee")[0].ID);
            }

            H.setValueByName("report_path", destination);
            H = H.Clone();
            File.WriteAllText(destination + "\\h.json", H.toJSON(0), System.Text.Encoding.Default);

            return(H);
        }
Ejemplo n.º 4
0
        private void recurciveVariableAdding(Hyperparameters h, int ID, string expertName)
        {
            List <Node> branches = h.getNodesByparentID(ID);

            if (branches.Count == 0)
            {
                if (h.getNodeById(ID).getAttributeValue("variable") != null)
                {
                    // добавить в список переменных
                    if (h.getNodeById(ID).getAttributeValue("variable") == "numerical")
                    {
                        variablesVisualizer.addParameter(expertName + " " + h.getNodeById(ID).name() + " id=" + ID.ToString(), Color.Cyan, 200);
                    }

                    if (h.getNodeById(ID).getAttributeValue("variable") == "categorical")
                    {
                        variablesVisualizer.addParameter(expertName + " " + h.getNodeById(ID).name() + " id=" + ID.ToString(), Color.Transparent, 60);
                        variablesVisualizer.parameters[variablesVisualizer.parameters.Count - 1].mainFontDepth = 12;
                    }
                    variablesNames.Add(expertName + " " + h.getNodeById(ID).name() + " id=" + ID.ToString());
                    variablesIDs.Add(ID);
                }
            }
            else
            {
                for (int i = 0; i < branches.Count; i++)
                {
                    recurciveVariableAdding(h, branches[i].ID, expertName);
                }
            }
        }
Ejemplo n.º 5
0
 private void refreshEOTree()
 {
     E = new Hyperparameters(form1, "Expert_Population");
     for (int i = 0; i < population_value; i++)
     {
         E.addBranch(population[i], population[i].nodes[0].name(), 0);
     }
 }
        public MyAlgorithmOptimization(MainForm form1, Algorithm algorithm)
        {
            this.algorithm      = algorithm;
            explored            = new List <TargetFunctionPoint>();
            this.form1          = form1;
            variablesVisualizer = new MultiParameterVisualizer(form1.picBox, form1);
            variablesVisualizer.addParameter("max Q", Color.Cyan, 400);
            variablesVisualizer.addParameter("target_function", Color.LightCyan, 800);



            //VVVVVVVVVVV



            //    1)разобраться с косяками на графике всех индивидов

            //    2)почему при нахождении центра получаются близкие, но разные числа

            //    3)добиться одинакового числа точек в currentHypercube связано с (1)

            //    4) прикрутить красивые графики везде, где уместно


            // -параллелизм
            //for (int i = 0; i < population_value; i++)
            //     variablesVisualizer.parameters[1].functions.Add(new Function(" [" + i.ToString() + "]", valueToColor(0, population_value, population_value - i - 1)));

            report_file_name = form1.pathPrefix + "/MyAlgorithmOptimization/Optimization report " + DateTime.Now.ToString().Replace(':', '-') + ".txt";

            P = algorithm.h.Clone();
            string reportHeader = "";

            for (int i = 0; i < P.nodes.Count; i++)
            {
                if (P.nodes[i].getAttributeValue("variable") == "numerical")
                {
                    reportHeader += P.nodes[i].getAttributeValue("name") + ';';
                    variablesCount++;
                }
            }

            variablesVisualizer.enableGrid = false;

            int fucnCount = Convert.ToInt32(Math.Pow(3.0, Convert.ToDouble(variablesCount)));

            for (int i = 0; i < fucnCount; i++)
            {
                variablesVisualizer.parameters[1].functions.Add(new Function(" [" + i.ToString() + "]", ParameterVisualizer.valueToColor(0, fucnCount, fucnCount - i - 1)));
            }

            File.WriteAllText(report_file_name, reportHeader + "Q;" + '\n');

            initPool();
            variablesVisualizer.refresh();
        }
Ejemplo n.º 7
0
        public Algorithm(MainForm form1, string modelName)
        {
            this.modelName = modelName;
            h = new Hyperparameters(form1, modelName);
            h.add("model_name", modelName);
            h.add("state", "created");
            h.add("parents", "создан в " + this.GetType().ToString());
            NNscructNodeId = h.add("name:NN_struct");

            this.form1 = form1;
        }
Ejemplo n.º 8
0
        public static void MoveFiles(Hyperparameters h, string source, string destination)
        {
            if (destination[destination.Length - 1] != '\\')
            {
                destination += '\\';
            }
            Directory.CreateDirectory(destination);
            if (source != destination)
            {
                foreach (string file in Directory.GetFiles(destination))
                {
                    File.Delete(file);
                }
                foreach (string file in Directory.GetFiles(source))
                {
repeat1:
                    try
                    {
                        if (Path.GetFileName(file) != "h.json")
                        {
                            File.Copy(file, destination + Path.GetFileName(file));
                        }
                        else
                        {
                            File.Delete(file);
                        }
                    }
                    catch
                    {
                        throw;
                        goto repeat1;
                    }
                }
            }
            else
            {
            }
            //указание пути сохранения в параметрах
            h.setValueByName("save_folder", destination);
            string jsonFilePath = destination + "h.json";

            h.setValueByName("json_file_path", jsonFilePath);
            string predictionsFilePath = destination + "predictions.txt";

            h.setValueByName("predictions_file_path", predictionsFilePath);
            File.WriteAllText(jsonFilePath, h.toJSON(0), System.Text.Encoding.Default);
        }
Ejemplo n.º 9
0
        private Hyperparameters get_child(Hyperparameters parent1, Hyperparameters parent2, Hyperparameters old)
        {
            Hyperparameters child = old.Clone();

            foreach (int variableID in variablesIDs)
            {
                //родитель гена выбирается случайно
                int parent_of_gene = r.Next(0, 2);
                if (parent_of_gene == 0)
                {
                    child.nodes[variableID].setAttribute("value", parent1.nodes[variableID].getValue());
                }
                else
                {
                    child.nodes[variableID].setAttribute("value", parent2.nodes[variableID].getValue());
                }
            }
            return(child);
        }
Ejemplo n.º 10
0
        public void addBranch(Hyperparameters branch, string branchName, int parentId)
        {
            int lastNodeId = newNodeIdWillBe;

            for (int i = 0; i < branch.nodes.Count; i++)
            {
                Node newNode = branch.nodes[i].Clone();
                if (newNode.parentID == -1)
                {
                    newNode.parentID = parentId;
                    newNode.setAttribute("name", branchName);
                }
                else
                {
                    newNode.parentID = lastNodeId + newNode.parentID;
                }

                newNode.ID = lastNodeId + newNode.ID;
                nodes.Add(newNode);

                newNodeIdWillBe++;
            }
        }
Ejemplo n.º 11
0
        internal void exmoAsIndicator()
        {
            log("вход в exmoAsIndicator()");
            vis.addParameter("USD_RUB", Color.Lime, 900);
            vis.parameters[0].functions.Add(new Function("AVG1", Color.Green));
            vis.parameters[0].functions.Add(new Function("AVG2", Color.Pink));
            vis.addParameter("Z", Color.Cyan, 200);
            vis.addParameter("OUTPUT", Color.Lime, 200);

            vis.parameters[0].showLastNValues = true;
            vis.enableGrid           = false;
            vis.parameters[0].window = 2000;


            mainThread = System.Threading.Thread.CurrentThread;
            Hyperparameters order_book;

            log("Подключение к ExmoApi...");
            var    api    = new ExmoApi("k", "s");
            string result = "";
            string s      = "";

            double        AVG1    = 0;
            double        AVG2    = 0;
            double        sum     = 0;
            List <double> history = new List <double>();

            string[] dataBase = null;

            DateTime lastTime = new DateTime(1, 1, 1, 0, 0, 0);

            if (tester)
            {
                dataBase = File.ReadAllLines("USD_RUB_exmo.txt");
                DateTime.TryParse(dataBase[1].Split(';')[0], out lastTime);
            }


            Invoke(new Action(() =>
            {
                TrackBar5_Scroll(null, null);
                TrackBar6_Scroll(null, null);
                TrackBar7_Scroll(null, null);
            }));



            log("Попытка запустить сервер...");
            MetaTraderLink mtLink = new MetaTraderLink(this);

            log("Сервер запущен.");
            bool parse;

            if (tester)
            {
                while (mtLink.actualTime == new DateTime(1, 1, 1, 0, 0, 0))
                {
                    log("ожидание подключения metatrader...");
                    Thread.Sleep(1000);
                }
            }


            int screenshotIterationTimer = 3600;
            int inc1 = 0;

            while (true)
            {
                try
                {
                    double bid = 0;
                    if (tester)
                    {
                        for (int i = 1; i < dataBase.Length; i++)
                        {
                            DateTime dt;
                            parse = DateTime.TryParse(dataBase[i].Split(';')[0], out dt);
                            if (parse)
                            {
                                if (mtLink.actualTime == dt)
                                {
                                    bid = Convert.ToDouble(dataBase[i].Split(';')[1].Replace('.', ','));

                                    if (mtLink.actualTime != lastTime.AddSeconds(1))
                                    {
                                        log("Актуальное время не равно предыдущему плюс 1 сек.", Color.Red);
                                        log("> mtLink.actualTime: " + mtLink.actualTime.ToString(), Color.Red);
                                        log("> lastTime: " + lastTime.ToString(), Color.Red);
                                    }
                                    lastTime = mtLink.actualTime;
                                }
                            }
                        }
                        s = mtLink.actualTime.ToString() + ";  " + bid.ToString();
                        log(s);
                    }
                    else
                    {
                        result = api.ApiQuery("order_book", new Dictionary <string, string> {
                            { "pair", "USD_RUB" }
                        });
                        order_book = new Hyperparameters(result, this);

                        s = DateTime.Now.ToString() + ';' + order_book.nodes[0].getAttributeValue("bid_top") + ';' + order_book.nodes[0].getAttributeValue("ask_top");

                        bid = Convert.ToDouble(order_book.nodes[0].getAttributeValue("bid_top").Replace('.', ','));
                        //   double ask = Convert.ToDouble(order_book.nodes[0].getAttributeValue("ask_top").Replace('.', ','));

                        File.AppendAllText("USD_RUB_exmo.txt", s + '\n');
                        log(s);
                    }



                    if (history.Count >= (Nl + Nh))
                    {
                        sum = 0;
                        for (int i = 0; i < Nl; i++)
                        {
                            sum += history[i];
                        }
                        AVG1 = sum / Nl;
                        sum  = 0;
                        for (int i = 0; i < Nh; i++)
                        {
                            sum += history[Nl + i];
                        }
                        AVG2 = sum / Nh;

                        double Zi = AVG2 - AVG1;

                        vis.addPoint(AVG1, "AVG1");
                        vis.addPoint(AVG2, "AVG2");
                        if (Zi >= Z)
                        {
                            vis.parameters[0].functions[0].points[vis.parameters[0].functions[0].points.Count - 1].mark = "buy";
                            vis.addPoint(1, "OUTPUT");
                            // ОТПРАВИТЬ КОМАНДУ НА ПОКУПКУ
                            if (mtLink.handler != null)
                            {
                                mtLink.ACTION = "buy";
                            }
                        }
                        else
                        {
                            vis.addPoint(0, "OUTPUT");
                        }
                        vis.addPoint(Zi, "Z");
                        history.RemoveAt(0);
                    }
                    else
                    {
                        vis.addPoint(0, "OUTPUT");
                        vis.addPoint(0, "Z");
                        vis.addPoint(bid, "AVG1");
                        vis.addPoint(bid, "AVG2");
                    }

                    history.Add(bid);
                    vis.addPoint(bid, "USD_RUB");
                    vis.refresh();


                    if (inc1 == screenshotIterationTimer)
                    {
                        inc1 = 0;
                        Bitmap screenShot = (Bitmap)picBox.Image.Clone();
                        int    h          = screenShot.Height;
                        if (screenShot.Height > 3000)
                        {
                            h = 3000;
                        }
                        for (int i = 0; i < screenShot.Width; i++)
                        {
                            for (int j = 0; j < h; j++)
                            {
                                Color c = screenShot.GetPixel(i, j);
                                if (c == Color.FromArgb(0, 0, 0, 0))
                                {
                                    screenShot.SetPixel(i, j, Color.Black);
                                }
                            }
                        }
                        screenShot.Save(DateTime.Now.ToString().Replace(':', '-') + ".bmp");
                    }
                    inc1++;
                }
                catch (Exception e)
                {
                    log(e.Message, Color.Red);
                }
                Thread.Sleep(1000);
            }
        }
Ejemplo n.º 12
0
        public Infrastructure(MainForm form1)
        {
            this.form1 = form1;
            h          = new Hyperparameters(form1, "Infrastructure");

            form1.logBox.Text += (Environment.MachineName);
            if (Environment.MachineName == "DESKTOP-B3G20T0")
            {
                form1.logBox.Font = new System.Drawing.Font(form1.logBox.Font.FontFamily, 8);
            }
            form1.collectLogWhileItFreezed = new List <logItem>();

            /////////    чтене файла конфигурации    ///////////////////////
            var configLines = File.ReadAllLines("CONFIG.txt");

            bool is_newPC = true;

            for (int i = 0; i < configLines.Length; i++)
            {
                //параметры конфигурации начинаются со строки содержащей имя компа
                if (configLines[i].Contains(Environment.MachineName))
                {
                    is_newPC = false;
                    for (int j = i + 1; j < configLines.Length; j++)
                    {
                        //параметры конфигурации заканчиваются, когда встречается пустая строка
                        if (configLines[j] != "")
                        {
                            try
                            {
                                h.add(configLines[j]);
                            }
                            catch { }
                        }
                        else
                        {
                            break;
                        }
                    }
                    break;
                }
            }
            ////////////////////////////////////////////////////////

            if (is_newPC | h.getValueByName("mode") == null)
            {
                showModeSelector();
            }
            else
            {
                newLog();
            }
            // ИНИЦИАЛИЗАЦИЯ ГЛАВНОЙ ФОРМЫ



            DpiFix();
            form1.WindowState = FormWindowState.Minimized;

            if (logPath != null)
            {
                form1.vis = new MultiParameterVisualizer(form1.picBox, form1);

                mode = h.getValueByName("mode");
                if (mode != "Агент")
                {
                    startAgentManager();
                    form1.log("");
                    //  agentManagerView = new AgentManagerView(agentManager);
                    //   agentManagerView.Show();
                }
                form1.pathPrefix = h.getValueByName("path_prefix");

                form1.log("");
                form1.WindowState = FormWindowState.Maximized;
            }
            else
            {
            }
        }
Ejemplo n.º 13
0
        public void train()
        {
            trainingResponse = "";

            h.setValueByName("state", "обучение..");

            if (h.getValueByName("input_file") == null)
            {
                log("файл датасета не задан");
            }
            File.WriteAllText(h.getValueByName("json_file_path"), h.toJSON(0), System.Text.Encoding.Default);
            args = "--json_file_path " + '"' + h.getValueByName("json_file_path") + '"';

            trainingResponse = Task.Run(() => form1.I.executePythonScript(getValueByName("train_script_path"), args)).Result;

            try
            {
                trainingResponse = trainingResponse.Substring(trainingResponse.IndexOf('{'));
                Hyperparameters responseH = new Hyperparameters(trainingResponse, form1);
                //   var avg = responseH.getValueByName("AVG");
                //   if (avg != null)
                //       h.setValueByName("AVG", avg);

                h.setValueByName("state", "обучение завершено");
                log(responseH.getValueByName("response"));

                string[] predictionsCSV = null;
                //попытка прочитать данные из файла, полученного из скрипта
                try
                {
                    predictionsCSV = File.ReadAllLines(h.getValueByName("predictions_file_path"));
                }
                catch { }
                //если данные имеются, то определить показатели точности прогнозирования
                if (predictionsCSV != null)
                {
                    try
                    {
                        getAccAndStdDev(predictionsCSV);
                    }
                    catch
                    {
                        log("Не удалось прочитать файл с тестовым прогнозом", Color.Red);
                        h.setValueByName("state", "Не удалось прочитать файл с тестовым прогнозом");
                    }
                }
                else
                {
                    log("Не удалось прочитать файл с тестовым прогнозом", Color.Red);
                    h.setValueByName("state", "Не удалось прочитать файл с тестовым прогнозом");
                }
            }
            catch
            {
                log("Не удалось спарсить RESPONSE");

                h.setValueByName("AVG", "-1");
                h.setValueByName("accuracy", "0");
                h.setValueByName("stdDev", "0");

                h.setValueByName("state", "ошибка при обучении");
            }
            // return "обучение алгоритма " + name + "заверешно.";
        }
        void calculateTargetFunction(Hyperparameters h)
        {
            if (poolLoading < pool.Length)
            {
                string reportLine = "";
                for (int i = 0; i < h.nodes.Count; i++)
                {
                    if (h.nodes[i].getAttributeValue("variable") == "numerical")
                    {
                        pool[poolLoading].nodes[i] = h.nodes[i].Clone();
                        reportLine += h.nodes[i].getAttributeValue("value").Replace(',', '.') + ";";
                    }
                }
                form1.log(reportLine);

                poolLoading++;
                if (poolLoading == pool.Length)
                {
                    if (form1.test_count != 0)
                    {
                        test_count = form1.test_count;
                    }
                    double[,] target_functions = new double[pool.Length, test_count];

                    for (int tc = 0; tc < test_count; tc++)
                    {
                        var now2   = new DateTimeOffset(DateTime.Now);
                        var start2 = now2.ToUnixTimeSeconds();

                        List <Algorithm> algorithms = new List <Algorithm>();
                        for (int i = 0; i < pool.Length; i++)
                        {
                            Algorithm alg = Algorithm.newInstance(algorithm);
                            alg.h = pool[i].Clone();
                            algorithms.Add(alg);
                        }

                        List <Thread> trainThreads = new List <Thread>();

                        foreach (Algorithm alg in algorithms)
                        {
                            Thread t = new Thread(new ThreadStart(alg.train));
                            trainThreads.Add(t);
                            t.Start();
                        }

                        foreach (var t in trainThreads)
                        {
                            t.Join();
                        }


                        for (int i = 0; i < pool.Length; i++)
                        {
                            pool[i] = algorithms[i].h.Clone();

                            switch (target_function_type)
                            {
                            case TargetFunctionType.ACCURACY:
                            {
                                target_functions[i, tc] = Convert.ToDouble(pool[i].getValueByName("accuracy").Replace('.', ','));
                                break;
                            }

                            case TargetFunctionType.STDDEV:
                            {
                                target_functions[i, tc] = Convert.ToDouble(pool[i].getValueByName("stdDev").Replace('.', ','));
                                break;
                            }
                            }
                        }
                        form1.log((tc + 1).ToString() + '/' + test_count.ToString() + " test comlete" + TimeSpan.FromSeconds((new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()) - start2).ToString(), Color.LimeGreen);
                    }

                    string[] lines = new string[pool.Length];
                    for (int i = 0; i < pool.Length; i++)
                    {
                        double sum = 0;

                        // AVG
                        for (int j = 0; j < test_count; j++)
                        {
                            sum += target_functions[i, j];
                        }
                        double AVG = sum / test_count;

                        sum = 0;
                        // StdDev
                        for (int j = 0; j < test_count; j++)
                        {
                            sum += (AVG - target_functions[i, j]) * (AVG - target_functions[i, j]);
                        }

                        double StdDev = Math.Sqrt(sum / test_count);

                        // если  target_function равна  (AVG - StdDev), то последующее вычисление критерия оптимальности будет давать результаты ВЫШЕ, чем  target_function
                        pool[i].setValueByName("target_function", (AVG - StdDev).ToString().Replace(',', '.'));
                        pool[i].setValueByName("target_function_AVG", (AVG).ToString().Replace(',', '.'));
                        pool[i].setValueByName("target_function_StdDev", (StdDev).ToString().Replace(',', '.'));

                        double Q = double.Parse(pool[i].getValueByName("target_function").Replace('.', ','));

                        TargetFunctionPoint newPoint = new TargetFunctionPoint(pool[i].Clone(), Q);
                        currentHypercube.Add(newPoint);
                        explored.Add(newPoint);

                        form1.log(newPoint.Q.ToString(), Color.LimeGreen);

                        lines[i] = getReportLine(newPoint);
                    }

                    File.AppendAllLines(report_file_name, lines);

                    poolLoading = 0;
                }
            }
        }
Ejemplo n.º 15
0
 public AgentTask(string type, Hyperparameters h)
 {
     this.type = type;
     this.h    = h;
     status    = "undone";
 }
Ejemplo n.º 16
0
        public void Process()
        {
            NetworkStream stream = null;

            /* try
            *  {   */
            stream = client.GetStream();
            BinaryReader reader = new BinaryReader(stream);
            BinaryWriter writer = new BinaryWriter(stream);

            while (true)
            {
                if (task != null)
                {
                    if (task.status != "done")
                    {
                        if (task.type == "train")
                        {
                            //log(hostName + ": " + task.h.getValueByName("code")+" .1");
                            status = "busy";
                            //объявление агенту типа задачи
                            sendCommand(writer, "train");

                            task.h.Save();

                            sendFile(writer, task.h.getValueByName("json_file_path"));
                            System.Threading.Thread.Sleep(10);
                            sendFile(writer, task.h.getValueByName("train_script_path"));
                            System.Threading.Thread.Sleep(10);
                            sendFile(writer, task.h.getValueByName("input_file"));
                            System.Threading.Thread.Sleep(10);
                            var trainingReport = recieveCommand(reader);
                            // log(trainingReport);
                            if (!trainingReport.Contains("Произошла ошибка"))
                            {
                                recieveFile(reader, task.h.getValueByName("json_file_path"));
                                System.Threading.Thread.Sleep(10);
                                recieveFile(reader, task.h.getValueByName("predictions_file_path"));
                                System.Threading.Thread.Sleep(10);
                                recieveFile(reader, task.h.getValueByName("save_folder") + "weights.h5");
                                System.Threading.Thread.Sleep(10);

                                // log(hostName + ": " + task.h.getValueByName("code") + " .2");


                                Hyperparameters hTemp = new Hyperparameters(File.ReadAllText(task.h.getValueByName("json_file_path"), Encoding.Default), form1);

                                hTemp.setValueByName("json_file_path", task.h.getValueByName("json_file_path"));
                                hTemp.setValueByName("predictions_file_path", task.h.getValueByName("predictions_file_path"));
                                hTemp.setValueByName("save_folder", task.h.getValueByName("save_folder"));
                                hTemp.setValueByName("train_script_path", task.h.getValueByName("train_script_path"));
                                hTemp.setValueByName("get_prediction_script_path", task.h.getValueByName("get_prediction_script_path"));
                                hTemp.setValueByName("input_file", task.h.getValueByName("input_file"));


                                task.h = hTemp.Clone();
                                task.h.Save();

                                log(hostName + ": " + task.h.getValueByName("code") + " trained;   accuracy = " + task.h.getValueByName("stdDev"));
                                task.status = "done";
                            }
                            else
                            {
                                task.status = "error";
                            }
                            status = "free";
                        }
                    }
                }
                System.Threading.Thread.Sleep(1000);
            }

            /*   }
             * catch (Exception ex)
             * {
             *     log(ex.Message);
             * }
             * finally
             * {
             *     if (stream != null)
             *         stream.Close();
             *     if (client != null)
             *         client.Close();
             * } */
        }
Ejemplo n.º 17
0
        internal void script()
        {
            mainThread = System.Threading.Thread.CurrentThread;
            Hyperparameters order_book;

            var api = new ExmoApi("k", "s");

            //  File.AppendAllText("USD_RUB_exmo.txt", "<local_time>;<bid>;<ask>");

            string result = "";

            //  result = api.ApiQuery("order_book", new Dictionary<string, string> { { "pair", "BTC_USD" } });
            //   order_book = new Hyperparameters(result, this);
            //order_book.draw(0, picBox, this, 25, 250);
            string s = "";

            vis.addParameter("BTC_USD", Color.White, 300);

            vis.parameters[0].showLastNValues = true;
            vis.enableGrid           = false;
            vis.parameters[0].window = 200;

            vis.parameters[0].functions.Add(new Function("bid_top", Color.Red));
            vis.parameters[0].functions.Add(new Function("ask_top", Color.Blue));

            //  vis.addParameter("BTC_USD количество", Color.White, 300);
            vis.addParameter("bid_quantity", Color.LightBlue, 300);
            vis.addParameter("ask_quantity", Color.Pink, 300);

            //  vis.addParameter("BTC_USD сумма", Color.White, 300);
            vis.addParameter("bid_amount", Color.Aqua, 300);
            vis.addParameter("ask_amount", Color.Coral, 300);

            int           step   = 60000;
            int           inc    = 0;
            List <string> buffer = new List <string>();

            while (true)
            {
                //       vis.parameters[0].window = 500;

                try
                {
                    result = api.ApiQuery("order_book", new Dictionary <string, string> {
                        { "pair", "BTC_USD" }
                    });
                    if (result.Length > 400)
                    {
                        result = result.Substring(0, 400);
                    }
                    if (!result.Contains("maintenance"))
                    {
                        order_book = new Hyperparameters(result, this);
                        //<bid_top>;<bid_quantity>;<bid_amount>;<ask_top>;<ask_quantity>;<ask_amount>
                        s = DateTime.Now.ToString() + ';'
                            + order_book.nodes[0].getAttributeValue("bid_top") + ';' + order_book.nodes[0].getAttributeValue("bid_quantity") + ';' + order_book.nodes[0].getAttributeValue("bid_amount") + ';'
                            + order_book.nodes[0].getAttributeValue("ask_top") + ';' + order_book.nodes[0].getAttributeValue("ask_quantity") + ';' + order_book.nodes[0].getAttributeValue("ask_amount");

                        double bid_top = Convert.ToDouble(order_book.nodes[0].getAttributeValue("bid_top").Replace('.', ','));
                        double ask_top = Convert.ToDouble(order_book.nodes[0].getAttributeValue("ask_top").Replace('.', ','));

                        double bid_quantity = Convert.ToDouble(order_book.nodes[0].getAttributeValue("bid_quantity").Replace('.', ','));
                        double ask_quantity = Convert.ToDouble(order_book.nodes[0].getAttributeValue("ask_quantity").Replace('.', ','));

                        double bid_amount = Convert.ToDouble(order_book.nodes[0].getAttributeValue("bid_amount").Replace('.', ','));
                        double ask_amount = Convert.ToDouble(order_book.nodes[0].getAttributeValue("ask_amount").Replace('.', ','));


                        buffer.Add(s);


                        vis.addPoint(bid_top, "bid_top");
                        vis.addPoint(ask_top, "ask_top");

                        vis.addPoint(bid_quantity, "bid_quantity");
                        vis.addPoint(ask_quantity, "ask_quantity");

                        vis.addPoint(bid_amount, "bid_amount");
                        vis.addPoint(ask_amount, "ask_amount");
                        vis.refresh();
                        // log(s);
                        inc++;

                        if (inc > 1800 / (step / 1000) & buffer.Count > 0)
                        {
                            File.AppendAllLines("BTC_USD_exmo.txt", buffer.ToArray());
                            buffer.Clear();
                            log(DateTime.Now.TimeOfDay.ToString() + "  Write!");
                            inc = 0;
                        }
                    }
                    else
                    {
                        log("Технические работы");
                    }
                }
                catch (Exception e)
                {
                    log(e.Message);
                    // log(result);
                }
                Thread.Sleep(step);
            }
        }
Ejemplo n.º 18
0
        public void TEST()
        {
            mainThread = System.Threading.Thread.CurrentThread;

            double balance_BTC = 0;
            double balance_USD = 100;

            double lot = 0.001;



            Hyperparameters order_book;
            var             api = new ExmoApi("k", "s");

            string responce = "";
            string s        = "";

            vis.addParameter("BTC_USD", Color.White, 1000);

            vis.parameters[0].showLastNValues = true;
            vis.enableGrid           = true;
            vis.parameters[0].window = 360;

            //наименьшая цена, по которой можно покупать
            vis.parameters[0].functions.Add(new Function("bid_top", Color.Blue));
            //наивысшая цена, по которой можно продать
            vis.parameters[0].functions.Add(new Function("ask_top", Color.Red));


            expert = new Expert("Expert_Flex", this);

            algorithm = new BidAsk(this, "BidAsk");

            algorithm.Open(@"E:\Anton\Desktop\MAIN\Экспертная система\Экспертная система\Алгоритмы прогнозирования\BidAsk\h.json");

            expert.AddAlgorithm(algorithm);

            int window = int.Parse(algorithm.h.getValueByName("window_size"));

            int step = 60000;

            string[]      input;
            double[]      predictions = null;
            List <string> rawInput    = new List <string>();

            // List<string> spreads = new List<string>();
            rawInput.Add("<DATE_TIME>;<bid_top>;<bid_quantity>;<bid_amount>;<ask_top>;<ask_quantity>;<ask_amount>");
            //ЗАПУСК СКРИПТОВ ПОТОЧНОГО ПРОГНОЗИРОВНИЯ
            Task[] RunTasks = new Task[expert.algorithms.Count];
            foreach (Algorithm algorithm in expert.algorithms)
            {
                RunTasks[expert.algorithms.IndexOf(algorithm)] = Task.Run(() => algorithm.runGetPredictionScript());
            }

            //ОЖИДАНИЕ ЗАВЕРШЕНИЯ ЗАПУСКА
            foreach (var task in RunTasks)
            {
                task.Wait();
            }


            List <double> positions = new List <double>();

            while (true)
            {
                vis.parameters[0].window = 20;
                try
                {
                    responce = api.ApiQuery("order_book", new Dictionary <string, string> {
                        { "pair", "BTC_USD" }
                    });
                    if (responce.Length > 400)
                    {
                        responce = responce.Substring(0, 400);
                    }
                    if (!responce.Contains("maintenance"))
                    {
                        order_book = new Hyperparameters(responce, this);
                        //<bid_top>;<bid_quantity>;<bid_amount>;<ask_top>;<ask_quantity>;<ask_amount>
                        s = DateTime.Now.ToString() + ';'
                            + order_book.nodes[0].getAttributeValue("bid_top") + ';' + order_book.nodes[0].getAttributeValue("bid_quantity") + ';' + order_book.nodes[0].getAttributeValue("bid_amount") + ';'
                            + order_book.nodes[0].getAttributeValue("ask_top") + ';' + order_book.nodes[0].getAttributeValue("ask_quantity") + ';' + order_book.nodes[0].getAttributeValue("ask_amount");

                        s = s.Replace('.', ',');

                        double bid_top = Convert.ToDouble(order_book.nodes[0].getAttributeValue("bid_top").Replace('.', ','));
                        double ask_top = Convert.ToDouble(order_book.nodes[0].getAttributeValue("ask_top").Replace('.', ','));

                        /*
                         * double bid_quantity = Convert.ToDouble(order_book.nodes[0].getAttributeValue("bid_quantity").Replace('.', ','));
                         * double ask_quantity = Convert.ToDouble(order_book.nodes[0].getAttributeValue("ask_quantity").Replace('.', ','));
                         *
                         * double bid_amount = Convert.ToDouble(order_book.nodes[0].getAttributeValue("bid_amount").Replace('.', ','));
                         * double ask_amount = Convert.ToDouble(order_book.nodes[0].getAttributeValue("ask_amount").Replace('.', ','));*/

                        vis.addPoint(bid_top, "bid_top");
                        vis.addPoint(ask_top, "ask_top");

                        vis.refresh();
                        // log(s);
                        //запись входных данных
                        File.AppendAllText("BTC_USD_exmo.txt", s);

                        rawInput.Add(s);
                        // spreads.Add(String.Format("{0:0.#####}", bid_top - ask_top));
                        showInpOutp.fillTextBox1(rawInput);

                        //+1 для заголовка, +1 для нормализации i/(i-1)
                        if (rawInput.Count < window + 2)
                        {
                            File.AppendAllText("BTC_USD_exmo.txt", "\n");
                        }
                        else
                        {
                            input = expert.prepareDataset(rawInput.ToArray(), "<DATE_TIME>", true);

                            //шапка должна оставаться на месте, поэтому 1, а не 0
                            rawInput.RemoveAt(1);

                            showInpOutp.fillTextBox2(input);

                            if (input.Length == window + 1)
                            {
                                predictions = expert.getCommitteePrediction(input);
                            }
                            else
                            {
                                log("input.Length =/= window+1; input.Length = " + input.Length.ToString());
                            }

                            string action = expert.getDecision(predictions);

                            if (action == "error")
                            {
                                log("action = error", Color.Red);
                            }
                            if (action == "buy")
                            {
                                if (balance_USD > ask_top * lot)
                                {
                                    balance_BTC = balance_BTC + lot;
                                    balance_USD = balance_USD - (ask_top * lot);
                                    positions.Add(ask_top);
                                    log("BUY", Color.Blue);
                                    log(ask_top.ToString());
                                    log("   USD:" + balance_USD.ToString());
                                    log("   BTC:" + balance_BTC.ToString());

                                    vis.parameters[0].functions[2].points[vis.parameters[0].functions[1].points.Count - 1].mark = "BUY‾‾‾‾‾‾‾‾";
                                }
                                else
                                {
                                    //      log("Баланс USD: " + balance_USD.ToString() + " Невозможно купить " + (bid_top * lot).ToString() + " BTC");
                                    action += " (fail)";
                                }
                            }

                            for (int i = 0; i < positions.Count; i++)
                            {
                                if (positions[i] < bid_top)
                                {
                                    action = "sell";
                                    positions.RemoveAt(i);
                                    break;
                                }
                            }

                            if (action == "sell")
                            {
                                if (bid_top != 0)
                                {
                                    if (balance_BTC >= lot)
                                    {
                                        balance_BTC = balance_BTC - lot;
                                        balance_USD = balance_USD + (bid_top * lot);
                                        log("SELL", Color.Red);
                                        log(bid_top.ToString());
                                        log("   USD:" + balance_USD.ToString());
                                        log("   BTC:" + balance_BTC.ToString());

                                        vis.parameters[0].functions[1].points[vis.parameters[0].functions[1].points.Count - 1].mark = "SELL‾‾‾‾‾‾‾‾";
                                    }
                                    else
                                    {
                                        //    log("Баланс BTC: " + balance_BTC.ToString() + " Невозможно продать " + (lot).ToString() + " BTC");
                                        action += " (fail)";
                                    }
                                }
                                else
                                {
                                    log("bid_top почему-то равно нулю!", Color.Red);
                                    action += " (fail)";
                                }
                            }
                            log(DateTime.Now.ToString() + " " + action);
                            //запись состояния балансов и последнего действия
                            File.AppendAllText("BTC_USD_exmo.txt", balance_BTC.ToString() + ';' + balance_USD.ToString() + ';' + action + "\n");
                        }
                    }
                    else
                    {
                        log("Технические работы", Color.Red);
                    }
                }
                catch (Exception e)
                {
                    log(e.Message, Color.Red);
                    // log(result);
                }
                Thread.Sleep(step);
            }
        }
 public TargetFunctionPoint(Hyperparameters h)
 {
     this.h = h;
 }
 public TargetFunctionPoint(Hyperparameters h, double Q)
 {
     this.h = h;
     this.Q = Q;
 }