Example #1
0
        public void SetUp()
        {
            _accountAssetsRepository.GetAllAsync().Result.ForEach(aa =>
                                                                  _accountAssetsRepository.Remove(aa.TradingConditionId, aa.BaseAssetId, "BTCUSD"));
            _accountAssetsManager.UpdateAccountAssetsCache().GetAwaiter().GetResult();

            _ordersCache.InitOrders(new List <Order>());

            _overnightSwapCache.ClearAll();

            _quoteCacheService.GetAllQuotes().Values.ForEach(x => _quoteCacheService.RemoveQuote(x.Instrument));

            _overnightSwapHistoryRepository.GetAsync().Result.ForEach(x =>
                                                                      _overnightSwapHistoryRepository.DeleteAsync(x));
        }
Example #2
0
        public async Task <IEnumerable <IAccountAssetPair> > AssignInstruments(string tradingConditionId,
                                                                               string baseAssetId, string[] instruments)
        {
            var defaults = _settings.DefaultAccountAssetsSettings ?? new AccountAssetsSettings();

            var currentInstruments = (await _pairsRepository.GetAllAsync(tradingConditionId, baseAssetId)).ToArray();

            if (currentInstruments.Any())
            {
                var toRemove = currentInstruments.Where(x => !instruments.Contains(x.Instrument)).ToArray();

                var existingOrderGroups = _orderReader.GetAll()
                                          .Where(o => o.TradingConditionId == tradingConditionId && o.AccountAssetId == baseAssetId)
                                          .GroupBy(o => o.Instrument)
                                          .Where(o => toRemove.Any(i => i.Instrument == o.Key))
                                          .ToArray();

                if (existingOrderGroups.Any())
                {
                    var errorMessage = "Unable to remove following instruments as they have active orders: ";

                    foreach (var group in existingOrderGroups)
                    {
                        errorMessage += $"{group.Key}({group.Count()} orders) ";
                    }

                    throw new InvalidOperationException(errorMessage);
                }

                foreach (var pair in toRemove)
                {
                    await _pairsRepository.Remove(pair.TradingConditionId, pair.BaseAssetId, pair.Instrument);
                }
            }

            var pairsToAdd = instruments.Where(x => currentInstruments.All(y => y.Instrument != x));

            var addedPairs = await _pairsRepository.AddAssetPairs(tradingConditionId, baseAssetId, pairsToAdd, defaults);

            await UpdateAccountAssetsCache();

            await _clientNotifyService.NotifyTradingConditionsChanged(tradingConditionId);

            return(addedPairs);
        }