Ejemplo n.º 1
0
        public void CancelExchangeOrder(IExchangeService exchangeService)
        {
            if (this.OrderStatus == OrderStatus.Started)
            {
                throw new InvalidOperationException("Order could not be cancel if it hasn't been submitted.");
            }
            if (this.OrderStatus == OrderStatus.Canceled)
            {
                throw new InvalidOperationException("The order has already been canceled.");
            }
            if (this.OrderStatus == OrderStatus.Filled)
            {
                throw new InvalidOperationException("Order could not be cancel if it was full filled.");
            }
            if (this.OrderStatus == OrderStatus.Rejected)
            {
                throw new InvalidOperationException("Order could not be cancel if it has been rejected.");
            }

            if (exchangeService == null)
            {
                throw new ArgumentNullException(nameof(exchangeService));
            }

            var success = exchangeService.CancelOrder(new Exchange(this.ExchangeId), this.ExchangeOrderId, this.BaseCurrency, this.QuoteCurrency);

            if (success)
            {
                this._dateCanceled = DateTime.UtcNow;
                //this.OrderStatus = OrderStatus.Canceled;
                this._orderStatusId = OrderStatus.Canceled.Id;

                this.AddDomainEvent(new OrderCanceledDomainEvent(
                                        this.OrderId,
                                        this.ArbitrageId,
                                        new Exchange(this.ExchangeId),
                                        this.ExchangeOrderId,
                                        this.BaseCurrency,
                                        this.QuoteCurrency,
                                        this.Price,
                                        this.QuantityTotal,
                                        this.QuantityFilled,
                                        this._dateCanceled ?? throw new FieldAccessException(nameof(this._dateCanceled))
                                        ));
            }
            else
            {
                throw new Exception("Order cancelation failed.");
            }
        }