private void Load(List <string> names) { while (names.Count != 0) { BotPanel bot = BotFactory.GetStrategyForName(_botType, names[0], _startProgramm, _isScript); names.RemoveAt(0); lock (_botLocker) { _bots.Add(bot); } } }
private void WorkerArea() { while (true) { if (MainWindow.ProccesIsWorked == false) { return; } if (_botNames == null || _botNames.Count == 0) { Thread.Sleep(500); continue; } for (int i = 0; i < _botNames.Count; i++) { lock (_lockStr) { if (_targetChange == true) { Thread.Sleep(500); _targetChange = false; break; } BotPanel bot = BotFactory.GetStrategyForName(_botType, _botNames[i], StartProgram.IsOsOptimizer, _isScript); lock (_botLocker) { _bots.Add(bot); } if (i + 1 == _botNames.Count) { _botNames.Clear(); break; } } } } }
/// <summary> /// create bot /// создать бота /// </summary> public void CreateNewBot() { try { BotCreateUi ui = new BotCreateUi(BotFactory.GetNamesStrategy(), BotFactory.GetScriptsNamesStrategy(), StartProgram.IsOsTrader); ui.ShowDialog(); if (ui.IsAccepted == false) { return; } if (ui.NameStrategy == "Martingale") { if (ui.NameBot.Split('h').Length != 1) { MessageBox.Show(OsLocalization.Trader.Label6); return; } if (ui.NameBot.Split('l').Length != 1) { MessageBox.Show(OsLocalization.Trader.Label7); return; } } if (File.Exists(@"Engine\" + @"SettingsRealKeeper.txt")) { using (StreamReader reader = new StreamReader(@"Engine\" + @"SettingsRealKeeper.txt")) { while (!reader.EndOfStream) { string[] str = reader.ReadLine().Split('@'); if (str[0] == ui.NameBot) { MessageBox.Show(OsLocalization.Trader.Label8); return; } } } } if (File.Exists(@"Engine\" + @"SettingsTesterKeeper.txt")) { using (StreamReader reader = new StreamReader(@"Engine\" + @"SettingsTesterKeeper.txt")) { while (!reader.EndOfStream) { string[] str = reader.ReadLine().Split('@'); if (str[0] == ui.NameBot) { MessageBox.Show(OsLocalization.Trader.Label8); return; } } } } BotPanel newRobot = BotFactory.GetStrategyForName(ui.NameStrategy, ui.NameBot, _startProgram, ui.IsScript); if (PanelsArray == null) { PanelsArray = new List <BotPanel>(); } PanelsArray.Add(newRobot); if (BotCreateEvent != null) { BotCreateEvent(newRobot); } SendNewLogMessage(OsLocalization.Trader.Label9 + newRobot.NameStrategyUniq, LogMessageType.System); ReloadActivBot(newRobot); Save(); ReloadRiskJournals(); } catch (Exception error) { SendNewLogMessage(error.ToString(), LogMessageType.Error); } }
/// <summary> /// load robots with saved names /// загрузить роботов по сохранённым именам /// </summary> private void Load() { if (!File.Exists(@"Engine\Settings" + _typeWorkKeeper + "Keeper.txt")) { // if there is no file we need. Just go out // если нет нужного нам файла. Просто выходим return; } int botCount = 0; using (StreamReader reader = new StreamReader(@"Engine\Settings" + _typeWorkKeeper + "Keeper.txt")) { while (!reader.EndOfStream) { if (!string.IsNullOrWhiteSpace(reader.ReadLine())) { botCount++; } } } if (botCount == 0) { return; } PanelsArray = new List <BotPanel>(); int botIterator = 0; using (StreamReader reader = new StreamReader(@"Engine\Settings" + _typeWorkKeeper + "Keeper.txt")) { while (!reader.EndOfStream) { string[] names = reader.ReadLine().Split('@'); BotPanel bot = null; if (names.Length > 2) { try { bot = BotFactory.GetStrategyForName(names[1], names[0], _startProgram, Convert.ToBoolean(names[2])); } catch (Exception e) { MessageBox.Show(" Error on bot creation. Bot Name: " + names[1] + " \n" + e.ToString()); continue; } } else { bot = BotFactory.GetStrategyForName(names[1], names[0], _startProgram, false); } if (bot != null) { PanelsArray.Add(bot); if (BotCreateEvent != null) { BotCreateEvent(bot); } if (_tabBotNames != null) { _tabBotNames.Items.Add(" " + PanelsArray[botIterator].NameStrategyUniq + " "); SendNewLogMessage(OsLocalization.Trader.Label2 + PanelsArray[botIterator].NameStrategyUniq, LogMessageType.System); } botIterator++; bot.NewTabCreateEvent += () => { ReloadRiskJournals(); }; } } } if (PanelsArray.Count != 0) { ReloadActivBot(PanelsArray[0]); } }
/// <summary> /// launch another robot as part of optimization /// запустить ещё одного робота, в рамках оптимизации /// </summary> /// <param name="parametrs">list of all parameters/список всех параметров</param> /// <param name="paramOptimized">brute force options/параметры по которым осуществляется перебор</param> /// <param name="faze">current optimization phase/текущая фаза оптимизации</param> /// <param name="botsInFaze">list of bots already running in the current phase/список ботов уже запущенный в текущей фазе</param> /// <param name="botName">the name of the created robot/имя создаваемого робота</param> private async void StartNewBot(List <IIStrategyParameter> parametrs, List <IIStrategyParameter> paramOptimized, OptimizerFaze faze, List <BotPanel> botsInFaze, string botName) { if (!MainWindow.GetDispatcher.CheckAccess()) { MainWindow.GetDispatcher.Invoke( new Action <List <IIStrategyParameter>, List <IIStrategyParameter>, OptimizerFaze, List <BotPanel>, string>(StartNewBot), parametrs, paramOptimized, faze, botsInFaze, botName); await Task.Delay(1000); return; } // 1. Create a new server for optimization. And one thread respectively // 1. создаём новый сервер для оптимизации. И один поток соответственно OptimizerServer server = ServerMaster.CreateNextOptimizerServer(_master.Storage, _serverNum, _master.StartDepozit); _serverNum++; _servers.Add(server); server.TestingEndEvent += server_TestingEndEvent; server.TypeTesterData = _master.Storage.TypeTesterData; server.TestintProgressChangeEvent += server_TestintProgressChangeEvent; for (int i = 0; i < _master.TabsSimpleNamesAndTimeFrames.Count; i++) { Security secToStart = _master.Storage.Securities.Find(s => s.Name == _master.TabsSimpleNamesAndTimeFrames[i].NameSecurity); server.GetDataToSecurity(secToStart, _master.TabsSimpleNamesAndTimeFrames[i].TimeFrame, faze.TimeStart, faze.TimeEnd); } // 2. create a new robot and upload it with the appropriate settings and parameters // 2. создаём нового робота и прогружаем его соответствующими настройками и параметрами BotPanel bot = BotFactory.GetStrategyForName(_master.StrategyName, botName, StartProgram.IsOsOptimizer, _master.IsScript); for (int i = 0; i < parametrs.Count; i++) { IIStrategyParameter par = paramOptimized.Find(p => p.Name == parametrs[i].Name); if (par == null) { par = parametrs[i]; } if (bot.Parameters[i].Type == StrategyParameterType.Bool) { ((StrategyParameterBool)bot.Parameters[i]).ValueBool = ((StrategyParameterBool)par).ValueBool; } else if (bot.Parameters[i].Type == StrategyParameterType.String) { ((StrategyParameterString)bot.Parameters[i]).ValueString = ((StrategyParameterString)par).ValueString; } else if (bot.Parameters[i].Type == StrategyParameterType.Int) { ((StrategyParameterInt)bot.Parameters[i]).ValueInt = ((StrategyParameterInt)par).ValueInt; } else if (bot.Parameters[i].Type == StrategyParameterType.Decimal) { ((StrategyParameterDecimal)bot.Parameters[i]).ValueDecimal = ((StrategyParameterDecimal)par).ValueDecimal; } } // custom tabs // настраиваем вкладки for (int i = 0; i < _master.TabsSimpleNamesAndTimeFrames.Count; i++) { bot.TabsSimple[i].Connector.ServerType = ServerType.Optimizer; bot.TabsSimple[i].Connector.PortfolioName = server.Portfolios[0].Number; bot.TabsSimple[i].Connector.NamePaper = _master.TabsSimpleNamesAndTimeFrames[i].NameSecurity; bot.TabsSimple[i].Connector.TimeFrame = _master.TabsSimpleNamesAndTimeFrames[i].TimeFrame; if (server.TypeTesterData == TesterDataType.Candle) { bot.TabsSimple[i].Connector.CandleMarketDataType = CandleMarketDataType.Tick; } else if (server.TypeTesterData == TesterDataType.MarketDepthAllCandleState || server.TypeTesterData == TesterDataType.MarketDepthOnlyReadyCandle) { bot.TabsSimple[i].Connector.CandleMarketDataType = CandleMarketDataType.MarketDepth; } } for (int i = 0; i < _master.TabsIndexNamesAndTimeFrames.Count; i++) { bot.TabsIndex[i].Tabs.Clear(); for (int i2 = 0; i2 < _master.TabsIndexNamesAndTimeFrames[i].NamesSecurity.Count; i2++) { if (i2 >= bot.TabsIndex[i].Tabs.Count) { bot.TabsIndex[i].CreateNewSecurityConnector(); } bot.TabsIndex[i].Tabs[i2].ServerType = ServerType.Optimizer; bot.TabsIndex[i].Tabs[i2].PortfolioName = server.Portfolios[0].Number; bot.TabsIndex[i].Tabs[i2].NamePaper = _master.TabsIndexNamesAndTimeFrames[i].NamesSecurity[i2]; bot.TabsIndex[i].Tabs[i2].TimeFrame = _master.TabsIndexNamesAndTimeFrames[i].TimeFrame; if (server.TypeTesterData == TesterDataType.Candle) { bot.TabsIndex[i].Tabs[i2].CandleMarketDataType = CandleMarketDataType.Tick; } else if (server.TypeTesterData == TesterDataType.MarketDepthAllCandleState || server.TypeTesterData == TesterDataType.MarketDepthOnlyReadyCandle) { bot.TabsIndex[i].Tabs[i2].CandleMarketDataType = CandleMarketDataType.MarketDepth; } } bot.TabsIndex[i].UserFormula = _master.TabsIndexNamesAndTimeFrames[i].Formula; } // wait for the robot to connect to its data server // ждём пока робот подключиться к своему серверу данных DateTime timeStartWaiting = DateTime.Now; while (bot.IsConnected == false) { await Task.Delay(1000); if (timeStartWaiting.AddSeconds(20) < DateTime.Now) { SendLogMessage( OsLocalization.Optimizer.Message10, LogMessageType.Error); return; } } server.TestingStart(); botsInFaze.Add(bot); }
private BotPanel CreateNewBot(string botName, List <IIStrategyParameter> parametrs, List <IIStrategyParameter> paramOptimized, OptimizerServer server, StartProgram regime) { BotPanel bot = BotFactory.GetStrategyForName(_master.StrategyName, botName, regime, _master.IsScript); for (int i = 0; i < parametrs.Count; i++) { IIStrategyParameter par = paramOptimized.Find(p => p.Name == parametrs[i].Name); if (par == null) { par = parametrs[i]; } if (par.Type == StrategyParameterType.Bool) { ((StrategyParameterBool)bot.Parameters[i]).ValueBool = ((StrategyParameterBool)par).ValueBool; } else if (par.Type == StrategyParameterType.String) { ((StrategyParameterString)bot.Parameters[i]).ValueString = ((StrategyParameterString)par).ValueString; } else if (par.Type == StrategyParameterType.Int) { ((StrategyParameterInt)bot.Parameters[i]).ValueInt = ((StrategyParameterInt)par).ValueInt; } else if (par.Type == StrategyParameterType.Decimal) { ((StrategyParameterDecimal)bot.Parameters[i]).ValueDecimal = ((StrategyParameterDecimal)par).ValueDecimal; } else if (par.Type == StrategyParameterType.TimeOfDay) { ((StrategyParameterTimeOfDay)bot.Parameters[i]).Value = ((StrategyParameterTimeOfDay)par).Value; } } // custom tabs // настраиваем вкладки for (int i = 0; i < _master.TabsSimpleNamesAndTimeFrames.Count; i++) { bot.TabsSimple[i].Connector.ServerType = ServerType.Optimizer; bot.TabsSimple[i].Connector.PortfolioName = server.Portfolios[0].Number; bot.TabsSimple[i].Connector.NamePaper = _master.TabsSimpleNamesAndTimeFrames[i].NameSecurity; bot.TabsSimple[i].Connector.TimeFrame = _master.TabsSimpleNamesAndTimeFrames[i].TimeFrame; bot.TabsSimple[i].Connector.ServerUid = server.NumberServer; if (server.TypeTesterData == TesterDataType.Candle) { bot.TabsSimple[i].Connector.CandleMarketDataType = CandleMarketDataType.Tick; } else if (server.TypeTesterData == TesterDataType.MarketDepthAllCandleState || server.TypeTesterData == TesterDataType.MarketDepthOnlyReadyCandle) { bot.TabsSimple[i].Connector.CandleMarketDataType = CandleMarketDataType.MarketDepth; } } for (int i = 0; _master.TabsIndexNamesAndTimeFrames != null && i < _master.TabsIndexNamesAndTimeFrames.Count; i++) { bot.TabsIndex[i].Tabs.Clear(); for (int i2 = 0; i2 < _master.TabsIndexNamesAndTimeFrames[i].NamesSecurity.Count; i2++) { if (i2 >= bot.TabsIndex[i].Tabs.Count) { bot.TabsIndex[i].CreateNewSecurityConnector(); } bot.TabsIndex[i].Tabs[i2].ServerType = ServerType.Optimizer; bot.TabsIndex[i].Tabs[i2].PortfolioName = server.Portfolios[0].Number; bot.TabsIndex[i].Tabs[i2].NamePaper = _master.TabsIndexNamesAndTimeFrames[i].NamesSecurity[i2]; bot.TabsIndex[i].Tabs[i2].ServerUid = server.NumberServer; bot.TabsIndex[i].Tabs[i2].TimeFrame = _master.TabsIndexNamesAndTimeFrames[i].TimeFrame; if (server.TypeTesterData == TesterDataType.Candle) { bot.TabsIndex[i].Tabs[i2].CandleMarketDataType = CandleMarketDataType.Tick; } else if (server.TypeTesterData == TesterDataType.MarketDepthAllCandleState || server.TypeTesterData == TesterDataType.MarketDepthOnlyReadyCandle) { bot.TabsIndex[i].Tabs[i2].CandleMarketDataType = CandleMarketDataType.MarketDepth; } } bot.TabsIndex[i].UserFormula = _master.TabsIndexNamesAndTimeFrames[i].Formula; } return(bot); }