public async Task WhenPositionFilledRecordTradeExitAsync()
        {
            // Arrange
            const double FillPrice = 10;
            const ushort Quantity  = 1000;

            var tradeRecordingService = Substitute.For <ITradeRecordingService>();
            var position = new PositionItem
            {
                Quantity = Quantity,
                Symbol   = new Symbol
                {
                    Code = Symbol
                }
            };
            var vm = GetVm(tradeRecordingService: tradeRecordingService);

            vm.Positions.Add(position);

            // Act
            var msg     = new OrderStatusEventArgs(1, BrokerConstants.OrderStatus.Filled, Quantity, 0, FillPrice, 0, 0, FillPrice, 0, null);
            var message = new OrderStatusChangedMessage(Symbol, msg);

            Messenger.Default.Send(message, OrderStatusChangedMessage.Tokens.Positions);

            // Assert
            await tradeRecordingService.Received().ExitTradeAsync(
                Arg.Is <PositionItem>(x => x == position),
                Arg.Is <OrderStatusChangedMessage>(x => x == message));
        }
Exemplo n.º 2
0
 private bool BasicCheck(OrderStatusChangedMessage m, OrderStatusEnum status)
 {
     return(
         m.PartnerId == partnerId &&
         m.PurchaseOrderNumber == purchaseOrderNumber &&
         m.Status == status
         );
 }
Exemplo n.º 3
0
        private Task CloseOpenTradeAsync(Trade trade, OrderStatusChangedMessage message)
        {
            trade.ExitTimeStamp = DateTime.UtcNow;
            trade.ExitPrice     = message.Message.AvgFillPrice;
            trade.ProfitLoss    = trade.CalculateProfitLoss();

            return(_tradeRepository.UpdateAsync(trade));
        }
Exemplo n.º 4
0
        public void PublishOrderStatusChangedMessage(int productId, int quantity, string topic)
        {
            var message = new OrderStatusChangedMessage
            {
                ProductId = productId, Quantity = quantity
            };

            bus.Publish(message, topic);
        }
Exemplo n.º 5
0
        public void PublishOrderStatusChangedMessage(List <ProductOrder> products, string topic)
        {
            var message = new OrderStatusChangedMessage
            {
                orderLine = products
            };

            bus.Publish(message, topic);
        }
Exemplo n.º 6
0
        public void PublishOrderStatusChangedMessage(int customerId, IEnumerable <Order.OrderLine> orderLines, string topic)
        {
            var message = new OrderStatusChangedMessage
            {
                CustomerID = customerId,
                OrderLines = orderLines
            };

            bus.Publish(message, topic);
        }
Exemplo n.º 7
0
        public void PublishOrderStatusChangedMessage(int?customerId, IList <OrderLine> orderLines, string topic)
        {
            var message = new OrderStatusChangedMessage
            {
                CustomerId = customerId,
                OrderLines = orderLines
            };

            bus.Publish(message, topic);
        }
Exemplo n.º 8
0
 private bool Check(OrderStatusChangedMessage m, OrderStatusEnum status)
 {
     return(
         m.PartnerId == partnerId &&
         m.PurchaseOrderNumber == purchaseOrderNumber &&
         m.Status == status &&
         m.OrderLines.Count == 1 &&
         m.OrderLines[0].ProductId == productId &&
         m.OrderLines[0].Quantity == quantity
         );
 }
        private void HandleCustomerCreditStandingGood(OrderStatusChangedMessage message)
        {
            using (var scope = provider.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var customerRepos = services.GetService <IRepository <Customer> >();

                var localCustomer = customerRepos.Get(message.CustomerID);

                localCustomer.CreditStanding = true;

                customerRepos.Edit(localCustomer);
            }
        }
        private void RemoveReservedItems(OrderStatusChangedMessage message)
        {
            using (var scope = provider.CreateScope())
            {
                var services     = scope.ServiceProvider;
                var productRepos = services.GetService <IRepository <Product> >();

                foreach (var item in message.orderLine)
                {
                    var product = productRepos.Get(item.ProductId);
                    product.ItemsReserved -= item.Quantity;
                    productRepos.Edit(product);
                }
            }
        }
Exemplo n.º 11
0
        //Handle SHIPPED order
        private void HandleOrderShipped(OrderStatusChangedMessage message)
        {
            using (var scope = provider.CreateScope())
            {
                var services     = scope.ServiceProvider;
                var productRepos = services.GetService <IRepository <Product> >();

                foreach (var orderLine in message.OrderLines)
                {
                    var product = productRepos.Get(orderLine.ProductId);
                    product.ItemsReserved -= orderLine.Quantity;
                    productRepos.Edit(product);
                }
            }
        }
Exemplo n.º 12
0
        private void ReserveItems(OrderStatusChangedMessage message)
        {
            // A service scope is created to get an instance of the product repository.
            // When the service scope is disposed, the product repository instance will
            // also be disposed.
            using (var scope = provider.CreateScope())
            {
                var services     = scope.ServiceProvider;
                var productRepos = services.GetService <IRepository <Product> >();

                // Reserve items of ordered product (should be a single transaction)
                // Beware that this operation is not idempotent.
                var product = productRepos.Get(message.ProductId);
                product.ItemsReserved += message.Quantity;
                productRepos.Edit(product);
            }
        }
Exemplo n.º 13
0
        public bool IsInStock(Order order)
        {
            RestClient client     = new RestClient("http://productapi/api/products/CheckIfInStock");
            var        orderLines = new OrderStatusChangedMessage {
                OrderLines = order.OrderLines
            };

            var request = new RestRequest(Method.PUT);

            request.AddJsonBody(orderLines.OrderLines);

            var response = client.Execute(request);
            //var response = bus.Request<OrderStatusChangedMessage, IsInSockRequest>(orderLines);
            var boo = bool.Parse(response.Content);

            return(boo);
        }
Exemplo n.º 14
0
        private void HandleOrderCompleted(OrderStatusChangedMessage message)
        {
            // A service scope is created to get an instance of the product repository.
            // When the service scope is disposed, the product repository instance will
            // also be disposed.
            using (var scope = provider.CreateScope())
            {
                var services     = scope.ServiceProvider;
                var productRepos = services.GetService <IRepository <Product> >();

                foreach (var orderLine in message.OrderLines)
                {
                    var product = productRepos.Get(orderLine.ProductId);
                    product.ItemsReserved += orderLine.Quantity;
                    product.ItemsInStock  -= orderLine.Quantity;
                    productRepos.Edit(product);
                }
            }
        }
        public async Task WhenOrderStatusChangedAndNotFilledDoNotRecordTradeExitAsync()
        {
            // Arrange
            const double FillPrice = 10;
            const ushort Quantity  = 1000;

            var tradeRecordingService = Substitute.For <ITradeRecordingService>();

            GetVm(tradeRecordingService: tradeRecordingService);

            // Act
            var msg     = new OrderStatusEventArgs(1, BrokerConstants.OrderStatus.Cancelled, Quantity, 0, FillPrice, 0, 0, FillPrice, 0, null);
            var message = new OrderStatusChangedMessage(Symbol, msg);

            Messenger.Default.Send(message, OrderStatusChangedMessage.Tokens.Positions);

            // Assert
            await tradeRecordingService.DidNotReceive().ExitTradeAsync(
                Arg.Any <PositionItem>(),
                Arg.Any <OrderStatusChangedMessage>());
        }
Exemplo n.º 16
0
        private IsInSockRequest HandleCheckIfInStock(OrderStatusChangedMessage message)
        {
            using (var scope = provider.CreateScope())
            {
                var services     = scope.ServiceProvider;
                var productRepos = services.GetService <IRepository <Product> >();

                foreach (var orderLine in message.OrderLines)
                {
                    var product = productRepos.Get(orderLine.ProductId);

                    if (product.ItemsInStock < orderLine.Quantity)
                    {
                        return(new IsInSockRequest {
                            isInStock = false
                        });
                    }
                }
            }
            return(new IsInSockRequest {
                isInStock = true
            });
        }
Exemplo n.º 17
0
        public async Task ExitTradeAsync(PositionItem position, OrderStatusChangedMessage message)
        {
            var trade = _trades.SingleOrDefault(x => x.Symbol == message.Symbol);

            if (trade == null || !position.IsOpen)
            {
                Log.Debug("Could not find open trade or position for {0}", message.Symbol);
                return;
            }

            var exit = new Exit {
                Trade = trade
            };

            MapMessageDetails(exit, message);

            await _tradeRepository.AddExitAsync(exit).ConfigureAwait(false);

            if (!AnyRemainingShares(message))
            {
                await CloseOpenTradeAsync(trade, message).ConfigureAwait(false);
            }
        }
 private bool BasicCheck(OrderStatusChangedMessage m, OrderStatusEnum status)
 {
     return (
                m.PartnerId == partnerId &&
                m.PurchaseOrderNumber == purchaseOrderNumber &&
                m.Status == status
            );
 }
 private bool Check(OrderStatusChangedMessage m, OrderStatusEnum status)
 {
     return (
                m.PartnerId == partnerId &&
                m.PurchaseOrderNumber == purchaseOrderNumber &&
                m.Status == status &&
                m.OrderLines.Count == 1 &&
                m.OrderLines[0].ProductId == productId &&
                m.OrderLines[0].Quantity == quantity
            );
 }
Exemplo n.º 20
0
 private bool AnyRemainingShares(OrderStatusChangedMessage message)
 {
     return(message.Message.Remaining != 0);
 }
Exemplo n.º 21
0
 private void MapMessageDetails(Exit exit, OrderStatusChangedMessage message)
 {
     exit.Quantity  = Convert.ToUInt16(message.Message.Filled);
     exit.Price     = message.Message.AvgFillPrice;
     exit.TimeStamp = DateTime.UtcNow;
 }