public async Task RefreshAsync(string id)
        {
            _log.InfoWithDetails("Refreshing pnl stop loss settings.", id);

            Domain.PnLStopLossSettings settings = await GetSettingsByIdAsync(id);

            IReadOnlyCollection <PnLStopLossEngine> existedEngines = await _pnLStopLossEngineService.GetAllAsync();

            IReadOnlyCollection <string> existedAssetPairs =
                existedEngines.Where(x => x.PnLStopLossSettingsId == id)
                .Select(x => x.AssetPairId)
                .ToList();

            IReadOnlyCollection <string> allAssetPairs = (await _instrumentService.GetAllAsync())
                                                         .Select(x => x.AssetPairId).ToList();

            IReadOnlyCollection <string> missedAssetPairIds = allAssetPairs.Except(existedAssetPairs).ToList();

            foreach (var assetPairId in missedAssetPairIds)
            {
                await CreateEngine(assetPairId, settings);
            }

            _log.InfoWithDetails("PnL stop loss settings refreshed.", id);
        }
        private async Task CreateEngine(string assetPairId, Domain.PnLStopLossSettings pnLStopLossSettings)
        {
            PnLStopLossEngine newEngine = new PnLStopLossEngine(pnLStopLossSettings);

            newEngine.AssetPairId = assetPairId;

            newEngine.PnLStopLossSettingsId = pnLStopLossSettings.Id;

            await _pnLStopLossEngineService.AddAsync(newEngine);
        }
        private async Task <Domain.PnLStopLossSettings> GetSettingsByIdAsync(string id)
        {
            IReadOnlyCollection <Domain.PnLStopLossSettings> pnLStopLossSettings = await GetAllAsync();

            Domain.PnLStopLossSettings result = pnLStopLossSettings.FirstOrDefault(o => o.Id == id);

            if (pnLStopLossSettings == null)
            {
                throw new EntityNotFoundException();
            }

            return(result);
        }
        public async Task AddAsync(Domain.PnLStopLossSettings pnLStopLossSettings)
        {
            pnLStopLossSettings.Id = Guid.NewGuid().ToString();

            _log.InfoWithDetails("Creating pnl stop loss settings.", pnLStopLossSettings);

            await _pnLStopLossSettingsRepository.InsertAsync(pnLStopLossSettings);

            _cache.Set(pnLStopLossSettings);

            _log.InfoWithDetails("PnL stop loss settings created.", pnLStopLossSettings);

            IReadOnlyCollection <Instrument> instruments = await _instrumentService.GetAllAsync();

            foreach (var instrument in instruments)
            {
                await CreateEngine(instrument.AssetPairId, pnLStopLossSettings);
            }

            _log.InfoWithDetails("PnL stop loss settings created.", pnLStopLossSettings);
        }