public void SARSA() { MetaTraderLink mtLink = new MetaTraderLink(this); // ИНИЦИАЛИЗАЦИЯ СИСТЕМЫ ПРИНЯТИЯ РЕШЕНИЙ DMS.epsilon = mtLink.getResponseDouble("epsilon"); DMS.alpha = mtLink.getResponseDouble("alpha"); DMS.gamma = mtLink.getResponseDouble("gamma"); string symbols = mtLink.getResponse("get_symbols"); string[] splittedSym = symbols.Split(','); ///////////////////////////////////////////////////////////////////// /////// ПАРАМЕТРЫ СОСТОЯНИЯ СИСТЕМЫ ПРИНЯТИЯ РЕШЕНИЙ //////////////// for (int i = 0; i < splittedSym.Length; i++) { DMS.addParameter(splittedSym[i], "0,1"); } // состояние депозитов 1 - баланс положительный, 0 - баланс нулевой DMS.addParameter("DEP1", "0,1"); DMS.defaultActions.Add(new DMSAction("buy")); DMS.defaultActions.Add(new DMSAction("sell")); DMS.defaultActions.Add(new DMSAction("nothing")); DMS.generateStates(); while (true) { //ожидание новых данных string prices = mtLink.getResponse("get_prices"); string deposit1 = mtLink.getResponse("get_deposit1"); //возврат действия string action = DMS.getAction(prices + ",DEP1:" + deposit1).type; mtLink.send(action); //ожидание вознаграждения double r = mtLink.getResponseDouble("r"); //обновление состояния prices = mtLink.getResponse("get_prices"); deposit1 = mtLink.getResponse("get_deposit1"); DMS.setActualState(prices + ",DEP1:" + deposit1); //установка R DMS.setR(r); } }
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); } }