private async Task ExecuteBulkOperation(string inProgressText, List <BotViewModel> bots, Func <BotViewModel, Task> updateOperation) { var cancellationTokenSource = new CancellationTokenSource(); var loadingView = new ProgressView.ProgressView(inProgressText, cancellationTokenSource, bots.Count); loadingView.Show(this); int i = 0; await bots.ParallelForEachAsync( async bot => { await updateOperation(bot); i++; loadingView.SetProgress(i); }, 5, cancellationTokenSource.Token).ConfigureAwait(true); loadingView.Close(); if (cancellationTokenSource.IsCancellationRequested) { _logger.LogInformation("Operation cancelled"); _mbs.ShowError("Operation cancelled!", ""); } else { _mbs.ShowInformation("Operation finished. See output section for details."); } _logger.LogInformation("Refreshing Bots"); await tableControl.RefreshData(_keys); }
public async Task OnDelete() { var ids = View.SelectedBotIds; if (IsValid(ids)) { var dr = _mbs.ShowQuestion($"Do you really want to delete {ids.Count} Bots?"); if (dr == DialogResult.Yes) { var cancellationTokenSource = new CancellationTokenSource(); var loadingView = new ProgressView.ProgressView("Bots are now being deleted", 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 res = await botMgr.DeleteBot(botId); if (res.IsSuccess) { _logger.LogInformation($"Bot {botId} deleted"); } else { _logger.LogError($"Could not delete Bot {botId}. Reason: {res.Error}"); } } loadingView.Close(); if (cancellationTokenSource.IsCancellationRequested) { _logger.LogInformation("Operation cancelled"); _mbs.ShowError("Operation cancelled!", ""); } else { _mbs.ShowInformation("Delete finished. See output section for details."); } _logger.LogInformation("Refreshing Bots"); await RefreshBots(); } } }
private async Task ExecuteBulkOperation(string confirmationMessage, string operationName, Func <DealViewModel, Task> updateOperation) { if (IsValid(tableControl.SelectedIds)) { var dr = _mbs.ShowQuestion(confirmationMessage); if (dr == DialogResult.Yes) { var cancellationTokenSource = new CancellationTokenSource(); var loadingView = new ProgressView.ProgressView(operationName, cancellationTokenSource, tableControl.SelectedIds.Count); loadingView.Show(this); int i = 0; await tableControl.SelectedItems.ParallelForEachAsync( async deal => { await updateOperation(deal); i++; loadingView.SetProgress(i); }, 2, cancellationTokenSource.Token).ConfigureAwait(true); loadingView.Close(); if (cancellationTokenSource.IsCancellationRequested) { _logger.LogInformation("Operation cancelled"); _mbs.ShowError("Operation cancelled!", ""); } else { _mbs.ShowInformation($"Operation finished. See output section for details."); } _logger.LogInformation("Refreshing Deals"); await tableControl.RefreshData(_keys); } } }
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(); } } } }
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(); } } }