public Task StartAsync(CancellationToken cancellationToken) { _cleanup = _cache .AsObservableCache() .Connect() .FilterAndSuppressRemoved(trade => trade.Status == TradeStatus.Filled) .Subscribe(changes => { foreach (var change in changes) { Scheduler.Default.Schedule(async() => { try { var trade = await _repository.GetById <Trade>(change.Current.EntityId); if (TradeServiceReferential.Rand.Next(1, 10) == 1) { var tradeRejectedEvent = new ComplianceRejectTrade() { ComplianceService = _configuration.Id }; await _repository.Apply(trade, tradeRejectedEvent); } else { var counterparty = TradeServiceReferential.Counterparties.Random(); var price = TradeServiceReferential.Assets.FirstOrDefault(asset => asset.Name == trade.Asset).Price; await Task.Delay(1000); var bookTradeEvent = new BookTrade() { ComplianceService = _configuration.Id }; await _repository.Apply(trade, bookTradeEvent); } } catch (Exception ex) { this.LogInformation($"Exception while handling trade {change}", ex); } }); } }); return(Task.CompletedTask); }
public async Task <TradeCreationResult> CreateTrade(TradeCreationRequest request) { var trade = new Trade(); var tradeCreationEvent = new CreateTrade() { Asset = request.Asset, Currency = request.Currency, Volume = request.Volume, Way = request.Way, TradeService = _configuration.Id, Trader = request.Trader }; await _repository.Apply(trade, tradeCreationEvent); return(new TradeCreationResult() { TradeId = trade.EntityId }); }
public Task StartAsync(CancellationToken cancellationToken) { _cleanup = _cache .AsObservableCache() .Connect() .FilterAndSuppressRemoved(trade => trade.Status == TradeStatus.Created) .Subscribe(changes => { foreach (var change in changes) { Scheduler.Default.Schedule(async() => { try { var trade = await _repository.GetById <Trade>(change.Current.EntityId); if (TradeServiceReferential.Rand.Next(1, 10) == 1) { var tradeRejectedEvent = new MarketRejectTrade() { MarketService = _configuration.Id, }; await _repository.Apply(trade, tradeRejectedEvent); } else { var counterparty = TradeServiceReferential.Counterparties.Random(); var price = await _priceService.GetPrice(change.Current.Asset); if (counterparty == TradeServiceReferential.HighLatencyCounterparty) { await Task.Delay(5000); } else { await Task.Delay(1000); } var fillTradeEvent = new FillTrade() { Counterparty = counterparty, Price = price.Value, Date = DateTime.Now, MarketService = _configuration.Id, Marketplace = TradeServiceReferential.Markets.Random().Name, }; await _repository.Apply(trade, fillTradeEvent); } } catch (Exception ex) { this.LogInformation($"Exception while handling trade {change}", ex); } }); } }); return(Task.CompletedTask); }