Example #1
0
        public async Task <TradeExecutionResponse> Get(string stockName)
        {
            var tradeExecutionResponse = new TradeExecutionResponse();

            try
            {
                tradeExecutionResponse = await _tradeProvider.GetTrades(stockName);
            }
            catch (Exception ex)
            {
                tradeExecutionResponse.Status = "Failure";
                tradeExecutionResponse.Error  = new Error()
                {
                    ErrorCode    = "500",
                    ErrorMessage = "Internal Server Error"
                };
            }
            return(tradeExecutionResponse);
        }
        public async Task <TradeExecutionResponse> GetTrades(string stockName)
        {
            var tradeExecutionResponse = new TradeExecutionResponse()
            {
                TradeExecutions = new List <TradeExecution>(), Status = "Success"
            };

            if (_tradeQueue.Find(x => x.Name == stockName) != null)
            {
                while (true)
                {
                    try
                    {
                        if (!CanTradeBePerformed(stockName))
                        {
                            if (tradeExecutionResponse.TradeExecutions.Count == 0)
                            {
                                tradeExecutionResponse.Error  = GetError("500", "No trade can be performed for Stock: " + stockName);
                                tradeExecutionResponse.Status = Constants.ResponseStatus.Failure;
                            }
                            break;
                        }

                        var sellRequests = _tradeQueue.FindAll(x => x.Name == stockName && x.Type == Constants.TradeType.SELL);
                        if (sellRequests != null && sellRequests.Count > 0)
                        {
                            sellRequests = (from sr in sellRequests orderby sr.Price select sr).ToList();
                            var lowestSellRequest = sellRequests[0];

                            var buyRequests = _tradeQueue.FindAll(x => x.Name == stockName && x.Type == Constants.TradeType.BUY);
                            buyRequests = (from br in buyRequests orderby br.Price descending select br).ToList();
                            if (buyRequests != null && buyRequests.Count > 0)
                            {
                                var highestBuyRequest = buyRequests[0];
                                if (lowestSellRequest.Quantity >= highestBuyRequest.Quantity && highestBuyRequest.Price >= lowestSellRequest.Price)
                                {
                                    tradeExecutionResponse.TradeExecutions.Add(new TradeExecution()
                                    {
                                        BuyRequestId  = highestBuyRequest.Id,
                                        SellRequestId = lowestSellRequest.Id,
                                        Price         = lowestSellRequest.Price,
                                        Quantity      = highestBuyRequest.Quantity
                                    });
                                    _tradeQueue.Find(x => x.Id == lowestSellRequest.Id).Quantity = lowestSellRequest.Quantity - highestBuyRequest.Quantity;
                                    _tradeQueue.Remove(highestBuyRequest);
                                }
                                else
                                {
                                    if (lowestSellRequest.Price <= highestBuyRequest.Price)
                                    {
                                        tradeExecutionResponse.TradeExecutions.Add(new TradeExecution()
                                        {
                                            BuyRequestId  = highestBuyRequest.Id,
                                            SellRequestId = lowestSellRequest.Id,
                                            Price         = lowestSellRequest.Price,
                                            Quantity      = lowestSellRequest.Quantity
                                        });
                                        _tradeQueue.Find(x => x.Id == highestBuyRequest.Id).Quantity = highestBuyRequest.Quantity - lowestSellRequest.Quantity;
                                        _tradeQueue.Remove(lowestSellRequest);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        tradeExecutionResponse.Error  = GetError("500", "An Error occurred while fetching trades on Stock: " + stockName);
                        tradeExecutionResponse.Status = Constants.ResponseStatus.Failure;
                    }
                }
            }
            else
            {
                tradeExecutionResponse.Error  = GetError("404", "No Trade Requests found for Stock: " + stockName);
                tradeExecutionResponse.Status = Constants.ResponseStatus.Failure;
            }
            return(tradeExecutionResponse);
        }