Beispiel #1
0
        /// <inheritdoc />
        public async Task <TradingEngineSnapshot> RunAsync(IEnumerable <ClosingFxRate> fxRates, IEnumerable <ClosingAssetPrice> cfdQuotes, string correlationId)
        {
            var fxRatesList   = fxRates?.ToList();
            var cfdQuotesList = cfdQuotes?.ToList();

            if (fxRatesList == null || !fxRatesList.Any())
            {
                throw new EmptyPriceUploadException();
            }

            if (cfdQuotesList == null || !cfdQuotesList.Any())
            {
                throw new EmptyPriceUploadException();
            }

            var positions = _draftSnapshotKeeper.GetPositions();
            var accounts  = (await _draftSnapshotKeeper.GetAccountsAsync()).ToImmutableArray();

            foreach (var closingFxRate in fxRatesList)
            {
                ApplyFxRate(positions, accounts, closingFxRate.ClosePrice, closingFxRate.AssetId);
            }

            var orders = _draftSnapshotKeeper.GetAllOrders();

            foreach (var closingAssetPrice in cfdQuotesList)
            {
                ApplyCfdQuote(positions, orders, accounts, closingAssetPrice.ClosePrice, closingAssetPrice.AssetId);
            }

            var quotesTimestamp = _dateService.Now();

            await _draftSnapshotKeeper.UpdateAsync(
                positions,
                orders,
                accounts,
                fxRatesList.Select(r => r.ToContract(quotesTimestamp)),
                cfdQuotesList.Select(q => q.ToContract(quotesTimestamp))
                );

            return(new TradingEngineSnapshot(_draftSnapshotKeeper.TradingDay,
                                             correlationId,
                                             _draftSnapshotKeeper.Timestamp,
                                             MapToFinalJson(orders, _draftSnapshotKeeper),
                                             MapToFinalJson(positions, _draftSnapshotKeeper),
                                             MapToFinalJson(accounts),
                                             MapToJson(_draftSnapshotKeeper.FxPrices),
                                             MapToJson(_draftSnapshotKeeper.CfdQuotes),
                                             SnapshotStatus.Final));
        }
Beispiel #2
0
        public ICollection <Order> GetActiveOrdersByAccountIds(params string[] accountIds)
        {
            if (accountIds == null || !accountIds.Any() || accountIds.Any(string.IsNullOrWhiteSpace))
            {
                throw new ArgumentNullException(nameof(accountIds));
            }

            if (_draftSnapshotKeeper.Initialized())
            {
                _log.WriteInfoAsync(nameof(OrdersProvider),
                                    nameof(GetActiveOrdersByAccountIds),
                                    _draftSnapshotKeeper.TradingDay.ToJson(),
                                    "Draft snapshot keeper initialized and will be used as orders provider");

                var orders = _draftSnapshotKeeper.GetAllOrders();

                return(orders
                       .Where(o => o.Status == OrderStatus.Active && accountIds.Contains(o.AccountId))
                       .ToList());
            }

            return(_ordersCache.Active.GetOrdersByAccountIds(accountIds));
        }