public async void OnEdit()
        {
            var ids = View.SelectedBotIds;

            if (!ids.Any())
            {
                _mbs.ShowInformation("No Bots selected");
                return;
            }

            var botsToEdit = _bots.Where(x => ids.Contains(x.Id)).ToList();

            if (botsToEdit.Any(x => x.Type != "Bot::SingleBot"))
            {
                _mbs.ShowError("Sorry, Bulk Edit only works for Simple Bots, not advanced.");
                return;
            }

            var     dlg      = new EditDialog.EditDialog(botsToEdit.Count);
            EditDto editData = new EditDto();

            dlg.EditDto = editData;
            var dr = dlg.ShowDialog(View);

            if (dr == DialogResult.OK)
            {
                var loadingView = new ProgressView(botsToEdit, editData, _keys, _logger);
                loadingView.ShowDialog(View);

                _logger.LogInformation("Refreshing Bots");
                await RefreshBots();
            }
        }
Exemplo n.º 2
0
        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();
                }
            }
        }