public async Task OnCopy() { var ids = View.SelectedBotIds; if (IsValid(ids)) { var dlg = new ChooseAccount.ChooseAccount(_keys, _logger); var dr = dlg.ShowDialog(View); if (dr == DialogResult.OK) { dr = _mbs.ShowQuestion($"Copy {ids.Count} Bots in Account '{dlg.Account.Name}' now?"); if (dr == DialogResult.Yes) { var cancellationTokenSource = new CancellationTokenSource(); var loadingView = new ProgressView.ProgressView("Bots are now being copied", cancellationTokenSource, ids.Count); loadingView.Show(View); var botMgr = new BotManager(_keys, _logger); int i = 0; foreach (var botId in ids) { i++; loadingView.SetProgress(i); if (cancellationTokenSource.IsCancellationRequested) { break; } var bot = await botMgr.GetBotById(botId); bot.AccountId = dlg.Account.Id; var res = await botMgr.CreateBot(dlg.Account.Id, bot.Strategy, bot); if (res.IsSuccess) { if (bot.IsEnabled) { await botMgr.Enable(res.Data.Id); } _logger.LogInformation($"Bot {botId} created"); } else { _logger.LogError($"Could not copy Bot {botId}. Reason: {res.Error}"); } } loadingView.Close(); if (cancellationTokenSource.IsCancellationRequested) { _logger.LogInformation("Operation cancelled"); _mbs.ShowError("Operation cancelled!", ""); } else { _mbs.ShowInformation("Bulk Copy finished. See output section for details."); } _logger.LogInformation("Refreshing Bots"); await RefreshBots(); } } } }
private async void LoadingView_Load(object sender, EventArgs e) { var botMgr = new BotManager(_keys, _logger); progressBar.Maximum = _bots.Count; int i = 0; foreach (var bot in _bots) { i++; progressBar.Value = i; decimal percentage = ((decimal)i / _bots.Count) * 100; lblPercentage.Text = $"{(int)percentage} %"; if (_cancelled) { lblProgress.Text = "Operation has been cancelled."; progressBar.Enabled = false; btnCancel.Visible = false; btnClose.Visible = true; _mbs.ShowError("Operation cancelled!", ""); return; } if (_newSettings.IsEnabled.HasValue) { if (_newSettings.IsEnabled.Value) { await botMgr.Enable(bot.Id); } else { await botMgr.Disable(bot.Id); } } var updateData = new BotUpdateData(bot); if (_newSettings.ActiveSafetyOrdersCount.HasValue) { updateData.ActiveSafetyOrdersCount = _newSettings.ActiveSafetyOrdersCount.Value; } if (_newSettings.BaseOrderVolume.HasValue) { updateData.BaseOrderVolume = _newSettings.BaseOrderVolume.Value; } if (_newSettings.Cooldown.HasValue) { updateData.Cooldown = _newSettings.Cooldown.Value; } if (_newSettings.MartingaleStepCoefficient.HasValue) { updateData.MartingaleStepCoefficient = _newSettings.MartingaleStepCoefficient.Value; } if (_newSettings.MartingaleVolumeCoefficient.HasValue) { updateData.MartingaleVolumeCoefficient = _newSettings.MartingaleVolumeCoefficient.Value; } if (_newSettings.MaxSafetyOrders.HasValue) { updateData.MaxSafetyOrders = _newSettings.MaxSafetyOrders.Value; } if (!string.IsNullOrWhiteSpace(_newSettings.Name)) { updateData.Name = BotManager.GenerateNewName(_newSettings.Name, bot.Strategy.ToString(), bot.Pairs.Single()); } if (_newSettings.SafetyOrderStepPercentage.HasValue) { updateData.SafetyOrderStepPercentage = _newSettings.SafetyOrderStepPercentage.Value; } if (_newSettings.StartOrderType.HasValue) { updateData.StartOrderType = _newSettings.StartOrderType.Value; } if (_newSettings.SafetyOrderVolume.HasValue) { updateData.SafetyOrderVolume = _newSettings.SafetyOrderVolume.Value; } if (_newSettings.TakeProfit.HasValue) { updateData.TakeProfit = _newSettings.TakeProfit.Value; } if (_newSettings.TrailingDeviation.HasValue) { updateData.TrailingDeviation = _newSettings.TrailingDeviation.Value; } if (_newSettings.TrailingEnabled.HasValue) { updateData.TrailingEnabled = _newSettings.TrailingEnabled.Value; } if (_newSettings.BaseOrderVolumeType.HasValue) { updateData.BaseOrderVolumeType = _newSettings.BaseOrderVolumeType.Value; } if (_newSettings.SafetyOrderVolumeType.HasValue) { updateData.SafetyOrderVolumeType = _newSettings.SafetyOrderVolumeType.Value; } if (_newSettings.DisableAfterDealsCountInfo != null) { if (_newSettings.DisableAfterDealsCountInfo.Enable) { updateData.DisableAfterDealsCount = _newSettings.DisableAfterDealsCountInfo.Value; } else { updateData.DisableAfterDealsCount = null; } } if (_newSettings.DealStartConditions.Any()) { updateData.Strategies.Clear(); updateData.Strategies.AddRange(_newSettings.DealStartConditions); } var res = await botMgr.SaveBot(bot.Id, updateData); if (res.IsSuccess) { _logger.LogInformation($"Bot {bot.Id} updated"); } else { _logger.LogError($"Could not update Bot {bot.Id}. Reason: {res.Error}"); } } _mbs.ShowInformation("Bulk Edit finished. See output section for details."); this.DialogResult = DialogResult.OK; this.Close(); }
public async void OnEdit() { var ids = View.SelectedBotIds; if (IsValid(ids)) { var dlg = new EditDialog.EditDialog(ids.Count, new BotManager(_keys, _logger)); EditDto editData = new EditDto(); dlg.EditDto = editData; var dr = dlg.ShowDialog(View); if (dr == DialogResult.OK) { var cancellationTokenSource = new CancellationTokenSource(); var loadingView = new ProgressView.ProgressView("Applying new settings", cancellationTokenSource, ids.Count); loadingView.Show(View); var botMgr = new BotManager(_keys, _logger); int i = 0; foreach (var botId in ids) { i++; loadingView.SetProgress(i); if (cancellationTokenSource.IsCancellationRequested) { break; } if (editData.IsEnabled.HasValue) { if (editData.IsEnabled.Value) { await botMgr.Enable(botId); } else { await botMgr.Disable(botId); } } var bot = await botMgr.GetBotById(botId); var updateData = new BotUpdateData(bot); if (editData.ActiveSafetyOrdersCount.HasValue) { updateData.ActiveSafetyOrdersCount = editData.ActiveSafetyOrdersCount.Value; } if (editData.BaseOrderVolume.HasValue) { updateData.BaseOrderVolume = editData.BaseOrderVolume.Value; } if (editData.Cooldown.HasValue) { updateData.Cooldown = editData.Cooldown.Value; } if (editData.MartingaleStepCoefficient.HasValue) { updateData.MartingaleStepCoefficient = editData.MartingaleStepCoefficient.Value; } if (editData.MartingaleVolumeCoefficient.HasValue) { updateData.MartingaleVolumeCoefficient = editData.MartingaleVolumeCoefficient.Value; } if (editData.MaxSafetyOrders.HasValue) { updateData.MaxSafetyOrders = editData.MaxSafetyOrders.Value; } if (!string.IsNullOrWhiteSpace(editData.Name)) { updateData.Name = BotManager.GenerateNewName(editData.Name, bot.Strategy.ToString(), bot.Pairs.Single(), bot.AccountName); } if (editData.SafetyOrderStepPercentage.HasValue) { updateData.SafetyOrderStepPercentage = editData.SafetyOrderStepPercentage.Value; } if (editData.StartOrderType.HasValue) { updateData.StartOrderType = editData.StartOrderType.Value; } if (editData.SafetyOrderVolume.HasValue) { updateData.SafetyOrderVolume = editData.SafetyOrderVolume.Value; } if (editData.TakeProfit.HasValue) { updateData.TakeProfit = editData.TakeProfit.Value; } if (editData.TakeProfitType.HasValue) { updateData.TakeProfitType = editData.TakeProfitType.Value; } if (editData.TrailingDeviation.HasValue) { updateData.TrailingDeviation = editData.TrailingDeviation.Value; } if (editData.TrailingEnabled.HasValue) { updateData.TrailingEnabled = editData.TrailingEnabled.Value; } if (editData.BaseOrderVolumeType.HasValue) { updateData.BaseOrderVolumeType = editData.BaseOrderVolumeType.Value; } if (editData.SafetyOrderVolumeType.HasValue) { updateData.SafetyOrderVolumeType = editData.SafetyOrderVolumeType.Value; } if (editData.StopLossPercentage.HasValue) { updateData.StopLossPercentage = editData.StopLossPercentage.Value; } if (editData.StopLossType.HasValue) { updateData.StopLossType = editData.StopLossType.Value; } if (editData.StopLossTimeoutEnabled.HasValue) { updateData.StopLossTimeoutEnabled = editData.StopLossTimeoutEnabled.Value; } if (editData.StopLossTimeout.HasValue) { updateData.StopLossTimeoutInSeconds = editData.StopLossTimeout.Value; } if (editData.LeverageType.HasValue) { updateData.LeverageType = editData.LeverageType.Value; } if (editData.LeverageCustomValue.HasValue) { updateData.LeverageCustomValue = editData.LeverageCustomValue.Value; } if (editData.DisableAfterDealsCountInfo != null) { if (editData.DisableAfterDealsCountInfo.Enable) { updateData.DisableAfterDealsCount = editData.DisableAfterDealsCountInfo.Value; } else { updateData.DisableAfterDealsCount = null; } } if (editData.DealStartConditions.Any()) { updateData.Strategies.Clear(); updateData.Strategies.AddRange(editData.DealStartConditions); } var res = await botMgr.SaveBot(botId, updateData); if (res.IsSuccess) { _logger.LogInformation($"Bot {botId} updated"); } else { _logger.LogError($"Could not update Bot {botId}. Reason: {res.Error}"); } } loadingView.Close(); if (cancellationTokenSource.IsCancellationRequested) { _logger.LogInformation("Operation cancelled"); _mbs.ShowError("Operation cancelled!", ""); } else { _mbs.ShowInformation("Bulk Edit finished. See output section for details."); } _logger.LogInformation("Refreshing Bots"); await RefreshBots(); } } }