Exemplo n.º 1
0
        /// <inheritdoc />
        public async Task UpdateAsync(ImmutableArray <Position> positions,
                                      ImmutableArray <Order> orders,
                                      ImmutableArray <MarginTradingAccount> accounts,
                                      IEnumerable <BestPriceContract> fxRates,
                                      IEnumerable <BestPriceContract> cfdQuotes)
        {
            if (!_tradingDay.HasValue)
            {
                throw new InvalidOperationException("Unable to update snapshot: the draft snapshot provider has not been initialized yet");
            }

            if (positions == null || !positions.Any())
            {
                throw new ArgumentNullException(nameof(positions), @"Unable to update snapshot: positions list is empty");
            }

            if (orders == null || !orders.Any())
            {
                throw new ArgumentNullException(nameof(orders), @"Unable to update snapshot: orders list is empty");
            }

            if (accounts == null || !accounts.Any())
            {
                throw new ArgumentNullException(nameof(accounts), @"Unable to update snapshot: accounts list is empty");
            }

            await EnsureSnapshotLoadedOrThrowAsync();

            var fxPrices = fxRates?.ToDictionary(r => r.Id, r => r);

            var tradingPrices = cfdQuotes?.ToDictionary(q => q.Id, q => q);

            _snapshot = new TradingEngineSnapshot(_snapshot.TradingDay,
                                                  _snapshot.CorrelationId,
                                                  _snapshot.Timestamp,
                                                  orders.ToJson(),
                                                  positions.ToJson(),
                                                  accounts.ToJson(),
                                                  _snapshot
                                                  .GetBestFxPrices()
                                                  .Merge(fxPrices)
                                                  .ToJson(),
                                                  _snapshot
                                                  .GetBestTradingPrices()
                                                  .Merge(tradingPrices)
                                                  .ToJson(),
                                                  _snapshot.Status);

            // to force keeper deserialize updated values from json next time data is accessed
            _positions = null;
            _orders    = null;
            _accounts  = null;
            _fxPrices  = null;
            _cfdQuotes = null;
        }