private async void btnCopy_Click(object sender, EventArgs e)
        {
            var dlg = new CopyBotDialog.CopyBotDialog(_keys, _logger);
            var dr  = dlg.ShowDialog(this);

            if (dr == DialogResult.OK)
            {
                var botMgr = new XCommasLayer(_keys, _logger);
                await ExecuteBulkOperation($"Copy {tableControl.SelectedIds.Count} Bots in Account '{dlg.Account.Name}' now?", "Bots are now being copied", GetSelectedBots(tableControl.SelectedIds), async botVm =>
                {
                    var botEntity       = await botMgr.GetBotById(botVm.Id, botVm.XCommasAccountId);
                    botEntity.AccountId = dlg.Account.Id;

                    var res = await botMgr.CreateBot(dlg.Account.Id, botEntity.Strategy, botEntity, dlg.Account.XCommasAccountId);
                    if (res.IsSuccess)
                    {
                        if (dlg.IsEnabled.HasValue && dlg.IsEnabled.Value || !dlg.IsEnabled.HasValue && botEntity.IsEnabled)
                        {
                            await botMgr.Enable(res.Data.Id, dlg.Account.XCommasAccountId);
                        }
                        _logger.LogInformation($"Bot {res.Data.Id} created (as a copy of Bot {botVm.Id})");
                    }
                    else
                    {
                        _logger.LogError($"Could not copy Bot {botVm.Id}. Reason: {res.Error}");
                    }
                });
            }
        }
Пример #2
0
        public EditDealDialog(int dealCount, XCommasLayer manager)
        {
            _dealCount = dealCount;
            _manager   = manager;
            InitializeComponent();

            numTargetProfit.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeTargetProfit, nameof(CheckBox.Checked));
            cmbTakeProfitType.DataBindings.Add(nameof(ComboBox.Visible), chkChangeTakeProfitType, nameof(CheckBox.Checked));
            cmbTtpEnabled.DataBindings.Add(nameof(ComboBox.Visible), chkChangeTrailingEnabled, nameof(CheckBox.Checked));
            numTrailingDeviation.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeTrailingDeviation, nameof(CheckBox.Checked));
            numMaxSafetyTradesCount.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeMaxSafetyTradesCount, nameof(CheckBox.Checked));
            numMaxActiveSafetyTradesCount.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeMaxActiveSafetyTradesCount, nameof(CheckBox.Checked));
            numStopLossPercentage.DataBindings.Add(nameof(NumericUpDown.Visible), chkStopLossPercentage, nameof(CheckBox.Checked));
            cmbStopLossType.DataBindings.Add(nameof(ComboBox.Visible), chkStopLossType, nameof(CheckBox.Checked));
            cmbStopLossTimeoutEnabled.DataBindings.Add(nameof(ComboBox.Visible), chkStopLossTimeoutEnabled, nameof(CheckBox.Checked));
            numStopLossTimeout.DataBindings.Add(nameof(NumericUpDown.Visible), chkStopLossTimeout, nameof(CheckBox.Checked));

            ControlHelper.AddValuesToCombobox <TakeProfitType>(cmbTakeProfitType);
            ControlHelper.AddValuesToCombobox <StopLossType>(cmbStopLossType);

            cmbTtpEnabled.Items.Add("Enable");
            cmbTtpEnabled.Items.Add("Disable");
            cmbStopLossTimeoutEnabled.Items.Add("Enable");
            cmbStopLossTimeoutEnabled.Items.Add("Disable");
        }
        public async Task RefreshData(XCommasAccounts keys)
        {
            await base.RefreshData <DealViewModel>(async() =>
            {
                var botMgr   = new XCommasLayer(keys, _logger);
                var allDeals = (await botMgr.GetAllDeals()).OrderBy(x => x.Deal.Id).ToList();

                var cfg = new MapperConfigurationExpression();
                cfg.CreateMap <Deal, DealViewModel>();
                var mapperConfig = new MapperConfiguration(cfg);
                var mapper       = mapperConfig.CreateMapper();

                var result = new List <DealViewModel>();
                foreach (var dealWithExchangeInfo in allDeals)
                {
                    var dealViewModel = mapper.Map <Deal, DealViewModel>(dealWithExchangeInfo.Deal);
                    dealViewModel.XCommasAccountId   = dealWithExchangeInfo.XCommasAccount;
                    dealViewModel.XCommasAccountName = dealWithExchangeInfo.XCommasAccountName;
                    result.Add(dealViewModel);
                }
                return(result);
            },
                                                   new Tuple <string, int>[]
            {
                new Tuple <string, int>(nameof(DealViewModel.Id), 65),
                new Tuple <string, int>(nameof(DealViewModel.DealType), 50),
                new Tuple <string, int>(nameof(DealViewModel.XCommasAccountName), 100),
                new Tuple <string, int>(nameof(DealViewModel.Account), 100),
                new Tuple <string, int>(nameof(DealViewModel.BotId), 55),
                new Tuple <string, int>(nameof(DealViewModel.BotName), 130),
                new Tuple <string, int>(nameof(DealViewModel.Pair), 100),
                new Tuple <string, int>(nameof(DealViewModel.DealHasErrors), 50),
                new Tuple <string, int>(nameof(DealViewModel.ErrorMessage), 100),
                new Tuple <string, int>(nameof(DealViewModel.TakeProfit), 50),
                new Tuple <string, int>(nameof(DealViewModel.TakeProfitType), 70),
                new Tuple <string, int>(nameof(DealViewModel.IsTrailingEnabled), 70),
                new Tuple <string, int>(nameof(DealViewModel.TrailingDeviation), 90),
                new Tuple <string, int>(nameof(DealViewModel.TrailingMaxPrice), 80),
                new Tuple <string, int>(nameof(DealViewModel.BoughtAmount), 80),
                new Tuple <string, int>(nameof(DealViewModel.BoughtVolume), 80),
                new Tuple <string, int>(nameof(DealViewModel.BoughtAveragePrice), 80),
                new Tuple <string, int>(nameof(DealViewModel.SoldAmount), 80),
                new Tuple <string, int>(nameof(DealViewModel.SoldVolume), 80),
                new Tuple <string, int>(nameof(DealViewModel.SoldAveragePrice), 80),
                new Tuple <string, int>(nameof(DealViewModel.BaseOrderVolume), 80),
                new Tuple <string, int>(nameof(DealViewModel.BaseOrderVolumeType), 80),
                new Tuple <string, int>(nameof(DealViewModel.SafetyOrderVolume), 80),
                new Tuple <string, int>(nameof(DealViewModel.SafetyOrderVolumeType), 80),
                new Tuple <string, int>(nameof(DealViewModel.MartingaleCoefficient), 80),
                new Tuple <string, int>(nameof(DealViewModel.MaxSafetyOrders), 80),
                new Tuple <string, int>(nameof(DealViewModel.ActiveSafetyOrdersCount), 80),
                new Tuple <string, int>(nameof(DealViewModel.CompletedSafetyOrdersCount), 80),
                new Tuple <string, int>(nameof(DealViewModel.CurrentActiveSafetyOrdersCount), 80),
                new Tuple <string, int>(nameof(DealViewModel.SafetyOrderStepPercentage), 80),
                new Tuple <string, int>(nameof(DealViewModel.IsCancellable), 80),
                new Tuple <string, int>(nameof(DealViewModel.IsPanicSellable), 80),
                new Tuple <string, int>(nameof(DealViewModel.CreatedAt), 100),
                new Tuple <string, int>(nameof(DealViewModel.UpdatedAt), 100)
            });
        }
Пример #4
0
        public async void btnEdit_Click(object sender, EventArgs e)
        {
            var bots = GetSelectedBots(tableControl.SelectedIds);

            if (IsValid(bots))
            {
                var            dlg      = new EditGridBotDialog.EditGridBotDialog(bots.Count, new XCommasLayer(_keys, _logger));
                EditGridBotDto editData = new EditGridBotDto();
                dlg.EditDto = editData;
                var dr = dlg.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    var botMgr = new XCommasLayer(_keys, _logger);
                    await ExecuteBulkOperation("Applying new settings", bots, async botVm =>
                    {
                        if (editData.IsEnabled.HasValue)
                        {
                            if (editData.IsEnabled.Value)
                            {
                                await botMgr.EnableGridBot(botVm.Id, botVm.XCommasAccountId);
                            }
                            else
                            {
                                await botMgr.DisableGridBot(botVm.Id, botVm.XCommasAccountId);
                            }
                        }

                        var botData    = await botMgr.GetGridBotById(botVm.Id, botVm.XCommasAccountId);
                        var updateData = new GridBotUpdateData(botVm.Id, botData);
                        if (editData.QuantityPerGrid.HasValue)
                        {
                            updateData.QuantityPerGrid = editData.QuantityPerGrid.Value;
                        }
                        if (editData.UpperLimitPrice.HasValue)
                        {
                            updateData.UpperLimitPrice = editData.UpperLimitPrice.Value;
                        }
                        if (editData.LowerLimitPrice.HasValue)
                        {
                            updateData.LowerLimitPrice = editData.LowerLimitPrice.Value;
                        }
                        if (editData.GridsQuantity.HasValue)
                        {
                            updateData.GridsQuantity = editData.GridsQuantity.Value;
                        }

                        var res = await botMgr.SaveGridBot(botVm.Id, updateData, botVm.XCommasAccountId);
                        if (res.IsSuccess)
                        {
                            _logger.LogInformation($"Grid Bot {botVm.Id} updated");
                        }
                        else
                        {
                            _logger.LogError($"Could not update Grid Bot {botVm.Id}. Reason: {res.Error}");
                        }
                    });
                }
            }
        }
        private async Task BindAccountsAndSetSelection()
        {
            var botMgr   = new XCommasLayer(_keys, _logger);
            var accounts = await botMgr.RetrieveAccounts();

            cmbAccount.DataSource    = accounts;
            cmbAccount.ValueMember   = nameof(AccountViewModel.Id);
            cmbAccount.DisplayMember = nameof(AccountViewModel.Name);
        }
        private async void btnAddFunds_Click(object sender, EventArgs e)
        {
            var ids = tableControl.SelectedIds;

            if (IsValid(ids))
            {
                var dlg      = new AddFundsDialog.AddFundsDialog();
                var editData = new AddFundsDto();
                dlg.EditDto = editData;
                var dr = dlg.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    Application.DoEvents();
                    var botMgr = new XCommasLayer(_keys, _logger);

                    var accountTableDataDict = new Dictionary <int, List <AccountTableData> >();
                    foreach (var a in tableControl.SelectedItems.Select(x => new { x.AccountId, x.XCommasAccountId }))
                    {
                        if (!accountTableDataDict.ContainsKey(a.AccountId))
                        {
                            accountTableDataDict.Add(a.AccountId, await botMgr.GetAccountTableData(a.AccountId, a.XCommasAccountId));
                        }
                    }
                    Cursor.Current = Cursors.Default;
                    await ExecuteBulkOperation($"Are you sure to add funds to {tableControl.SelectedIds.Count} deals?", "Adding funds", async deal =>
                    {
                        var accountTableData = accountTableDataDict[deal.AccountId];
                        var quoteCoin        = deal.Pair.Split('_')[0];
                        var baseCoin         = deal.Pair.Split('_')[1];
                        var quote            = accountTableData.Single(x => x.CurrencyCode == quoteCoin);
                        var baseC            = accountTableData.Single(x => x.CurrencyCode == baseCoin);

                        decimal qtyInBaseCoin      = 0;
                        decimal qtyInQuoteCurrency = 0;
                        if (editData.QtyInBTC.HasValue)
                        {
                            var btc            = editData.QtyInBTC.Value;
                            qtyInBaseCoin      = btc / baseC.CurrentPrice;
                            qtyInQuoteCurrency = btc / quote.CurrentPrice;
                        }
                        if (editData.QtyInUSD.HasValue)
                        {
                            var usdt           = editData.QtyInUSD.Value;
                            qtyInBaseCoin      = usdt / baseC.CurrentPriceUsd;
                            qtyInQuoteCurrency = usdt / quote.CurrentPriceUsd;
                        }

                        DealAddFundsParameters updateData = new DealAddFundsParameters();
                        updateData.DealId   = deal.Id;
                        updateData.IsMarket = true;
                        updateData.Quantity = qtyInBaseCoin;
                        await botMgr.AddFundsAsync(updateData, baseCoin, qtyInQuoteCurrency, quoteCoin, deal.XCommasAccountId);
                    });
                }
            }
        }
Пример #7
0
        public ChooseSignal(XCommasLayer botManager)
        {
            _botManager = botManager;
            InitializeComponent();

            ControlHelper.AddValuesToCombobox <TradingViewTime>(cmbTradingViewTime);
            ControlHelper.AddValuesToCombobox <TradingViewIndicatorType>(cmbTradingViewType);
            ControlHelper.AddValuesToCombobox <IndicatorTime>(cmbRsiTime);
            ControlHelper.AddValuesToCombobox <IndicatorTime>(cmbUltTime);
            ControlHelper.AddValuesToCombobox <IndicatorTime>(cmbTaPresetsTime);
            ControlHelper.AddValuesToCombobox <TaPresetsType>(cmbTaPresetsType);
            ControlHelper.AddValuesToCombobox <QflType>(cmbQflType);
        }
Пример #8
0
        private async Task LoadAccounts()
        {
            var xCommas   = new XCommasLayer(_keys, _logger);
            var exchanges = await xCommas.RetrieveAccounts();

            ObjectContainer.Cache.SetAccounts(exchanges);
            var pairs = await xCommas.GetMarketPairs();

            ObjectContainer.Cache.SetPairs(pairs);
            View.SetAccountCount(_keys.Accounts.Count);
            View.SetExchangeCount(exchanges.Count);
            View.EnablePanicButton();
        }
        public EditGridBotDialog(int botCount, XCommasLayer manager)
        {
            _botCount = botCount;
            _manager  = manager;
            InitializeComponent();

            cmbIsEnabled.DataBindings.Add(nameof(ComboBox.Visible), chkChangeIsEnabled, nameof(CheckBox.Checked));
            numUpperLimit.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeUpperLimit, nameof(CheckBox.Checked));
            numLowerLimit.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeLowerLimit, nameof(CheckBox.Checked));
            numGridQuantity.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeGridQuantity, nameof(CheckBox.Checked));
            numQuantityPerGrid.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeQuantityPerGrid, nameof(CheckBox.Checked));

            cmbIsEnabled.Items.Add("Enable");
            cmbIsEnabled.Items.Add("Disable");
        }
        private async void btnDelete_Click(object sender, EventArgs e)
        {
            var botMgr = new XCommasLayer(_keys, _logger);

            await ExecuteBulkOperation($"Do you really want to delete {tableControl.SelectedIds.Count} Bots?", "Bots are now being deleted", GetSelectedBots(tableControl.SelectedIds), async botVm =>
            {
                var res = await botMgr.DeleteBot(botVm.Id, botVm.XCommasAccountId);
                if (res.IsSuccess)
                {
                    _logger.LogInformation($"Bot {botVm.Id} deleted");
                }
                else
                {
                    _logger.LogError($"Could not delete Bot {botVm.Id}. Reason: {res.Error}");
                }
            });
        }
        public async Task StopAllBots()
        {
            var botMgr = new XCommasLayer(_keys, _logger);

            await ExecuteBulkOperation($"Do you really want to stop {tableControl.Items.Count} Bots?", "Bots are now being stopped", tableControl.Items, async botVm =>
            {
                var res = await botMgr.Disable(botVm.Id, botVm.XCommasAccountId);
                if (res.IsSuccess)
                {
                    _logger.LogInformation($"Bot {botVm.Id} stopped");
                }
                else
                {
                    _logger.LogError($"Could not stop Bot {botVm.Id}. Reason: {res.Error}");
                }
            });
        }
Пример #12
0
        public async Task RefreshData(XCommasAccounts keys)
        {
            await base.RefreshData <GridBotViewModel>(async() =>
            {
                var botMgr  = new XCommasLayer(keys, _logger);
                var allBots = (await botMgr.GetAllGridBots()).OrderBy(x => x.Bot.Id).ToList();

                var cfg = new MapperConfigurationExpression();
                cfg.CreateMap <GridBot, GridBotViewModel>();
                var mapperConfig = new MapperConfiguration(cfg);
                var mapper       = mapperConfig.CreateMapper();

                var result = new List <GridBotViewModel>();
                foreach (var botWithExchangeInfo in allBots)
                {
                    var botViewModel = mapper.Map <GridBot, GridBotViewModel>(botWithExchangeInfo.Bot);
                    botViewModel.XCommasAccountId   = botWithExchangeInfo.XCommasAccount;
                    botViewModel.XCommasAccountName = botWithExchangeInfo.XCommasAccountName;
                    result.Add(botViewModel);
                }

                return(result);
            },
                                                      new[]
            {
                new Tuple <string, int>(nameof(GridBotViewModel.Id), 55),
                new Tuple <string, int>(nameof(GridBotViewModel.IsEnabled), 80),
                new Tuple <string, int>(nameof(GridBotViewModel.XCommasAccountName), 120),
                new Tuple <string, int>(nameof(GridBotViewModel.AccountName), 120),
                new Tuple <string, int>(nameof(GridBotViewModel.Name), 120),
                new Tuple <string, int>(nameof(GridBotViewModel.Pair), 100),
                // new Tuple<string, int>(nameof(GridBotViewModel.LeverageType), 80),
                // new Tuple<string, int>(nameof(GridBotViewModel.LeverageCustomValue), 80),
                new Tuple <string, int>(nameof(GridBotViewModel.UpperLimitPrice), 100),
                new Tuple <string, int>(nameof(GridBotViewModel.LowerLimitPrice), 100),
                new Tuple <string, int>(nameof(GridBotViewModel.GridsQuantity), 100),
                new Tuple <string, int>(nameof(GridBotViewModel.QuantityPerGrid), 100),
                new Tuple <string, int>(nameof(GridBotViewModel.CreatedAt), 100),
                new Tuple <string, int>(nameof(GridBotViewModel.UpdatedAt), 100),
            });
        }
        public async Task RefreshData(XCommasAccounts keys)
        {
            await base.RefreshData <BotViewModel>(async() =>
            {
                var botMgr  = new XCommasLayer(keys, _logger);
                var allBots = (await botMgr.GetAllBots()).OrderBy(x => x.Bot.Id).ToList();

                var cfg = new MapperConfigurationExpression();
                cfg.CreateMap <Bot, BotViewModel>();
                var mapperConfig = new MapperConfiguration(cfg);
                var mapper       = mapperConfig.CreateMapper();

                var result = new List <BotViewModel>();
                foreach (var botWithExchangeInfo in allBots)
                {
                    var botViewModel = mapper.Map <Bot, BotViewModel>(botWithExchangeInfo.Bot);
                    botViewModel.XCommasAccountId   = botWithExchangeInfo.XCommasAccount;
                    botViewModel.XCommasAccountName = botWithExchangeInfo.XCommasAccountName;
                    result.Add(botViewModel);
                }
                return(result);
            },
                                                  new[]
            {
                new Tuple <string, int>(nameof(BotViewModel.Id), 55),
                new Tuple <string, int>(nameof(BotViewModel.BotType), 70),
                new Tuple <string, int>(nameof(BotViewModel.IsEnabled), 65),
                new Tuple <string, int>(nameof(BotViewModel.XCommasAccountName), 120),
                new Tuple <string, int>(nameof(BotViewModel.AccountName), 120),
                new Tuple <string, int>(nameof(BotViewModel.Name), 120),
                new Tuple <string, int>(nameof(BotViewModel.Strategy), 70),
                new Tuple <string, int>(nameof(BotViewModel.Pair), 100),
                new Tuple <string, int>(nameof(BotViewModel.MaxActiveDeals), 100),
                new Tuple <string, int>(nameof(BotViewModel.ActiveDealsCount), 100),
                new Tuple <string, int>(nameof(BotViewModel.TakeProfit), 50),
                new Tuple <string, int>(nameof(BotViewModel.TakeProfitType), 70),
                new Tuple <string, int>(nameof(BotViewModel.ProfitCurrency), 90),
                new Tuple <string, int>(nameof(BotViewModel.TrailingEnabled), 70),
                new Tuple <string, int>(nameof(BotViewModel.TrailingDeviation), 90),
                new Tuple <string, int>(nameof(BotViewModel.DealStartCondition), 200),
                new Tuple <string, int>(nameof(BotViewModel.StopLossPercentage), 100),
                new Tuple <string, int>(nameof(BotViewModel.StopLossType), 70),
                new Tuple <string, int>(nameof(BotViewModel.StopLossTimeoutEnabled), 100),
                new Tuple <string, int>(nameof(BotViewModel.StopLossTimeoutInSeconds), 80),
                new Tuple <string, int>(nameof(BotViewModel.BaseOrderVolume), 60),
                new Tuple <string, int>(nameof(BotViewModel.BaseOrderVolumeType), 80),
                new Tuple <string, int>(nameof(BotViewModel.StartOrderType), 60),
                new Tuple <string, int>(nameof(BotViewModel.SafetyOrderVolume), 60),
                new Tuple <string, int>(nameof(BotViewModel.SafetyOrderVolumeType), 80),
                new Tuple <string, int>(nameof(BotViewModel.MaxSafetyOrders), 80),
                new Tuple <string, int>(nameof(BotViewModel.ActiveSafetyOrdersCount), 80),
                new Tuple <string, int>(nameof(BotViewModel.SafetyOrderStepPercentage), 80),
                new Tuple <string, int>(nameof(BotViewModel.LeverageType), 80),
                new Tuple <string, int>(nameof(BotViewModel.LeverageCustomValue), 80),
                new Tuple <string, int>(nameof(BotViewModel.MartingaleVolumeCoefficient), 80),
                new Tuple <string, int>(nameof(BotViewModel.MartingaleStepCoefficient), 80),
                new Tuple <string, int>(nameof(BotViewModel.MinPrice), 80),
                new Tuple <string, int>(nameof(BotViewModel.MaxPrice), 80),
                new Tuple <string, int>(nameof(BotViewModel.MinVolumeBtc24h), 80),
                new Tuple <string, int>(nameof(BotViewModel.Cooldown), 80),
                new Tuple <string, int>(nameof(BotViewModel.DisableAfterDealsCount), 80),
                new Tuple <string, int>(nameof(BotViewModel.FinishedDealsCount), 80),
                new Tuple <string, int>(nameof(BotViewModel.FinishedDealsProfitUsd), 80),
                new Tuple <string, int>(nameof(BotViewModel.ProfitRatio), 80),
                new Tuple <string, int>(nameof(BotViewModel.CreatedAt), 100),
                new Tuple <string, int>(nameof(BotViewModel.UpdatedAt), 100),
                new Tuple <string, int>(nameof(BotViewModel.IsDeleteable), 80)
            });
        }
        private async void btnPanicSell_Click(object sender, EventArgs e)
        {
            var mgr = new XCommasLayer(_keys, _logger);

            await ExecuteBulkOperation($"Are you sure to panic sell {tableControl.SelectedIds.Count} deals?", "Panic Sell Deals", deal => mgr.PanicSellDeal(deal.Id, deal.XCommasAccountId));
        }
Пример #15
0
 private void txtName_TextChanged(object sender, EventArgs e)
 {
     lblNamePreview.Text = XCommasLayer.GenerateNewName(txtName.Text, "Long", new[] { "USDT_BTC" }, "AccountName");
 }
Пример #16
0
        public EditBotDialog(int botCount, XCommasLayer manager)
        {
            _botCount = botCount;
            _manager  = manager;
            InitializeComponent();

            cmbIsEnabled.DataBindings.Add(nameof(ComboBox.Visible), chkChangeIsEnabled, nameof(CheckBox.Checked));
            cmbPair.DataBindings.Add(nameof(ComboBox.Visible), chkChangePair, nameof(CheckBox.Checked));
            cmbProfitCurrency.DataBindings.Add(nameof(ComboBox.Visible), chkChangeProfitCurrency, nameof(CheckBox.Checked));
            lblPairWarning.DataBindings.Add(nameof(Label.Visible), chkChangePair, nameof(CheckBox.Checked));
            txtName.DataBindings.Add(nameof(TextBox.Visible), chkChangeName, nameof(CheckBox.Checked));
            numMaxActiveDeals.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeMaxActiveDeals, nameof(CheckBox.Checked));
            lblPreviewTitle.DataBindings.Add(nameof(Label.Visible), chkChangeName, nameof(CheckBox.Checked));
            lblNamePreview.DataBindings.Add(nameof(Label.Visible), chkChangeName, nameof(CheckBox.Checked));
            cmbStartOrderType.DataBindings.Add(nameof(ComboBox.Visible), chkChangeStartOrderType, nameof(CheckBox.Checked));
            numBaseOrderVolume.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeBaseOrderSize, nameof(CheckBox.Checked));
            cmbBaseOrderVolumeType.DataBindings.Add(nameof(ComboBox.Visible), chkChangeBaseOrderSizeType, nameof(CheckBox.Checked));
            numSafetyOrderVolume.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeSafetyOrderSize, nameof(CheckBox.Checked));
            cmbSafetyOrderVolumeType.DataBindings.Add(nameof(ComboBox.Visible), chkChangeSafetyOrderSizeType, nameof(CheckBox.Checked));
            numTargetProfit.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeTargetProfit, nameof(CheckBox.Checked));
            cmbTakeProfitType.DataBindings.Add(nameof(ComboBox.Visible), chkChangeTakeProfitType, nameof(CheckBox.Checked));
            cmbTtpEnabled.DataBindings.Add(nameof(ComboBox.Visible), chkChangeTrailingEnabled, nameof(CheckBox.Checked));
            numTrailingDeviation.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeTrailingDeviation, nameof(CheckBox.Checked));
            numMaxSafetyTradesCount.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeMaxSafetyTradesCount, nameof(CheckBox.Checked));
            numMaxActiveSafetyTradesCount.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeMaxActiveSafetyTradesCount, nameof(CheckBox.Checked));
            numPriceDeviationToOpenSafetyOrders.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangePriceDeviationToOpenSafetyOrders, nameof(CheckBox.Checked));
            numSafetyOrderVolumeScale.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeSafetyOrderVolumeScale, nameof(CheckBox.Checked));
            numSafetyOrderStepScale.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeSafetyOrderStepScale, nameof(CheckBox.Checked));
            numCooldownBetweenDeals.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeCooldownBetweenDeals, nameof(CheckBox.Checked));
            cmbDisableAfterDealsCount.DataBindings.Add(nameof(NumericUpDown.Visible), chkDisableAfterDealsCount, nameof(CheckBox.Checked));
            numDisableAfterDealsCount.DataBindings.Add(nameof(NumericUpDown.Visible), chkDisableAfterDealsCount, nameof(CheckBox.Checked));
            listViewStartConditions.DataBindings.Add(nameof(ListView.Visible), chkChangeDealStartCondition, nameof(CheckBox.Checked));
            btnAddStartCondition.DataBindings.Add(nameof(Button.Visible), chkChangeDealStartCondition, nameof(CheckBox.Checked));
            btnRemoveStartCondition.DataBindings.Add(nameof(Button.Visible), chkChangeDealStartCondition, nameof(CheckBox.Checked));
            lblStartConditionWarning.DataBindings.Add(nameof(Label.Visible), chkChangeDealStartCondition, nameof(CheckBox.Checked));
            numStopLossPercentage.DataBindings.Add(nameof(NumericUpDown.Visible), chkStopLossPercentage, nameof(CheckBox.Checked));
            cmbStopLossType.DataBindings.Add(nameof(ComboBox.Visible), chkStopLossType, nameof(CheckBox.Checked));
            cmbStopLossTimeoutEnabled.DataBindings.Add(nameof(ComboBox.Visible), chkStopLossTimeoutEnabled, nameof(CheckBox.Checked));
            numStopLossTimeout.DataBindings.Add(nameof(NumericUpDown.Visible), chkStopLossTimeout, nameof(CheckBox.Checked));
            cmbLeverageType.DataBindings.Add(nameof(ComboBox.Visible), chkChangeLeverageType, nameof(CheckBox.Checked));
            numLeverageCustomValue.DataBindings.Add(nameof(NumericUpDown.Visible), chkChangeCustomLeverageValue, nameof(CheckBox.Checked));

            ControlHelper.AddValuesToCombobox <StartOrderType>(cmbStartOrderType);
            ControlHelper.AddValuesToCombobox <TakeProfitType>(cmbTakeProfitType);
            ControlHelper.AddValuesToCombobox <StopLossType>(cmbStopLossType);
            ControlHelper.AddValuesToCombobox <LeverageType>(cmbLeverageType);
            ControlHelper.AddValuesToCombobox <ProfitCurrency>(cmbProfitCurrency);
            cmbBaseOrderVolumeType.Items.Add(new ComboBoxItem(VolumeType.QuoteCurrency, "Quote"));
            cmbBaseOrderVolumeType.Items.Add(new ComboBoxItem(VolumeType.BaseCurrency, "Base"));
            cmbBaseOrderVolumeType.Items.Add(new ComboBoxItem(VolumeType.Percent, "% (Base)"));
            cmbSafetyOrderVolumeType.Items.Add(new ComboBoxItem(VolumeType.QuoteCurrency, "Quote"));
            cmbSafetyOrderVolumeType.Items.Add(new ComboBoxItem(VolumeType.BaseCurrency, "Base"));
            cmbSafetyOrderVolumeType.Items.Add(new ComboBoxItem(VolumeType.Percent, "% (Base)"));

            cmbPair.Items.AddRange(ObjectContainer.Cache.Pairs.ToArray());
            cmbIsEnabled.Items.Add("Enable");
            cmbIsEnabled.Items.Add("Disable");
            cmbTtpEnabled.Items.Add("Enable");
            cmbTtpEnabled.Items.Add("Disable");
            cmbDisableAfterDealsCount.Items.Add("Enable");
            cmbDisableAfterDealsCount.Items.Add("Disable");
            cmbStopLossTimeoutEnabled.Items.Add("Enable");
            cmbStopLossTimeoutEnabled.Items.Add("Disable");
        }
        public async void btnEdit_Click(object sender, EventArgs e)
        {
            var bots = GetSelectedBots(tableControl.SelectedIds);

            if (IsValid(bots))
            {
                var        dlg      = new EditBotDialog.EditBotDialog(bots.Count, new XCommasLayer(_keys, _logger));
                EditBotDto editData = new EditBotDto();
                dlg.EditDto = editData;
                var dr = dlg.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    var botMgr = new XCommasLayer(_keys, _logger);
                    await ExecuteBulkOperation("Applying new settings", bots, async botVm =>
                    {
                        if (editData.IsEnabled.HasValue)
                        {
                            if (editData.IsEnabled.Value)
                            {
                                await botMgr.Enable(botVm.Id, botVm.XCommasAccountId);
                            }
                            else
                            {
                                await botMgr.Disable(botVm.Id, botVm.XCommasAccountId);
                            }
                        }

                        var bot        = await botMgr.GetBotById(botVm.Id, botVm.XCommasAccountId);
                        var updateData = new BotUpdateData(bot);

                        string[] pairs = bot.Pairs;
                        if (!string.IsNullOrWhiteSpace(editData.Pair))
                        {
                            pairs            = new[] { editData.Pair };
                            updateData.Pairs = pairs;
                        }
                        if (!string.IsNullOrWhiteSpace(editData.Name))
                        {
                            updateData.Name = XCommasLayer.GenerateNewName(editData.Name, bot.Strategy.ToString(), pairs, bot.AccountName);
                        }
                        if (editData.MaxActiveDeals.HasValue)
                        {
                            updateData.MaxActiveDeals = editData.MaxActiveDeals.Value;
                        }
                        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 (editData.SafetyOrderStepPercentage.HasValue)
                        {
                            updateData.SafetyOrderStepPercentage = editData.SafetyOrderStepPercentage.Value;
                        }
                        if (editData.ProfitCurrency.HasValue)
                        {
                            updateData.ProfitCurrency = editData.ProfitCurrency.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(botVm.Id, updateData, botVm.XCommasAccountId);
                        if (res.IsSuccess)
                        {
                            _logger.LogInformation($"Bot {botVm.Id} updated");
                        }
                        else
                        {
                            _logger.LogError($"Could not update Bot {botVm.Id}. Reason: {res.Error}");
                        }
                    });
                }
            }
        }
        private async void btnDisableTTP_Click(object sender, EventArgs e)
        {
            var mgr = new XCommasLayer(_keys, _logger);

            await ExecuteBulkOperation($"Are you sure to disable TTP for {tableControl.SelectedIds.Count} deals?", "Disable Trailing Take Profit", deal => mgr.DisableTrailing(deal.Id, deal.XCommasAccountId));
        }
        private async void btnEdit_Click(object sender, EventArgs e)
        {
            var ids = tableControl.SelectedIds;

            if (IsValid(ids))
            {
                var dlg      = new EditDealDialog.EditDealDialog(ids.Count, new XCommasLayer(_keys, _logger));
                var editData = new EditDealDto();
                dlg.EditDto = editData;
                var dr = dlg.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    var botMgr = new XCommasLayer(_keys, _logger);
                    await ExecuteBulkOperation($"Are you sure to update {tableControl.SelectedIds.Count} deals?", "Applying new settings", async deal =>
                    {
                        var updateData = new DealUpdateData(deal.Id);
                        if (editData.ActiveSafetyOrdersCount.HasValue)
                        {
                            updateData.MaxSafetyOrdersCount = editData.ActiveSafetyOrdersCount.Value;
                        }
                        if (editData.MaxSafetyOrders.HasValue)
                        {
                            updateData.MaxSafetyOrders = editData.MaxSafetyOrders.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.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;
                        }

                        await botMgr.UpdateDealAsync(updateData, deal.XCommasAccountId);
                    });
                }
            }
        }